2016-03-15 164 views
0

我周圍搜索,但無法找到類似的答案。SQL Server 2000:支持條件

推薦的最佳方法是在SQL Server 2000中使用條件來排除NULL和'NA'字符串?

我想從這個去(只選擇ORDER_ID = 100和LINE_NO = 1):

+----------+---------+-----+------+-----+------+----+ 
| order_id | line_no | a | b | c | d | e | 
+----------+---------+-----+------+-----+------+----+ 
|  100 |  1 | 123 | NULL | NA | 456 | X1 | 
|  101 |  1 | 789 | NA | 123 | NULL | BB | 
+----------+---------+-----+------+-----+------+----+ 

要這樣:

+----------+---------+------+--------+ 
| order_id | line_no | type | config | 
+----------+---------+------+--------+ 
|  100 |  1 | a | 123 | 
|  100 |  1 | d | 456 | 
|  100 |  1 | e | X1  | 
+----------+---------+------+--------+ 

回答

1

在SQL Server 2000中,有沒有這麼多可用選項。您可以使用UNION ALL

SELECT 'a' AS type, a 
FROM mytable 
WHERE a IS NOT NULL AND a <> 'NA' 

UNION ALL 

SELECT 'b' AS type, b 
FROM mytable 
WHERE b IS NOT NULL AND b <> 'NA' 

UNION ALL 

SELECT 'c' AS type, c 
FROM mytable 
WHERE c IS NOT NULL AND c <> 'NA' 

UNION ALL 

SELECT 'd' AS type, d 
FROM mytable 
WHERE d IS NOT NULL AND d <> 'NA'