Solved

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

Posted on 2009-07-14
2
845 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 143

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 143

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

828 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