首页 > 代码库 > SqlServer--模糊查询-通配符

SqlServer--模糊查询-通配符

 

--通配符:_    、  %   、 []   、 ^

--  _  表示任意的单个字符

--姓张,两个字的。

select * from MyStudent where fname like ‘张_‘

--姓张,三个字的

select * from MyStudent where fname like ‘张__‘

 

-- % 匹配任意多个任意字符

--无论姓名字数,只要第一个字符是‘张‘的就查询出来

select * from MyStudent where fname like ‘张%‘

select * from MyStudent where fname like ‘张%‘ and len(fname)=2

 

--  []  表示筛选,范围。

select * from TblStudent

select * from TblStudent where tsname like ‘张[0-9]妹‘

select * from TblStudent where tsname like ‘张_妹‘

select * from TblStudent where tsname like ‘张[a-z]妹‘

select * from TblStudent where tsname like ‘张[a-z0-9]妹‘

select * from TblStudent where tsname like ‘张[^0-9]妹‘

select * from TblStudent where tsname not like ‘张[0-9]妹‘

update TblStudent set tsname=replace(tsname,‘(女)‘,‘‘)

--查询出姓名中包含%的那些人

--通配符放到[]中就转义了就不认为是通配符了。

select * from TblStudent where tsname like ‘%[%]%‘

--WHERE ColumnA LIKE ‘%5/%%‘ ESCAPE ‘/‘

--自己指定一个转义符

select * from TblStudent where tsname like ‘%/]%‘ ESCAPE ‘/‘

select * from TblStudent where tsname like ‘%/[%‘  ESCAPE ‘/‘

select * from TblStudent where tsname like ‘%/[%/]%‘  ESCAPE ‘/‘

 

SqlServer--模糊查询-通配符