编写SQL语句以将所有客户的名称从“ T”开头更改为“ F”

时间:2019-06-16 18:00:58

标签: sql oracle oracle11g

编写SQL语句以将所有客户的名称从“ T”更改为“ F” 我正在尝试解决这个问题。

UPDATE customers
SET first_name ='T'
WHERE First_name like 'F%'

但是我必须更改唯一的首字母。

3 个答案:

答案 0 :(得分:1)

我认为您有倒退的逻辑:

UPDATE customers
    SET first_name = 'F'
    WHERE First_name like 'T%';

如果问题是“将T更改为F”,则:

UPDATE customers
    SET first_name = 'F' || substr(first_name, 2)
    WHERE First_name like 'T%';

答案 1 :(得分:1)

您可以使用SUBSTR

UPDATE customers 
SET first_name = CONCAT('F', SUBSTR(first_name, 2))
WHERE First_name like 'T%'

答案 2 :(得分:1)

和一个正则表达式选项:

SQL> with customers (first_name) as
  2    (select 'Francisco' from dual union all
  3     select 'fixed'     from dual union all
  4     select 'FFrica'    from dual union all
  5     select 'Marof'     from dual union all
  6     select 'Taurus'    from dual
  7    )
  8  select regexp_replace(first_name, '^F', 'T') result
  9  from customers;

RESULT
-----------------------------------------------------------
Trancisco
fixed
TFrica
Marof
Taurus

SQL>