SQL Wildcard Question

Sql can't do this ... placing the percentage symbol in the middle.

SELECT * FROM users WHERE last_name LIKE 'S%th' 

you would need to write a where clause and an and clause.

 SELECT * FROM users WHERE last_name LIKE 'S%' and last_name LIKE '%th' 

The LIKE operator is what you are searching for, so for your example you would need something like:

SELECT * 
  FROM [Users]
 WHERE LastName LIKE 'S%'

The % character is the wild-card in SQL.


to get all the users with a lastname of smith

SELECT * 
  FROM [Users]
 WHERE LastName ='Smith'

to get all users where the lastname contains smith do this, that will also return blasmith, smith2 etc etc

SELECT * 
  FROM [Users]
 WHERE LastName LIKE '%Smith%'

If you want everything that starts with smith do this

SELECT * 
  FROM [Users]
 WHERE LastName LIKE 'Smith%'

Tags:

Sql

Wildcard