how to append prefix if the size of the data is less than 6

SELECT Cust_Id,
CASE 
WHEN LEN(Cust_Id)<6 
THEN CONCAT((SELECT REPLICATE('0',(SELECT LEN(Cust_Id) - 6 ) )), Cust_Id)
ELSE Cust_Id
END as Cust_Id
FROM Customers
2

There are 2 answers

0
ScaisEdge On

you could try using mysql lpad function

select  Cust_Id, lpad(Cust_Id, 6, '0')
FROM Customers

https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_lpad

0
Dhananjay Yadav On

use LPAD fucntion of MySQL, to add leading number of any char as prefix.

SELECT LPAD(Cust_Id, 6, '0') FROM `Customers`;