How to quickly select DISTINCT dates from a Date/Time field, SQL Server

后端 未结 10 2096
别跟我提以往
别跟我提以往 2021-01-31 22:42

I am wondering if there is a good-performing query to select distinct dates (ignoring times) from a table with a datetime field in SQL Server.

My problem isn\'t getting

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-31 22:55

    I've used the following:

    CAST(FLOOR(CAST(@date as FLOAT)) as DateTime);
    

    This removes the time from the date by converting it to a float and truncating off the "time" part, which is the decimal of the float.

    Looks a little clunky but works well on a large dataset (~100,000 rows) I use repeatedly throughout the day.

提交回复
热议问题