Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Starting from unique and progressive `ID` to build a new string using MySQL

Starting from a MySQL table with the following unique and progressive IDs

ID pID
1
10
100
1000

I need update the column pID of the same MySQL table by constructing a string of type YYYY-ID

Expected Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ID pID
1 2022-0001
10 2022-0010
100 2022-0100
1000 2022-1000

Any help really appreciated.

>Solution :

You can CONCAT ‘2022-‘ string to your ID column, padded with LPAD, filled with 0 on the left:

UPDATE table SET pID = CONCAT('2022-', LPAD(ID, 4, 0))

If you need current year:

UPDATE table SET pID = CONCAT(DATE_FORMAT(CURDATE(), '%Y'), '-', LPAD(ID, 4, 0)) 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading