2

I have a column named unique_id which can take values,

16-01
16-10,
16-250,
16-1594

etc

16 is the year(eg.2016) and 01 is a count for something in this year. I manage to get this format using a BEFORE INSERT TRIGGER. Now I want to have 4 char format in count number like

16-0001
16-0010
16-0250
16-1594

How can I manage this formatting?

2

2 Answers 2

2

You can use LPAD(yourcolumn, 4, '0');)

select
    concat(left(data,3), lpad(substring(data, 4), 4, '0'))
from test

DEMO HERE

And check manual here.

Sign up to request clarification or add additional context in comments.

Comments

2

Use:

UPDATE Table1
SET Column1=concat(left(Column1,3), lpad(substring(Column1, 4), 4, '0'));

SQL Fiddle

Alternatively, RegEx may be used to replace using MYSQL UDF.

4 Comments

I think there is no big difference between UPDATE and SELECT, and what I've provided is just a way to solve problem, if you agree with that, you can upvote it, there is no need post this again here.
I agree. No difference for me If it's select or update
you can do this update using github.com/hholzgra/mysql-udf-regexp
@PatroklosPatroklou sorry I felt it was the answer to the question as a SELECT merely prints the output rather than update the table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.