Solved

T-SQL, Select a date range from now and 6 months in past.

Posted on 2009-07-14
2
835 Views
Last Modified: 2012-05-07
Hi Guys.

I need to find a way in t-sql to select a date range from now to 6 months in the past.

We have a field name called DatePublished that holds the date the item was added to the database.

So the results that would come back would be 6 months worth of stories.

Cheers!
0
Comment
Question by:TheAnvilGroup
  • 2
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24847598
check out DATEADD() function:
http://msdn.microsoft.com/en-us/library/ms189794.aspx

in short:
where datepublished >= dateadd(month, -6, getdate())
  and datepublished < getdate()

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24847601
note: getdate() is now, date + time.
if you need full day, resp full month, you will need to add some more to get the "corrected" date value...
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

758 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

16 Experts available now in Live!

Get 1:1 Help Now