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 range

Author  Topic 

igorblackbelt
Constraint Violating Yak Guru

407 Posts

Posted - 2005-09-08 : 10:02:14
Hello Guys -

What's the code to pull records only for the last 30 days ?

datepart(dd, GETDATE ()) -30 ?

Igor.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-08 : 10:11:11

Try this

Select columns from yourTable
Where Datecol between dateadd(dd,-30,getdate()) and getdate()


Madhivanan

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

igorblackbelt
Constraint Violating Yak Guru

407 Posts

Posted - 2005-09-08 : 10:37:02
It works, I also thought about

>GETDATE()-30

Thanks!

Igor.
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-09-08 : 11:00:32
Don't forget about times (12/23/2005 12:31:24.123)


--this would include the time portion of the getdate()
Where Datecol between dateadd(dd,-30,getdate()) and getdate()

--this would not include the time portion of the getdate()
Where Datecol between dateadd(dy,datediff(dy,0,getdate())-30,0) and dateadd(dy,datediff(dy,0,getdate()),0)




Corey

Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now."
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-08 : 11:03:13
Thats good point Corey

Madhivanan

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

Kristen
Test

22859 Posts

Posted - 2005-09-08 : 11:26:41
"dateadd(dy,..."

Is it safe to use "dy" rather than "dd" [or my preference "Day"]?

I can't get my head around how "dayofyear" compares in this calculation!!

Kristen
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-09 : 01:13:58
>>I can't get my head around how "dayofyear" compares in this calculation!!

I think both dd and dy play the same role when they are used in DateDiff function but differently in DatePart


Madhivanan

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

- Advertisement -