Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

split single row into four rows. Expand / Collapse
Author
Message
Posted Tuesday, April 9, 2013 12:29 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, August 22, 2014 5:49 AM
Points: 195, Visits: 497
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.
Post #1440163
Posted Tuesday, April 9, 2013 1:03 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:55 AM
Points: 7,219, Visits: 13,684
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
Post #1440177
Posted Tuesday, April 9, 2013 1:04 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 11:55 PM
Points: 225, Visits: 1,930
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

Post #1440178
Posted Tuesday, April 9, 2013 1:28 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, August 22, 2014 5:49 AM
Points: 195, Visits: 497
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.
Post #1440186
Posted Tuesday, April 9, 2013 1:35 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:55 AM
Points: 7,219, Visits: 13,684
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
Post #1440191
Posted Tuesday, April 9, 2013 1:37 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 11:55 PM
Points: 225, Visits: 1,930
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

Post #1440194
Posted Tuesday, April 9, 2013 1:38 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 11:55 PM
Points: 225, Visits: 1,930
Keep in mind what Chris says - im assuming they are all datetime.
The code will need to change if they are different.
Post #1440196
Posted Tuesday, April 9, 2013 3:20 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, August 22, 2014 5:49 AM
Points: 195, Visits: 497
Hi Matak/Chris,

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

Thanks
Abhas.
Post #1440230
Posted Tuesday, April 9, 2013 3:33 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, August 22, 2014 5:49 AM
Points: 195, Visits: 497
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.
Post #1440235
Posted Tuesday, April 9, 2013 4:04 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 11:55 PM
Points: 225, Visits: 1,930
Do a search for tally table on the forums - you need one.
N is the column name in mine
Post #1440244
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse