Home > Software engineering >  Sql query to match string after a hyphen (-)
Sql query to match string after a hyphen (-)

Time:07-13

I want to fetch all the records using a SQL query with the string 'John' in it. Condition is, John should appear after at least one hyphen (-)

Examples:

  1. Anna - Pam - John (Yes)
  2. John - Anna - Pam (No)
  3. Anna - John (Yes)
  4. John (No)
  5. Anna - Pam - Mike - PhilJohn (Yes)
  6. John - John (Yes)

Bad query example: Select * from users where name LIKE '%John%';

CodePudding user response:

You can use '%' in between - and John to allow any numbers of characters in between.

your query will be like this

Select * from users where name LIKE '%-%John%';

  • Related