Skip to content
Advertisement

How to replace current value to defined string

the query result

select ( LENGTH(testid) - LENGTH(replace(testid, '/', '')) ) as num_slashes,
 count(*) from test.1 where isdeleted = false  group by ( LENGTH(testid) - LENGTH(replace(testid, '/', '')) )
 ORDER BY COUNT(*) 

from database returned :

enter image description here

i would like to replaced the 0, 1, 2 to master1, master2, master3.

what query should I used so the query result will be

enter image description here

Advertisement

Answer

You can use:

select (case ( LENGTH(testid) - LENGTH(replace(testid, '/', '')) ) 
            when 1 then 'master1'
            when 2 then 'master2' 
            when 3 then 'master3'
        end) as num_slashes,
       count(*)
from test.1
where isdeleted = false
group by ( LENGTH(testid) - LENGTH(replace(testid, '/', '')) )
order by COUNT(*) 
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement