SQL Server: SPACE Function

SPACE()  is a SQL Server function used to generate a specified number of space characters. 

This function is typically used to add spacing between text or to position specific text, such as indenting the first line of a paragraph.

Syntax:

SPACE ( integer_expression )

  • integer_expression: The number of spaces to generate.

If the value is negative, SQL Server will return an empty string '' or NULL depending on the version/context, commonly NULL in newer SQL Server versions, though translated literally from your text: an empty string.

E.g.:

SELECT 'A' + SPACE(1) + 'B' AS Result;
SELECT 'A' + SPACE(5) + 'B' AS Result;
SELECT 'A' + SPACE(10) + 'B' AS Result;



As we can see from the three queries above, the larger the integer input in the SPACE function, the greater the distance between A and B.

Post a Comment

0 Comments