I am attempting to unpivot a table column but am encountering a conversion error from varchar to int. All of the columns in my table are ints except one, which is varchar. The error is detailed below.
I don't want to use any type of conversion.
Conversion failed when converting the varchar value 'View Test' to data type int.
DECLARE @tableData AS TABLE
(
TD_ID INT PRIMARY KEY IDENTITY(1,1),
View0 INT,
View1 INT,
View2 INT,
Remarks Varchar(20)
)
INSERT INTO @tableData (View0, View1, View2, Remarks)
VALUES(1, 0, 1, 'View Test')
SELECT column_Name, column_value
FROM @tableData
CROSS APPLY (
VALUES
('View0',View0)
, ('View1',View1)
, ('View2',View2)
, ('Remarks',Remarks)
) Q (column_Name, column_value)
Like @Dale K said, you can convert to varchar first. Here is the sample code:
Here is the fiddle link
Or you can use like this too:
Here is the fiddle link