Removing the Time from SQL SERVER datetime

By geekfreak

Just discovered a new trick, one that speeds up my SQL. I often use datetime columns and hate the SQL Server format where all dates include the time, which results in date conversion or bizarre arithmetics. Here ’s a nice solution


RETURN CAST( FLOOR( CAST( @d AS INTEGER) ) AS DATETIME )

What does it do? The cast/floor/cast trio pushes the internal datetime to a float, then removes the decimal part and converts it back to a datetime, as the time portion of the datetime datatype in SQL server is stored in the decimal places of a float equivalent type, then converting to an integer removes them and sets the date to midnight.

2 Responses to “Removing the Time from SQL SERVER datetime”

  1. pallavi Says:

    convert(varchar(20),getdate(),108)

  2. Groucho Says:

    The initial cast should be a cast to float:

    RETURN CAST( FLOOR( CAST( @d AS FLOAT) ) AS DATETIME )

    Otherwise the time fraction will cause a round up to the next full day.

Leave a Reply