Hacker
Hacker

Reputation: 7906

Update column in table with concat option and make it 2 digits

I want to update a column in my table with concat of 2 fields. It works fine, but i want to append 0 to one of the field if its not 2 digits because i want my month data to be 2 digits long. What i have in months column is single digit data.

update employee_status set year_month = concat(year,month);

Upvotes: 1

Views: 210

Answers (3)

Andreas Wederbrand
Andreas Wederbrand

Reputation: 40021

Sounds like you want lpad

update employee_status set year_month = concat(year, lpad(month, 2, '0'));

Upvotes: 2

Dewasish Mitruka
Dewasish Mitruka

Reputation: 2896

Try this :

update employee_status set year_month = concat(year, (if(length(month)) == 2, month, concat(0,month)));

Upvotes: 1

Rahul
Rahul

Reputation: 77896

Try this:

update employee_status set year_month = concat(year, lpad(month,2,'0')); 

Upvotes: 2

Related Questions