Home > Database > Mysql Tutorial > SQL Server通配符妙用

SQL Server通配符妙用

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 15:04:01
Original
1349 people have browsed it

在某些情况下熟悉SQL Server 通配符 的使用可以帮助我们简单的解决很多问题。 --使用_运算符查找Person表中以an结尾的三字母名字 USEAdventureWorks2012; GO SELECT FirstName, LastName FROM Person.Person WHERE FirstName LIKE'_an' ORDER BY FirstName;

 在某些情况下熟悉SQL Server 通配符的使用可以帮助我们简单的解决很多问题。

--使用_运算符查找Person表中以an结尾的三字母名字
USEAdventureWorks2012;
GO
SELECT FirstName, LastName
FROM Person.Person
WHERE FirstName LIKE'_an'
ORDER BY FirstName;

---使用[^]运算符在Contact表中查找所有名字以Al开头且第三个字母不是字母a的人
USEAdventureWorks2012;
GO
SELECT FirstName, LastName
FROM Person.Person
WHERE FirstName LIKE'Al[^a]%'
ORDER BY FirstName;

---使用[]运算符查找其地址中有四位邮政编码的所有Adventure Works雇员的ID和姓名
USEAdventureWorks2012;
GO
SELECT e.BusinessEntityID, p.FirstName, p.LastName, a.PostalCode
FROMHumanResources.EmployeeAS e
INNER JOIN Person.PersonAS pON e.BusinessEntityID= p.BusinessEntityID
INNER JOIN Person.BusinessEntityAddressAS eaON e.BusinessEntityID=ea.BusinessEntityID
INNER JOIN Person.AddressAS aON a.AddressID= ea.AddressID
WHERE a.PostalCodeLIKE'[0-9][0-9][0-9][0-9]';

结果集:
EmployeeID FirstName LastName PostalCode
---------- --------- --------- ----------
290 Lynn Tsoflias 3000

--将一张表中名字为中英文的区分出来(借鉴论坛中的代码)
create table tb(namenvarchar(20))

insert into tbvalues('kevin')
insert into tbvalues('kevin刘')
insert into tbvalues('刘')

select *,'Eng'from tbwherepatindex('%[a-z]%',name)>0and(patindex('%[吖-坐]%',name)=0)
union all
select *,'CN'from tbwherepatindex('%[吖-坐]%',name)>0andpatindex('%[a-z]%',name)=0
union all
select *,'Eng&CN'from tbwhere(patindex('%[吖-坐]%',name)>0)andpatindex('%[a-z]%',name)>0

结果集:
name
-------------------- ------
kevin Eng
刘 CN
kevin刘 Eng&CN

(3 row(s) affected)

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
sql file
From 1970-01-01 08:00:00
0
0
0
php - Overhead of prepare vs sql?
From 1970-01-01 08:00:00
0
0
0
Print sql statement
From 1970-01-01 08:00:00
0
0
0
Pass array to SQL insert query using PHP
From 1970-01-01 08:00:00
0
0
0
sql optimization or
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template