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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Old Forums
 CLOSED - General SQL Server
 How to code 30 days before today?
 Forum Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 05/11/2006 :  10:09:49  Show Profile
I want to code date >= (today - 30). How to code it?

madhivanan
Premature Yak Congratulator

India
22864 Posts

Posted - 05/11/2006 :  10:11:34  Show Profile  Send madhivanan a Yahoo! Message

Where DateCol>=DateAdd(day, DateDiff(day,0,getdate()),-30) 
and   DateCol<DateAdd(day, DateDiff(day,0,getdate()),1) 


Madhivanan

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

kpmkrishnan
Starting Member

11 Posts

Posted - 05/11/2006 :  10:12:39  Show Profile

Declare @Date datetime
set @Date = GetDate()
select @Date-30
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22864 Posts

Posted - 05/11/2006 :  10:15:29  Show Profile  Send madhivanan a Yahoo! Message
quote:
Originally posted by kpmkrishnan


Declare @Date datetime
set @Date = GetDate()
select @Date-30


That wont give all the data required

Madhivanan

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

khtan
In (Som, Ni, Yak)

Singapore
17689 Posts

Posted - 05/11/2006 :  20:54:47  Show Profile
quote:
Originally posted by kpmkrishnan


Declare @Date datetime
set @Date = GetDate()
select @Date-30


getdate() will return date & time. so @date - 30 will gives you the date 30 days before today but not at 0 hour. So you might miss out some record that the date is 30 days before and time less than the current time.
Example :
select getdate(),        getdate() - 30
result :
2006-05-12 08:52:24.640	 2006-04-12 08:52:24.640

So you will missed out any record which date falls between 2006-04-12 00:00 to 8:52




KH

Go to Top of Page
  Previous Topic Topic Next Topic  
 Forum Locked
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000