Skip to content

Commit ce69aca

Browse files
ittuannhfxsdqiancai
authored
Fix: Enrich instructions for string functions FORMAT() and FROM_BASE64() (#15487) (#16085)
* Enrich instructions for string functions FORMAT() and FROM_BASE64() * Add a detailed description * Update the lint CI * Apply suggestions from code review * Apply reviewer recommendations Co-authored-by: xixirangrang <[email protected]> * Apply suggestions from code review Co-authored-by: xixirangrang <[email protected]> * Update functions-and-operators/string-functions.md * Update functions-and-operators/string-functions.md * Update functions-and-operators/string-functions.md * Apply suggestions from code review Co-authored-by: Grace Cai <[email protected]> * Update functions-and-operators/string-functions.md Co-authored-by: Grace Cai <[email protected]> * Update functions-and-operators/string-functions.md * Update functions-and-operators/string-functions.md --------- Co-authored-by: xixirangrang <[email protected]> Co-authored-by: Grace Cai <[email protected]>
1 parent 420f7f7 commit ce69aca

File tree

1 file changed

+91
-2
lines changed

1 file changed

+91
-2
lines changed

functions-and-operators/string-functions.md

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,100 @@ Return the index position of the first argument within the second argument.
440440

441441
### [`FORMAT()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_format)
442442

443-
Return a number formatted to specified number of decimal places.
443+
The `FORMAT(X,D[,locale])` function is used to format the number `X` to a format similar to `"#,###,###. ##"`, rounded to `D` decimal places, and return the result as a string.
444+
445+
Arguments:
446+
447+
- `X`: the number to be formatted. It can be a direct numeric value, a numeric string, or a number in scientific notation.
448+
- `D`: the number of decimal places for the returned value. The function rounds the number `X` to `D` decimal places. If `D` is greater than the actual number of decimal places in `X`, the result is padded with zeros to the corresponding length.
449+
`[locale]`: specifies a locale setting to be used for grouping between decimal points, thousands separators, and separators for resultant numbers. A valid locale value is the same as the valid value of the [`lc_time_names`](https://dev.mysql.com/doc/refman/8.3/en/server-system-variables.html#sysvar_lc_time_names) system variable. If not specified or the region setting is `NULL`, the `'en_US'` region setting is used by default. This argument is optional.
450+
451+
Behaviors:
452+
453+
- If the first argument is a string and contains only numbers, the function returns a result based on that numeric value. For example, `FORMAT('12.34', 1)` and `FORMAT(12.34, 1)` return the same result.
454+
- If the first argument is a number represented in scientific notation (using `E/e`), the function returns the result based on that number. For example, `FORMAT('1E2', 3)` returns `100.000`.
455+
- If the first argument is a string starting with non-numeric characters, the function returns zero and a warning `(Code 1292)`. For example, `FORMAT('q12.36', 5)` returns `0.00000`, but also includes a warning `Warning (Code 1292): Truncated incorrect DOUBLE value: 'q12.36'`.
456+
- If the first argument is a string mixing numbers and non-numbers, the function returns a result based on the consecutive numeric part at the beginning of the argument, and also includes a warning `(Code 1292)`. For example, `FORMAT('12.36q56.78', 1)` returns the same numeric result as `FORMAT('12.36', 1)`, but includes a warning `Warning (Code 1292): Truncated incorrect DOUBLE value: '12.36q56.78'`.
457+
- If the second argument is zero or a negative number, the function truncates the decimal part and returns an integer.
458+
- If any of the arguments is `NULL`, the function returns `NULL`.
459+
460+
Examples:
461+
462+
The following examples show how to format the number 12.36 to different decimal places:
463+
464+
```sql
465+
mysql> SELECT FORMAT(12.36, 1);
466+
+------------------+
467+
| FORMAT(12.36, 1) |
468+
+------------------+
469+
| 12.4 |
470+
+------------------+
471+
```
472+
473+
```sql
474+
mysql> SELECT FORMAT(12.36, 5);
475+
+------------------+
476+
| FORMAT(12.36, 5) |
477+
+------------------+
478+
| 12.36000 |
479+
+------------------+
480+
```
481+
482+
```sql
483+
mysql> SELECT FORMAT(12.36, 2);
484+
+------------------+
485+
| FORMAT(12.36, 2) |
486+
+------------------+
487+
| 12.36 |
488+
+------------------+
489+
```
444490

445491
### [`FROM_BASE64()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_from-base64)
446492

447-
Decode to a base-64 string and return result.
493+
The `FROM_BASE64()` function is used to decode a [Base64](https://datatracker.ietf.org/doc/html/rfc4648) encoded string and return the decoded result in its hexadecimal form.
494+
495+
- This function accepts a single argument, that is, the Base64 encoded string to be decoded.
496+
- If the argument is `NULL` or not a valid Base64 encoded string, the `FROM_BASE64()` function returns `NULL`.
497+
498+
Examples:
499+
500+
The following example shows how to decode the Base64 encoded string `'SGVsbG8gVGlEQg=='`. This string is the result of encoding `'Hello TiDB'`, using the [`TO_BASE64()`](#to_base64) function.
501+
502+
```sql
503+
mysql> SELECT TO_BASE64('Hello TiDB');
504+
+-------------------------+
505+
| TO_BASE64('Hello TiDB') |
506+
+-------------------------+
507+
| SGVsbG8gVGlEQg== |
508+
+-------------------------+
509+
510+
mysql> SELECT FROM_BASE64('SGVsbG8gVGlEQg==');
511+
+------------------------------------------------------------------+
512+
| FROM_BASE64('SGVsbG8gVGlEQg==') |
513+
+------------------------------------------------------------------+
514+
| 0x48656C6C6F2054694442 |
515+
+------------------------------------------------------------------+
516+
```
517+
518+
```sql
519+
mysql> SELECT CONVERT(FROM_BASE64('SGVsbG8gVGlEQg==') USING utf8mb4);
520+
+--------------------------------------------------------+
521+
| CONVERT(FROM_BASE64('SGVsbG8gVGlEQg==') USING utf8mb4) |
522+
+--------------------------------------------------------+
523+
| Hello TiDB |
524+
+--------------------------------------------------------+
525+
```
526+
527+
The following example shows how to decode the Base64 encoded number `MTIzNDU2`. This string is the result of encoding `123456`, which can be done using the [`TO_BASE64()`](#to_base64) function.
528+
529+
```sql
530+
mysql> SELECT FROM_BASE64('MTIzNDU2');
531+
+--------------------------------------------------+
532+
| FROM_BASE64('MTIzNDU2') |
533+
+--------------------------------------------------+
534+
| 0x313233343536 |
535+
+--------------------------------------------------+
536+
```
448537

449538
### [`HEX()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_hex)
450539

0 commit comments

Comments
 (0)