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
 last five days

Author  Topic 

Irfan.abdullah
Starting Member

9 Posts

Posted - 2012-08-10 : 20:34:15
Hi,

I needed to limit the number of dates im getting from a column to the last 5 dates from the sysdate. For example if the dates in the table were for the last 5 days that including today, i wanted to pull the last 5 dates, and limit it to those.i need something to pull the last five run dates from the sysdate, not necessarily from the last five days

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-10 : 22:36:34
[code]
where datefield >= dateadd(dd,datediff(dd,0,getdate()),-4)
AND datefield < dateadd(dd,datediff(dd,0,getdate()),1)
[/code]
------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2012-08-11 : 21:16:43
Hi,

You should look into the top function
[url]http://msdn.microsoft.com/en-us/library/ms189463.aspx[/url]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-12 : 11:34:27
quote:
Originally posted by bitsmed

Hi,

You should look into the top function
[url]http://msdn.microsoft.com/en-us/library/ms189463.aspx[/url]


why?
using top means selecting all records and then getting top 5 dates

why not put condition as a filter and get only required records which is more efficient?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2012-08-12 : 15:59:49
The way I read the question, we want to return the latest 5 used dates. So if the records were:

20120801
20120802
20120804
20120806
20120808
20120810
20120811
20120812

The result should be:
20120806
20120808
20120810
20120811
20120812

By hey - maybe I'm reading the question wrong, and in that case, I'm sorry
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-12 : 16:04:21
limit the number of dates im getting from a column to the last 5 dates from the sysdate

sounded like OP wanted last 5 days data including current date

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2012-08-12 : 16:14:28
quote:
if the dates in the table were for the last 5 days that including today, i wanted to pull the last 5 dates

Only he who asked the question, knows what he really want.

Either way, he's got (in my oppinion) two good answers.
Go to Top of Page
   

- Advertisement -