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

后端 未结 10 2094
别跟我提以往
别跟我提以往 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:54

    I'm not sure why your existing query would take over 5s for 40,000 rows.

    I just tried the following query against a table with 100,000 rows and it returned in less than 0.1s.

    SELECT DISTINCT DATEADD(day, 0, DATEDIFF(day, 0, your_date_column))
    FROM your_table
    

    (Note that this query probably won't be able to take advantage of any indexes on the date column, but it should be reasonably quick, assuming that you're not executing it dozens of times per second.)

提交回复
热议问题