Dean Flaherty
Dean Flaherty

Reputation: 351

SQL - losing time part of datetime

I have the following code which converts an nvarchar to a datetime.

SELECT TRY_PARSE(CONVERT(NVARCHAR(255), LEFT([SubmitDate], 10), 103) AS DATETIME USING 'en-gb') AS [SubmitDate]
FROM [ITSM_INCIDENT]

When I do the conversion I lose the time part of the datetime

e.g.

'2017-01-01 13:23:01.000' goes to '2017-01-01 00:00:00.000'

How can I write this so the time doesn't all go to 0's?

Upvotes: 0

Views: 344

Answers (1)

SqlZim
SqlZim

Reputation: 38043

You are using left() to only grab the date part (first 10) of the variable.

Try using this instead:

SELECT TRY_PARSE(CONVERT(NVARCHAR(255), LEFT([SubmitDate], 23), 103) AS DATETIME USING 'en-gb') AS [SubmitDate]
FROM [ITSM_INCIDENT]

Upvotes: 1

Related Questions