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

Count the number of existing values in a field in a record

  1. Such as I have a table of User—(id,name,age,hobby,sports)
  2. Then it has a record that (1,"zhangsan",18,null,null);
  3. Next you can see the total of record is 3, because hobby and sports are null.

use SQL sentences to count the number of existing values in a field in a record

Help and thanks

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

>Solution :

Use CASE expressions to count each column.

SELECT
    CASE WHEN Column1 IS NOT NULL THEN 1 ELSE 0 END
    + CASE WHEN Column2 IS NOT NULL THEN 1 ELSE 0 END
    -- repeat for all columns
    AS NumberOfNonNullColumns
FROM MyTable
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