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

I need to select only one occurrence of dash in my table

Im facing this problem: I need to select columns with only one occurence of dash in table like this:

Id   Name
1    Boots - black
2    Sneakers - Logan - white
3    Shirt - white
4    Pants - cargo - blue
5    Hat – green

So it will type out only column 1,3 and 5 – because they have only one dash and others have 2 or more.

Ive tried this query: Select * from products where Name like “%-%“; ,but it selected all occurences of dash, no matter if theres one or more.

Thanks a lot!

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 LENGTH and REPLACE:

SELECT *
FROM products 
WHERE (LENGTH(Name) - LENGTH(REPLACE(Name, '-', ''))) = 1

If there is only one "dash" in the Name, the calculation of LENGTH(Name) - LENGTH(REPLACE(Name, '-', '')) will return one.

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