Solved

getdate() - subtract 6 months

Posted on 2009-07-01
5
1,480 Views
Last Modified: 2012-05-07
I am using getdate() to return todays date.

How am i able to use getdate() and subtract 6 months from it?
0
Comment
Question by:Mr_Shaw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 200 total points
ID: 24755056
select dateadd(m, -6, getdate())
0
 
LVL 10

Expert Comment

by:dwe761
ID: 24755089
select dateadd(m,6, getdate())
0
 
LVL 10

Assisted Solution

by:dwe761
dwe761 earned 150 total points
ID: 24755112
I concede.  I went ahead 6 months.  chapmandew is correct.
0
 
LVL 26

Assisted Solution

by:Chris Luttrell
Chris Luttrell earned 150 total points
ID: 24755180
chapmandew has the correct syntax to subtract 6 months.  The other one add 6 months.
Remember that getdate() will include the time portion and the dateadd function will change the date part and leave the time alone.  If you want the start of the day you have to use code like this for todays date:
select dateadd(d,datediff(d,0,getdate()),0)

and this for 6 months ago:
select dateadd(m, -6, dateadd(d,datediff(d,0,getdate()),0))
0
 

Author Closing Comment

by:Mr_Shaw
ID: 31598796
thanks
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question