Abhilash PS
Abhilash PS

Reputation: 794

How to get records from a table with a string column where values of the string column starts with a given substring?

Example - Sub string is "ab" And if the contents are

abhi
babu
abdullah

Then after running the query I should be getting only the values

abhi 
abdullah

Even though value 'babu' contains the sub string ab

Suppose the table name is person and the column name is name

Upvotes: 1

Views: 87

Answers (2)

Himanshu
Himanshu

Reputation: 32602

You can use LIKE operator like this:

SELECT * FROM person
WHERE name LIKE 'ab%'

If you add % ahead of ab then babu will also come in result so only add % after ab.

Upvotes: 2

Farnabaz
Farnabaz

Reputation: 4066

Use sql LIKE operator

SELECT name FROM person WHERE name LIKE 'ab%'

above query return all name starting with ab, if you want to get names like hiabhi use like this

SELECT name FROM person WHERE name LIKE '%ab%'

Upvotes: 1

Related Questions