添加新数据列

时间:2016-07-07 15:42:27

标签: sql

如何将新编辑的列添加到现有查询中?

我的查询返回此结果:

product_name    task_state  operation_code
------------------------------------------
KABY LAKE U 2+2 NULL        7571
KABY LAKE U 2+2 FUTURE WIP  7571
KABY LAKE U 2+2 NULL        6881
KABY LAKE U 2+2 NULL        7571
KABY LAKE U 2+2 NULL        7460
KABY LAKE U 2+2 NULL        7571
KABY LAKE U 2+2 NULL        6881
KABY LAKE U 2+2 FUTURE WIP  7571

我希望将所有'NULL'(空单元格而非'NULL'txt)替换为“Done”。

“tasks_state”下的所有其余数据可以按原样粘贴到新列。

最后,我希望查询结果如下:

product_name    task_state  operation_code  Task status
-----------------------------------------------------------
KABY LAKE U 2+2 NULL        7571            Done
KABY LAKE U 2+2 FUTURE WIP  7571            FUTURE WIP
KABY LAKE U 2+2 NULL        6881            Done
KABY LAKE U 2+2 NULL        7571            Done
KABY LAKE U 2+2 NULL        7460            Done
KABY LAKE U 2+2 NULL        7571            Done
KABY LAKE U 2+2 NULL        6881            Done
KABY LAKE U 2+2 FUTURE WIP  7571            FUTURE WIP

由于

2 个答案:

答案 0 :(得分:1)

使用NULLIF

试试这个

SELECT NULLIF(task_state ,'Done') FROM YourTable

查询将是

SELECT product_name,NULLIF(task_state ,'Done'),operation_code,[Task status]
FROM YourTable

答案 1 :(得分:1)

使用的最佳功能是COALESCE(),这是ANSI标准:

SELECT COALESCE(task_state, 'Done')
FROM t;

如果文本还包含'NULL',我会切换到`CASE:

SELECT (CASE WHEN task_state IS NULL or task_state = 'NULL'
             THEN 'Done' ELSE task_state
        END) as "Task Status"