Friday, May 4, 2012

SQL Wildcard Characters

Hello Friends, Welcome to this post where I will discuss the Wildcard characters, this obviously helps a lot, in searching records from a Table.

So its very important to know, how to use the Wildcard characters.

Lets look at our Employee Table:

PID    Name        Designation
222     Whiskey       Moderator
444     Onty            Admin
111     Gaurav         Admin
333    Kumaar        Admin
555    Rishabh        Admin
666    Rose            Admin
888    White           Admin
999    Mikey          Admin

Now lets make some quick changes to our table so that we can use our Wildcard characters.

UPDATE Employee
SET Designation="Member"
WHERE Name="Mikey"


UPDATE Employee
SET Designation="Member"
WHERE Name="Onty"


UPDATE Employee
SET Designation="Member"
WHERE Name="White"


So our Table now is:

PID    Name        Designation
222     Whiskey       Moderator
444     Onty             Member
111     Gaurav          Admin
333    Kumaar         Admin
555    Rishabh         Admin
666    Rose             Admin
888    White            Member
999    Mikey           Member


Now lets see the usage of Wildcard Characters:

1.

SELECT * FROM Employee
WHERE Designation LIKE "Mem%"


So this would display, the records, where Designation starts with Mem....

PID    Name        Designation
444    Onty             Member
888    White           Member
999     Mikey          Member

2.

SELECT * FROM Employee
WHERE Designation LIKE "%m%"

So this would display all records, whose Designation has the letter m in the word,

PID    Name        Designation
444    Onty             Member
111     Gaurav         Admin
333    Kumaar        Admin
555    Rishabh        Admin
666    Rose            Admin
888    White           Member
999    Mikey          Member


3.

SELECT * FROM Employee
WHERE Name LIKE "[W,O]%"


This is little different, by [], means I m referring to the first position, and specifying that, names that starts with W and O.

PID    Name        Designation
222     Whiskey       Moderator
888     White           Member
444     Onty            Member

4.

SELECT * FROM Employee
WHERE Name LIKE "[!W,O]%"


This is just the opposite, and by this I meant, show me all the records whose Names doesn't start with W and O. [Note: ! means NOT]

PID    Name        Designation
111     Gaurav         Admin
333    Kumaar        Admin
555    Rishabh        Admin
666    Rose             Admin
999     Mikey          Member

5.

SELECT * FROM Employee
WHERE Name LIKE "_hi%"


This means show me all the records that has the second and third letter as h and i, followed by anything.

PID    Name        Designation
222     Whiskey       Moderator
888     White           Member

Interesting isn't it, Table Records at your Fingers, if you know to play with the Queries.

Well thats what I have for you in this post. Don't Forget to check my next Post.

Thank You!

0 comments:

Post a Comment