dans.blog


The miscellaneous ramblings and thoughts of Dan G. Switzer, II

Remove time from date/time stamp in SQL Server

I thought I've blogged this tip before, but I saw someone else mention it on a mailing list this morning. If you ever want to remove the time portion in a MSSQL Server, you can do this by removing the decimal portion of the date/time stamp.

SQL Server stores date/times as a numeric value where the integer portion of the value represents the number of days since epoch and the time is represented by the decimal portion of the value. This allows us to cast a datetime field as a float and then round down to the nearest integer:

select cast(floor(cast(getutcdate() as float)) as datetime)

We first convert the datetime to a float:

cast(getutcdate() as float)

We next use the floor() function to round down to the nearest integer:

more…