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
 How to get last 30 days data from a table

Author  Topic 

soori457
Yak Posting Veteran

85 Posts

Posted - 2009-04-13 : 00:50:27
Hai all,

I need to retrieve last 30 days data from the current date

Employee(table)

Employee table has date_created(column) of datetime datatype

Any one help me plzzz....


Thanks in Advance

Suresh Kumar

soori457
Yak Posting Veteran

85 Posts

Posted - 2009-04-13 : 00:58:43
I have written a query for this

select * from Employee where date_created between dateadd(day, datediff(day, 0 ,getdate())-30, 0) and getdate()

anyone please tell, it is correct way or not.


Suresh Kumar
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-04-13 : 02:17:30
quote:
Originally posted by soori457

I have written a query for this

select * from Employee where date_created between dateadd(day, datediff(day, 0 ,getdate())-30, 0) and getdate()

anyone please tell, it is correct way or not.


Suresh Kumar


If you want to include today's time too then it is correct

Madhivanan

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

soori457
Yak Posting Veteran

85 Posts

Posted - 2009-04-13 : 02:34:43
Thanks Madhivanan for your reply.

If I want only dates but not time, then What should I have to do..

Suresh Kumar
Go to Top of Page

karthik_padbanaban
Constraint Violating Yak Guru

263 Posts

Posted - 2009-04-13 : 02:46:43
select * from Employee where date_created between dateadd(day, datediff(day, 0 ,getdate())-30, 0)
and
dateadd(day,datediff(day, 0 ,getdate()), 0)


Karthik
Go to Top of Page

soori457
Yak Posting Veteran

85 Posts

Posted - 2009-04-13 : 02:59:53
Thanks Karthik
Its working fine

Suresh Kumar
Go to Top of Page
   

- Advertisement -