This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Check the below input data and expected output to find the name of the column which has all NULL records.
Input Data:
1 2 3 4 5 6 7 |
COL1 COL2 COL3 ----------- ----------- ----------- 1 NULL 2 1 NULL 2 1 NULL 2 1 NULL 2 1 NULL 2 |
Expected Output:
1 2 3 |
NullColumn ------------ COL2 |
Create a sample table with data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
CREATE TABLE tbl_ids ( COL1 INT ,COL2 INT ,COL3 INT ) GO INSERT INTO tbl_ids VALUES (1,NULL,2) ,(1,NULL,2) ,(1,NULL,2) ,(1,NULL,2) ,(1,NULL,2) GO |
Solution:
1 2 3 4 5 6 7 8 |
SELECT CONCAT ( MAX(CASE WHEN COL1 IS NULL THEN 'COL1' ELSE '' END ) ,MAX(CASE WHEN COL2 IS NULL THEN 'COL2' ELSE '' END ) ,MAX(CASE WHEN COL3 IS NULL THEN 'COL3' ELSE '' END ) ) NullColumn FROM tbl_ids |
Please try the different solution for this puzzle and share it via comment...