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

Week 1 Dual Table

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Week 1 Dual Table

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

WEEK-1

Problem statement: Implementation of SQL built-in functions under the category of single
row functions using DUAL Table

Queries:
A. Character Functions
1. LOWER – converts char values to lowercase
SYNTAX: lower(char_value)
SQL> select lower('OrAcLe') from dual;
LOWER(
------
oracle
SQL> select lower('12$Hi*6') from dual;
LOWER('
-------
12$hi*6
2. UPPER – converts char values to uppercase
SYNTAX: upper(char_value)
SQL> select upper('HarShiThAa') from dual;
UPPER('HAR
----------
HARSHITHAA
SQL> select upper('(01ar23)') from dual;
UPPER('(
--------
(01AR23)
3. INITCAP – converts first character to uppercase and remaining to lowercase
SYNTAX: initcap(char_value)
SQL> select initcap('hello, guru!') from dual;
INITCAP('HEL
------------
Hello, Guru!
SQL> select initcap('9th cLouD') from dual;
INITCAP('
---------
9th Cloud
4. CONCAT – concats first string with second string
SYNTAX: concat(string1,string2)
SQL> select Concat('Naam tho ','suna hoga') from dual;
CONCAT('NAAMTHO','
------------------
Naam tho suna hoga
SQL> select Concat('im','5n') from dual;
CONC
----
im5n
5. SUBSTR – returns specified character from character value starting at position m and n
characters long. If you omit n, all characters from m to end are returned.
SYNTAX: substr(string[,m,n])
SQL> select substr('abdul rab',5,3) from dual;
SUB
---
lr
SQL> select substr('abdul rab',7) from dual;
SUB
---
rab
SQL> select substr('hello world',2,-3) from dual;
S
-
SQL> select substr('hello world',-2) from dual;

SU
--
ld
SQL> select substr('hello world',-7,3) from dual;
SUB
---
ow
6. LENGTH – to find no. Of characters in a string
SYNTAX: length(string)
SQL> select length('GolMaAl3') from Dual;
LENGTH('GOLMAAL3')
------------------
8
SQL> select length('Naam mei kya rakha hai!') from Dual;
LENGTH('NAAMMEIKYARAKHAHAI!')
-----------------------------
23
7. INSTR – to find position of a string in another string (may be starting at given position m
or at given nth occurance of string)
SYNTAX: instr(mainstring,substring,[m],[n])
SQL> select instr('Oracle Apps','apps') from dual;
INSTR('ORACLEAPPS','APPS')
--------------------------
0
SQL> select instr('Oracle Apps,Mango apps,Apple apps','Apps',1,2) from dual;
INSTR('ORACLEAPPS,MANGOAPPS,APPLEAPPS','APPS',1,2)
--------------------------------------------------
0
SQL> select instr('Mango apps,Apple apps','Apps',2) from dual;
INSTR('MANGOAPPS,APPLEAPPS','APPS',2)
-------------------------------------
0
SQL> select instr('Apple Apps, mango Apps','Apps',2) from dual;
INSTR('APPLEAPPS,MANGOAPPS','APPS',2)
-------------------------------------
7
SQL> select instr('Oracle Apps,Mango Apps,Apple Apps','Apps',1,2) from dual;
INSTR('ORACLEAPPS,MANGOAPPS,APPLEAPPS','APPS',1,2)
--------------------------------------------------
19
8. LPAD – pads character value to right-justified to total width of n-character positions
SYNTAX: lapd(coloumn,n,string)
SQL> select lpad('100',5,'x') from dual;
LPAD(
-----
xx100
SQL> select lpad('AR',5,'the') from dual;
LPAD(
-----
theAR
SQL> select lpad('abdurab',5,'the') from dual;
LPAD(
-----
abdur
SQL> select lpad('abdurab',10,'the') from dual;
LPAD('ABDU
----------
Theabdurab
9. RPAD – pads character value to left-justified to total width of n-character positions
SYNTAX: rapd(coloumn,n,string)
SQL> select rpad('abdurab',10,'the') from dual;
RPAD('ABDU
----------
abdurabthe
SQL> select rpad('ARbolthe',7,'e') from dual;
RPAD('A
-------
ARbolth
SQL> select rpad('ARbolthe',10,'e') from dual;
RPAD('ARBO
----------
ARboltheee
10. TRIM – removes leading/trailing/ both characters from string
SYNTAX: trim(leading|trailing|both, trim_char from trim_source)
SQL> select trim('o' from 'oracle') from dual;
TRIM(
-----
racle
SQL> select trim('*' from '*AR02100*') from dual;
TRIM('*
-------
AR02100
11. REPLACE – to replace a character with other character in string
SYNTAX: replace(coloumn,old_char,new_char)
SQL> select replace('data in database','in','in the') from dual;
REPLACE('DATAINDATAB
--------------------
data in the database
SQL> select replace('data in database','data','DATA') from dual;
REPLACE('DATAIND
----------------
DATA in DATAbase

B. Number Functions
1. ROUND –rounds to n decimal values
SYNTAX: round(number,n)
SQL> select round(135.24,1) from dual;
ROUND(135.24,1)
---------------
135.2
SQL> select round(135.24,-1) from dual;
ROUND(135.24,-1)
----------------
140
SQL> select round(139.99,-1) from dual;
ROUND(139.99,-1)
----------------
140
SQL> select round(131.99,-1) from dual;
ROUND(131.99,-1)
----------------
130
2. TRUNC – truncates value to n decimal places. If n is omitted, then n defaults to 0.
SYNTAX: trunc(number,n)
SQL> select trunc(123.45) from dual;
TRUNC(123.45)
-------------
123
SQL> select trunc(123.45,3) from dual;
TRUNC(123.45,3)
---------------
123.45
SQL> select trunc(123.45,1) from dual;
TRUNC(123.45,1)
---------------
123.4
SQL> select trunc(123.45,-2) from dual;
TRUNC(123.45,-2)
----------------
100
SQL> select trunc(123.45,-3) from dual;
TRUNC(123.45,-3)
----------------
0
SQL> select trunc(123.45,-1) from dual;
TRUNC(123.45,-1)
----------------
120
3. MOD – returns remainder of m divided by n
SYNTAX: mod(m,n)
SQL> select mod(100,3) from dual;
MOD(100,3)
----------
1
SQL> select mod(100,34) from dual;
MOD(100,34)
-----------
32
4. ABS – returns absolute value of a number
SYNTAX: abs(number)
SQL> select abs(333.33) from dual;
ABS(333.33)
-----------
333.33
SQL> select abs(-333.33) from dual;
ABS(-333.33)
------------
333.33
SQL> select abs(-333.67899) from dual;
ABS(-333.67899)
---------------
333.67899
5. CEIL – rounded up any positive/negative/decimal value when function upwards
SYNTAX: ceil(number)
SQL> select ceil(-2.34) from dual;
CEIL(-2.34)
-----------
-2
SQL> select ceil(-2.89) from dual;
CEIL(-2.89)
-----------
-2
SQL> select ceil(2.89) from dual;
CEIL(2.89)
----------
3
SQL> select ceil(2.34) from dual;
CEIL(2.34)
----------
3
6. FLOOR –rounded up any positive/negative/decimal value down to next integer
SYNTAX: floor(number)
SQL> select floor(2.34) from dual;
FLOOR(2.34)
-----------
2
SQL> select floor(2.97) from dual;
FLOOR(2.97)
-----------
2
SQL> select floor(-2.97) from dual;
FLOOR(-2.97)
------------
-3
SQL> select floor(-2.22) from dual;
FLOOR(-2.22)
------------
-3
7. EXP – returns ‘e’ raised to nth power (where e=2.71828183)
SYNTAX: exp(n)
SQL> select exp(3) from dual;
EXP(3)
----------
20.0855369
SQL> select exp(5) from dual;
EXP(5)
----------
148.413159
8. POWER – returns value of a number rasied to another number
SYNTAX: power(base_number,exponent_number)
SQL> select power(5,4) from dual;
POWER(5,4)
----------
625
SQL> select power(2,7) from dual;
POWER(2,7)
----------
128
SQL> select power(2,-6) from dual;
POWER(2,-6)
-----------
.015625
9. SQRT – returns square root of a number
SYNTAX: sqrt(number)
SQL> select sqrt(36) from dual;
SQRT(36)
----------
6
SQL> select sqrt(441) from dual;
SQRT(441)
----------
21
C. Date Functions
1. SYSDATE – returns current Oracle Database server date and time
SQL> select sysdate from dual;
SYSDATE
---------
13-JAN-20
2. ARITHMETIC WITH DATES – add or subtract days/hours/dates to dates
SQL> select sysdate+2 from dual;
SYSDATE+2
---------
15-JAN-20
SQL> select sysdate-19 from dual;
SYSDATE-1
---------
25-DEC-19
SQL> select sysdate-2/24 from dual;
SYSDATE-2
---------
13-JAN-20
3. MONTHS BETWEEN – returns no. Of months between two dates
SYNTAX: months_between(date1,date2)
SQL> select months_between('26-dec-2019','26-dec-2001') from dual;
MONTHS_BETWEEN('26-DEC-2019','26-DEC-2001')
-------------------------------------------
216
SQL> select months_between('26-mar-2019','26-mar-2001') from dual;

MONTHS_BETWEEN('26-MAR-2019','26-MAR-2001')
-------------------------------------------
216
4. ADD MONTHS – adds/subtracts n no. of calander months
SYNTAX: add_months(date,n)
SQL> select add_months(sysdate,2) from dual;
ADD_MONTH
---------
13-MAR-20
SQL> select add_months('09-feb-2007',2) from dual;
ADD_MONTH
---------
09-APR-07
5. NEXT DAY – prints date of next specified weekday
SYNTAX: next_day(date,’day’)
SQL> select next_day(sysdate,'monday') from dual;
NEXT_DAY(
---------
20-JAN-20
SQL> select next_day('11-oct-2002','monday') from dual;
NEXT_DAY(
---------
14-OCT-02
SQL> select next_day('11-oct-2002','friday') from dual;
NEXT_DAY(
---------
18-OCT-02
6. LAST DAY – prints last day of month
SYNTAX: last_day(date)
SQL> select last_day(sysdate) from dual;
LAST_DAY(
---------
31-JAN-20
SQL> select last_day('22-feb-2222') from dual;
LAST_DAY(
---------
28-FEB-22
7. ROUND – returns date rounded in specified format
SYNTAX: round(date,[‘fmt’])
SQL> select round(sysdate,'year') from dual;
ROUND(SYS
---------
01-JAN-20
SQL> select round(sysdate,'month') from dual;
ROUND(SYS
---------
01-JAN-20
SQL> select round(to_date('26-mar-2001'),'year') from dual;
ROUND(TO_
---------
01-JAN-01
SQL> select round(to_date('16-mar-2001'),'month') from dual;
ROUND(TO_
---------
01-APR-01
SQL> select round(to_date('15-mar-2001'),'month') from dual;
ROUND(TO_
---------
01-MAR-01
8. TRUNC – returns date truncated to specidied format
SYNTAX: trunc(date,[‘fmt’])
SQL> select trunc(to_date('17-apr-2001'),'month') from dual;
TRUNC(TO_
---------
01-APR-01
SQL> select trunc(to_date('10-apr-2001'),'month') from dual;
TRUNC(TO_
---------
01-APR-01
SQL> select trunc(sysdate,'month') from dual;
TRUNC(SYS
---------
01-JAN-20
SQL> select trunc(sysdate,'year') from dual;
TRUNC(SYS
---------
01-JAN-20

D. Conversion Functions
1. TO CHAR – converts to string value for number and date values
SYNTAX: to_char(x[,y])
SQL> select to_char(sysdate) from dual;
TO_CHAR(S
---------
13-JAN-20
SQL> select to_char(sysdate,'day,month YYYY') from dual;
TO_CHAR(SYSDATE,'DAY,MON
------------------------
monday ,january 2020
SQL> select to_char(sysdate,'dd,mon YYYY') from dual;
TO_CHAR(SYS
-----------
13,jan 2020
SQL> select to_char(to_date('26-mar-2001'),'dd,mon YYYY day') from dual;
TO_CHAR(TO_DATE('26-M
---------------------
26,mar 2001 monday
2. TO DATE – converts valid number/character value to date
SYNTAX: to_date(x[,’date_format’])
SQL> select to_date('11102001','dd/mm/yy') from dual;
TO_DATE('
---------
11-OCT-01
SQL> select to_date('26032001','dd-mm-yy') from dual;
TO_DATE('
---------
26-MAR-01
3. NVL – replaces value of NULL
SYNTAX: nvl(x,y) {where, x and y are of same datatype}
SQL> select nvl(null,1) from dual;
NVL(NULL,1)
-----------
1
SQL> select nvl('ab','xyz') from dual;
NV
--
ab
SQL> select nvl(null,'xyz') from dual;
NVL
---
xyz
SQL> select nvl('ab',null) from dual;
NV
--
ab

You might also like