关于db2 between and 的疑问
假设table: A(column VARCHAR(18))其中存有身份证号码。身份证号除末尾含字母,其他为数字。
用select * from A where substr(column,9,2) between '00' and '99' ,不能排除如‘55013419681M032016’这样的记录。
请问这是怎么一回事?
...全文
12972打赏收藏
关于db2 between and 的疑问
关于db2 between and 的疑问 假设table: A(column VARCHAR(18))其中存有身份证号码。身份证号除末尾含字母,其他为数字。 用select * from A where substr(column,9,2) between '00' and '99' ,不能排除如‘55013419681M032016’这样的记录。 请问这是怎么一回事?
--呵呵,看来还是旁观者清啊,你不是between '00' and '99'吗,这条记录substtr完后是68,当然在这之间了。怎么能排除呢。要排除加个not:
Select * from (values substr('55013419681M032016',9,2),'99','00') as test(id) where id not between '00' and '99';
我觉得如果确定substr完后不包括字母还是转成数值行再做:
Select * from (values substr('55013419681M032016',9,2),'99','00') as test(id) where int(id) <0 and int(id) >99;
条件当然也可以变:
Select * from (values substr('55013419681M032016',9,2),'99','00') as test(id) where int(id) >=0 and int(id) <=99;