Tuesday, 2 August 2016

DIV: Perform integer division



DIV is used to perform integer division on numbers.

Example
5 DIV 2

mysql> SELECT 56 DIV 33;
+-----------+
| 56 DIV 33 |
+-----------+
|         1 | 
+-----------+
1 row in set (0.00 sec)

mysql> SELECT 56 DIV 3;
+----------+
| 56 DIV 3 |
+----------+
|       18 |
+----------+
1 row in set (0.00 sec)


I am going to use following sample data.

CREATE TABLE employee(
  id int PRIMARY KEY,
  firstName VARCHAR(20),
  lastName VARCHAR(20),
  salary DECIMAL(10, 2)
);

INSERT INTO employee VALUES (1, "Hari Krishna", "Gurram", 12345.67);
INSERT INTO employee VALUES (2, "Rama Devi", "Gurram", 1234578.67);
INSERT INTO employee VALUES (3, "Lakshmana", "Rao", 9876543.67);
INSERT INTO employee VALUES (4, "Rama", "Krishna", 1234587.67);
INSERT INTO employee VALUES (5, "Sowmya", "asd", 1238745.67);
INSERT INTO employee VALUES (6, "Jyotsna", "PS", 76543.67);
INSERT INTO employee VALUES (7, "Gireesh", "Amara", 87698);
INSERT INTO employee VALUES (8, "Sravani", "Nidamanuri", 987654);
INSERT INTO employee VALUES (9, "Saranya", "Amara", 1987654);

mysql> SELECT * FROM employee;
+----+--------------+------------+------------+
| id | firstName    | lastName   | salary     |
+----+--------------+------------+------------+
|  1 | Hari Krishna | Gurram     |   12345.67 |
|  2 | Rama Devi    | Gurram     | 1234578.67 |
|  3 | Lakshmana    | Rao        | 9876543.67 |
|  4 | Rama         | Krishna    | 1234587.67 |
|  5 | Sowmya       | asd        | 1238745.67 |
|  6 | Jyotsna      | PS         |   76543.67 |
|  7 | Gireesh      | Amara      |   87698.00 |
|  8 | Sravani      | Nidamanuri |  987654.00 |
|  9 | Saranya      | Amara      | 1987654.00 |
+----+--------------+------------+------------+
9 rows in set (0.00 sec)


Get only integer part of employee salaries.

mysql> SELECT (salary DIV 1) as "Employee Salary" FROM employee;
+-----------------+
| Employee Salary |
+-----------------+
|           12345 |
|         1234578 |
|         9876543 |
|         1234587 |
|         1238745 |
|           76543 |
|           87698 |
|          987654 |
|         1987654 |
+-----------------+
9 rows in set (0.00 sec)






Previous                                                 Next                                                 Home

No comments:

Post a Comment