0

I have stored procedure in MySql which user will pass in a number e.g. 'cr002149' Now I want to increase the number by 1 for each record in select statment. How can I do that to let the number become e.g. cr002150,cr002151...

Thanks.

2
  • 1
    will cr remain always the same Commented Aug 6, 2013 at 3:53
  • have you tried anything? Commented Aug 6, 2013 at 3:54

1 Answer 1

3

You should reconsider your data structure, you might get better luck just using an auto_increment'd integer. Either way, this should do.

CONCAT('cr',
  LPAD(
    CAST(
      SUBSTRING('cr002149',3) AS DECIMAL(0)
    )+1,
    6,'0'
  )
);

The above will return cr002150. First, we get 002149 using SUBSTRING, then cast the string 002149 to an integer and get 2149. Now, we increment this to get 2150, followed by left-padding it by 0s to get 002150, and finally we concatenate cr on the left side.

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

Comments

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.