【发布时间】:2019-06-05 15:36:11
【问题描述】:
我有一个名为“companyInfo”的表,“Amount”的数据类型是nvarchar,示例数据如下所示:
公司信息
IDs | company | year | Amount
----+-----------+------+-------
1 | Company A | 2011 | 40.00
2 | Company B | 2011 | Null
2 | Company C | 2011 | 100.00
4 | Company D | 2011 | 205.11
5 | Company E | 2011 | 0
6 | Company F | 2011 | Null
我写了如下查询:
select IDs
, company
, sum(ISNULL(CAST(Amount AS MONEY), 0)) Amount
, year
from companyInfo
where Amount is not null
and year(cast(year as date)) = '2018'
group by IDs
, company
, Amount
, year
出现以下错误:
错误:
消息 235,第 16 级,状态 0,第 27 行 无法将 char 值转换为货币。 char 值的语法不正确。
【问题讨论】:
-
使用 try_convert(money,...) 如果转换失败而不是抛出错误,它将返回 NULL。识别问题值 Select * from yourtable where try_convert(money,Amount) is null
-
谢谢@JohnCappelletti,它通过使用“try_convert”起作用,不确定为什么我在使用 CAST 时出错
标签: sql sql-server database data-analysis