Space Function In Sql in SQL
The SPACE function in SQL is used to return a string of repeated space characters.
? Syntax
SPACE(integer_expression)integer_expression: The number of space characters to return.
? Return Type
Returns a string (typically
VARCHAR) consisting of the specified number of space characters.
? Example Usage
SELECT 'Hello' + SPACE(3) + 'World' AS Greeting;Result:
Hello World(There are 3 spaces between "Hello" and "World")
? Use Cases
Formatting output strings (e.g., padding).
Creating fixed-width text data.
Generating indents or gaps for display in reporting systems.
Simulating tab spacing in plain text output.
? Notes
If the argument is 0, it returns an empty string.
If the argument is negative, it returns NULL.
Works with
+string concatenation in SQL Server and some other dialects.
? More Examples
SELECT SPACE(5) AS FiveSpaces;SELECT 'Data' + SPACE(2) + 'Science' AS Combined;SELECT LEN(SPACE(10)) AS LengthOfSpaces; -- Returns 10Let me know if you'd like a custom function to generate formatted strings using SPACE for reports or exports.