Unit 2
Unit 2
Functions
&
Operators
operators
Like E.g.
Operator 1.Select * from Emp
where FNM LIKE ‘Me%’;
String/Character functions
Aggregate functions
Arithmetic functions
Conversion functions
Date function
String/character function
1.LOWER:- returns char, with all letters in
lowercase
Syntax:-
String or e.g.
lower(char)
from dual;
output:
Substring
--------------
CURE
5.ASCII:-returns the number code that
represents the specified character.
If more than one character is entered, the
function will return the value for the first
character and ignore all the characters after
the first.
syntax:-
ascii(character)
e.g.
select ascii(‘a’) “Ascii 1”, ascii(‘A’) “Ascii 2”,
ascii(‘cure’) “Ascii3” from dual;
output:
Ascii1 Ascii2 Ascii3
-------------------------------
97 65 99
6. LENGTH:- returns a length of a word.
Syntax:-
length(word)
e.g.
select length(‘sharanam’) “length of
string” from dual;
Output
length of string
--------------
8
7.LTRIM:- returns characters from the left of
char with initial characters removed upto
the first character not in set.
Syntax:-
ltrim(char[,set])
e.g.
select ltrim(‘nisha’,’n’) “LTRIM” from dual;
Output
LTRIM
---------
isha
8.RTRIM:- returns char, with final characters
removed after the last character not in set.
‘set’ is optional, it defaults to spaces.
Syntax:-
rtrim(char[,set])
e.g.
select rtrim(‘sunila’,’a’) “RTRIM” from
dual;
Output
RTRIM
-------------
sunil
9. TRIM:- remove all specified character either from beginning or
the ending of a string.
Syntax:-
trim([leading|trailing|both[<trim_character>from]]<string>)
e.g.
select trim(‘ hansel ’) “trim both side” from dual;
Output
trim both side
-------------------
hansel
e.g.
select trim(leading ‘x’ from ‘xxxhanselxxx’) “remove prefixes” from
dual;
Output:
remove prefixes
---------------------
hanselxxx
e.g.
select trim(both ‘x’ from ‘xxxhanselxxx’) “remove both” from dual;
Output:
remove both
--------------------
hansel
10.LPAD:- returns char1, left-papped to
length n with the sequence of character
specified in char2.
Syntax:-
lpad(‘char1,n[,char2])
E.g.
select lpad(‘page1’,10, ‘*’) “lpad” from dual;
Output
lapad
---------------
*****page1
11. RPAD:- returns char1, right papped to
length n with the character specified in
char2.
Syntax:-
rpad(char1,n[,char2])
e.g.
select rpad(ivan,10,’x’) “RPAD” from dual;
Output
RPAD
----------------
ivanxxxxxx
NUMERIC FUNCTIONS…..
1. ABS:- returns the absolute value of ‘n’.
syntax:- ABS(-15)
e.g. Select ABS(-15) “absolute” from dual;
output: absolute
------------
15
Arithmetic 2.POWER:- returns m raised to the nth power.
functions n must be an integer else an error is
returned.
syntax:-power(m,n)
e.g. Select power(3,2)”raised” from dual;
output: raised
---------
9
3.Round:-returns n, rounded to m places to
the right of the decimal point. If m is omitted,
n is rounded to 0 places.
syntax:-round(n,[m])
e.g.: select round(15.91,1) “round” from
dual;
output round
-----------
15.9
4.SQRT:- returns square root of n.
syntax:-sqrt(n)
e.g. select sqrt(25) “square root” from dual;
output square root
--------------
5
5. GREATEST :- returns a greatest value in a
list of expressions.
Syntax:-greatest(expr1,expr2,expr3…expr n)
e.g.:-
select greatest(4,5,17) “num”,
Example:
SELECT SYSDATE, TO_CHAR(‘Oct-05-2020’,’mon’) FROM DUAL;
OUTPUT:
TO_DATE(Date [,fmt])
Converts a date to any date format.
Example:
SELECT SYSDATE, TO_CHAR(‘Oct-05-2020’,’mm/yy’) FROM
DUAL;
OUTPUT:
SYSDATE TO _CHAR(S
-------------- ------------- ----------------------------------------
Oct-05-2020 10/2020
SYSDATE
SYSDATE is a pseudo-column that returns the system’s current date
and time of type DATE.
The SYSDATE can be used just as any other column name. it takes no
arguments.
Example:
SELECT SYSDATE FROM DUAL;
Functions ADD_MONTH(d,n)
OUTPUT:
SYSDATE ADD_MONTHS
------------ ------------------
Oct-03-2020 Feb-03-2021
MONTHS_BETWEEN(d1,d2)
This function returns the number of months
between two dates, d1and d2. if d1 is later than d2, then
the result is positive. If d1 is earlier than d2, then the
result is negative. The output will be a number.
Example
SELECT MONTHS_BETWEEN(“25-DEC-81’,25-DEC-79’) AS DATE1,
MONTHS_BETWEEN(‘25-DEC-79’,’25-DEC-81’) AS DATE2 FROM DUAL;
OUTPUT:
DATE1 DATE2
--------- ----------
24 -24
NEXT_DAY(DATE,DAY)
THIS FUNCTION RETURNS THE DATE OF NEXT SPECIFIED DAY
OF THE WEEK AFTER THE ‘DATE’.
EXAMPLE
SELECT SYSDATE, NEXT_DAY(SYSDATE,’FRIDAY’) FROM DUAL;
OUTPUT:
SYSDATE NEXT_Day(
------------- ---------------
03-OCT-20 09-oct-20
LAST_DAY(d)
This function returns the date of the last day
of the month specified. The result will be a date.
Example:
SELECT SYSDATE, LAST_DAY(SYSDATE) FROM
DUAL;
OUTPUT:
SYSDATE LAST_DAY(
------------ ---- -------------------
03-SEP-13 30-SEP-13
Thanks