SQL Server中萬用字元的使用樣本_MsSql

來源:互聯網
上載者:User

在某些情況下熟悉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----------   ---------   ---------   ----------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 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        -------------------- ------kevin        Eng劉          CNkevin劉       Eng&CN (3 row(s) affected) 

       

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.