MySQL SPACE() Function

The SPACE() function in MySQL generates a string containing a specified number of space characters. This might seem simple, but it's surprisingly useful for tasks involving string padding, formatting, or creating strings of a specific length.



SPACE(): Definition and Usage

SPACE() is often used to create strings filled with spaces. This can be helpful for padding data, aligning text, or generating strings of a particular length for various formatting needs. The number of spaces generated is controlled by the input parameter.

Syntax

Syntax

SPACE(number)
      

Parameter Values

Parameter Description
number The number of spaces to generate. This is required and must be a non-negative integer.

Example

Generating a String of Spaces

This example generates a string containing ten spaces.

Syntax

SELECT SPACE(10);
      
Output

          (Ten spaces will be displayed here)
      

**Note:** The output of this query will be ten spaces. You won't see them visually in the code output box above, but when you run this query in a MySQL client, ten spaces will be displayed. If you provide a negative number to the `SPACE()` function you'll get an error.