MySQL String Formatting: Methods & Examples

In MySQL, you can format strings using the following methods:

  1. Concatenate strings using the CONCAT function.
SELECT CONCAT('Hello', 'World') AS formatted_string;
  1. Format numbers using the FORMAT function.
SELECT FORMAT(1234567.89, 2) AS formatted_number;
  1. Insert the string using the INSERT function.
SELECT INSERT('Hello World', 6, 0, 'MySQL ') AS formatted_string;
  1. Use the LPAD and RPAD functions to pad a string on the left or right side.
SELECT LPAD('123', 5, '0') AS formatted_string;
SELECT RPAD('ABC', 5, '0') AS formatted_string;
  1. Replace function is used to replace a substring in a string.
SELECT REPLACE('Hello World', 'World', 'MySQL') AS formatted_string;

These methods can assist you in performing various formatting operations on strings. You can choose the appropriate method to format the string based on your specific needs.

bannerAds