Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL : select when a character occurs more than once in a column

Tags:

sql

sql-server

I have a select statement

select * from B2B_CardTechCards
        where Field NOT LIKE '%-%'

I only want to select the cells that has more than one occurrence of "-" in it. if it occurred once I don't need it.

like image 607
Monir Tarabishi Avatar asked Dec 24 '22 12:12

Monir Tarabishi


2 Answers

Simply search for two of them:

select * from B2B_CardTechCards
where Field LIKE '%-%-%'
like image 90
jarlh Avatar answered Dec 26 '22 02:12

jarlh


You can find the count of '-' in Field column by using

(len(Field) - len(replace(Field, '-', '')))

Now we can select rows that has the above count value more than one

select * 
from B2B_CardTechCards
where  (len(Field) - len(replace(Field, '-', ''))) > 1
like image 25
jophab Avatar answered Dec 26 '22 00:12

jophab