Wednesday, June 23, 2010

MSSQL LIKE Clause


SELECT * FROM myTable WHERE name LIKE '%abc%'
- This will return rows that contains the substring "abc"

SELECT * FROM myTable WHERE name LIKE '_abc'
- This will return rows that starts with any single character followed by the word "abc"

SELECT * FROM myTable WHERE name LIKE '[A-J]abc'
- This will return rows that starts with letter A to J followed by the word "abc"


SELECT * FROM myTable WHERE name LIKE '[^z]abc'
- This will return rows that starts with any single character except letter "z" followed by the word "abc"

SELECT * FROM myTable WHERE name LIKE '[abc]efg'
- This will return rows that starts with a or b or c followed by efg


SELECT * FROM myTable WHERE name LIKE '[ja-ju]'
 - This will return rows that starts from ja- to ju-


SELECT * FROM myTable WHERE name NOT LIKE '%[_]%'
SELECT * FROM myTable WHERE name LIKE '%[^_]%'
- This will return rows that does not contain the character "_" (underscore)

No comments:

Post a Comment