Sample Header Ad - 728x90

Unexplained MSSQL default date formatting

6 votes
3 answers
664 views
I have a problem with T-SQL that is out of my control. Their T-SQL is failing to work with a date that was cast to **VARCHAR(10)**. They are expecting to get the default yyyy-mm-dd (the documented default style for 'date' - 21) Most of the time it works ok but it fails sometimes. I've found that it fails when the cast returns 'mmm dd yyyy' format - i.e Jun 21 2022. The **VARCHAR(10)** makes the result of the cast unusable as it truncates to 'mmm dd yyy' - i.e Jun 21 202. After some mucking around I found the following strange behaviour SELECT CONVERT(VARCHAR, CAST(GETDATE() as date)) FROM Invoices where InvoiceNumber > -1000000 Result: Jun 12 2025 (x50,000) SELECT TOP 1CONVERT(VARCHAR, CAST(GETDATE() as date)) FROM Invoices where InvoiceNumber > -1000000 Result: 2025-06-12 (x1) SELECT CONVERT(VARCHAR, CAST(GETDATE() as date)) FROM Invoices Result: 2025-06-12 (x50,000) It's hard to know what other information to provide as these results are so strange. Select @@LANGUAGE returns British. SQL2019, 15.0.4410.1 (november)
Asked by Patrick (163 rep)
Jun 12, 2025, 05:30 AM
Last activity: Jun 13, 2025, 08:44 AM