sql - 更新 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';
解决方案
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>
推荐阅读
- scala - 日期时间的 Spark SQL 函数适用于 AM 而非 PM 的时间格式
- sql - 在 SQL 中将日期变量放在引号中
- spring-boot - Mapstruct 1.4.2.Final:NullValuePropertyMappingStrategy.SET_TO_DEFAULT 未按预期工作
- python - REST 框架——序列化多对多。创建和保存
- ionic-framework - Ionic 3 iframe 中的 Magento 2 设置 cookie SameSite=None 而不是默认的 SameSite=LAX
- c++ - 错误:没有上下文类型信息的重载函数使用 LEMON-graph-library
- react-native - 索引在反应导航状态下做什么
- java - 在测试中使用 @Autowired 时出现 IllegalArgumentException
- node.js - Nodejs 应用程序如何始终作为 Windows 服务运行?
- c# - 如何获得更快的 ManagementObject 结果