sql - 如何在字符串中插入零?

标签 sql string oracle toad

我有一个字符串,例如 X1。我需要通过在其中插入零来创建一个新字符串。更具体地说:

for X1 the new string will be X0001;
for X10 the new string will be X0010;
for X100 the new string will be X0100;
for X1000 the new string will be X1000.


The length of the new string is 5.

我试过这样做:

select substr('X1', 1, 1) || '000' || substr('X1', 2) from dual;

但这只解决了第一种情况并返回 X0001。

最佳答案

尝试将 LPADSUBSTR 一起使用:

SELECT
    col,
    SUBSTR(col, 1, 1) || LPAD(SUBSTR(col, 2), 4, '0') AS col_padded
FROM yourTable;

enter image description here

Demo

关于sql - 如何在字符串中插入零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57425556/

相关文章:

sql - 从给定数据列表中查询最新记录

java - 将 List<Integer> 转换为字符串

c# - 我在以下割让中遇到错误。 (System.Data.OleDb.OleDbException : ORA-00933: SQL command not properly ended)

SQL Server 2008 - 默认列值 - 我应该使用 null 还是空字符串?

sql - Presto中包含 ' '字符的 key 的JSON_EXTRACT问题

sql - 如何获取前 10 个结果并按最常见的结果排序

java - 为什么 Integer.getInteger 不起作用?

string - 在matlab中用运算符和宏替换字符串

sql - 如何在 Oracle SQL 中处理单引号

oracle - 在 PL/SQL 中读取大型 csv 文件