Friday, December 20, 2024

MySQL LEFT() Function

 The LEFT() function in MySQL is used to extract a specified number of characters from the left side (beginning) of a string. It takes two arguments:

  1. The string from which you want to extract characters.
  2. The number of characters you want to extract.

Syntax:

LEFT(string, number_of_characters)
  • string: The string value from which you want to extract the characters.
  • number_of_characters: The number of characters to extract from the beginning of the string.

Example:

  1. Extracting the first 3 characters from a string:
SELECT LEFT('Hello, World!', 3);

Result: 'Hel'

  1. Extracting the first 5 characters from a column in a table:
SELECT LEFT(column_name, 5) FROM table_name;

This function is helpful when you want to extract a specific portion of a string, especially when the string's length is variable, and you need consistent behavior from the start of the string.

No comments:

Post a Comment