SQL Server | Other
Additional SQL Server features and topics not covered by specific categories
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I want NULL value still in the output.
See below sample:
Declare @tbl table (id int, val1 int,val2 int,val3 int)
insert into @tbl values
(1,23,null,34),(2,16,74,25),(3,33,24,null)
select *
from @tbl
Unpivot(val for val_type in ([val1],[val2],[val3]))u
Additional SQL Server features and topics not covered by specific categories
Answer accepted by question author
Hi @Siver
You could use Cross Apply like this:
SELECT id,C.*
FROM @tbl CROSS APPLY(VALUES(val1,'val1'),(val2,'val2'),(val3,'val3'))C(val,valtype)
Best regards,
Cosmog Hong