Tabular IQ
Back to Function Index

LENGTH()

Overview

LENGTH() returns the number of characters in a specified string, including any leading or trailing blanks. This function is useful for string validation, data cleaning, and text analysis where you need to determine the size of a string.

Function Format

LENGTH(string)

Return Value

LENGTH() returns an integer value

Examples


LENGTH("APPLES") = 6                (counts all characters)
LENGTH(" APPLES ") = 8              (includes leading and trailing spaces)
LENGTH(TRIM(" APPLES ")) = 6        (spaces removed before counting)
LENGTH("") = 0                      (empty string)
LENGTH(NULL) = NULL                 (NULL input)
LENGTH("Hello World") = 11          (includes space between words)

Notes

  • Important behaviors:
    • Counts all characters, including spaces
    • Returns 0 for empty strings
    • Returns NULL for NULL input
    • Counts each character exactly once
  • Common use cases include:
    • String validation
    • Data cleaning
    • Text analysis
    • Input validation
    • String manipulation
  • To exclude leading and trailing spaces from the count:
    • Use TRIM() before LENGTH()
    • Example: LENGTH(TRIM(" APPLES "))
  • 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 TRIM(), LEFT(), and RIGHT() for more complex string manipulations.

See Also