Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have the following query works fine, but if I were to additional columns to unpivot,I am getting the error.


Source Table Data:

ID StateID SourceID Department EffectiveDate
8 36 1 All 2015-08-01
65 36 2 All 2015-08-01

Expected Output:

FieldName FieldValue
ID 8
StateID 36
SourceID 1
Department All
EffectiveDate 2015-08-01
ID 65
StateID 36
Department All
EffectiveDate 2015-08-01
Posted
Comments
ZurdoDev 2-Dec-15 8:33am    
1. What is the error?
2. The error means something is wrong with the sql, right? So, post your sql.
3. Click Improve question and provide us more info.

SQL
declare @tab table(
	ID int, StateID int, SourceID int, Department varchar(max), EffectiveDate date
)	
insert into @tab values
(8,	36,	1, 'All', '2015-08-01'),
(65, 36, 2, 'All', '2015-08-01')

SELECT FieldName, FieldValue
FROM   (
    SELECT	cast(ID as varchar) as ID 
			, cast(StateID as varchar) as StateID 
			, cast(SourceID as varchar) as SourceID
			, cast(Department as varchar) as Department
			, cast(EffectiveDate as varchar) as EffectiveDate
    FROM	@tab
) AS t1  
UNPIVOT (
    FieldValue FOR FieldName IN (ID, StateID, SourceID, Department, EffectiveDate)
) AS t2
 
Share this answer
 
v2
Please refer the below link, you will get a fair idea on how to implement it.

http://blog.sqlauthority.com/2008/05/29/sql-server-unpivot-table-example/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900