在某些情況下熟悉SQL Server 通配符的使用可以幫助我們簡單的解決很多問題。
--使用_運算符查找Person表中以an結尾的三字母名字USEAdventureWorks2012;GOSELECT FirstName, LastNameFROM Person.PersonWHERE FirstName LIKE'_an'ORDER BY FirstName;---使用[^]運算符在Contact表中查找所有名字以Al開頭且第三個字母不是字母a的人USEAdventureWorks2012;GOSELECT FirstName, LastNameFROM Person.PersonWHERE FirstName LIKE'Al[^a]%'ORDER BY FirstName;---使用[]運算符查找其地址中有四位郵政編碼的所有Adventure Works雇員的ID和姓名USEAdventureWorks2012;GOSELECT e.BusinessEntityID, p.FirstName, p.LastName, a.PostalCodeFROMHumanResources.EmployeeAS eINNER JOIN Person.PersonAS pON e.BusinessEntityID= p.BusinessEntityIDINNER JOIN Person.BusinessEntityAddressAS eaON e.BusinessEntityID=ea.BusinessEntityIDINNER JOIN Person.AddressAS aON a.AddressID= ea.AddressIDWHERE a.PostalCodeLIKE'[0-9][0-9][0-9][0-9]';
結果集:
EmployeeID FirstName LastName PostalCode---------- --------- --------- ----------290LynnTsoflias 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 allselect *,'CN'from tbwherepatindex('%[吖-坐]%',name)>0andpatindex('%[a-z]%',name)=0union all select *,'Eng&CN'from tbwhere(patindex('%[吖-坐]%',name)>0)andpatindex('%[a-z]%',name)>0
結果集:
name-------------------- ------kevinEng劉CNkevin劉Eng&CN(3 row(s) affected)
新聞熱點
疑難解答