首页 > 解决方案 > SQL 数据转换

问题描述

任何想法为什么这不起作用?

 Select [Name]
      ,TimeStamp
      ,UserKey
      ,Field
      ,WasValue
      ,Cast(IsValue as int)
    From DebtorHistory
    Left Join Debtors on Debtors.DebtorKey = DebtorHistory.DebtorKey
    Where Field = 'Total Credit Limit'
    and TimeStamp > GETDATE()-7

尝试将 isValue 转换为 int 时出现此错误:

将 nvarchar 值“15,000.00”转换为数据类型 int 时转换失败。

标签: sql

解决方案


您的字符串(IsValue)中有千位分隔符“,”不能转换为int,您可以通过以下方式将其删除replace

Select [Name]
      ,TimeStamp
      ,UserKey
      ,Field
      ,WasValue
      ,Cast(replace(IsValue,',','') as int)  -- <-- replace , with empty string
    From DebtorHistory
    Left Join Debtors on Debtors.DebtorKey = DebtorHistory.DebtorKey
    Where Field = 'Total Credit Limit'
    and TimeStamp > GETDATE()-7

推荐阅读