SQL Server date comparisons based on month and year only

后端 未结 6 961
南方客
南方客 2021-02-05 06:27

I am having trouble determining the best way to compare dates in SQL based on month and year only.

We do calculations based on dates and since billing occurs on a monthl

6条回答
  •  终归单人心
    2021-02-05 07:28

    You can use formatting your date like this 'yyyyMM' , so only the month in the same year will be selected.

    SELECT * 
    FROM tableName 
    WHERE FORMAT(date_month_bill, 'yyyyMM') < FORMAT(DATEADD(MONTH, -1, GETDATE()), 'yyyyMM') 
    AND FORMAT(date_month_bill, 'yyyyMM') > FORMAT(DATEADD(MONTH, -3, GETDATE()), 'yyyyMM')
    

提交回复
热议问题