Skip to content
Advertisement

columns in SQL Server

I have a sample table below:

I would like to display the result from this table like below using SQL server:

Currently, I am using the messy method below in SQL server, text to column in Excel, and some data cleansing to get the result.

Step1:

Step2: Paste the data result into Excel and do a Text to columns (in Delimited) to separate each transaction of Airport and Datetimes like the display desired result as mentioned above.

Step3: Manually cleanup the data and convert the date columns in text back to datetime format.

As you can see, my messy method works but needs a lot of work. Is there a way in SQL server that can be written so it separates the data in Airport and Datetime respectively?

Advertisement

Answer

You can achieve this using dynamic Pivot, try like following query and you will get the desired output. For the simplicity purpose, I have used a temp table, if you want you can avoid it using a inline table.

Online Demo

Output :

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement