案例陈述中的TODATETIMEOFFSET

时间:2014-09-02 16:09:10

标签: sql datetimeoffset

我正在尝试使TODATETIMEOFFSET在case语句中工作。当我尝试这样做时,sql会返回错误信息。如果它不在案例陈述中,则可以正常工作。我究竟做错了什么?

SELECT AP.POR,
       AP.POD Path,
       TODATETIMEOFFSET(AP.StartTime, '-06:00') as StartTime,
       MinimumPrice = (
         CASE WHEN ( CHARINDEX('','' + '''+@Provider+''' + '','', '','' + '''+@UserCompanyList+''' + '','') > 0 ) 
                THEN TODATETIMEOFFSET(AP.MinimumPrice, '-06:00')
              ELSE ( (
                CASE WHEN (getdate() < C.ClearingTime and C.OpenPriceMask = 0)
                       THEN NULL
                     WHEN (getdate() > C.ClearingTime and C.ClearedPriceMask = 0)
                       THEN NULL 
                     ELSE AP.MinimumPrice
                END
              ) )
         END
       ), 
       AP.ClearingPrice,
       AP.PriceUnits
  FROM TES_Auction C
       INNER JOIN TES_AuctionPrice AP ON AP.AuctionID = C.ID
Msg 206, Level 16, State 2, Line 1
Operand type clash: decimal is incompatible with datetime2

我该如何制作&#39; as&#39; case语句中的语法工作?

1 个答案:

答案 0 :(得分:4)

您的查询的这部分内容似乎有误:

THEN TODATETIMEOFFSET(AP.MinimumPrice, '-06:00')

AP.MinimumPrice我希望得到十进制类型,而SQL不能将小数转换为datetime2

此外,要在case语句中使用列别名,您可以将它放在下一个逗号之前的最后,例如:

ELSE AP.MinimumPrice END))END) AS myColumnName, -- etc.