0

How can I select a row specific by string values

SELECT * FROM my_table WHERE name=
   -- then some function to test what string it starts with

Its a bit hard to explain so i will explain an example with JavaScript

if(mystring.indexOf('targetstring') != -1){
// if that variable contains this string

}

if(mystring.indexOf('targetstring') == 0){
// if that variable starts with this string

}

So all in all, I want to select rows, when their name(a string column) starts with or contains a specific string.

2 Answers 2

4

You can use LIKE and the wildcard %:

SELECT * FROM my_table WHERE name LIKE 'john%'; /* name starts with john*/ 

SELECT * FROM my_table WHERE name LIKE '%john%'; /* name contains john*/ 
Sign up to request clarification or add additional context in comments.

1 Comment

I was basically going to say just that and add this: techonthenet.com/sql/like.php
2
SELECT * FROM my_table WHERE name like "%string%"

1 Comment

This selects more than it should. The % should be at the end only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.