Friday, December 20, 2024

MySQL LTRIM() Function

 The LTRIM() function in MySQL is used to remove leading spaces (spaces at the beginning of a string) from a given string. It does not remove spaces in the middle or at the end of the string, only those that appear at the start.

Syntax:

LTRIM(str)
  • str: The string from which the leading spaces will be removed.

Example Usage:

  1. Basic Example:

    SELECT LTRIM('   Hello World') AS result;
    

    Result:

    'Hello World'
    

    This query removes the leading spaces before "Hello World."

  2. Using with a table:

    SELECT LTRIM(name) AS trimmed_name FROM users;
    

    If the name column in the users table has leading spaces, this will remove them in the output.

Important Notes:

  • LTRIM() only removes the spaces at the beginning of the string. If you need to remove spaces from both the beginning and the end, use the TRIM() function instead.
  • It does not remove any non-space characters at the start of the string.

No comments:

Post a Comment