unpivot in sql server above version 2016

 Declare @YourTable Table ([ID] varchar(50),[Col1] varchar(50),[Col2] varchar(50))

Insert Into @YourTable Values 

 (1,'A','B')

,(2,'R','C')

,(3,'X','D')


Select A.[ID]

      ,Item  = B.[Key]

      ,Value = B.[Value]

 From  @YourTable A

 Cross Apply ( Select * 

                From  OpenJson((Select A.* For JSON Path,Without_Array_Wrapper )) 

                Where [Key] not in ('ID','Other','Columns','ToExclude')

             ) B




Comments

Popular posts from this blog

UNPIVOT in sql server