首页 > 解决方案 > 更新 varchar(22 BYTE) 的最后两位数字

问题描述

我试图弄清楚如何更改以 0230 结尾的数字列表。我将如何只更改最后两位数字。

例子:

254541563200222200230

Into :
254541563200222200228

示例 2:

11154561560222200230

Into : 
11154561560222200228

我知道用于查找以这些数字结尾的值的选择命令只是不知道如何更新它们,因为它们都有不同的起始数字。它们的长度都是 22 位。

SELECT * from data_table here data = '%0230'.

我最好的选择是:

UPDATE data_table SET data = '%0229' where data = '%0230';

标签: sqloracle

解决方案


SUBSTR可以做到这一点。

您的第二个示例比第一个短,所以我稍微修改了示例数据;包括y在末尾以强调0230(即最后 4 个字符)。第三个示例根本不应该被修改,因为它没有以0230(就是where这样)结尾。

SQL> with test (col) as
  2    (select '2545415632002222y0230' from dual union all
  3     select 'x111545615602222y0230' from dual union all
  4     select '2412412412412412y1234' from dual
  5    )
  6  select col,
  7    substr(col, 1, length(col) - 2) || '28' result
  8  from test
  9  where substr(col, -4) = '0230';

COL
---------------------
RESULT
----------------------------------------------------------------
2545415632002222y0230
2545415632002222y0228

x111545615602222y0230
x111545615602222y0228


SQL>

当您更新列时,然后:

SQL> create table test as
  2    (select '2545415632002222y0230' col from dual union all
  3     select 'x111545615602222y0230'     from dual union all
  4     select '2412412412412412y1234'     from dual
  5    );

Table created.

SQL> update test set
  2    col = substr(col, 1, length(col) - 2) || '28'
  3  where substr(col, -4) = '0230';

2 rows updated.

SQL> select * From test;

COL
---------------------
2545415632002222y0228
x111545615602222y0228
2412412412412412y1234

SQL>

推荐阅读