SQL中varchar类型staff_id字段查询'3032'无结果问题排查
问题现象
- 执行无过滤条件查询:
select staff_id from table1;
返回结果如下,可确认结果集中存在staff_id为3032的记录:
staff_id ----- 3032 3036 3037 3037
- 执行带字符串等值过滤条件的查询:
select staff_id from table1 where staff_id = '3032'
无任何结果返回,无法匹配到对应记录。
补充排查信息
- 传入int类型参数执行查询:
select staff_id from table1 where staff_id = 3032
抛出如下错误:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '3032 ' to data type int.
(报错说明:将varchar值'3032 '转换为int数据类型时转换失败)
- 尝试匹配带末尾空格的字符串,执行查询:
select staff_id from table1 where staff_id = '3032 '
仍无结果返回。
- 查询字段元数据,执行语句:
select * from information_schema.columns where column_name = 'staff_id';
返回结果确认table1表的staff_id字段属性为:非空(IS_NULLABLE=NO)、数据类型为varchar、字符最大长度为5。
参考排查语句
用户@David דודו Markovitz提供如下排查语句:
select cast(staff_id as varchar(5)) from table1 where staff_id like '3032%'
内容的提问来源于stack exchange,提问作者wel
相关产品推荐
相关产品推荐

