Husnain Iqbal
Husnain Iqbal

Reputation: 93

SQL Query to get all names having (whole or part of) searched name

I'm using sqlite right now and wants to know if any SQL query exists to get all the names having searched name as whole or some part of name. I have saved all names in one column i.e. "name" column and wants to get like if I have saved "abc", "xyz" and "abc xyz" as names and searched abc then it should give results like: "abc" and "abc xyz" and similar for for other names....

Upvotes: 2

Views: 14163

Answers (4)

A.Goutam
A.Goutam

Reputation: 3494

you need to use Like operator for this situation

 SELECT  * 
    FROM table  
    WHERE Name  LIKE '%abcd' 

Upvotes: 1

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107536

If the search term can be in any part of the name column, surround your search term with wildcard characters ('%') and use the LIKE operator:

WHERE
    name LIKE '%abc%'

If you want all names that start with "abc":

WHERE
    name LIKE 'abc%'

Or all names that end with "abc":

WHERE
    name LIKE '%abc'

Upvotes: 3

Michael Durrant
Michael Durrant

Reputation: 96484

If you want:

"abc", "xyz" and "abc xyz" as names
search by "abc" 
results like: "abc" and "abc xyz" 

You should use field like "abc%"

Upvotes: 0

hd1
hd1

Reputation: 34657

SQLite3 supports regular expressions, merely need to put where name REGEXP 'abc' as your where clause. Hope that helps...

Upvotes: 0

Related Questions