如何在 SQL SERVER 中将 varchar 列转换为位列 [英] How to convert a varchar column to bit column in SQL SERVER

查看:42
本文介绍了如何在 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!

这篇关于如何在 SQL SERVER 中将 varchar 列转换为位列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆