| Author |
Topic |
|
haz33
Starting Member
15 Posts |
Posted - 2008-05-22 : 11:37:50
|
| Hi, does anyone know a SQL statement that will take two DATETIME values and list all minutes in between them like so:START VALUE: 5/22/2008 10:00END VALUE: 5/22/2008 10:10RESULT:5/22/2008 10:005/22/2008 10:015/22/2008 10:025/22/2008 10:035/22/2008 10:045/22/2008 10:055/22/2008 10:065/22/2008 10:075/22/2008 10:085/22/2008 10:095/22/2008 10:10Thanks! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-22 : 11:42:57
|
| [code]SET DATEFORMAT mdyDECLARE @StartDate,@EndDateSELECT @StartDate='5/22/2008 10:00',@EndDate='5/22/2008 10:10'SELECT DATEADD(mi,number,@StartDate)FROM master..spt_valuesWHERE type='p'AND DATEDIFF(mi,DATEADD(mi,number,@StartDate),@EndDate)=0[/code] |
 |
|
|
haz33
Starting Member
15 Posts |
Posted - 2008-05-22 : 11:53:35
|
| If I do that, I only get one row as a result:2008-05-22 10:10:00.000 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-22 : 12:11:21
|
quote: Originally posted by haz33 If I do that, I only get one row as a result:2008-05-22 10:10:00.000
SET DATEFORMAT mdyDECLARE @StartDate,@EndDateSELECT @StartDate='5/22/2008 10:00',@EndDate='5/22/2008 10:10'SELECT DATEADD(mi,number,@StartDate)FROM master..spt_valuesWHERE type='p'AND DATEADD(mi,number,@StartDate)<=@EndDate |
 |
|
|
haz33
Starting Member
15 Posts |
Posted - 2008-05-22 : 12:37:29
|
| Looks like it works. Thanks for the help! |
 |
|
|
haz33
Starting Member
15 Posts |
Posted - 2008-05-22 : 14:22:53
|
| One follow-up on this......I see that the master..spt_values table only contains a limited number of records (2,047, I believe). So if I needed a huge list of times, I couldn't get more than that. What do I do if I need more? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-22 : 14:26:41
|
quote: Originally posted by haz33 One follow-up on this......I see that the master..spt_values table only contains a limited number of records (2,047, I believe). So if I needed a huge list of times, I couldn't get more than that. What do I do if I need more?
If you want more than that, create a count table yourselves containing number of values you want and use it in place of spt_vaues.spt_values is simply used as a count table in above query. |
 |
|
|
haz33
Starting Member
15 Posts |
Posted - 2008-05-22 : 15:13:21
|
| This was one way to generate a list of dates between two separate dates. It's completely independent of other tables and can create a seemingly endless number of rows.with mycte as(select cast('2008-05-01' as datetime) DateValueunion allselect DateValue+1 as DateValuefrom myctewhere DateValue+1<'2008-05-10')select DateValuefrom mycteOPTION (MAXRECURSION 0)I'd like to use something similar to this to generate a list of all times between the two dates in five-minute increments, but I can't figure out how to do it. Any ideas would be much appreciated. |
 |
|
|
haz33
Starting Member
15 Posts |
Posted - 2008-05-22 : 15:42:09
|
| Never mind. I think I got it. Here's the solution for five-minute intervals between two dates.....with mycte as(select cast('2008-05-01' as datetime) DateValueunion allselect dateadd(n,5,cast(DateValue as datetime))from myctewhere dateadd(n,5,cast(DateValue as datetime))<'2008-05-10')select DateValuefrom mycteOPTION (MAXRECURSION 0) |
 |
|
|
|