MySQL 数字字符串格式化,在用数字填充字符串后用斜杠填充字符串的左侧
我们先创建一个表 -
mysql> create table DemoTable1369 -> ( -> BatchId varchar(20) -> ); Query OK, 0 rows affected (0.46 sec)
使用 insert 命令插入一些记录到表中。我们在此插入了用斜杠分隔的数字 -
mysql> insert into DemoTable1369 values('19/5'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1369 values('19/78'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1369 values('19/567'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1369 values('19/1234'); Query OK, 1 row affected (0.11 sec)
使用 select 语句从表中显示所有记录 -
mysql> select * from DemoTable1369;
这样将产生以下输出 -
+---------+ | BatchId | +---------+ | 19/5 | | 19/78 | | 19/567 | | 19/1234 | +---------+ 4 row>
以下是数字字符串格式化的查询。我们已在斜杠后设置零来填充字段。总字段宽度由此处数字“1234”的最高字段值确定,即 4 -
mysql> select -> concat(left(BatchId,3), lpad(substring(BatchId, 4), 4, '0')) -> from DemoTable1369;
这样将产生以下输出 -
+--------------------------------------------------------------+ | concat(left(BatchId,3), lpad(substring(BatchId, 4), 4, '0')) | +--------------------------------------------------------------+ | 19/0005 | | 19/0078 | | 19/0567 | | 19/1234 | +--------------------------------------------------------------+ 4 rows in set (0.00 sec)
广告