Is there a printf()-like formatting in MySQL?
I couldn't find a straightforward way in the documentation.
For example, how can I make something like:
SELECT STRFORMATFUNCTIONIMLOOKINGFOR("%03d", 17)
to get 017 ?
MySQL FORMAT() Function The FORMAT() function formats a number to a format like "#,###,###. ##", rounded to a specified number of decimal places, then it returns the result as a string.
SELECT ROUND(-4.535,2); Explanation: The above MySQL statement will round the given number -4.535 up to 2 decimal places.
MySQL ROUND() Function The ROUND() function rounds a number to a specified number of decimal places.
Inserts a string within a string at the specified position and for a certain number of characters. INSTR. Returns the position of the first occurrence of a string in another string. LCASE. Converts a string to lower-case.
for your example, you could use
SELECT LPAD(17, 3, '0');
there is also
SELECT FORMAT(17, 3); -- 17.000
otherwise, UDF as mentioned above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With