SQL Server 2005: how to subtract 6 month

后端 未结 2 2027
一生所求
一生所求 2020-12-18 18:51

I have a date, suppose today date

declare @d datetime
set @d = \'20101014\'

I need

select @d - 
<         


        
相关标签:
2条回答
  • 2020-12-18 19:07

    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))
    
    0 讨论(0)
  • 2020-12-18 19:17

    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
    
    0 讨论(0)
提交回复
热议问题