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.
| 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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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] |
 |
|
|
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 dateswhy not put condition as a filter and get only required records which is more efficient?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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:2012080120120802201208042012080620120808201208102012081120120812The result should be:2012080620120808201208102012081120120812By hey - maybe I'm reading the question wrong, and in that case, I'm sorry |
 |
|
|
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 sysdatesounded like OP wanted last 5 days data including current date------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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. |
 |
|
|
|
|
|
|
|