Example of using wildcard characters in SQL Server _mssql

Source: Internet
Author: User

Being familiar with the use of SQL Server wildcard characters in some situations can help us solve a lot of problems simply.

--Use the _ operator to find the three-letter name at the end of the person table
USEAdventureWorks2012;
Go
SELECT FirstName, LastName from
person.person
WHERE FirstName like ' _an ' order by
FirstName;
 
---Use the [^] operator to find all names in the Contact table that start with Al and the third letter is not the letter a
USEAdventureWorks2012;
Go
SELECT FirstName, LastName from
person.person
WHERE FirstName like ' al[^a]% ' ORDER by
FirstName ;
 
---Use the [] operator to find the ID and name USEAdventureWorks2012 of all Adventure Works employees whose addresses have four-bit postal codes
;
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] ';


Result set:

 EmployeeID   FirstName   LastName   PostalCode
----------   ---------   ---------   ----------
290       Lynn      Tsoflias   3000
--Differentiate a table name from Chinese and English (refer to the code in the Forum)
CREATE table TB (Namenvarchar)
 
insert INTO Tbvalues (' Kevin ')
insert Into Tbvalues (' Kevin Liu ')
insert into tbvalues (' Liu ')
 
select *, ' Eng ' from Tbwherepatindex ('%[a-z]% ', name) > 0and (Patindex ('%[-sit]% ', name) =0)
UNION ALL
SELECT *, ' CN ' from Tbwherepatindex ('%[acridine-sit]% ', name) > 0andpatindex ('%[a-z]% ', name) =0
UNION ALL 
SELECT *, ' ENG&CN ' from Tbwhere (Patindex ('%[acridine-sit]% ', name) > 0) Andpatindex ('%[a-z]% ', name) >0

Result set:

Name        
--------------------------
Kevin        Eng
Liu          CN
Kevin Liu       eng&cn
 
(3 Row (s) affected) 

Related Article

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.