· 8 years ago · Jan 17, 2018, 10:02 PM
1CREATE TABLE testnulls (ID INT)
2INSERT INTO testnulls VALUES (1)
3INSERT INTO testnulls VALUES (2)
4INSERT INTO testnulls VALUES (null)
5
6SELECT count(*) FROM testnulls WHERE ID IS NULL --1
7
8SELECT count(ID) FROM testnulls WHERE ID IS NULL --0
9
10IF EXISTS (SELECT 1 FROM testnulls WHERE ID IS NULL)
11PRINT 'YES'
12ELSE
13PRINT 'NO'
14
15SELECT COL1 FROM TABLE1 WHERE COL1 IS NULL AND ROWNUM = 1;
16
17SELECT TOP 1 COL1 FROM TABLE1 WHERE COL1 IS NULL;
18
19Column_1 Column_2 Column_3
20-------- -------- --------
211 2 NULL
221 NULL NULL
235 6 NULL
24
25select
26 sum(case when Column_1 is null then 1 else 0 end) as Column_1,
27 sum(case when Column_2 is null then 1 else 0 end) as Column_2,
28 sum(case when Column_3 is null then 1 else 0 end) as Column_3,
29from TestTable
30
31Column_1 Column_2 Column_3
320 1 3
33
34select
35 sum(case when Column_1 is null then 0 else 1 end) as Column_1,
36 sum(case when Column_2 is null then 0 else 1 end) as Column_2,
37 sum(case when Column_3 is null then 0 else 1 end) as Column_3,
38from TestTable
39
40select
41 count(Column_1) as Column_1,
42 count(Column_2) as Column_2,
43 count(Column_3) as Column_3,
44from TestTable
45
46Column_1 Column_2 Column_3
473 2 0
48
49select count(*) from (select top 1 'There is at least one NULL' AS note from TestTable where Column_3 is NULL) a
50
51select count(*) from (select top 1 'There is at least one non-NULL' AS note from TestTable where Column_3 is not NULL) a