Easy tech stuff!

Wildcards(“_” and “%”) in SQL

Posted: January 17th, 2008 | Author: Nischal Shetty | Filed under: SQL | 2 Comments »

We can use the WILDCARDS “_” and “%” in our SQL queries be it MS SQL or My SQl as follows:

Wildcard underscore “_” :

“_” helps to match exactly one character.

Ex. select name from name_table where name like “c_p”

The above query will return words such as “cap”, “cop”, “cup” or basically anything which is a three letter word starting with the letter “c” and ending with “p”.

Wildcard “%” :

“%” helps match any number of characters. Let’s take the same example as above:

select name from name_table where name like “c%p”

The output can be “cap”, “cop” and can also be “coooop”, “caawjshygfvsbchjsp” etc etc.

So what basically happens with “%” is that it will include any number of characters whereas “_” will include only one character.

http://www.techtamasha.com/wp-content/plugins/sociofluid/images/digg_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/reddit_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/delicious_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/furl_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/technorati_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/google_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/myspace_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/facebook_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/twitter_48.png

2 Comments on “Wildcards(“_” and “%”) in SQL”

  1. 1 JJJ said at 3:37 pm on July 13th, 2008:

    THANK ALOT! Was studying for my upcoming test and had no idea what my textbook is talking about.

  2. 2 Nischal Shetty said at 4:16 pm on July 13th, 2008:

    hey anytime buddy.. If there’s any more information you need I’ll be more than happy to help you :) All the best for your exams ;o)


Leave a Reply