Wednesday, August 12, 2009

SQL LIKE THAN YOU LIKE



Do not confuse with the title, let me explain what it is. Consider about the following table, (Persons)







One might need to get only the FirstName that starts with letter ‘W’
in this case we can write our query as follows

SELECT * FROM Persons WHERE FirstName LIKE 'W%'

Result would be





OK cool , what if you need to get all the records that FirstName starts
with letter ‘W’ and letter ‘R’, in this case I am sure most of developes
query would be as follows.

SELECT * FROM Persons WHERE FirstName LIKE 'W%' OR FirstName LIKE 'R%'







Fine, you got the result what you expected but there is another way to do this
without using OR FirstName LIKE ‘R%’

SELECT * FROM Persons WHERE FirstName LIKE '[WR]%'

Don’t you think this is better? Keep in mind you can use this not only for the first letter
you can use this for last letter even you can use with negation ( LIKE ‘[^WR]%’)

0 comments:

My Achievements

Member of

Blog Archive

Followers

free counters