oracle 匹配条件时更改列值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21551920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:13:03  来源:igfitidea点击:

Change column value when matching condition

sqloracle

提问by Lucas Rezende

I need to replace a NULLvalue in a column only when other conditions are matched.

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

Columns: Parent, Child, flag01, lag02

Parent columns has many NULLvalues, but I want to replace the nullvalues only when flag01and flag02is "ok".

父列有很多NULL价值,但是我想更换null,只有当价值flag01flag02为“OK”。

If flag01and flag02are both "Ok" and Parent is NULL, replace to 'CT_00000'. Else, keep the original value (when NOT NULL).

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

回答by M.Ali

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

just to select data

只是为了选择数据

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

回答by Jorge Campos

So as I though you want a select statement.

所以我虽然你想要一个选择语句。

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

回答by SpiderCode

Have a look at below SQL Query :

看看下面的 SQL 查询:

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

回答by bdn02

If you search for an update:

如果您搜索更新:

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

回答by Miller

I am writing in SQL server, you can have equivalent for others too

我正在用 SQL Server 编写,你也可以为其他人编写等效的

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

or Alternatively

或 或者

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

For select Query

对于选择查询

in SQL Server

在 SQL Server 中

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

in Oracle

在甲骨文

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