db2 like +% 匹配不能自动忽略char型尾部的空格
$ db2 describe table lc.t1
Column Type Type
name
schema name Length Scale Nulls
------------------------------
--------- ------------------ -------- ----- ------
C1
SYSIBM CHARACTER 8 0 Yes
C2
SYSIBM VARCHAR 4 0 Yes
2 record(s) selected.
--对于char类型的字段值,如果长度小于定义的字段长度,则db2会自动在其后补足空格
--即如下第一条记录中,c1字段实际存储的为
‘22355 ' (尾部是三个空格)
$ db2 "select * from lc.t1"
C1 C2
-------- ----
22355 232
33336777 3345
2 record(s) selected.
$ db2 "select length(c1) ,c1 from
lc.t1"
1 C1
----------- --------
8 22355
8 33336777
2 record(s) selected.
--使用"="操作符时,系统会自动忽略尾部空格
$ db2
"select * from lc.t1 where c1 ='22355'"
C1 C2
-------- ----
22355 232
1 record(s) selected.
--使用like %方式 系统不会忽略尾部空格,所以找不到纪录
$
db2 "select * from lc.t1 where c1 like '%355'"
C1 C2
-------- ----
0 record(s) selected.
--匹配时在尾部再增加一个%通配符,则可以找到记录
$ db2 "select * from lc.t1 where c1 like
'%355%'"
C1 C2
-------- ----
22355 232
1 record(s) selected.
-- 如果条件中补足所缺的三个空格,也可以查询到预期的结果
$ db2 "select * from lc.t1 where
c1 like '%355 '"
C1 C2
-------- ----
22355 232
1 record(s) selected.
--如果字段内容已经够8位,则末尾没有系统添加的空格,则可以查到预期的内
容
$ db2 "select * from lc.t1 where c1 like '%777'"
C1 C2
-------- ----
33336777 3345
1 record(s) selected.
--对于varchar字段类型,由于存储的就是实际值,尾部没有补充空格,所以可以检索到预期结果
$ db2 "select
length (c2),c2 from lc.t1"
1 C2
----------- ----
3 232
4 3345
2 record(s) selected.
$ db2 "select * from lc.t1 where c2
like '%32'"
C1 C2
-------- ----
22355 232
1 record(s) selected.
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22894392/viewspace-687498/,如需转载,请注明出处,否则将追究法律责任。