Integer division

Newbies in SQL Server sometimes fall in bewilderment when getting the results of queries like this

select 1/3 as a, 5/3 as b;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]

Somebody (I suspect, they are users of MySQL or Oracle) expects the results in somewhat as 

ab
0.33331.6667
ab
02
ab
01

To resolve this bewilderment I shall tell, that operation “/” designates just the integer part of the result of dividing two integers  (namely, quotient) if both operands are integers. I.e. the separate designation for this operation is not supplied, and the symbol of “usual division” is used. If you wish to receive decimal number you need convert at least one operand to real data type explicitly (the first column) or implicitly (the second column):

select cast(1 as DEC(12,4))/3 as a, 5./3 as b;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]

ab
0.3333331.66667

Modulo operation is designated as “%” in SQL Server:

select 1 % 3 as a, 5 % 3 as b;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]
ab
12

As for some other DBMS.

PostgreSQL behaves similarly SQL Server.

MySQL has a special operator DIV for obtaining a quotient:

select 1 DIV 3 as a, 5 DIV 3 as b;

The remainder of division can be received also a la Pascal:

select 1 MOD 3 as a, 5 MOD 3 as b;

though “standard” operator will also work:

select 1 % 3 as a, 5 % 3 as b;

Oracle has not operator for getting quotient in general, so the division result

select 1/3 as a, 5/3 as b from dual;
ab
0.3333331.66667
select CEIL(1/3) as a, CEIL(5/3) as b from dual;
ab
12
select FLOOR(1/3) as a, FLOOR(5/3) as b from dual;
ab
01

MOD function is used in Oracle for getting  remainder of division:

select MOD(1,3) as a, MOD(5,3) as b from dual;

At last, if the divider equals zero

select
1/0 as a;
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]
MySQL returns NULL, whereas other DBMS considered here return divide by zero error.

Suggested exercises: 137