I have a table like below:
Create table EmpDetails
(
ID int primary key,
DOB datetime not null,
Name nvarchar(100) not null,
Salary float not null,
Email nvarchar(50) not null,
IsActive bit not null
)
Insert into EmpDetails VALUES (2134, '1985-09-19 03:37:21.757', 'Jack Sparrow', 75000,'abc@gmail.com', 1)
When I tried to convert the columns into Rows using below query I am getting error and the query failed.
select ID, Attrib, Value
from EmpDetails
cross apply
(
select 'DOB', DOB union all
select 'Name', Name union all
select 'Salary', Salary union all
select 'Email', Email union all
select 'IsActive', IsActive
)empd(Attrib, Value);
EDIT: I have also tried below, but got same error
select ID, Attrib, Value
from EmpDetails
cross apply
(
values
('DOB', DOB),
('Name', Name),
('Salary', Salary),
('Email', Email),
('IsActive', IsActive)
) empd(Attrib, Value);
Error:
Conversion failed when converting date and/or time from character string.
When I tried to CAST the DOB field as datetime
still I am getting same error:
select 'DOB', CAST(DOB as datetime)
How can I proceed here ? Any help is appreciated.