Reputation: 9
declare @FromDate varchar(30)
set @FromDate= '2014-06-06'
I need to convert @FromDate
to a datetime
:
set @fromdate = convert(datetime, @fromdate, 126)
Then I need the output in the format of '2014-06-06 00:00:00.000'
.
Can anyone suggest how to perform this conversion?
Upvotes: 0
Views: 84
Reputation: 21381
Try this
DECLARE @FromDate varchar(30)
SET @FromDate= '2014-06-06'
SELECT CONVERT(VARCHAR, CONVERT(DATETIME, @fromdate), 21)
Upvotes: 0
Reputation: 315
DECLARE @FromDate VARCHAR(30) SET @FromDate = '2014-06-06'
DECLARE @FromDateDT DATETIME SET @FromDateDT = @FromDate
-- DECLARE @FromDateDT DATETIME SET @FromDateDT = CONVERT(DATETIME, @FromDate) -- It's the same as line above
SET @FromDate = CONVERT(VARCHAR, @FromDateDT, 120)
SELECT @FromDate
SET @FromDate = CONVERT(VARCHAR, @FromDateDT, 121)
SELECT @FromDate
or simple
SELECT CONVERT(VARCHAR, CONVERT(DATETIME, '2014-06-06'), 121)
Some other formats:
https://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/
Feel free to ask.
Upvotes: 1