简体   繁体   English

SQL Server 2005:如何减去6个月

[英]SQL Server 2005: how to subtract 6 month

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. 从@d开始,包含过去六个月的实际天数在哪里。

You can use DATEADD : 你可以使用DATEADD

select DATEADD(month, -6, @d)

EDIT : if you need the number of days up to 6 months ago you can use DATEDIFF : 编辑 :如果您需要最多6个月前的天数,您可以使用DATEDIFF

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

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). 我需要根据条件选择algorythm - 如果两个日期之间有多少天,如6个月(从最后一个日期开始)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM