LEFT Function - SQL


Overview


The LEFT function in SQL returns the specified number of characters from the beginning of a string. It is useful for extracting a portion of a string from the left side.

Example:

SELECT LEFT('Astronomy', 5)

Syntax:

SELECT LEFT(string, num_of_characters)

string is the string from which you want to extract characters.

num_of_characters is the number of characters to extract from the beginning of the string.

Sample Data:

department_id first_name
3 Frank
2 Jane
3 Ashley
NULL Glenn
2 Kelly
1 Richard
1 George
5 Kyle
2 James
1 Gustavo

Example: Hard-coded value


In this example, we are returning the 5 characters to the left of the string ‘astronomy,’ the final output is ‘astro.’


Example: Query without the WHERE Statement


In this example, we are returning the 3 characters to the left of all of the rows in the first_name column.


Example: Query with the WHERE Statement


In this example, we are returning the 3 characters to the left of all of the rows in the first_name column where the department_id equals 1.