Oracle PL/SQL Tutorial/Numerical Math Functions/POWER — различия между версиями

Материал из SQL эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 13:45, 26 мая 2010

POWER(2, 1)

SQL> select POWER(2, 1) from dual;
POWER(2,1)
----------
         2


POWER(2, 3)

SQL> select POWER(2, 3) from dual;
POWER(2,3)
----------
         8


POWER(x, y) gets the result of x raised to the power y.

The following example uses POWER() to display 2 raised to the power 1 and 3, respectively:



SQL> SELECT POWER(2, 1), POWER(2, 3) FROM dual;
POWER(2,1) POWER(2,3)
---------- ----------
         2          8
SQL>


select power(2,3), power(-2,3)

SQL>
SQL>
SQL>
SQL> select power(2,3), power(-2,3)
  2  from   dual;
POWER(2,3) POWER(-2,3)
---------- -----------
         8          -8
SQL>


Using the POWER function

The POWER function requires two arguments.

The first argument is the value that you would like raised to some exponential power.

The second argument is the power (exponent) that you would like the number raised to.



SQL>
SQL>
SQL> -- create demo table
SQL> create table myTable(
  2    id           NUMBER(2),
  3    value        NUMBER(6,2)
  4  )
  5  /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into myTable(ID,  value)values (1,9)
  2  /
1 row created.
SQL> insert into myTable(ID,  value)values (2,2.11)
  2  /
1 row created.
SQL> insert into myTable(ID,  value)values (3,3.44)
  2  /
1 row created.
SQL> insert into myTable(ID,  value)values (4,-4.21)
  2  /
1 row created.
SQL> insert into myTable(ID,  value)values (5,10)
  2  /
1 row created.
SQL> insert into myTable(ID,  value)values (6,3)
  2  /
1 row created.
SQL> insert into myTable(ID,  value)values (7,-5.88)
  2  /
1 row created.
SQL> insert into myTable(ID,  value)values (8,123.45)
  2  /
1 row created.
SQL> insert into myTable(ID,  value)values (9,98.23)
  2  /
1 row created.
SQL>
SQL> select * from myTable
  2  /
        ID      VALUE
---------- ----------
         1          9
         2       2.11
         3       3.44
         4      -4.21
         5         10
         6          3
         7      -5.88
         8     123.45
         9      98.23
9 rows selected.
SQL>
SQL> select power(value, 2) from myTable
  2  /
POWER(VALUE,2)
--------------
            81
        4.4521
       11.8336
       17.7241
           100
             9
       34.5744
    15239.9025
     9649.1329
9 rows selected.
SQL>
SQL> select power(value, 3) from myTable
  2  /
POWER(VALUE,3)
--------------
           729
      9.393931
     40.707584
    -74.618461
          1000
            27
    -203.29747
    1881365.96
    947834.325
9 rows selected.
SQL>
SQL>
SQL> -- clean the table
SQL> drop table myTable
  2  /
Table dropped.
SQL>