0% found this document useful (0 votes)
69 views

Dual

This document provides examples of various SQL functions including mathematical, string, conversion, and date functions. It also provides examples of cursors and the GOTO statement in PL/SQL. The mathematical functions calculate values like absolute value, power, round, square root, ceiling, floor, and modulus. The string functions manipulate strings with functions like lower, upper, initcap, length, substr, ltrim, rtrim, rpad, and lpad. Conversion functions convert between character and numeric formats. Date functions work with dates and return values like today's date in different formats and calculating months between dates. The cursor examples demonstrate updating records in a loop and the GOTO statement example jumps execution to different labels based on a condition.

Uploaded by

bsrinivas43
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Dual

This document provides examples of various SQL functions including mathematical, string, conversion, and date functions. It also provides examples of cursors and the GOTO statement in PL/SQL. The mathematical functions calculate values like absolute value, power, round, square root, ceiling, floor, and modulus. The string functions manipulate strings with functions like lower, upper, initcap, length, substr, ltrim, rtrim, rpad, and lpad. Conversion functions convert between character and numeric formats. Date functions work with dates and return values like today's date in different formats and calculating months between dates. The cursor examples demonstrate updating records in a loop and the GOTO statement example jumps execution to different labels based on a condition.

Uploaded by

bsrinivas43
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

DUAL FUNCTIONS:-

1.Mathematical Functions:-
SQL> select abs(-19) from dual;

ABS(-19)
---------
19

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

POWER(3,2)
----------
9

SQL> select round(15.176,1) from Dual;

ROUND(15.176,1)
---------------
15.2

SQL> select round(15.176,2) from dual;

ROUND(15.176,2)
---------------
15.18

SQL> select sqrt(16) from dual;

SQRT(16)
---------
4

SQL> select ceil(25.6) from dual;

CEIL(25.6)
----------
26

SQL> select floor(25.6) from dual;


FLOOR(25.6)
-----------
25

SQL> select trunc(25.563,2) from dual;

TRUNC(25.563,2)
---------------
25.56
SQL> select mod(7,2) from dual;

MOD(7,2)
---------
1

2.String Functions:-

SQL> select lower('JOSEPH') from dual;

LOWER(
------
joseph

SQL> select upper('joseph') from dual;

UPPER(
------
JOSEPH

SQL> select initcap('joseph') from dual;

INITCAP
------
Joseph

SQL> select length('joseph') from dual;

LENGTH('JOSEPH')
----------------
6

SQL> select substr('joseph',3,2) from dual;

SUBSTR
--
se

SQL> select LTRIM('xxxxjosephxxx','x') from dual;

LTRIM('XX
---------
josephxxx

SQL> select RTRIM('xxxxjosephxxx','x') from dual;

RTRIM('XXX
----------
xxxxjoseph
SQL> select RPAD('joseph',10,'*') from dual;

RPAD('JOSE
----------
joseph****

SQL> select LPAD('joseph',10,'*') from dual;

LPAD('JOSE
----------
****joseph

Conversion Functions:-
SQL> select chr(97) from dual;

C
-
a

SQL> select chr(122) from dual;

C
-
z

SQL> select ASCII('a') from dual;

ASCII(‘a')
----------
97
SQL> select ASCII('b') from dual;

ASCII('B')
----------
98

Date Functions:-
SQL> select sysdate from dual;

SYSDATE
---------
08-APR-06

SQL> select to_char(sysdate,'Month DD,YYYY') from dual;

TO_CHAR(SYSDATE,'
-----------------
April 08,2006

SQL> select to_date('08-apr-06','YYYY-MM-DD') from dual;

TO_DATE('
---------
06-APR-08

SQL> select months_between('08-jun-06','08-apr-06') from dual;

MONTHS_BETWEEN('08-JUN-06','08-APR-06')
---------------------------------------
2
Cursors:-
1.Write a PL/SQL code to
SQL> declare
2 cursor c is select empno,sal from emp where deptno=20;
3 eno emp.empno%type;
4 salary emp.sal%type;
5 begin
6 open c;
7 loop
8 fetch c into eno,salary;
9 update emp set sal=salary + (salary*0.05) where empno=eno;
10 end loop;
11 commit;
12 end;
13 /
PL/SQL procedure successfully completed.

2.write a PL/SQL code to demonstrate goto statement.

SQL> declare
2 sal number(7,2):=&sal;
3 begin
4 if sal>=5000 then
5 goto a2;
6 else
7 goto a1;
8 end if;
9 <<a1>>
10 sal:=sal+(sal*0.05);
11 dbms_output.put_line('salary='||sal);
12 <<a2>>
13 sal:=sal+(sal*0.02);
14 dbms_output.put_line('salary='||sal);
15 end;
16 /
Enter value for sal: 8000
old 2: sal number(7,2):=&sal;
new 2: sal number(7,2):=8000;
salary=8160

PL/SQL procedure successfully completed.

You might also like