Error : The type of column “DOB” conflicts with the type of other columns specified in the UNPIVOT list

五迷三道 提交于 2019-12-11 15:25:43

问题


MY QUERY,

simply to convert the row into column

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;

But it showing an error.

The type of column "DOB" conflicts with the type of other columns specified in the UNPIVOT list.


回答1:


As the result will bring back all columns in rows, building a new derived column with all the values, you must ensure, that the types fit together.

You can wrap all your columns in 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;

The DOB will be converted to the default setting of your machine. Using CONVERT you might enforce a given date/time format.



来源:https://stackoverflow.com/questions/47904733/error-the-type-of-column-dob-conflicts-with-the-type-of-other-columns-specif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!