Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add leading zero to different char length in mysql?

Tags:

php

mysql

Hi,I have one query for different tables and all of them have different length which requires leading zero for the ID. Is it possible to use one query that will add leading zero to any table?

lets say first query is insert to tbl1_field1(CHAR 3) 001, then next query is insert to tbl2_field2(CHAR 4) 0001 then next is insert to tlb3_field3(CHAR 10) 0000000001. I am using php and mysql

like image 988
Cindy Tao Avatar asked Dec 28 '25 21:12

Cindy Tao


1 Answers

You can do it in a SELECT query using LPAD function -

SELECT LPAD(id, 4, 0) FROM table;

the query will show a string like this - '0001'.

like image 114
Devart Avatar answered Dec 31 '25 12:12

Devart