PARSE_DATA_SIZE
Description
The PARSE_DATA_SIZE function parses a string with storage units (such as "1.5GB") and converts it to a numeric value in bytes.
Syntax
PARSE_DATA_SIZE(<str>)
Parameters
| Parameter | Description |
|---|---|
<str> | Data size string with unit (e.g., "100MB", "2.5GB"). Type: VARCHAR |
Return Value
Returns BIGINT type, representing the numeric value converted to bytes.
Special cases:
- Supported units (case-insensitive): B, kB, MB, GB, TB, PB, EB, ZB, YB
- Units use base 1024 (e.g., 1kB = 1024B)
- Supports decimals (e.g., "2.5MB")
- If parameter format is invalid, returns error
- If parameter is NULL, returns NULL
Supported Units Table:
| Unit | Name | Bytes |
|---|---|---|
| B | Bytes | 1 |
| kB | Kilobytes | 1024 |
| MB | Megabytes | 1024² |
| GB | Gigabytes | 1024³ |
| TB | Terabytes | 1024⁴ |
| PB | Petabytes | 1024⁵ |
| EB | Exabytes | 1024⁶ |
Examples
- Basic usage: Parse bytes
SELECT parse_data_size('1024B');
+--------------------------+
| parse_data_size('1024B') |
+--------------------------+
| 1024 |
+--------------------------+
- Parse kilobytes
SELECT parse_data_size('1kB');
+------------------------+
| parse_data_size('1kB') |
+------------------------+
| 1024 |
+------------------------+
- Parse megabytes with decimals
SELECT parse_data_size('2.5MB');
+--------------------------+
| parse_data_size('2.5MB') |
+--------------------------+
| 2621440 |
+--------------------------+
- Parse gigabytes
SELECT parse_data_size('1GB');
+------------------------+
| parse_data_size('1GB') |
+------------------------+
| 1073741824 |
+------------------------+
- Parse terabytes
SELECT parse_data_size('1TB');
+------------------------+
| parse_data_size('1TB') |
+------------------------+
| 1099511627776 |
+------------------------+
- Unsupported unit, error
SELECT parse_data_size('1iB');
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[INVALID_ARGUMENT]Invalid Input argument "1iB" of function parse_data_size
- Input NULL
SELECT parse_data_size(NULL);
+-----------------------+
| parse_data_size(NULL) |
+-----------------------+
| NULL |
+-----------------------+
Keywords
PARSE_DATA_SIZE