MySQL

MySQL LEFT() Function

In SQL language, LEFT() function allows returning the desired number of characters from the first characters of a string. This function will explicitly truncate the input text to the desired length.

Note: it is similar to the SUBSTR() function which allows extracting a part of a string, except that it only allows extracting the characters located at the beginning.
 

Syntax:

The function is used in an SQL query using the following syntax:

SELECT LEFT(str, length);

The first parameter is the string to be truncated. The second parameter represents the desired number of characters for the desired output value.
 

 

Example :

The SQL queries executed below represent typical examples of the use of LEFT() function with the output for each statement.

SELECT LEFT('HelloWorld', 2);    -- output: 'He'
SELECT LEFT('HelloWorld', 50);   -- output: 'HelloWorld'
SELECT LEFT('HelloWorld', 0);    -- output: ''
SELECT LEFT('HelloWorld', -1);   -- output: ''
SELECT LEFT('HelloWorld', NULL); -- output: NULL

The result shows that the return string will be truncated if necessary and will not be changed if the start string is shorter than the limit. The function is therefore ideal for forcing a result not to exceed a length limit.

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *