Calculating byte values to megabyte (MB) in MySQL?


Here, we are taking BIGINT type, since it takes 8 byte signed integer. Let us first create a table with column as BIGINT type −

mysql> create table DemoTable2031
   -> (
   -> ByteValue bigint
   -> );
Query OK, 0 rows affected (1.17 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable2031 values(1048576);
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable2031 values(1073741824);
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable2031;

This will produce the following output −

+------------+
| ByteValue  |
+------------+
| 1048576    |
| 1073741824 |
+------------+
2 rows in set (0.00 sec)

Here is the query to calculate bytes to Megabyte (MB) −

mysql> select (ByteValue/(1024*1024)) as MB from DemoTable2031;

This will produce the following output −

+-----------+
| MB        |
+-----------+
| 1.0000    |
| 1024.0000 |
+-----------+
2 rows in set (0.00 sec)

Updated on: 06-Apr-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements