Examples of using wildcards in SQL Server: server wildcards

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.