oracle 取余/求积

https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions112.htm

POWER

Syntax

Description of power.gif follows
Description of the illustration power.gif
 

Purpose

POWER returns n2 raised to the n1 power. The base n2 and the exponent n1 can be any numbers, but if n2 is negative, then n1 must be an integer.

This function takes as arguments any numeric datatype or any nonnumeric datatype that can be implicitly converted to a numeric datatype. If any argument is BINARY_FLOAT or BINARY_DOUBLE, then the function returns BINARY_DOUBLE. Otherwise the function returns NUMBER.

See Also:

Table 2-10, "Implicit Type Conversion Matrix" for more information on implicit conversion

Examples

The following example returns 3 squared:

SELECT POWER(3,2) "Raised" FROM DUAL;

    Raised
----------
         9

https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions088.htm

MOD

Syntax

Description of mod.gif follows
Description of the illustration mod.gif
 

Purpose

MOD returns the remainder of n2 divided by n1. Returns n2 if n1 is 0.

This function takes as arguments any numeric datatype or any nonnumeric datatype that can be implicitly converted to a numeric datatype. Oracle determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that datatype, and returns that datatype.

See Also:

Table 2-10, "Implicit Type Conversion Matrix" for more information on implicit conversion and "Numeric Precedence" for information on numeric precedence

Examples

The following example returns the remainder of 11 divided by 4:

SELECT MOD(11,4) "Modulus" FROM DUAL;

   Modulus
----------
         3

转载于:https://my.oschina.net/u/2308739/blog/1504559

Oracle数据库中,可以使用 `MOD` 函数来进行运算。该函数返回两个数值相除后的数。其语法如下: ```sql MOD(dividend, divisor) ``` 其中,`dividend` 是被除数,`divisor` 是除数。如果除数为0,则 `MOD` 函数会返回被除数本身。 例如,计算 10 除以 3 的数,可以使用以下 SQL 查询: ```sql SELECT MOD(10, 3) AS MODRESULT FROM DUAL; ``` 该查询将返回结果为 1,因为 10 除以 3 的数是 1 [^1]。 此外,如果涉及到负数的情况,Oracle 的 `MOD` 函数也会正确处理。例如: ```sql SELECT MOD(-10, 3) AS MODRESULT FROM DUAL; ``` 这个查询将返回 -1,因为 -10 除以 3 的数是 -1。 需要注意的是,`MOD` 函数在处理浮点数时可能会有精度问题,因此建议在进行操作时尽量使用整数类型的数据 [^2]。 ### 整与结合使用 在某些情况下,可能需要同时进行整和操作。Oracle 提供了多种整函数,如 `CEIL`、`FLOOR`、`TRUNC` 和 `ROUND`,这些函数可以与 `MOD` 结合使用来实现更复杂的逻辑。 例如,若想获一个数除以另一个数后的商并向下整,可以使用 `FLOOR` 函数: ```sql SELECT FLOOR(10 / 3) AS QUOTIENT FROM DUAL; ``` 此查询将返回 3,因为 10 除以 3 的商是 3.333...,向下整后得到 3 [^2]。 若想获商并向上整,可以使用 `CEIL` 函数: ```sql SELECT CEIL(10 / 3) AS QUOTIENT FROM DUAL; ``` 此查询也将返回 4,因为 10 除以 3 的商是 3.333...,向上整后得到 4 [^2]。 若想直接获商并截断小数部分,可以使用 `TRUNC` 函数: ```sql SELECT TRUNC(10 / 3) AS QUOTIENT FROM DUAL; ``` 此查询将返回 3,因为 10 除以 3 的商是 3.333...,截断小数部分后得到 3 [^2]。 若想获商并四舍五入,可以使用 `ROUND` 函数: ```sql SELECT ROUND(10 / 3) AS QUOTIENT FROM DUAL; ``` 此查询将返回 3,因为 10 除以 3 的商是 3.333...,四舍五入后得到 3 [^2]。 通过这些函数的组合使用,可以在 Oracle 数据库中灵活地进行整操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值