匹配条件时更改列值

时间:2014-02-04 11:59:50

标签: sql oracle

只有在匹配其他条件时,我才需要替换列中的NULL值。

Columns: Parent, Child, flag01, lag02

父列有很多NULL个值,但我想仅在nullflag01为“ok”时替换flag02值。

如果flag01flag02都是“Ok”且Parent为NULL,请替换为“CT_00000”。否则,保留原始值(当NOT NULL时)。

5 个答案:

答案 0 :(得分:3)

UPDATE Table_Name
SET   Column_Name = 'CT_00000'
WHERE flag01 = 'OK'
AND   flag02 = 'OK'
AND   Parent IS NULL

只是选择数据

SELECT CASE WHEN (flag01 = 'OK' AND flag02 = 'OK' AND Parent IS NULL)
            THEN 'CT_00000'
            ELSE Column_Name END AS Column_Name
FROM Table_Name

答案 1 :(得分:2)

因为我认为你想要一个选择陈述。

select case when (parent is null and flag01 = 'OK' and flag02 = 'OK') 
       then 'CT_00000'
       else parent end as columnSomeName,
       Child, flag01, lag02
 from yourTable

答案 2 :(得分:1)

如果您搜索更新:

Update tablename
set Parent = 'CT_00000'
Where Parent is null
and flag01 = 'Ok'
and flag02 = 'Ok'

答案 3 :(得分:1)

我在SQL服务器上写作,你也可以为其他人写作

Update myTable
set Column_name=ISNUll(Column_name,'CT_00000')
WHERE flag01 = 'OK'
AND   flag02 = 'OK'
AND   Parent IS NULL

或者

Update myTable
set Column_name='CT_00000'
WHERE flag01 = 'OK'
AND   flag02 = 'OK'
AND Column_name is null
AND   Parent IS NULL

选择查询

SQL Server中的

Select ISNUll(Column_name,'CT_00000')
    from myTable
    WHERE flag01 = 'OK'
    AND   flag02 = 'OK'
    AND   Parent IS NULL
Oracle中的

 Select NVL(Column_name,'CT_00000')
            from myTable
            WHERE flag01 = 'OK'
            AND   flag02 = 'OK'
            AND   Parent IS NULL

答案 4 :(得分:1)

看看下面的SQL Query:

SELECT 
    CASE 
        WHEN 
            LOWER(flag01) = 'ok' and 
            LOWER(flag02) = 'ok' 
        then 
            ISNULL(Parent, 'CT_00000')
        ELSE 
            Parent 
    END AS 'Parent', 
    flag01, 
    flag02 
FROM 
    [TableName]