Solved

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

Posted on 2009-07-14
2
847 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
[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
  • 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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

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…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

734 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