sql - 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 时转换失败。
解决方案
您的字符串(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
推荐阅读
- reactjs - React 中的异步调用与 Back4app
- node.js - 如何创建同一个快速端点的 POST 和 GET 版本?
- javascript - Vue插值中的样式文本
- python-3.x - 出错时自动重新启动python脚本
- javascript - 如何创建具有这种效果的菜单?
- azure-pipelines - 如何使用 Azure PowerShell 读取和设置 DevOps 管道变量?
- python - 用于排序循环的 odoo 电子邮件模板
- python - 浏览器顶部的python屏幕截图
- php - FPDF - 在多列表上输出跳过行
- javascript - 如何在 Angular 的服务和组件之间共享数据?