IT tutorials
 
Technology
 

SQL Server 2012 : Introducing Basic Query Flow - WHERE Conditions (part 2) - Using the LIKE Search Condition

4/19/2014 1:48:25 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Using the LIKE Search Condition

The LIKE search condition uses wildcards to search for patterns within a string. The wildcards, however, are different from the MS-DOS wildcards with which you may be familiar. Table 2 shows both the SQL and MS-DOS wildcards.

Table 2 SQL Wildcards

c06tnt002

The next query uses the LIKE search condition to locate all products that begin with ‘Chain' optionally followed by any number of characters:

USE AdventureWorks;
SELECT Name
FROM Production.Product
WHERE
Name LIKE ‘Chain%'

Result:

Name
-------------------
Chain
Chain Stays
Chainring
Chainring Bolts
Chainring Nut

The following query finds any StateProvince name beginning with a letter between d and f, inclusive:

USE AdventureWorks;
SELECT Name
FROM Person.StateProvince
WHERE Name LIKE ‘[d-f]%';

Result:

Name
--------------------------------------------------
Delaware
District of Columbia
Dordogne
Drome
England
Essonne
Eure
Eure et Loir
Finistere
Florida
France

The two possible methods for searching for a pattern that contains a wildcard are to either enclose the wildcard in square brackets or put an escape character before it. The trick to the latter workaround is that the escape character is defined within the LIKE expression.

When using the LIKE operator, be aware that the database collation's sort order determines both case-sensitivity and the sort order for the range of characters. You can optionally use the keyword COLLATE to specify the collation sort order used by the LIKE operator.


Best Practice
Although the LIKE operator can be useful, it can also cause a performance hit. Indexes are based on the beginning of a column, not on phrases in the middle of the column. If you find that the application requires frequent use of the LIKE operator, you should enable full-text indexing — a powerful indexing method that can even take into consideration weighted words and variations of inflections and can return the result set in table form for joining.

Multiple WHERE Conditions

You can combine multiple WHERE conditions within the WHERE clause using the Boolean logical operators: AND, OR, and NOT. As with the mathematical operators of multiplication and division, an order of precedence exists with the Boolean logical operators: NOT comes first, then AND, and then OR:

USE AdventureWorks;
SELECT ProductID, Name
FROM Production.Product
WHERE
Name LIKE ‘Chain%'
OR
ProductID BETWEEN 320 AND 324
AND
Name Like ‘%s%';

Result:

ProductID  Name
----------- ---------------------
952 Chain
324 Chain Stays
322 Chainring
320 Chainring Bolts
321 Chainring Nut

With parentheses, the result of the query is radically changed:

USE AdventureWorks;
SELECT ProductID, Name
FROM Production.Product
WHERE
(
Name LIKE ‘Chain%'
OR
ProductID BETWEEN 320 AND 324
)
AND
Name Like ‘%s%';

Result:

ProductID    Name
--------------- ---------------------
324 Chain Stays
320 Chainring Bolts

Although the two preceding queries are similar, in the first query the natural order of precedence for Boolean operators caused the AND to be evaluated before the OR. The OR included the Chains in the results.

The second query used parentheses to explicitly dictate the order of the Boolean operators. The OR collected the Chains and products with a ProductID of 952, 324, 322, 320, 321, or 323. This list was then ANDed with products that included the letter g in their names. Only products 320 and 324 passed both of those tests.


Best Practice
When coding complex Boolean or mathematical expressions, explicitly stating your intentions with parentheses and detailed comments reduces misunderstandings and errors based on false assumptions.

SELECT … WHERE

Surprisingly, using the WHERE clause in a SELECT statement does not require the use of a FROM clause or any data source reference. A SELECT statement without a FROM clause returns a single row that includes any expressions in the SELECT's column list.

A WHERE clause on a nontable SELECT statement serves as a restriction to the entire SELECT statement. If the WHERE condition is true, the SELECT statement functions as expected:

SELECT ‘abc' AS col
WHERE 1>0;

Result:

col
----
abc
(1 row(s) affected)

If the WHERE condition is false, the SELECT statement still executes but it returns zero rows:

SELECT ‘abc' AS col WHERE 1<0;

Result:

col
----
 
Others
 
- SQL Server 2012 : Introducing Basic Query Flow - WHERE Conditions (part 1) - Comparing with a List
- SQL Server 2012 : Introducing Basic Query Flow - FROM Clause Data Sources
- SQL Server 2012 : Understanding Query Flow (part 2) - Logical Flow of the Query Statement, Physical Flow of the Query Statement
- SQL Server 2012 : Understanding Query Flow (part 1) - Syntactical Flow of the Query Statement, A Graphical View of the Query Statement
- Windows Server 2012 : Deploying and configuring virtual machines (part 5) - Configuring virtual machine management
- Windows Server 2012 : Deploying and configuring virtual machines (part 4) - Adding and configuring virtual network adapters
- Windows Server 2012 : Deploying and configuring virtual machines (part 3) - Configuring virtual machines - Adding virtual disks
- Windows Server 2012 : Deploying and configuring virtual machines (part 2) - Creating virtual machines
- Windows Server 2012 : Deploying and configuring virtual machines (part 1) - Planning virtual machine deployment
- Sharepoint 2013 : Manage Tags and Notes
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us