Skip to main content
Code Review

Return to Question

meanwhile tables in Markdown are supported, so no need for semigraphics; fix typos; use inline linking of unnecessary images
Source Link

SQL Queryquery to check if 100+ (or Dynamicdynamic) columns in table isare null

LetLet’s assume a table has more than 100 of columns &and I want to display all the rows when all of its columns (Col1, Col2, ......, ColN) are nulls except for the column(s) I mentioned (PK).

This is just a sample table I created to explainillustrate this question.

Sample TableSample Table with data

+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
+----+------+------+------+------+
| 1 | 45 | 12 | 171 | 15 |
| 2 | 13 | NULL | 15 | NULL |
| 3 | 17 | 14 | 130 | NULL |
| 4 | 10 | 11 | 110 | 12 |
| 5 | 45 | 15 | 185 | 54 |
| 6 | NULL | NULL | NULL | NULL |
| 7 | 16 | 48 | NULL | 25 |
| 8 | NULL | NULL | NULL | NULL |
| 9 | 13 | 45 | 41 | 64 |
| 10 | NULL | NULL | NULL | NULL |
| 11 | 21 | 12 | 165 | 56 |
| 12 | NULL | NULL | NULL | NULL |
+----+------+------+------+------+

Image: https://i.sstatic.net/z17GK.png

PKCol1Col2Col3Col4
1451217115
213NULL15NULL
31714130NULL
4101111012
5451518554
6NULLNULLNULLNULL
71648NULL25
8NULLNULLNULLNULL
913454164
10NULLNULLNULLNULL
11211216556
12NULLNULLNULLNULL

In this case, the rows 6, 8, 10 and 12 rows (Column name: PK) are the result I want to see since all of the columns (Col1 to Col4) are null except for the PK column.

Desired OutputDesired Output

+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
+----+------+------+------+------+
| 6 | NULL | NULL | NULL | NULL |
| 8 | NULL | NULL | NULL | NULL |
| 10 | NULL | NULL | NULL | NULL |
| 12 | NULL | NULL | NULL | NULL |
+----+------+------+------+------+

Image: https://i.sstatic.net/prwFo.png

PKCol1Col2Col3Col4
6NULLNULLNULLNULL
8NULLNULLNULLNULL
10NULLNULLNULLNULL
12NULLNULLNULLNULL

SQL Query to check if 100+ (or Dynamic) columns in table is null

Let assume a table has more than 100 of columns & I want to display all the rows when all of its columns (Col1, Col2, ..., ColN) are nulls except for the column(s) I mentioned (PK).

This is just a sample table I created to explain this question.

Sample Table with data

+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
+----+------+------+------+------+
| 1 | 45 | 12 | 171 | 15 |
| 2 | 13 | NULL | 15 | NULL |
| 3 | 17 | 14 | 130 | NULL |
| 4 | 10 | 11 | 110 | 12 |
| 5 | 45 | 15 | 185 | 54 |
| 6 | NULL | NULL | NULL | NULL |
| 7 | 16 | 48 | NULL | 25 |
| 8 | NULL | NULL | NULL | NULL |
| 9 | 13 | 45 | 41 | 64 |
| 10 | NULL | NULL | NULL | NULL |
| 11 | 21 | 12 | 165 | 56 |
| 12 | NULL | NULL | NULL | NULL |
+----+------+------+------+------+

Image: https://i.sstatic.net/z17GK.png

In this case, 6, 8, 10 and 12 rows (Column name: PK) are the result I want to see since all of the columns (Col1 to Col4) are null except for the PK column.

Desired Output

+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
+----+------+------+------+------+
| 6 | NULL | NULL | NULL | NULL |
| 8 | NULL | NULL | NULL | NULL |
| 10 | NULL | NULL | NULL | NULL |
| 12 | NULL | NULL | NULL | NULL |
+----+------+------+------+------+

Image: https://i.sstatic.net/prwFo.png

SQL query to check if 100+ (or dynamic) columns in table are null

Let’s assume a table has more than 100 columns and I want to display all the rows when all of its columns (Col1, Col2, ..., ColN) are nulls except for the column(s) I mentioned (PK).

This is just a sample table I created to illustrate this question.

Sample Table with data

PKCol1Col2Col3Col4
1451217115
213NULL15NULL
31714130NULL
4101111012
5451518554
6NULLNULLNULLNULL
71648NULL25
8NULLNULLNULLNULL
913454164
10NULLNULLNULLNULL
11211216556
12NULLNULLNULLNULL

In this case, the rows 6, 8, 10 and 12 (Column name: PK) are the result I want to see since all of the columns (Col1 to Col4) are null except for the PK column.

Desired Output

PKCol1Col2Col3Col4
6NULLNULLNULLNULL
8NULLNULLNULLNULL
10NULLNULLNULLNULL
12NULLNULLNULLNULL
use simpler characters to render correctly in all fonts
Source Link
╔════╦══════╦══════╦══════╦══════╗+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
╠════╬══════╬══════╬══════╬══════╣+----+------+------+------+------+
| 1 | 45 | 12 | 171 | 15 |
| 2 | 13 | NULL | 15 | NULL |
| 3 | 17 | 14 | 130 | NULL |
| 4 | 10 | 11 | 110 | 12 |
| 5 | 45 | 15 | 185 | 54 |
| 6 | NULL | NULL | NULL | NULL |
| 7 | 16 | 48 | NULL | 25 |
| 8 | NULL | NULL | NULL | NULL |
| 9 | 13 | 45 | 41 | 64 |
| 10 | NULL | NULL | NULL | NULL |
| 11 | 21 | 12 | 165 | 56 |
| 12 | NULL | NULL | NULL | NULL |
╚════╩══════╩══════╩══════╩══════╝+----+------+------+------+------+
╔════╦══════╦══════╦══════╦══════╗+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
╠════╬══════╬══════╬══════╬══════╣+----+------+------+------+------+
| 6 | NULL | NULL | NULL | NULL |
| 8 | NULL | NULL | NULL | NULL |
| 10 | NULL | NULL | NULL | NULL |
| 12 | NULL | NULL | NULL | NULL |
╚════╩══════╩══════╩══════╩══════╝+----+------+------+------+------+
╔════╦══════╦══════╦══════╦══════╗
 PK  Col1  Col2  Col3  Col4 
╠════╬══════╬══════╬══════╬══════╣
 1  45  12  171  15 
 2  13  NULL  15  NULL 
 3  17  14  130  NULL 
 4  10  11  110  12 
 5  45  15  185  54 
 6  NULL  NULL  NULL  NULL 
 7  16  48  NULL  25 
 8  NULL  NULL  NULL  NULL 
 9  13  45  41  64 
 10  NULL  NULL  NULL  NULL 
 11  21  12  165  56 
 12  NULL  NULL  NULL  NULL 
╚════╩══════╩══════╩══════╩══════╝
╔════╦══════╦══════╦══════╦══════╗
 PK  Col1  Col2  Col3  Col4 
╠════╬══════╬══════╬══════╬══════╣
 6  NULL  NULL  NULL  NULL 
 8  NULL  NULL  NULL  NULL 
 10  NULL  NULL  NULL  NULL 
 12  NULL  NULL  NULL  NULL 
╚════╩══════╩══════╩══════╩══════╝
+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
+----+------+------+------+------+
| 1 | 45 | 12 | 171 | 15 |
| 2 | 13 | NULL | 15 | NULL |
| 3 | 17 | 14 | 130 | NULL |
| 4 | 10 | 11 | 110 | 12 |
| 5 | 45 | 15 | 185 | 54 |
| 6 | NULL | NULL | NULL | NULL |
| 7 | 16 | 48 | NULL | 25 |
| 8 | NULL | NULL | NULL | NULL |
| 9 | 13 | 45 | 41 | 64 |
| 10 | NULL | NULL | NULL | NULL |
| 11 | 21 | 12 | 165 | 56 |
| 12 | NULL | NULL | NULL | NULL |
+----+------+------+------+------+
+----+------+------+------+------+
| PK | Col1 | Col2 | Col3 | Col4 |
+----+------+------+------+------+
| 6 | NULL | NULL | NULL | NULL |
| 8 | NULL | NULL | NULL | NULL |
| 10 | NULL | NULL | NULL | NULL |
| 12 | NULL | NULL | NULL | NULL |
+----+------+------+------+------+
deleted 12 characters in body
Source Link

Thank you.

Thank you.

Source Link
DxTx
  • 133
  • 8
Loading
lang-sql

AltStyle によって変換されたページ (->オリジナル) /