What is LEFT() in SQL?

The LEFT() function returns a specific number of characters from the left side of a string.

Figure 1 shows a visual representation of the LEFT() function.

Figure 1: Visual representation of LEFT() function

Syntax

LEFT(string, numOfChars)

Parameter

The LEFT() function requires two parameters:

  • A string from which the characters need to be extracted.

If a number instead of a string is sent as a parameter, then the number will be cast to a string and the function will then be applied.

  • The number of characters to be extracted from the original string.

Return value

The LEFT() function returns the specific number of characters from the left side of a string sent as a parameter.

If the number of characters is greater than the actual length of the string, then this function returns the entire string.

Code

-- string sent
SELECT LEFT('HELLO',3);
-- number of characters greater than length of string
SELECT LEFT('HELLO',7);
-- number is sent instead of string
-- it will be cast as a string and then left() is applied
SELECT LEFT(12345,2);