Error : The type of column "DOB" conflicts with the type of other columns specified in the UNPIVOT list
Error : The type of column "DOB" conflicts with the type of other columns specified in the UNPIVOT list
我的查询,
只是把行转成列
SELECT
ColumnName,
value
FROM (SELECT
id [ID],
firstname [First Name],
lastname [Last Name],
dob [DOB],
sex [Gender]
FROM client
WHERE id = '11') d
UNPIVOT
(
Value FOR
ColumnName IN ([ID], [First Name], [Last Name], [DOB], [Gender])
) unpiv;
但是显示错误。
The type of column "DOB" conflicts with the type of other columns specified in the UNPIVOT list.
由于结果将返回行中的所有列,构建包含所有值的新派生列,因此您必须确保类型适合。
您可以将所有列包装在 CAST
中
SELECT
ColumnName,
value
FROM (SELECT
CAST(id AS NVARCHAR(MAX)) [ID],
CAST(firstname AS NVARCHAR(MAX)) [First Name],
CAST(lastname AS NVARCHAR(MAX)) [Last Name],
CAST(dob AS NVARCHAR(MAX)) [DOB],
CAST(sex AS NVARCHAR(MAX)) [Gender]
FROM client
WHERE id = '11') d
UNPIVOT
(
Value FOR
ColumnName IN ([ID], [First Name], [Last Name], [DOB], [Gender])
) unpiv;
DOB
将转换为您机器的默认设置。使用 CONVERT
您可以强制执行给定的 date/time 格式。
我的查询,
只是把行转成列
SELECT
ColumnName,
value
FROM (SELECT
id [ID],
firstname [First Name],
lastname [Last Name],
dob [DOB],
sex [Gender]
FROM client
WHERE id = '11') d
UNPIVOT
(
Value FOR
ColumnName IN ([ID], [First Name], [Last Name], [DOB], [Gender])
) unpiv;
但是显示错误。
The type of column "DOB" conflicts with the type of other columns specified in the UNPIVOT list.
由于结果将返回行中的所有列,构建包含所有值的新派生列,因此您必须确保类型适合。
您可以将所有列包装在 CAST
SELECT
ColumnName,
value
FROM (SELECT
CAST(id AS NVARCHAR(MAX)) [ID],
CAST(firstname AS NVARCHAR(MAX)) [First Name],
CAST(lastname AS NVARCHAR(MAX)) [Last Name],
CAST(dob AS NVARCHAR(MAX)) [DOB],
CAST(sex AS NVARCHAR(MAX)) [Gender]
FROM client
WHERE id = '11') d
UNPIVOT
(
Value FOR
ColumnName IN ([ID], [First Name], [Last Name], [DOB], [Gender])
) unpiv;
DOB
将转换为您机器的默认设置。使用 CONVERT
您可以强制执行给定的 date/time 格式。