From 67e0b9f5486e36f6ccf827e1f9234bd83752ab7b Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Wed, 3 Apr 2024 16:25:47 +0800 Subject: [PATCH] Add LPAD() example (#16946) (#16953) --- functions-and-operators/string-functions.md | 38 ++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/functions-and-operators/string-functions.md b/functions-and-operators/string-functions.md index 5245dd4d9d1e2..596ad201b9b9c 100644 --- a/functions-and-operators/string-functions.md +++ b/functions-and-operators/string-functions.md @@ -1185,7 +1185,43 @@ SELECT LOWER(-012); ### [`LPAD()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_lpad) -Return the string argument, left-padded with the specified string. +The `LPAD(str, len, padstr)` function returns the string argument, left-padded with the specified string `padstr` to a length of `len` characters. + +- If `len` is less than the length of the string `str`, the function truncates the string `str` to the length of `len`. +- If `len` is a negative number, the function returns `NULL`. +- If any argument is `NULL`, the function returns `NULL`. + +Examples: + +```sql +SELECT LPAD('TiDB',8,'>'); ++--------------------+ +| LPAD('TiDB',8,'>') | ++--------------------+ +| >>>>TiDB | ++--------------------+ +1 row in set (0.00 sec) +``` + +```sql +SELECT LPAD('TiDB',2,'>'); ++--------------------+ +| LPAD('TiDB',2,'>') | ++--------------------+ +| Ti | ++--------------------+ +1 row in set (0.00 sec) +``` + +```sql +SELECT LPAD('TiDB',-2,'>'); ++---------------------+ +| LPAD('TiDB',-2,'>') | ++---------------------+ +| NULL | ++---------------------+ +1 row in set (0.00 sec) +``` ### [`LTRIM()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_ltrim)