SQL命令的Select查询之通配符

1、通配符_(下划线)

功能:代表一个字符
用法:只能用在where字句中,通常_与运算符like配合使用

/*查询LastName中第三个字符是a的人*/
select LastName from Employees where LastName like '__a%';

/*查询LastName中第三个字符是a,并且LastName只有三个字符*/
select LastName from Employees where LastName like '__a';

2、通配符[](方括号)

/*[]用来限定单个字符界定于指定的范围或集合中*/
select City from Customers where City like '[A-E]%'
select City from Customers where City like '[MC]%'

/*查询电话号码 :第一位为2-4的电话号码*/
select Phone from Customers where Phone like '[2-4]%'

/*查询电话号码 :第一位为2或4的电话号码*/
select Phone from Customers where Phone like '[24]%'

/*查询城市 :第一字符为B,第二字符为A或E的城市*/
select City from Customers where City like 'B[AE]%'

/*重点注意:查询姓名 查得姓名完全是中文文字(不包含任何英文或数字)*/
select name from my where name not like '%[a-z]%' and name not like '%[0-9]%'

3、 [^] 不在指定的范围或集合中,相当于“非[]”

如:[^A-D] [^1-9] 或者 [^ACD] [^135]

/*第一个字符不包括A、B、C、D*/
select LastName from Employees where LastName like '[^A-D]%'

/*第一个字符不是A、C、D*/
select LastName from Employees where LastName like '[^ACD]%'

原文地址:
http://www.cnblogs.com/quanjia/archive/2010/08/10/1796694.html

This entry was posted in Technology and tagged , , , . Bookmark the permalink. 2,110 views

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>