Skip to content

Commit

Permalink
Add LPAD() example
Browse files Browse the repository at this point in the history
  • Loading branch information
hfxsd authored and dveeden committed Apr 2, 2024
1 parent 4eefbc0 commit 143fad7
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions functions-and-operators/string-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,51 @@ SELECT LOWER(-012);

Return the string argument, left-padded with the specified string.

```sql
SELECT LPAD('TiDB',8,'>');
```

```
+--------------------+
| LPAD('TiDB',8,'>') |
+--------------------+
| >>>>TiDB |
+--------------------+
1 row in set (0.00 sec)
```

In the example above the string "TiDB" is left padded until 8 characters with 4 `>` characters.

```sql
SELECT LPAD('TiDB',2,'>');
```

```
+--------------------+
| LPAD('TiDB',2,'>') |
+--------------------+
| Ti |
+--------------------+
1 row in set (0.00 sec)
```

In the example above the string "TiDB" is left padded until 2 characters with `>` characters. The result is "Ti". This shows that the string in the first argument will be truncated if it is longer than the second argument.

```sql
SELECT LPAD('TiDB',-2,'>');
```

```
+---------------------+
| LPAD('TiDB',-2,'>') |
+---------------------+
| NULL |
+---------------------+
1 row in set (0.00 sec)
```

In the example above the string "TiDB" is left padded until -2 characters with `>` characters. The result is NULL as the second argument is negative.

### [`LTRIM()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_ltrim)

Remove leading spaces.
Expand Down

0 comments on commit 143fad7

Please sign in to comment.