question

mikko118 avatar image
mikko118 asked

sql nvarchar to date

I have a question to ask regarding convertion from nvarchar to datetime, my sql is written below:

I canno tunderstand why some records before the data have been shown below. Can someone tell me what's wrong.



SELECT CONVERT(nvarchar(4000),[Date of Presentation],103) AS Britishformat

FROM data source

WHERE [Date of Presentation] >= '01/01/2023'


Results show:

Britishformat

20/08/2022

19/03/2023

13/10/2022

25/03/2023

25/01/2023

14/03/2023

01/09/2022

20/07/2022

sql query
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Oleg avatar image Oleg commented ·
The data makes perfect sense, I am not sure why you think it is incorrect. It looks like the [Date of Presentation] might not be the date or datetime data type, and so the where clause means the string comparison. For example, 13/10/2022 (as string) is certainly greater than 01/01/2023 so it is included in the results. By the way, IMHO it is a pure evil to specify the date values in mm/dd/yyyy or dd/mm/yyyy formats in where clauses. The only string representation of the date value which any computer in the world understands without any possibility of ambiguity is yyyymmdd format (ISO format 112). When the date value is presented (as string) in this format then it does not matter what date format setting is used by the session, the date will always be interpreted correctly. Please clarify the data type of the source column and I can post the sample query to get the results you need. Thank you.
0 Likes 0 ·

0 Answers

·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.