Lpad
Lpad is a string manipulation function used to pad the left side of a text string with a specified padding string until a desired total length is reached. It is commonly employed to align output in fixed-width formats, format numbers as identifiers, or prepare values for data export. The operation typically involves providing the source string, the target length, and an optional padding string (which may default to a single space in some implementations). If the source string is longer than the target length, the result is usually truncated to the requested length.
In mainstream SQL dialects, Lpad generally follows a pattern such as LPAD(string, length, pad_string). The padding
- Oracle: LPAD(string, length, pad_string)
- MySQL: LPAD(str, length, padstr)
- PostgreSQL: LPAD(string, length, pad_string)
- SQL Server: There is no built-in LPAD function; a typical workaround uses REPLICATE combined with LEFT
- LPAD('cat', 5, 'x') yields 'xxcat'
- LPAD('123', 6, '0') yields '000123'
- If the target length is less than the input length, results are often truncated to the
- Some systems count length in characters, other systems in bytes, which can affect results with multibyte