SQL Server 2005: how to subtract 6 month

喜你入骨 提交于 2019-11-28 02:30:52

问题


I have a date, suppose today date

declare @d datetime
set @d = '20101014'

I need

select @d - <six month>

where is the real number of days that contains last six month, beginning from @d.


回答1:


You can use DATEADD:

select DATEADD(month, -6, @d)

EDIT: if you need the number of days up to 6 months ago you can use DATEDIFF:

select DATEDIFF(day, @d, DATEADD(month, -6, @d))



回答2:


Also check this up (developing this theme):

i need to choose the algorythm depending on the condition - if there are as many days between two dates as in 6 month (ago from the last date).

I did it in this way:

    case
      when
        DATEDIFF(day, DATEADD(month, -6, @pDateEnd), @pDateEnd)
        >
        DATEDIFF(day, @pDateBegin, @pDateEnd)
      then 'there is no 6-month difference between two dates'
      else 'there is 6-month difference ore more between two dates'
    end


来源:https://stackoverflow.com/questions/3932947/sql-server-2005-how-to-subtract-6-month

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!