【问题标题】:How do I replace blank spaces with null values and query where values are not null? (I'm filling in a table)如何用空值替换空格并查询值不为空的地方? (我在填表)
【发布时间】:2021-09-01 09:54:46
【问题描述】:

我有两张桌子:

表1:

USER_NUM FIRST_NAME LAST_NAME
A123 Billy Bob
A124 Billy Joe
Jane Doe
John Doe

我正在尝试将USER_NAME 下的FIRST_NAMELAST_NAME 插入table2:

OWNER_ID USER_NUM USER_NAME
111 A123
112 A124

使用这个查询:

BEGIN
  FOR c1 IN (SELECT UPPER(t1.USER_NUM) number
                    ,t1.FIRST_NAME || ' ' || LAST_NAME user_name
               FROM table1 t1
              INNER JOIN table2 t2
                 ON number = t2.USER_NUM
              WHERE regexp_replace(number, '[[:space:]]+', NULL) IS NOT NULL
             )
  LOOP
    INSERT INTO table2 (USER_NAME) values (c1.user_name)
  END LOOP;
END;

问题是,如果我隔离我的选择语句,我仍然会得到 USER_NUM 的空白值,表格如下所示:

USER_NUM USER_NAME
A123 Billy Bob
A124 Billy Joe
Jane Doe
John Doe

我收到一条错误消息,提示我无法在表中插入空值。

如何去除USER_NUM 中的这些空白值?

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    你想要update:

    update table2 t2
        set user_name = (select t1.FIRST_NAME || ' ' || t1.LAST_NAME
                         from table1 t1
                         where t1.user_num = t2.user_num
                        )
        where exists (select 1
                      from table1 t1
                      where t1.user_num = t2.user_num
                     );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 2014-04-09
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      相关资源
      最近更新 更多