Solved

getdate() - subtract 6 months

Posted on 2009-07-01
5
1,447 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
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now