Oracle中如何取模?

InFerNaL_LioN 2007-06-26 02:17:55
如题~~~
...全文
6952 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyspark 2007-07-27
  • 打赏
  • 举报
回复
mod()函数
zbyh331 2007-07-26
  • 打赏
  • 举报
回复
select mod(1, 3) from dual
he MOD or modula function is used to return the remainder from a division. If there is no remainder, the number zero is returned. This function can be used to control a loop that is only to process every nth record.

For example, a sampling algorithm for marketing may only want to look at every hundredth record out of a base of a million records to get a representative sample and not have to deal with the full set of records.

Syntax
>ÄÄMODÄÄ(m,n)ÄÄÄ><
PL/SQL Example
You can use MOD to determine quickly if a number is odd or even:

FUNCTION is_odd (num_in IN NUMBER) RETURN BOOLEAN
IS
BEGIN
RETURN MOD (num_in, 2) = 1;
END;
FUNCTION is_even (num_in IN NUMBER) RETURN BOOLEAN
IS
BEGIN
RETURN MOD (num_in, 2) = 0;
END;
SQL Example
SELECT MOD(11,4) "Modulus"
FROM DUAL
Modulus
----------
3
This function behaves differently from the classical mathematical modulus function when m is negative. The classical modulus can be expressed using the MOD function with this formula:

m - n * FLOOR(m/n)
The following statement illustrates the difference between the MOD function and the classical modulus:

SELECT m, n, MOD(m, n),
m - n * FLOOR(m/n) "Classical Modulus"
FROM test_mod_table
M N MOD (M,N) Classical Modulus
--- ---- -------- --------- -------
11 4 3
-11 4 -3 1
11 -4 3 -1
-11 -4 -3 -3
hongqi162 2007-06-26
  • 打赏
  • 举报
回复
select mod(1, 3) from dual

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧