Oracle Number Function - Oracle/PLSQL SQRT Function






This Oracle tutorial explains how to use the Oracle/PLSQL SQRT function.

Sqrt returns the square root of a number or value.

Syntax

The syntax for the Oracle/PLSQL SQRT function is:

SQRT( n )

n is a positive number.

Example


SQL> select sqrt(4) from dual;

   SQRT(4)
----------
         2

SQL>

create table TestTable(
      ID                 VARCHAR2(4 BYTE)         NOT NULL,
      MyNumber           Number(8,2)--   w  w w  .  ja v a2  s. c  o  m
    );

insert into TestTable (ID, MyNumber)values('1',12.12);
insert into TestTable (ID, MyNumber)values('1',1.1);


SQL> select myNumber from testTable;

  MYNUMBER
----------
     12.12
       1.1

SQL> select sqrt(myNumber) from testTable;

SQRT(MYNUMBER)
--------------
    3.48137904
    1.04880885

SQL>

sqrt doesn't accept negative values.


create table TestTable(
      ID                 VARCHAR2(4 BYTE)         NOT NULL,
      MyNumber           Number(8,2)-- from  w w  w  .  j a va 2s  .c  o m
    );

insert into TestTable (ID, MyNumber)values('1',12.12);
insert into TestTable (ID, MyNumber)values('1',-1.1);

SQL> select myNumber from testTable;

  MYNUMBER
----------
     12.12
      -1.1

SQL> select sqrt(myNumber) from testTable;
ERROR:
ORA-01428: argument '-1.1' is out of range



no rows selected

SQL> drop table testTable;

Table dropped.

SQL>
SQL>




Nest SQRT and ABS

Functions can be nested. The following example combines the sqrt and abs:


SQL> select myNumber from testTable;
-- from   w  ww . j a  v a 2  s .co  m
  MYNUMBER
----------
     12.12
      -1.1

SQL> select sqrt(abs(myNumber)) from testTable;

SQRT(ABS(MYNUMBER))
-------------------
         3.48137904
         1.04880885

SQL> drop table testTable;