Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does SQL Server figure out the entered date's format?

I use SQL Server 2014 Developer. Assume I have this scenario where I filter payment date of an Invoice

select * 
from Invoices 
where PaymentDate > '1/2/2012'

In the Invoice table, PaymentDate is in the format of 2012-01-08 00:00:00 ( YYYY-MM-DD HH:MM:SS )

But RHS operand of the where is 1/2/2012 So, how does SQL know if this in the format of M/D/YYYY or D/M/YYYY ?

like image 274
tm_ello Avatar asked May 17 '26 13:05

tm_ello


1 Answers

Datetimes don't have a format (other than binary): they are (hopefully) being stored as datetime and not strings. Don't confuse presentation with representation.

When you have a string literal, '1/2/2012', SQL Server interprets via your locale.

Best practice to always use (and export) dates in the ISO 8601 format yyyy-MM-dd (yyyyMMdd).

See ISO 8601
What date/time literal formats are LANGUAGE and DATEFORMAT safe?

like image 69
Mitch Wheat Avatar answered May 20 '26 08:05

Mitch Wheat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!