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

How to count/sum the number of text cells in a row

I have a simple modell that stores text values. I like to count how many text cells I have in a single row where the result is not "nincs".

In this case I like to have (kap_bar_01 + kap_bar_02 + kap_bar_03 + kap_bar_04 + kap_bar_05) and the result I need is 3.

enter image description here

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

I tried this but it works not:

SELECT (kap_bar_01 + kap_bar_02 + kap_bar_03 + kap_bar_04 + kap_bar_05) FROM mymodell

Result is 0.

Thank you in advance!

>Solution :

Assuming the columns are not nullable, you can sum boolean expressions:

SELECT (kap_bar_01 <> 'nincs') + 
       (kap_bar_02 <> 'nincs') + 
       (kap_bar_03 <> 'nincs') + 
       (kap_bar_04 <> 'nincs') + 
       (kap_bar_05 <> 'nincs') AS result 
FROM mymodell;

If they are nullable, use also COALESCE():

SELECT (COALESCE(kap_bar_01, '') <> 'nincs') + 
       (COALESCE(kap_bar_02, '') <> 'nincs') + 
       (COALESCE(kap_bar_03, '') <> 'nincs') + 
       (COALESCE(kap_bar_04, '') <> 'nincs') + 
       (COALESCE(kap_bar_05, '') <> 'nincs') AS result 
FROM mymodell;
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