What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 24841 |  Welcome, Guest!   Register  Login
Home > Articles > Sql Server > Use wildcards characters in SQL queries

Use wildcards characters in SQL queries

Article posted by Samrat153 on 3/14/2012 | Views: 3893 | Category: Sql Server | Level: Beginner | Points: 200 red flag


Use wildcards characters like %, _ in the where clause of an sql statement.
Manage Error "xml parsing <line and character details> illegal name character"

Introduction

Most SQL users know the importance and power of the basic usage of wildcards. Using wildcards allows us to do pattern matches. And the most common example of this is The LIKE condition which can be used in any valid SQL statement. But Here I am not discussing about it.

Objective


Here I am discuss about condition when user want to use wildcards in the where clause of an SQL statement.

Wildcard characters List

wildcard characters

Description

%

The % wildcard matches zero or more characters of any type.

   Matches any character or multiple characters in its position.

_

The _ wildcard matches exactly one character of any type.  

Matches any single character in its position.

[list]

Matches any character in list.

[^list]

Excludes any character in list.

 

Use wildcard characters in query

Suppose I want all users whose name contains _. For example, it would be a value such as 'sam_b'.
Ex.

     SELECT * FROM Users WHERE UserName LIKE '_';

What kind of output you can predict after execute above query. Is It will gives UserName contains ‘_’ ? No it will return all the records.

Another important point is which I would to share here that if you are using Stored Procedures & proper sqlParameters then anything mentioned in wildcard character inside XML tags will throw below mentioned exception.
Error: xml parsing <line and character details> illegal name character

Now you must be thinking about what is the solution ???
Here it is :)

Solution

Use the ESCAPE clause!

This will return all records from the wildcard field or we can say that by using this clause we're explicitly telling SQL that what escape need to escape. SQL will be treated them like a part of the string not like any special wildcard character.

Ex.
     SELECT * FROM Users WHERE UserName LIKE '%!_%'  escape '!';
After executing above query, Ii gives only return those UserName which contains ‘_’ .

Conclusion

In this article I have shown you a basics use of wildcards in the where clause of an SQL statement.


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Latest Articles from Samrat153
Experience:6 year(s)
Home page:http://www.dotnetfunda.com
Member since:Saturday, February 12, 2011
Level:Starter
Status: [Member]
Biography:Hi, I am Samrat Banerjee from India.
I am a Software Engineer working in .net platform.
I love to explore new technologies.
 Responses
Posted by: Patel28rajendra | Posted on: 15 Mar 2012 03:09:03 AM | Points: 25

hi

useful article for newer to sql

nice explained

Thanks

Posted by: Yrbyogi | Posted on: 17 Mar 2012 04:59:01 AM | Points: 25

We can also use the PATINDEX sql function (using regex) to replace the unwanted characters.

>> Write Response - Respond to this post and get points
Related Posts

In this article we will go through the most basic and frequently asked interview questions on SQL Server. Please do not think that I am writing this article to show shortcuts to candidates who are searching for jobs on SQL Server. But I think no matter how much experience you have, an interview is a different ball game. A good project executioner can get knocked off on simple SQL Server questions.

In this article we will learn Last_Value function of Sql Server 2012 (Denali).

This is part 28 of the series of article on SSIS. In this article we are going to learn how to create remote FTP folder using SSIS.

This is part 9 of the series of article on SSIS

Many a times we need to present bulk data to the user in our day to day application development. Loading all data at one shot is okay when we have few hundreads data but when it comes to thousands of thousands records, it hardly work. In scenario like this we need to go for custom paging in our application.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 5:17:24 AM