Bird
Raised Fist0
MySQLquery~5 mins

FORMAT and LPAD/RPAD in MySQL

Choose your learning style10 modes available
Introduction
FORMAT helps show numbers nicely with commas and decimals. LPAD and RPAD add characters to the left or right of text to make it a certain length.
When you want to show money amounts with commas and two decimals.
When you need to make a text field a fixed length by adding spaces or zeros on the left.
When you want to add characters on the right side of a string to align text.
When preparing data for reports that need neat columns.
When formatting numbers for easier reading by people.
Syntax
MySQL
FORMAT(number, decimal_places)
LPAD(string, length, pad_string)
RPAD(string, length, pad_string)
FORMAT adds commas and rounds the number to the given decimal places.
LPAD and RPAD add the pad_string repeatedly until the string reaches the desired length.
Examples
Shows the number with commas and 2 decimals: '1,234,567.89'
MySQL
SELECT FORMAT(1234567.891, 2);
Adds zeros on the left to make the string length 3: '007'
MySQL
SELECT LPAD('7', 3, '0');
Adds '*' on the right to make length 5: 'Hi***'
MySQL
SELECT RPAD('Hi', 5, '*');
Sample Program
This query formats a number with 1 decimal, pads '42' on the left with zeros to length 5, and pads 'Cat' on the right with '!' to length 6.
MySQL
SELECT FORMAT(9876543.21, 1) AS formatted_number,
       LPAD('42', 5, '0') AS left_padded,
       RPAD('Cat', 6, '!') AS right_padded;
OutputSuccess
Important Notes
If the string is already longer than the length in LPAD or RPAD, it returns the original string.
FORMAT returns a string, not a number, so you cannot do math on the result without converting it back.
Pad strings can be more than one character, and they repeat as needed.
Summary
FORMAT makes numbers easier to read by adding commas and decimals.
LPAD adds characters to the left of a string to reach a set length.
RPAD adds characters to the right of a string to reach a set length.

Practice

(1/5)
1. What does the MySQL function FORMAT(12345.6789, 2) do?
easy
A. Formats the number with commas and 2 decimal places, like '12,345.68'.
B. Rounds the number to the nearest integer, like '12346'.
C. Converts the number to a string without decimals, like '12345'.
D. Adds leading zeros to make the number 2 digits long.

Solution

  1. Step 1: Understand FORMAT function usage

    The FORMAT function formats a number with commas and rounds it to the specified decimal places.
  2. Step 2: Apply FORMAT(12345.6789, 2)

    The number 12345.6789 is rounded to 2 decimals and commas are added, resulting in '12,345.68'.
  3. Final Answer:

    Formats the number with commas and 2 decimal places, like '12,345.68'. -> Option A
  4. Quick Check:

    FORMAT adds commas and decimals = A [OK]
Hint: FORMAT adds commas and rounds decimals [OK]
Common Mistakes:
  • Thinking FORMAT rounds to integer only
  • Confusing FORMAT with LPAD or RPAD
  • Expecting no commas in output
2. Which of the following is the correct syntax to add leading zeros to make a string 6 characters long using LPAD in MySQL?
easy
A. LPAD(123, '6', '0')
B. LPAD('123', '6', 0)
C. LPAD('123', 6)
D. LPAD('123', 6, '0')

Solution

  1. Step 1: Recall LPAD syntax

    LPAD(string, length, pad_string) pads the string on the left to the given length using pad_string.
  2. Step 2: Check each option

    LPAD('123', 6, '0') uses correct types: string '123', length 6, pad '0'. Others have wrong types or missing arguments.
  3. Final Answer:

    LPAD('123', 6, '0') -> Option D
  4. Quick Check:

    LPAD needs string, length, pad string = C [OK]
Hint: LPAD needs string, length, and pad string in order [OK]
Common Mistakes:
  • Using numbers instead of strings for pad character
  • Missing the pad string argument
  • Passing length as a string instead of number
3. What is the result of this query?
SELECT LPAD('abc', 6, '*'), RPAD('abc', 6, '!');
medium
A. 'abc***', '!!!abc'
B. '***abc', 'abc!!!'
C. 'abc', 'abc'
D. '***abc', '!!!abc'

Solution

  1. Step 1: Understand LPAD and RPAD behavior

    LPAD pads on the left, RPAD pads on the right to reach total length 6.
  2. Step 2: Calculate LPAD('abc', 6, '*')

    'abc' length is 3, so 3 '*' are added on left: '***abc'.
  3. Step 3: Calculate RPAD('abc', 6, '!')

    Similarly, 3 '!' are added on right: 'abc!!!'.
  4. Final Answer:

    '***abc', 'abc!!!' -> Option B
  5. Quick Check:

    LPAD left stars, RPAD right exclamations = B [OK]
Hint: LPAD pads left, RPAD pads right to total length [OK]
Common Mistakes:
  • Confusing LPAD and RPAD directions
  • Not counting original string length
  • Expecting padding to replace characters
4. Identify the error in this query:
SELECT LPAD('test', 4, '0');
medium
A. Syntax error due to missing RPAD.
B. Error because length is less than string length.
C. No error; it returns 'test'.
D. Error because pad string must be numeric.

Solution

  1. Step 1: Check LPAD length vs string length

    LPAD pads string to length 4, but 'test' length is 4, so no padding needed.
  2. Step 2: Confirm behavior when length equals string length

    LPAD returns original string if length equals string length, no error occurs.
  3. Final Answer:

    No error; it returns 'test'. -> Option C
  4. Quick Check:

    LPAD returns original if length equals string length = D [OK]
Hint: LPAD returns original if length equals string length [OK]
Common Mistakes:
  • Expecting error when length equals string length
  • Thinking pad string must be numeric
  • Confusing LPAD with RPAD requirements
5. You want to display a price column with 2 decimals and commas, and pad the result on the left with '#' to make it 12 characters wide. Which query achieves this?
hard
A. SELECT LPAD(FORMAT(price, 2), 12, '#') FROM products;
B. SELECT FORMAT(LPAD(price, 12, '#'), 2) FROM products;
C. SELECT RPAD(FORMAT(price, 2), 12, '#') FROM products;
D. SELECT LPAD(price, 12, '#') FROM products;

Solution

  1. Step 1: Format price with commas and 2 decimals

    Use FORMAT(price, 2) to get string like '1,234.56'.
  2. Step 2: Pad formatted string on left to length 12 with '#'

    Use LPAD(formatted_string, 12, '#') to add '#' on left if needed.
  3. Step 3: Check options

    SELECT LPAD(FORMAT(price, 2), 12, '#') FROM products; correctly formats then pads left. SELECT FORMAT(LPAD(price, 12, '#'), 2) FROM products; tries to pad number before formatting (wrong). SELECT RPAD(FORMAT(price, 2), 12, '#') FROM products; pads right, not left. SELECT LPAD(price, 12, '#') FROM products; pads number without formatting.
  4. Final Answer:

    SELECT LPAD(FORMAT(price, 2), 12, '#') FROM products; -> Option A
  5. Quick Check:

    Format first, then LPAD left padding = A [OK]
Hint: Format number first, then LPAD to pad left [OK]
Common Mistakes:
  • Padding before formatting number
  • Using RPAD instead of LPAD for left padding
  • Padding raw number without formatting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard