- Categories:
String & binary functions (General)
LPAD¶
Left-pads a string with characters from another string, or left-pads a binary value with bytes from another binary value.
The argument (base
) is left-padded to length length_expr
with characters/bytes from the pad
argument.
Syntax¶
LPAD(<base>, <length_expr> [, <pad>])
Arguments¶
base
This must be a
VARCHAR
orBINARY
value.length_expr
The length should be an expression that evaluates to an integer. It should specify:
The number of UTF-8 characters to return if the input is
VARCHAR
.The number of bytes to return if the input is
BINARY
.
pad
This must be a
VARCHAR
orBINARY
value. The type must match the data type of thebase
parameter. Characters (or bytes) from this parameter are used to pad thebase
.
Returns¶
The data type of the returned value is the same as the data type of the base
input value (BINARY
or VARCHAR
).
Usage notes¶
If the
base
argument is longer thanlength_expr
, then thebase
is truncated to lengthlength_expr
.The
pad
argument can be multiple characters/bytes long. Thepad
argument is repeated in the result until the desired lengthlength_expr
is reached, truncating any superfluous characters/bytes in thepad
argument. If thepad
argument is empty, no padding is inserted, but the result is still truncated to lengthlength_expr
.When
base
is a string, the defaultpad
string default is ‘ ‘ (a single blank space). Whenbase
is a binary value, thepad
argument must be provided explicitly.
Collation details¶
Collation applies to VARCHAR
inputs. Collation does not apply if the input data type of the first parameter is
BINARY
.
Although collation is accepted syntactically, collations have no impact on processing. For example, languages with two-character and three-character letters (e.g. “dzs” in Hungarian, “ch” in Czech) still count those as two or three characters (not one character) for the length argument.
The collation of the result is the same as the collation of the first input. This might be useful if the returned value is passed to another function as part of nested function calls.
Currently, Snowflake allows the base
and pad
arguments to have different collation specifiers.
However, the individual collation specifiers cannot both be retained because the returned value will have only one
collation specifier. Snowflake recommends that users avoid using pad
strings that have different
collation from the base
string.
Examples¶
This example shows padding of VARCHAR
and BINARY
data:
Create and fill a table:
CREATE TABLE demo (v VARCHAR, b BINARY); INSERT INTO demo (v, b) SELECT 'Hi', HEX_ENCODE('Hi'); INSERT INTO demo (v, b) SELECT '-123.00', HEX_ENCODE('-123.00'); INSERT INTO demo (v, b) SELECT 'Twelve Dollars', TO_BINARY(HEX_ENCODE('Twelve Dollars'), 'HEX');Demonstrate padding of
VARCHAR
:SELECT v, LPAD(v, 10, ' '), LPAD(v, 10, '$') FROM demo ORDER BY v; +----------------+------------------+------------------+ | V | LPAD(V, 10, ' ') | LPAD(V, 10, '$') | |----------------+------------------+------------------| | -123.00 | -123.00 | $$$-123.00 | | Hi | Hi | $$$$$$$$Hi | | Twelve Dollars | Twelve Dol | Twelve Dol | +----------------+------------------+------------------+Demonstrate padding of
BINARY
:SELECT b, LPAD(b, 10, TO_BINARY(HEX_ENCODE(' '))) AS PAD_WITH_BLANK, LPAD(b, 10, TO_BINARY(HEX_ENCODE('$'))) AS PAD_WITH_DOLLAR_SIGN FROM demo ORDER BY b; +------------------------------+----------------------+----------------------+ | B | PAD_WITH_BLANK | PAD_WITH_DOLLAR_SIGN | |------------------------------+----------------------+----------------------| | 2D3132332E3030 | 2020202D3132332E3030 | 2424242D3132332E3030 | | 4869 | 20202020202020204869 | 24242424242424244869 | | 5477656C766520446F6C6C617273 | 5477656C766520446F6C | 5477656C766520446F6C | +------------------------------+----------------------+----------------------+
This example shows padding when multiple characters are used and when the padding is not an even multiple of the length of the multi-character padding string:
SELECT LPAD('123.50', 19, '*_'); +--------------------------+ | LPAD('123.50', 19, '*_') | |--------------------------| | *_*_*_*_*_*_*123.50 | +--------------------------+