TSQL retrieve all records in current month/year

后端 未结 2 1949
没有蜡笔的小新
没有蜡笔的小新 2021-02-08 23:06

I have a datetime field called DateFinished. I need to be able to retrieve all records in which DateFinished is within the current month/year.

2条回答
  •  死守一世寂寞
    2021-02-08 23:57

    Just as an alternative - this should use an index on DateFinished.

    SELECT * 
    FROM MyTable
    WHERE DateFinished BETWEEN 
         DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
         AND 
         DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0)
    

提交回复
热议问题