读书人

下划线_代表一个汉字或字母吗?该如何

发布时间: 2012-03-16 16:34:56 作者: rapoo

下划线_代表一个汉字或字母吗?
我原以为是两个_ _ 代表一个汉字
结果一试 like '张_ _ '
张a
张飞
都可以!


[解决办法]
如果你的字段是nchar 不是char那么的确是这样,
因为nchar 不管是什么字母都占两位

或者你的字段是char 不是 varchar 那么也是这样
因为 张a 后面还有 空格,所以也能匹配
[解决办法]
“_”表示任何单个字符。一个汉字、一个字母、一个数字、一个符号、一个其它字符集的字符,都是一个字符。

like '张_ _ '
应该“张a”和“张飞”都查找不出来吧

declare @temp table (test varchar(10))
insert @temp select '张飞 ' union all select '张a ' union all select '张弓长 '
select * from @temp where test like '张_ '
select * from @temp where test like '张__ '



[解决办法]
declare @temp table (test varchar(10))
insert @temp select '张飞 ' union all select '张a ' union all select '张弓长 '
select * from @temp where test like '张_ '
select * from @temp where test like '张__ '

/*结果
test
----------
张飞
张a

test
----------
张弓长
*/


declare @temp table (test char(10))
insert @temp select '张飞 ' union all select '张a ' union all select '张弓长 '
select * from @temp where test like '张_ '
select * from @temp where test like '张__ '

/*结果
test
----------
张飞
张a

test
----------
张飞
张a
张弓长
*/

读书人网 >SQL Server

热点推荐