I have this string manipulation
select product,
CAST((LENGTH(currencies) - LENGTH(REPLACE(currencies, '*', ''))) / LENGTH('*') AS UNSIGNED) AS currencies_count
from MY_TABLE
to count “currencies” in a single field in this table
product currencies
----------- --------------
prod_name_1 *usd*cad*euro*
prod_name_2 *usd*cad*
prod_name_3 *usd*cad*euro*
prod_name_4
prod_name_5 *usd*
The string manipulation returns a “+1” on all results (for exaple: prod_name_1, 4 currencies instead of 3). This because an external component store checkboxes data in this non-coherent manner (*usd*cad*euro* instead of *usd*cad*euro).
The point is that I can’t change the storage method of this component. There is a way to count the exact number of currencies in a field, ignoring the last “blank” value after the last ‘*’ separator?
How can I modify my string manipulation?
Getting crazy, any help will be very, very appreciated!
The query above is not counting the empty part of the string after the final *. It’s not even splitting it at all. It’s simply saying that:
is 3 characters longer than
To get the correct value I think you should just check for an empty currencies value, and if found then set the result to 0. Otherwise the result is the number of * symbols minus 1:
I don’t have a mysql database to to test that against but it should be OK.