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
 General SQL Server Forums
 New to SQL Server Programming
 Sql Server Date handling

Author  Topic 

kalyformin
Starting Member

15 Posts

Posted - 2007-10-14 : 03:24:21
I have a field in one of my tables called renew_date. The renew_date is always the first of every month. My requirement is to have a query that's run on the first of every month to select records that have renew_date coming up in the next 150 days.(5 months from today).

ANY help is really appreciated.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-10-14 : 03:29:30
SELECT * FROM Table1
WHERE renew_date >= DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), 0)
AND renew_data < DATEADD(MONTH, 5, DATEDIFF(DAY, -1, CURRENT_TIMESTAMP))



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-14 : 04:43:41
In case the report isn't run until, say, the second of the month, or a renew_date is some date within a month, rather than the 1st of the moth, I would use:

WHERE renew_date >= DATEADD(Month, DATEDIFF(Month, 0, CURRENT_TIMESTAMP), 0)
AND renew_data < DATEADD(Month, DATEDIFF(Month, 0, CURRENT_TIMESTAMP)+5, 0)

Kristen
Go to Top of Page

kalyformin
Starting Member

15 Posts

Posted - 2007-10-14 : 17:10:47
Thank you both. It worked like a charm!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-15 : 03:09:08
For more info on how to handle dates refer www.sql-server-performance.com/fk_datetime.asp

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -