SQL Server DATEPART() function -
today wrote 2 queries on datepart()
, different returns below
query #1:
select datepart(day,'2015-07-05')
returns '5', expected.
query #2:
select datepart(day, 2015-07-05)
returns '27', little bit funny, , don't understand how 27 being returned.
the difference between these 2 queries 1 date inside ' ', , other without.
anybody can me out here?
2015-07-05
mathematical expression adds integer 2003. (subtracting 7 2015 gives 2008 subtract 5)
2003 evaluates '1905-06-27'
when implicitly cast datetime casting int
datetime
works same adding number of days base date of 1 jan 1900 (i.e. equals dateadd(day, 2003,'19000101')
).
so 27 comes from.
the correct way denote date literals in sql server string '2015-07-05'
(iso format - unambiguous newer datetime datetypes) or '20150705'
(unambiguous legacy datatypes) or using odbc format { d '2015-07-05' }
.
Comments
Post a Comment