位数据类型匹配任何数字字符串

Bit datatype matches any string of numbers

我的数据库中有一列数据类型位。它只包含 1 或 0。如果我在 where 子句中将此列与“0”匹配,它只会返回带 0 的行。但是,如果我有此列并将其与随机数字符串匹配,它将在任何地方返回行该列是 1。为什么会这样?

SELECT  
aBitColumn
FROM aTableName
where aBitColumn='7256'

Converting to bit promotes any nonzero value to 1.

From Microsoft docs

首先,SQL 服务器通常不会将字符串转换为位。所以,你看到的是整数到位的转换。单引号无关紧要。

惯例是 0 以外的任何数字都转换为位 1

我建议从另一个方向进行转换:

where convert(varchar(255), aBitColumn) = '7256'

不要依赖隐式转换。