📜  mysql pad zeros - SQL (1)

📅  最后修改于: 2023-12-03 15:33:01.787000             🧑  作者: Mango

MySQL Pad Zeros - SQL

In MySQL, you can pad zeros to a number using the LPAD function. This function pads the specified character to the left of a string until it reaches the specified length.

Here's the syntax of the LPAD function in MySQL:

LPAD(string, length, pad_string)

The first argument is the string to be padded with zeros. The second argument is the final length of the string. The third argument is the character you want to pad the string with. In this case, it's zero.

SELECT LPAD(123, 5, 0);

Output:

00123

This query will pad zeros to the left of the number '123' until it reaches the specified length of 5.

You can also use the RPAD function to pad zeros to the right of a string in MySQL.

RPAD(string, length, pad_string)

Here's an example query:

SELECT RPAD(123, 5, 0);

Output:

12300

This query pads zeros to the right of the number '123' until it reaches the specified length of 5.

Overall, the LPAD and RPAD functions are handy tools for padding zeros to numbers in MySQL. This can be useful in cases where you need to display numbers with a fixed number of digits, such as invoice numbers or product codes.