【问题标题】:"Date" conflicts with the type of other columns specified in the UNPIVOT list SQL server“日期”与 UNPIVOT 列表 SQL server 中指定的其他列的类型冲突
【发布时间】:2014-08-21 11:41:18
【问题描述】:

我正在这样做:

select    
--ID,
 Value
 from dbo.[staging]
 unpivot
 (
    Value
  for col in ([ Code]
  , [Date]
  ,[ Registered Name]
  ,[Entity Name]
  ,[transactions]
  ,[ PID])

 ) un

我遇到了错误:

“日期”与 UNPIVOT 列表中指定的其他列的类型冲突。

注意:我的临时表有日期时间类型的日期列。

我该如何解决这个错误?请帮忙。

【问题讨论】:

    标签: sql-server tsql sql-server-2005 pivot unpivot


    【解决方案1】:

    通常您会在unpivot 中包含其他列,例如标识符。但是解决您的问题的方法是使用子查询将列转换为varchar()

    select --ID, Value
    from (select s.*, convert(varchar(255), [date], 121) as datestr
          from dbo.[staging]
         ) t
    unpivot (Value for col in ([ Code], datestr, [ Registered Name], [Entity Name], [transactions], [ PID]
                              )
            ) un;
    

    【讨论】:

    • 出现错误:关键字'from'附近的语法不正确,'t'附近的语法不正确。
    • 修改了上面的查询。它应该是“convert(nvarchar(255),...”。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    相关资源
    最近更新 更多