SQL Server – Changing Date Order System Wide

datedate formatsql serversql-server-2012

I use Laravel with PHP from https://laravel.com/

MSSQL is a driver and works perfectly – with th exception of dates.

For some reason the project has decided that the follwoing is a universal date format

Y-m-d H:i:s
2021-01-21 12:00:00

However this does not seem to be universal as MSSQL interprets this as a US based date eg. The date above is invalid as 21 is considered the month not the day.

I will try to get them to allow date to be set to a custom format but this may take a while.

My MSSQL user is set to Language:British English which solves most of my issues, but not all.

Is it possible to in some way tell MSSQL to interpret a date like this

2021-01-22 12:00:00

as

YYYY-MM-DD HH:II:SS

instead of

YYYY-DD-MM HH:II:SS

which is what it is doing?

Can this be done on a system wide basis?

Best Answer

This is caused by the user being set to "British English". I've always considered it a bug (I imagine someone was told that UK dates had month & day inverted from the US norms, and they incorrectly assumed that applied to ISO8601-a-like dates also). This has been the case since at least SQL7 IIRC. It is irritating because in every other context YYYY-MM-DD is unambiguous.

If you explicitly convert string to dates using CONVERT() you can specify the string format that is being used using its optional third parameter. This may not help if the issue is with date type parameters being sent in SQL strings (instead of prepared queries with typed parameters) as the conversion in that case is out of your hands.

If you only ever provide dates in a year-first format then just avoid the British English user setting. This may not help if your application sometimes passes in non-US year-last dates.