我坚持这个。
基本上我选择VARCHAR
类型的列中的值。但是当我选择这些值时,我需要将CAST
/ CONVERT
全部转换为整数。
问题出现在列I中,从某些值中选择值包含字符串字符,因此当我运行查询时,出现错误,说明以下内容:
Msg 245,Level 16,State 1,Line 1转换时转换失败 varchar值' NA'数据类型int。
这是我的select
声明
SELECT DISTINCT
DP.Owner_Faculty_Full_Desc AS 'Faculty'
, DP.Programme_Full_Desc AS 'Programme'
, LEFT(FA.Teaching_Start_Date_Key, 4) AS 'Year'
, SD.Last_Secondary_School_Name AS 'School'
, CAST(CASE WHEN SD.Last_Secondary_School_Decile_Code = 'UN' THEN '0' ELSE SD.Last_Secondary_School_Decile_Code END AS INT) AS 'Decile'
, COUNT(FA.Student_Demographics_Key) AS 'Total Applicants 2014'
, SUM(SE.Total_EFTS) AS 'Total EFTS'
我需要将SD.Last_Secondary_School_Code
列中的所有值作为整数拉入。
答案 0 :(得分:2)
我希望知道这个新的(至少对我:) :)语法很好:
SELECT
id,
value,
IIF(TRY_PARSE(value AS int) IS not NULL, value, 0) AS Result
FROM
table1
参考:https://msdn.microsoft.com/en-us/library/hh213126(v=sql.110).aspx
答案 1 :(得分:1)
如果我理解正确,只需使用case
:
(CASE WHEN SD.Last_Secondary_School_Decile_Code = 'UN' THEN 0
WHEN SD.Last_Secondary_School_Decile_Code not like '%[^0-9]%'
THEN CAST(SD.Last_Secondary_School_Decile_Code AS INT)
END) AS Decile