| Author |
Topic |
|
gavakie
Posting Yak Master
221 Posts |
Posted - 2008-11-11 : 17:01:55
|
| how would I get a list of dates say between the getdate() and getdate()-20? |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-11-11 : 17:07:52
|
| [code];with listdate ([date],i) as (select getdate() as [date], 0 as iunion allselect dateadd(day, i, [date]),i-1 from listdatewhere i > -20)select convert(varchar(10), [date],101) as [date] from listdatewhere i < 0[/code] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-11 : 22:46:52
|
| [code]SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()),-1 * number) AS DateFROM master..spt_valuesWHERE type='p'AND number<=20order by Date DESCoutput--------------------------------2008-11-12 00:00:00.0002008-11-11 00:00:00.0002008-11-10 00:00:00.0002008-11-09 00:00:00.0002008-11-08 00:00:00.0002008-11-07 00:00:00.0002008-11-06 00:00:00.0002008-11-05 00:00:00.0002008-11-04 00:00:00.0002008-11-03 00:00:00.0002008-11-02 00:00:00.0002008-11-01 00:00:00.0002008-10-31 00:00:00.0002008-10-30 00:00:00.0002008-10-29 00:00:00.0002008-10-28 00:00:00.0002008-10-27 00:00:00.0002008-10-26 00:00:00.0002008-10-25 00:00:00.0002008-10-24 00:00:00.0002008-10-23 00:00:00.000[/code] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-11 : 22:47:56
|
quote: Originally posted by hanbingl
;with listdate ([date],i) as (select getdate() as [date], 0 as iunion allselect dateadd(day, i, [date]),i-1 from listdatewhere i > -20)select convert(varchar(10), [date],101) as [date] from listdatewhere i < 0
hanbingl this is not returning correct resultsoutput----------------11/12/200811/11/200811/09/200811/06/200811/02/200810/28/200810/22/200810/15/200810/07/200809/28/200809/18/200809/07/200808/26/200808/13/200807/30/200807/15/200806/29/200806/12/200805/25/200805/06/2008 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-11 : 22:52:41
|
this is enough;with listdate ([date],i) as (select dateadd(dd,datediff(dd,0,getdate()),0) as [date], 1 as iunion allselect dateadd(day, -1 , [date]),i+1 from listdatewhere i<=20)select date as [dispdate] from listdateorder by date descoutput----------------------------2008-11-12 00:00:00.0002008-11-11 00:00:00.0002008-11-10 00:00:00.0002008-11-09 00:00:00.0002008-11-08 00:00:00.0002008-11-07 00:00:00.0002008-11-06 00:00:00.0002008-11-05 00:00:00.0002008-11-04 00:00:00.0002008-11-03 00:00:00.0002008-11-02 00:00:00.0002008-11-01 00:00:00.0002008-10-31 00:00:00.0002008-10-30 00:00:00.0002008-10-29 00:00:00.0002008-10-28 00:00:00.0002008-10-27 00:00:00.0002008-10-26 00:00:00.0002008-10-25 00:00:00.0002008-10-24 00:00:00.0002008-10-23 00:00:00.000 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-12 : 01:44:10
|
quote: Originally posted by hanbingl
;with listdate ([date],i) as (select getdate() as [date], 0 as iunion allselect dateadd(day, i, [date]),i-1 from listdatewhere i > -20)select convert(varchar(10), [date],101) as [date] from listdatewhere i < 0
Also dont convert dates to varcharsMadhivananFailing to plan is Planning to fail |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-11-12 : 10:28:22
|
Thanks for the fix visakh16 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-12 : 10:31:48
|
quote: Originally posted by hanbingl
Thanks for the fix visakh16 
No problem You're welcome |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-13 : 01:42:44
|
And you dont need extra column i;with listdate ([date]) as ( select dateadd(dd,datediff(dd,0,getdate()),0) as [date] union all select dateadd(day, -1 , [date]) from listdate where dateadd(day, -1 , [date])+20>=dateadd(dd,datediff(dd,0,getdate()),0))select date as [dispdate] from listdateorder by date desc MadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-13 : 01:46:07
|
quote: Originally posted by madhivanan And you dont need extra column i;with listdate ([date]) as ( select dateadd(dd,datediff(dd,0,getdate()),0) as [date] union all select dateadd(day, -1 , [date]) from listdate where dateadd(day, -1 , [date])+20>=dateadd(dd,datediff(dd,0,getdate()),0))select date as [dispdate] from listdateorder by date desc MadhivananFailing to plan is Planning to fail
yup...i just copy pasted it from prev code anf forgot to remove it |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2008-11-17 : 06:11:24
|
| declare @t table (date Datetime)declare @i intset @i = 1while (@i<=20)begininsert into @tselect getdate() -@iselect @i = @i +1endselect * from @tdDates--------------2008-11-16 16:46:08.5202008-11-15 16:46:08.5202008-11-14 16:46:08.5202008-11-13 16:46:08.5202008-11-12 16:46:08.5202008-11-11 16:46:08.5202008-11-10 16:46:08.5202008-11-09 16:46:08.5202008-11-08 16:46:08.5202008-11-07 16:46:08.5202008-11-06 16:46:08.5202008-11-05 16:46:08.5202008-11-04 16:46:08.5202008-11-03 16:46:08.5202008-11-02 16:46:08.5202008-11-01 16:46:08.5202008-10-31 16:46:08.5202008-10-30 16:46:08.5202008-10-29 16:46:08.5202008-10-28 16:46:08.520I Struggle For Excellence |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-17 : 07:20:33
|
quote: Originally posted by Nageswar9 declare @t table (date Datetime)declare @i intset @i = 1while (@i<=20)begininsert into @tselect getdate() -@iselect @i = @i +1endselect * from @tdDates--------------2008-11-16 16:46:08.5202008-11-15 16:46:08.5202008-11-14 16:46:08.5202008-11-13 16:46:08.5202008-11-12 16:46:08.5202008-11-11 16:46:08.5202008-11-10 16:46:08.5202008-11-09 16:46:08.5202008-11-08 16:46:08.5202008-11-07 16:46:08.5202008-11-06 16:46:08.5202008-11-05 16:46:08.5202008-11-04 16:46:08.5202008-11-03 16:46:08.5202008-11-02 16:46:08.5202008-11-01 16:46:08.5202008-10-31 16:46:08.5202008-10-30 16:46:08.5202008-10-29 16:46:08.5202008-10-28 16:46:08.520I Struggle For Excellence
To include today's date toodeclare @t table (date Datetime)declare @i intset @i = 0while (@i<20)begininsert into @tselect getdate() -@iselect @i = @i +1endselect * from @tMadhivananFailing to plan is Planning to fail |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-11-17 : 08:05:06
|
| --------------------------------------------------------------------------------Originally posted by Nageswar9declare @t table (date Datetime)declare @i intset @i = 1while (@i<=20)begininsert into @tselect getdate() -@iselect @i = @i +1endselect * from @tdDates--------------2008-11-16 16:46:08.5202008-11-15 16:46:08.5202008-11-14 16:46:08.5202008-11-13 16:46:08.5202008-11-12 16:46:08.5202008-11-11 16:46:08.5202008-11-10 16:46:08.5202008-11-09 16:46:08.5202008-11-08 16:46:08.5202008-11-07 16:46:08.5202008-11-06 16:46:08.5202008-11-05 16:46:08.5202008-11-04 16:46:08.5202008-11-03 16:46:08.5202008-11-02 16:46:08.5202008-11-01 16:46:08.5202008-10-31 16:46:08.5202008-10-30 16:46:08.5202008-10-29 16:46:08.5202008-10-28 16:46:08.520I Struggle For Excellence--------------------------------------------------------------------------------To include today's date toodeclare @t table (date Datetime)declare @i intset @i = 0while (@i<20)begininsert into @tselect getdate() -@iselect @i = @i +1endselect * from @tMadhivananFailing to plan is Planning to failhai madhivanan,then set @i value to 0 then u will get today's date also |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-17 : 08:08:33
|
<<hai madhivanan,then set @i value to 0 then u will get today's date also>>Thats what I did. See my previous reply MadhivananFailing to plan is Planning to fail |
 |
|
|
|