Abdy
Abdy

Reputation: 3

Convert a row into two row in sql server

I have an Attendance table with columns (Check_In, Check_Out). I want to split each row and insert into another column. How can I accomplish that?

For example:

Check_In, Check_Out 
23:10   , 08:10

Want to convert it to one column table

Entry_Time
23:10
08:10

Upvotes: 0

Views: 52

Answers (2)

GMB
GMB

Reputation: 222422

Just use union all:

select check_in entry_time from mytable
union all select check_out from mytable

For this to be easier to consume, you would need to add at least one column to the resultset so you can identify from which record the data originally came from. Assuming that the primary key of the table is id:

select id, check_in entry_time from mytable
union all select id, check_out from mytable

Upvotes: 1

Yogesh Sharma
Yogesh Sharma

Reputation: 50163

You can use apply :

select tt.*
from table t cross apply
     ( values (Check_In), (Check_Out) 
     ) tt(Entry_Time)
order by Entry_Time;

Upvotes: 3

Related Questions