即使将其转换为varchar之后也会出错:将数据类型varchar转换为float时出错,

时间:2017-02-17 05:45:46

标签: sql sql-server

SELECT a.[Emp_No],
       a.[Emp_Name],
       a.[Band],
       b.[Max_Exp] AS Max_Yrs_Experience,
       CASE WHEN a.Performance_Score = 0 AND (a.Status_Active > 0 OR a.Status_New > 0)
            THEN CONVERT(varchar, 0)
            WHEN a.Performance_Score > 0
            THEN [Performance_Score]
            ELSE 'No History Data Found'
       END AS Performance_Score
INTO #EXPER
FROM #PERFORMANCE a, #agg_exp b
WHERE a.[Emp_No] = b.[Emp_No]
ORDER BY Performance_Score DESC
  

将varchar转换为float时出错。

我不知道为什么会收到此错误。

1 个答案:

答案 0 :(得分:1)

您遇到的困难是由于SQL Server要求CASE表达式中生成的所有可能值都具有相同的类型。由于您的ELSE使用的文字无法转换为任何数字类型,因此唯一的选择是将数字转换为文字。

CASE WHEN a.Performance_Score = 0 AND (a.Status_Active > 0 OR a.Status_New > 0)
     THEN CONVERT (VARCHAR(10), 0)                     -- zero as text
     WHEN a.Performance_Score > 0
     THEN CONVERT (VARCHAR(10), [Performance_Score])   -- performance score as text
     ELSE 'No History Data Found'                      -- this is already text
END AS Performance_Score

通过错误消息,我假设[Performance Score]是一个浮点列。如果是其他类型,则必须适当修改对CONVERT()的调用。