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

replace some value in sql with Null and perform count function in sql server while performing group by

there is LAT_LONG field which I want to count for each ID, the LAT_LONG contain some string value as ‘null/null’ which I want to replace with NULL, and than perform the group by operation

 select ID, COUNT(LAT_LONG) as LAT_LONG_CAT from mytable group by ID

How to replace ‘null/null’ string with Null in LAT_LONG field

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 a CASE expression:

SELECT ID,
       COUNT(CASE WHEN LAT_LONG <> 'null/null'
                  THEN LAT_LONG END) AS LAT_LONG_CNT
FROM mytable
GROUP BY ID;

The above CASE expression will treat a value of null/null as NULL, otherwise it counts the LAT_LONG field directly.

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