MySQL数字字符串格式,以斜杠后的数字填充字符串左侧的零
让我们首先创建一个表-
mysql> create table DemoTable1369 -> ( -> BatchId varchar(20) -> );
使用insert命令在表中插入一些记录。我们在此处插入了以斜杠分隔的数字-
mysql> insert into DemoTable1369 values('19/5'); mysql> insert into DemoTable1369 values('19/78'); mysql> insert into DemoTable1369 values('19/567'); mysql> insert into DemoTable1369 values('19/1234');
使用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)