SQL Server 2005: how to subtract 6 month

skaeff picture skaeff · Oct 14, 2010 · Viewed 77.5k times · Source

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.

Answer

Alex Bagnolini picture Alex Bagnolini · Oct 14, 2010

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))