How do you test if something is older than 3 months?

后端 未结 4 1011
再見小時候
再見小時候 2021-02-12 21:36

I have been having some trouble to select the rows of my table which has a date of 3 months prior of today. I tried using DATE(NOW() - INTERVAL 3 MONTH) in my where

4条回答
  •  一整个雨季
    2021-02-12 22:01

    Use dateadd().

    update [TCTdb].[dbo].[Stock]
    set [WareHouse] = 'old'
    where [ManufacturedDate] < dateadd(month,-3,getdate())
    

    I suggest dateadd() over datediff() because I think you're going to get unexpected results using datediff() with the month datepart.

    Consider that the following statements both return 3:

    select datediff(month, '1/1/2011','4/1/2011')
    select datediff(month, '1/1/2011','4/30/2011')
    

    Either works in this particular case... Just keep that behavior in mind.

提交回复
热议问题