Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sql query to match string after a hyphen (-)

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%’;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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%';

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading