Tabular IQ
Back to Function Index

LEFT()

Overview

LEFT() extracts a specified number of characters from the beginning of a string. This function is useful for string manipulation, data extraction, and formatting where you need to work with the left portion of a string.

Function Format

LEFT(string, length)

Return Value

LEFT() returns a character string

Examples


LEFT("APPLES AND ORANGES", 5)     = "APPLE"           (first 5 characters)
LEFT("APPLES AND ORANGES", 10)    = "APPLES AND"      (first 10 characters)
LEFT("APPLES AND ORANGES", 20)    = "APPLES AND ORANGES" (entire string)
LEFT("Hello World", 3)            = "Hel"             (first 3 characters)
LEFT("12345", 2)                  = "12"              (first 2 characters)
LEFT("", 5)                       = ""                (empty string)
LEFT(NULL, 5)                     = NULL              (NULL input)

Notes

  • Important behaviors:
    • If length exceeds the string length, the entire string is returned
    • If length is 0, an empty string is returned
    • If the input string is NULL, NULL is returned
    • If length is negative, an error may occur
  • Common use cases include:
    • String truncation
    • Data extraction
    • Text formatting
    • Prefix extraction
    • Data cleaning
  • The function can be used with:
    • String literals
    • String columns
    • String expressions
    • String functions
  • This function is often used in combination with other string functions like RIGHT() and SUBSTR() for more complex string manipulations.
  • When used with LENGTH(), it can help extract variable-length prefixes from strings.
  • For extracting from the right side of a string, use the RIGHT() function instead.

See Also