Click here to Skip to main content
15,888,119 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi every one.

I want a collection between two times with special increment.

For example i have two times ( 08:00:00 And 16:00:00 , And also increment 00:30:00 )

I need all times between these times, by that increment, in this example it means :

{08:00:00 , 08:30:00, 09:00:00, 09:30:00, 10:00:00 , ... , , 16:00:00}


So i want a "Select" That do that.

Any suggestion please ?
Thanks in advance.
Posted
Comments
Abhinav S 29-Nov-11 6:47am    
What have you tried so far?

Hi Armin,
Here it is :

SQL
declare @st time
set @st = '8:0:0'

declare @ft time
set @ft = '16:0:0';

declare @interval int
set @interval = 30;

with a( gt )
as
(
select @st 
union all
select dateadd(mi, @interval , gt) sequence from a where dateadd(mi, @interval , gt)<=@ft
)
select * from a



Hope it helps.
 
Share this answer
 
Comments
arminamini 29-Nov-11 7:42am    
Great!
Amir Mahfoozi 29-Nov-11 7:57am    
Thanks ;)
RaisKazi 29-Nov-11 8:41am    
Perfect Answer. My 5.
Amir Mahfoozi 29-Nov-11 9:27am    
Thank you Rais :)
try this..

SQL
DECLARE @Start_Time DATETIME, @End_Time DATETIME
SET @Start_Time = CONVERT(DATETIME,'08:00:00',103)
SET @End_Time = CONVERT(DATETIME,'16:00:00',103)

WHILE(@Start_Time <= @End_Time)
BEGIN
   SELECT CONVERT(VARCHAR(8),@Start_Time,108)
   SET @Start_Time = (SELECT DATEADD(minute,30,@Start_Time))
END


hope this helps...
 
Share this answer
 
Comments
arminamini 29-Nov-11 7:25am    
Hi. its good idea. but it can be great if i have these Times in One Select and many Rows. because i need to make this select as a gridview datasource. is it possible?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900