Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Date dynamic Filter

Author  Topic 

dac9901
Starting Member

2 Posts

Posted - 2009-01-19 : 09:08:46
hello,

I have a query that will run once a month. I would like the date filter to dynamically set the date to the previous month.

***
WHERE BASE.WHSTARTDT BETWEEN CONVERT(DATETIME,'12/1/2008') AND CONVERT(DATETIME,'12/31/2008 11:59:59 PM')
***
In this example, since it is January I want to return data for 12/1/08 through 12/31/08. How do I update the query so next month (February) it will return January's data (1/1/09 - 1/31/09) without hardcoding a month date in the query?

Any help is greatly appreciated.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-19 : 09:33:30
WHERE BASE.WHSTARTDT >= DATEADD(mm,DATEDIFF(mm,0,GETDATE())-1,0) AND
BASE.WHSTARTDT < DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)
Go to Top of Page

dac9901
Starting Member

2 Posts

Posted - 2009-01-19 : 11:09:46
Thanks visakh16 for your help!

Have a great day.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-20 : 02:59:52
welcome
Go to Top of Page
   

- Advertisement -