Reputation: 31
Can anyone let me know what exactly date difference this below statement is verifying for column check constraint?
DATEDIFF(millisecond,[MyDateColumn],
DATEADD(day,DATEDIFF(day,(0),[MyDateColumn]),(0)))=(0)))
Upvotes: 1
Views: 588
Reputation: 9042
CAST(0 AS DATETIME)
is 1900-01-01 00:00:00.000
(The default value of the DATETIME
datatype). The integer 0
is implicitly converted to DATETIME
.datediff(day, (0), [MyDateColumn])
is the difference between the above date in daysdateadd(day,datediff(day,(0),[MyDateColumn]),(0))
is the midnight of the given day.datediff(millisecond,[MyDateColumn],dateadd(day,datediff(day,(0),[MyDateColumn]),(0)))
is the elapsed miliseconds since midnight to the given datetime value.Basically it checks if the value in the column is at midnight. (A constraint to force the DATETIME
data type to act as a DATE
column.)
Upvotes: 1