Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


split single row into four rows.


split single row into four rows.

Author
Message
abhas
abhas
SSC Veteran
SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)

Group: General Forum Members
Points: 280 Visits: 662
Hi All,

I am having a table which contains 4 field as below.

stuid,studname,startdate,starttime,endtime.

now if starttime is 7am and endtime is 8am then i want to display data in the same table in the interval of 15 mins.

example if the user select 7am as starttime and 9am as endtime then 8 rows wolud be populated as below.

Studentid Studentname effectivedate starttime endtime
1 john 2012/23/03 7am 7.15am
1 john 2012/23/03 7.15am 7.30am
1 john 2012/23/03 7.30am 7.45am
1 john 2012/23/03 7.45am 8am
1 john 2012/23/03 8am 8.15am
1 john 2012/23/03 8.15am 8.30am
1 john 2012/23/03 8.30am 8.45am
1 john 2012/23/03 8.45am 9am


Thanks
Abhas.
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8957 Visits: 19016
Can you post ddl and dml for your source table please? If you're not sure how to do this, have a read of the link in my sig.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
matak
matak
Old Hand
Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)

Group: General Forum Members
Points: 328 Visits: 3521
Something like this ?

IF OBJECT_ID('TempDB..#test','U') IS NOT NULL
drop table #test
create table #test
(studentid int, studentname varchar(20), startdate datetime, starttime datetime, endtime datetime
)

insert into #test
select 1, 'john', '2012-01-03 00:00:00', '2012-01-03 07:00:00', '2012-01-03 09:00:00'

select studentid, studentname, startdate, DATEADD(mi, (N-1)*15, starttime), DATEADD(mi, N*15, starttime)
from #test cross join Tally
where n >= 1 and n <= datediff(mi, starttime, endtime)/15


abhas
abhas
SSC Veteran
SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)

Group: General Forum Members
Points: 280 Visits: 662
Thanks matak.

I will try this.But my actual requirement is to insert this data into table after selecting from front end dropdown. for example if user selects date like 2012/03/23 and select start time as 7am and end time as 8am then i want to insert this by splitting into four rows.

Thanks
Abhas.
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8957 Visits: 19016
abhas (4/9/2013)
Thanks matak.

I will try this.But my actual requirement is to insert this data into table after selecting from front end dropdown. for example if user selects date like 2012/03/23 and select start time as 7am and end time as 8am then i want to insert this by splitting into four rows.

Thanks
Abhas.


Assuming you want to pass the date and the two times into SQL Server, what datatype are these three variables?

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
matak
matak
Old Hand
Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)

Group: General Forum Members
Points: 328 Visits: 3521
That should be fine.
Try this. Just uncomment the insert into statement and replace with your actual table


declare @studentid as datetime = '2012-01-03 00:00:00'
declare @studentname as varchar(20) = 'john'
declare @startdate as datetime = '2012-01-03 00:00:00'
declare @starttime as datetime = '2012-01-03 07:00:00'
declare @endtime as datetime = '2012-01-03 09:00:00'

--insert into tablename
select @studentid, @studentname, @startdate, DATEADD(mi, (N-1)*15, @starttime), DATEADD(mi, N*15, @starttime)
from Tally
where n >= 1 and n <= datediff(mi, @starttime, @endtime)/15


matak
matak
Old Hand
Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)

Group: General Forum Members
Points: 328 Visits: 3521
Keep in mind what Chris says - im assuming they are all datetime.
The code will need to change if they are different.
abhas
abhas
SSC Veteran
SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)

Group: General Forum Members
Points: 280 Visits: 662
Hi Matak/Chris,

I want to use starttime and endtime as varchar. is it possible?

Thanks
Abhas.
abhas
abhas
SSC Veteran
SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)SSC Veteran (280 reputation)

Group: General Forum Members
Points: 280 Visits: 662
Hi Matak,

Could you please let me know the purpose of DATEADD(mi, (N-1)*15 as i am getting below error in your script.

Msg 207, Level 16, State 1, Line 11
Invalid column name 'n'.
Msg 207, Level 16, State 1, Line 11
Invalid column name 'n'.
Msg 207, Level 16, State 1, Line 9
Invalid column name 'N'.
Msg 207, Level 16, State 1, Line 9
Invalid column name 'N'.


Thanks
Abhas.
matak
matak
Old Hand
Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)Old Hand (328 reputation)

Group: General Forum Members
Points: 328 Visits: 3521
Do a search for tally table on the forums - you need one.
N is the column name in mine
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search