Reputation: 57
I am trying to check if the o appears 2 times in the string and I did it like this, but it doesn't work, is there a mistake in my code?
SELECT cu.*
FROM Customers cu
WHERE LOWER(cu.FirstName) REGEXP 'o{2}'
-- 'zoom' => correct
-- 'antonio' => correct
-- 'leo jisoo' => fail
thank you
Upvotes: 1
Views: 1693
Reputation: 222502
You code checks for two consecutive o
s, which is not what you want.
You could write this as:
where
FirstName like '%o%o%'
and FirstName not like '%o%o%o%'
This checks if the string contains exactly to o
s.
Another approach is:
length(replace(FirstName, 'o', '')) = length(FirstName) - 2
Upvotes: 5
Reputation: 1085
Try:
SELECT cu* FROM Customers cu WHERE LENGTH(LOWER(cu.FirstName)) - LENGTH(REPLACE(LOWER(cu.FirstName), 'o', '')) = 2;
Upvotes: 1