Examples of using wildcards in SQL Server: server wildcards
In some cases, familiarity with the use of SQL Server wildcard can help us solve many problems.
-- Use the _ operator to search for USEAdventureWorks2012, a three-letter name ending with an in the Person table; GOSELECT FirstName, LastNameFROM Person. personWHERE FirstName LIKE '_ any' order by FirstName; --- use the [^] operator to search for USEAdventureWorks2012, a person whose names start with Al and whose third letter is not a, in the Contact table; GOSELECT FirstName, lastNameFROM Person. personWHERE FirstName LIKE 'al [^ a] % 'order BY FirstName; --- use the [] operator to find the ID and name of all Adventure Works employees whose addresses contain four postal codes USEAdventureWorks2012; GOSELECT e. businessEntityID, p. firstName, p. lastName,. postalCodeFROMHumanResources. employeeAS eINNER JOIN Person. personAS pON e. businessEntityID = p. businessEntityIDINNER JOIN Person. businessEntityAddressAS eaON e. businessEntityID = ea. businessEntityIDINNER JOIN Person. addressAS aON. addressID = ea. addressIDWHERE. postalCodeLIKE '[0-9] [0-9] [0-9] [0-9]';
Result set:
EmployeeID FirstName LastName PostalCode---------- --------- --------- ----------290 Lynn Tsoflias 3000
-- Differentiate a table from Chinese and English (referring to the Code in the Forum) create table tb (namenvarchar (20) insert into tbvalues ('kevin ') insert into tbvalues ('kevin Liu ') insert into tbvalues ('Liu') select *, 'eng 'from tbwherepatindex (' % [a-z] % ', name)> 0and (patindex ('% [sitting] %', name) = 0) union allselect *, 'cn' from tbwherepatindex ('% [sitting] % ', name)> 0 andpatindex ('% [a-z] %', name) = 0 union all select *, 'eng & cn' from tbwhere (patindex ('% [sitting] %', name)> 0) andpatindex ('% [a-z] %', name)> 0
Result set:
Name -------------------- ------ kevin Eng Liu CNkevin Liu Eng & CN (3 row (s) affected)