我正在尝试抓取信息以发送到Google Charts的图表我正在查看Active
列,该列可以是true / false但在我的数据库中设置为varchar。
使用此查询:
SELECT
SUM(CASE WHEN CONVERT(int, Active) Active = 1 THEN 1 ELSE 0 END) AS Active,
SUM(CASE WHEN CONVERT(int, Active) Active = 0 THEN 1 ELSE 0 END) AS Inactive
FROM
Events
抛出此错误:
将varchar值'True'转换为数据类型int。
时转换失败
答案 0 :(得分:2)
Ints不是布尔值......它们在SQL Server中被称为位。尝试;
SELECT
SUM(CASE WHEN CONVERT(bit, Active) = 1 THEN 1 ELSE 0 END) AS Active,
SUM(CASE WHEN CONVERT(bit, Active) = 0 THEN 1 ELSE 0 END) AS Inactive
FROM Events
或者只测试你的String
SELECT
SUM(CASE WHEN Active = 'True' THEN 1 ELSE 0 END) AS Active,
SUM(CASE WHEN Active = 'False' THEN 1 ELSE 0 END) AS Inactive
FROM Events
P.S。将布尔值存储为String列是愚蠢的...考虑将列更改为位列,这样您甚至不需要转换它