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
 Old Forums
 CLOSED - General SQL Server
 How to code 30 days before today?

Author  Topic 

Sun Foster
Aged Yak Warrior

515 Posts

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

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-05-11 : 10:11:34
[code]
Where DateCol>=DateAdd(day, DateDiff(day,0,getdate()),-30)
and DateCol<DateAdd(day, DateDiff(day,0,getdate()),1)
[/code]

Madhivanan

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

kpmkrishnan
Starting Member

11 Posts

Posted - 2006-05-11 : 10:12:39

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

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-05-11 : 10:15:29
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)

17689 Posts

Posted - 2006-05-11 : 20:54:47
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
   

- Advertisement -