SQL Server Cast varchar列值'1218300.00'为int

时间:2014-10-30 22:27:23

标签: sql-server casting varchar

列中的大值导致我的SQL抛出错误消息: 将数据类型varchar转换为数字

时出错

我已将此信息隔离到特定行。这是一个简化的脚本,"工作":

SELECT 
  MtgeLoanAmount
  , CAST(convert(numeric(15,2),'1218300.00') as int) as TrialValue
FROM dbo.Processed_VA_From_Excel
  where FipsStateCode='06'
  and FipsCountyCode='013' 
  and GuarantyAmount = '304575'

返回粘贴结果: enter image description here

所以,当我尝试"概括"我的测试通过添加第3列,如下所示无法转换:

SELECT
MtgeLoanAmount
, CAST(convert(numeric(15,2),'1218300.00') as int)  as TrialValue 
, CAST(convert(numeric(15,2),MtgeLoanAmount) as int)
FROM dbo.Processed_VA_From_Excel
where 
    FipsStateCode='06'
and FipsCountyCode='013' 
and GuarantyAmount = '304575'

返回:

Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.

1 个答案:

答案 0 :(得分:1)

这可能对您有用:

SELECT
  MtgeLoanAmount,
  CONVERT(INT, ROUND(MtgeLoanAmount, 0)) AS MtgeLoanAmountNoCents
FROM
  dbo.Processed_VA_From_Excel
WHERE
  FipsStateCode = '06' AND
  FipsCountyCode = '013' AND
  GuarantyAmount = '304575'