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
 Date Functions

Author  Topic 

theHydra
Starting Member

42 Posts

Posted - 2011-01-28 : 18:42:03
Hi All,

I know this is very easy to do, but I'm having a hard time understanding how to code this...

So here's what I'm trying to do...

I'm just trying to pull everything in from jodrtg, but have it only display records that have a fddue_date of today and the next thirty days, so a month in the future.

I was told i need to be using the dateadd function, but as I said the code examples and explanation I've found on-line doesn't seem to be applicable to what I'm trying to do.

Hope this makes sense.

Thanks!!


select * from jodrtg RTG

WHERE

AND (((RTG.fddue_date) >= (getDate()+30)))

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-01-28 : 22:38:30
You want to use something like the below. the 1st one strips the time stamp off of getdate() so you get midnight for today. The second goes 1 month forward + 1 day

For demonstration run these
SELECT dateadd(d,0,datediff(d,0,getdate())) --2011-01-28 00:00:00.000
SELECT dateadd(d,1,dateadd(m,1,datediff(d,0,getdate())))-- 2011-03-01 00:00:00.000

so use those to get everything >= midnight today and < 1 day more than 1 month from now.
SELECT * FROM jodrtg RTG
WHERE RTG.fddue_date >= SELECT dateadd(d,0,datediff(d,0,getdate())) --midnight today
and RTG.fddue_daet < dateadd(d,1,dateadd(m,1,datediff(d,0,getdate()))) -- the 1st day following 1 month from now





Poor planning on your part does not constitute an emergency on my part.
Go to Top of Page
   

- Advertisement -