EdomGroup: Inspire, Innovate, Create

Removing time from datetime via casting

SQL

Sometimes you want to compare a datetime value without the time portion to a date. If you use query param and specify a timestamp for the type but only provide a date as the value you'll be given 00:00:00 for the time, which is midnight. To strip, or more specifically, set the time porition of the value in the database or midnight also use this cast:

CAST(FLOOR(CAST(DateCompleted AS FLOAT)) AS DATETIME)

What this will do is convert the date to a floating point number, remove the decimal, then flip it back to a datetime data type. When the datetime data type is represented as a floating point number the whole number is the date portion and the decimal portion represents the time. Flooring removes the decimal and effectively set it to midnight.

Categories

Monthly Archives

Feeds