如何在 SQL SERVER 中将 varchar 列转换为位列
Flag1
是一个 varchar
列,其值为true"和false".我需要将其转换为位列.
Flag1
is a varchar
column with values "true" and "false". I need to convert this into bit column.
当我尝试这样做时:
Convert(Bit,Flag1)
显示错误
Msg 245, Level 16, State 1, Line 2
Syntax error converting the varchar value 'False' to a column of data type bit.
我怀疑在 'Flag1' 字段中除了 'true' 和 'false' 之外还有其他值.因此,请检查 Flag1 中的值.
I suspect that there are other values in addition to 'true' and 'false' in the field 'Flag1'. So check for the values in Flag1.
从 YouTable 中选择不同的 Flag1.
select distinct Flag1 from YouTable.
这是我的证明:
declare @Flag varchar(25) = 'False'
select CONVERT(Bit, @Flag)
效果很好.
然而,这会产生同样的错误.
However, this will give the same error.
declare @Flag varchar(25) = ' False' -- Pay attention to the the space in ' False'!
select CONVERT(Bit, @Flag)
-> 消息 245,级别 16,状态 1,第 2 行将 varchar 值False"转换为数据类型 bit 时转换失败.
-> Msg 245, Level 16, State 1, Line 2 Conversion failed when converting the varchar value ' False' to data type bit.
注意错误信息中'False'中的空格!
Pay attention to the the space in ' False' in the error message!