Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I have a text file with content as followsdept_1 = |ajay|mukesh|suresh|dept_2 = |suresh|subash|chandrashaker|babu paul|and so onnow i need an out put of dept_name employee_name ------->column_namedept_1 ajaydept_1 mukeshdept_1 sureshdept_2 Sureshdept_2 subashdept_2 chandrashakerdept_2 babu paulplease provide a solution for this problem
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts
Posted - 2010-06-15 : 14:02:14
One way is to insert into a temp table and UNPIVOT like..
declare @t table (dept_name varchar(10), emp1 varchar(10), emp2 varchar(10), emp3 varchar(10), emp4 varchar(10))insert @tselect 'dept_1','ajay','mukesh','suresh',nullunion all select 'dept_2','suresh','subash','chandra','babu'SELECT dept_name, Employee_NameFROM (SELECT * FROM @t) pUNPIVOT (Employee_Name FOR Employee IN (emp1, emp2, emp3, emp4))AS unpvt