SQL- Operators and
Functions
HKPATEL
HKPATEL 2
DUAL & SYSDATE
• DUAL
– It is a table having
one row & One
Column
– Oracle created by
itself
– Select * from Dual;
– Select 5*5 from Dual;
DBMS-3330703-2017
D
X
5*5
25
• SYSDATE
– It contain the current
date and time
– Select sysdate from
Dual; SYSDATE
20-jun-17
HKPATEL 3
Operators
• Arithmetic
– +, -, *, /,()
• Relational
– <, >, <=, >=, +, !== or <>,
• Logical
– AND, OR, NOT
• Range searching
• Set searching
• Character
DBMS-3330703-2017
HKPATEL 4
Range Searching Operator-
BETWEEN
• Syntax
– Column_name BETWEEN lower limit and upper limit;
• Ex.
– Select* from EMP where sal BETWEEN 500 and 1500;
• Add NOT in this Operator
– Select* from EMP where sal NOT BETWEEN 500 and
1500;
DBMS-3330703-2017
HKPATEL 5
Set Searching Operator - IN
• Syntax
– Column_Name IN (val1, val2,…,valN);
• EX.
– Select * from emp where city IN(‘vvn’,
‘and’, ‘ahm’);
• Add NOT in this Operator
• Select * from emp where city NOT
IN(‘vvn’, ‘and’, ‘ahm’);
DBMS-3330703-2017
HKPATEL 6
Character Operators – LIKE
• LIKE
– Syntax
• Col_name like pattern
– Pattern can be formed by using 2 wild card character
– % (Modulo) allow matching with any string having no. of
char.
– _ (Under score) allow matching with single charcater.
– EX.
– Display emp having city name starting with A
• Select * from emp where city like ‘A%’
– Display emp whose name have second character a
• Select * from emp where city like ‘_a%’
• _
DBMS-3330703-2017
HKPATEL 7
Character Operators – ||
• Concatenates (combine two string)
• Syntax
– String1 || string2
• Ex.
– Select ename||city from emp;
DBMS-3330703-2017
HKPATEL 8
SQL Functions
• Aggregate Functions
• Scalar Functions
– Numeric
– Character
– Date
– Conversion
– Miscalleous
DBMS-3330703-2017
HKPATEL 9
Aggregate Functions
• Max (colname)
• Min (colName)
• Sum([Distinct|all] colName)
• Avg([Distinct|all] colName)
• Count (*)
• Count([Distinct|all] colName)
DBMS-3330703-2017
HKPATEL 10
Numeric Functions
• ABS – Abs(n)- convert n to +ve
• SQRT – sqrt(n) - returns square root of n
• POWER- power(m,n)- returns m raised to n
• MOD- mod(m,n)- Remainders of m divide by n
• CEIL-ceil(n)- return largest int value
• FLOOR- floor(n)- return Smallest int value
• EXP-exp(n)- e raised to n
• LN-ln(n)- log n to the base e.
• LOG- log(b,n)- log n to the base b
• COS, SIN & TAN- cos(n), sin(n) & Tan(n)
• COSH, SINH & TANH- cosh(n), sinh(n) & Tanh(n)
• SIGN- sign(n)-return sign of n
• ROUND
• TRUNC
DBMS-3330703-2017
HKPATEL 11
• Round
– Round(m,n)
• Return m, rounded to n places
• If n is omited, m is rounded to zero places
• If n is negetive, m is rounded to n places to the left
• If n is positive, m is rounded to n places to the left
DBMS-3330703-2017
Select round(157.732,2), round(157.732,-2), round(157.732) from dual;
157.73 200 158
• Trunc
– trunc(m,n)
• Return m, rounded to n places
• If n is omited, m is truncated to zero places
• If n is negetive, m is rounded to n places to the left
• If n is positive, m is truncated to n places to the left
Select trunc(157.732,2), trunc(157.732,-2), trunc(157.732) from dual;
157.73 100 157
HKPATEL 12
Character functions
• Length(str)
• Lower(str)
• Upper(str)
• Initcap(str)
• Ascii(char)
• Lpad(str,n,str2)
– Rerurn str, left padded with str2 up to length n
– lpad(‘ind’,5,’*’)- **ind
• Rpad (str,n,str2)
– Rerurn str, right padded with str2 up to length
n
– - rpad(‘ind’,5,’*’)- ind**
DBMS-3330703-2017
HKPATEL 13
Character functions
• Ltrim (str,set)
– Removes the character from the left to str
– ltrim(‘abhkp’, ‘baaks’) - hkp
• Rtrim (str,set)
– Removes the character from the right to str
– rtrim(‘abhkp’, ‘pkhs’) - ab
• Translate (str, from_set, to_set)
• Replace (str, from_set, to_set)
• Substr (str,pos,length)
DBMS-3330703-2017
HKPATEL 14
Translate V/S Replace
• Translate(str, from_set, to_set)
– Character of str that occur in from_set are
translated to the corresponding char in to_set.
– Translate(‘abc12efg3’,’1234’,xyzw)
– abcxyefgz
• Replace (str, from_set, to_set)
– Replace to similar translate but it works on
string rathar than character
– Replace (‘abc12efg3’,’1234’,xyzw)
– Abcxyzefg
DBMS-3330703-2017
HKPATEL 15
SUBSTRING
• Substr(str,pos,length)
• Returns a portion of str, beginning at pos and
going up to length charcters
• SUBSTR(‘KARM HARDIK PATEL’,6,4)
– HARD
DBMS-3330703-2017
HKPATEL 16
Conversion functions
• To_NUMBER( str)
– convert value of character data type ,expressing a
number to number data type
• TO_CHAR(n, format)
– convert numeric value n to a character data type
using optional format.
• TO_CHAR( date, format)
– converting date value to character value using format.
• TO_DATE( str, format)
– converts character value to date value
DBMS-3330703-2017
HKPATEL 17
Date Functions
• Add_months (date1, n)
– Return new date after adding n months in date1
• Months_between(date1, date2)
– Return number of months between date1 and date2.
– Date1-Date2
• Last_Day (date1)
– Returns the last date of the month specified by date1
• Next_Day(day, date1)
– Return the next named week-day specified by day to relative
date1.
DBMS-3330703-2017
HKPATEL 18
Continue
• Round (date, format)
– Returns rounded date according to format
– Select(TO_DATE(‘31-DEC-18 03:30:45PM’,’DD_MON_YY
HH:MM:SS PM’))from dual;
– O/P: 01-JAN-19
• Trunc (date, format)
– Returns truncated date according to format
– Select(TO_DATE(‘31-DEC-18 03:30:45PM’,’DD_MON_YY
HH:MM:SS PM’))from dual;
– O/P: 31-DEC-18
• New_Time(date,Zone1,Zone2)
– Returns the date after converting it from time zone 1 to time zone
2
DBMS-3330703-2017
HKPATEL 19
Miscellaneous Function
• UID
– Returns the uid of the current user
• USER
– Returns the user name
• GREATEST(exp1,exp2,exp3)
– Returns the greatest exp
• LEAST(exp1,exp2,exp3)
– Returns the least exp
• DECODE(Value,if1,then1,if2,then2,…,else)
– It similar if-then-else
• NVL(exp1,exp2)
– Returns exp2, if exp1 is null--Returns exp1, if it is not null
• VSIZE(exp)
– Retuns the storage size of exp in oracle
DBMS-3330703-2017
HKPATEL 20
Thank You
DBMS-3330703-2017

DBMS Chapter-SQL operation presentation4.ppsx

  • 1.
  • 2.
    HKPATEL 2 DUAL &SYSDATE • DUAL – It is a table having one row & One Column – Oracle created by itself – Select * from Dual; – Select 5*5 from Dual; DBMS-3330703-2017 D X 5*5 25 • SYSDATE – It contain the current date and time – Select sysdate from Dual; SYSDATE 20-jun-17
  • 3.
    HKPATEL 3 Operators • Arithmetic –+, -, *, /,() • Relational – <, >, <=, >=, +, !== or <>, • Logical – AND, OR, NOT • Range searching • Set searching • Character DBMS-3330703-2017
  • 4.
    HKPATEL 4 Range SearchingOperator- BETWEEN • Syntax – Column_name BETWEEN lower limit and upper limit; • Ex. – Select* from EMP where sal BETWEEN 500 and 1500; • Add NOT in this Operator – Select* from EMP where sal NOT BETWEEN 500 and 1500; DBMS-3330703-2017
  • 5.
    HKPATEL 5 Set SearchingOperator - IN • Syntax – Column_Name IN (val1, val2,…,valN); • EX. – Select * from emp where city IN(‘vvn’, ‘and’, ‘ahm’); • Add NOT in this Operator • Select * from emp where city NOT IN(‘vvn’, ‘and’, ‘ahm’); DBMS-3330703-2017
  • 6.
    HKPATEL 6 Character Operators– LIKE • LIKE – Syntax • Col_name like pattern – Pattern can be formed by using 2 wild card character – % (Modulo) allow matching with any string having no. of char. – _ (Under score) allow matching with single charcater. – EX. – Display emp having city name starting with A • Select * from emp where city like ‘A%’ – Display emp whose name have second character a • Select * from emp where city like ‘_a%’ • _ DBMS-3330703-2017
  • 7.
    HKPATEL 7 Character Operators– || • Concatenates (combine two string) • Syntax – String1 || string2 • Ex. – Select ename||city from emp; DBMS-3330703-2017
  • 8.
    HKPATEL 8 SQL Functions •Aggregate Functions • Scalar Functions – Numeric – Character – Date – Conversion – Miscalleous DBMS-3330703-2017
  • 9.
    HKPATEL 9 Aggregate Functions •Max (colname) • Min (colName) • Sum([Distinct|all] colName) • Avg([Distinct|all] colName) • Count (*) • Count([Distinct|all] colName) DBMS-3330703-2017
  • 10.
    HKPATEL 10 Numeric Functions •ABS – Abs(n)- convert n to +ve • SQRT – sqrt(n) - returns square root of n • POWER- power(m,n)- returns m raised to n • MOD- mod(m,n)- Remainders of m divide by n • CEIL-ceil(n)- return largest int value • FLOOR- floor(n)- return Smallest int value • EXP-exp(n)- e raised to n • LN-ln(n)- log n to the base e. • LOG- log(b,n)- log n to the base b • COS, SIN & TAN- cos(n), sin(n) & Tan(n) • COSH, SINH & TANH- cosh(n), sinh(n) & Tanh(n) • SIGN- sign(n)-return sign of n • ROUND • TRUNC DBMS-3330703-2017
  • 11.
    HKPATEL 11 • Round –Round(m,n) • Return m, rounded to n places • If n is omited, m is rounded to zero places • If n is negetive, m is rounded to n places to the left • If n is positive, m is rounded to n places to the left DBMS-3330703-2017 Select round(157.732,2), round(157.732,-2), round(157.732) from dual; 157.73 200 158 • Trunc – trunc(m,n) • Return m, rounded to n places • If n is omited, m is truncated to zero places • If n is negetive, m is rounded to n places to the left • If n is positive, m is truncated to n places to the left Select trunc(157.732,2), trunc(157.732,-2), trunc(157.732) from dual; 157.73 100 157
  • 12.
    HKPATEL 12 Character functions •Length(str) • Lower(str) • Upper(str) • Initcap(str) • Ascii(char) • Lpad(str,n,str2) – Rerurn str, left padded with str2 up to length n – lpad(‘ind’,5,’*’)- **ind • Rpad (str,n,str2) – Rerurn str, right padded with str2 up to length n – - rpad(‘ind’,5,’*’)- ind** DBMS-3330703-2017
  • 13.
    HKPATEL 13 Character functions •Ltrim (str,set) – Removes the character from the left to str – ltrim(‘abhkp’, ‘baaks’) - hkp • Rtrim (str,set) – Removes the character from the right to str – rtrim(‘abhkp’, ‘pkhs’) - ab • Translate (str, from_set, to_set) • Replace (str, from_set, to_set) • Substr (str,pos,length) DBMS-3330703-2017
  • 14.
    HKPATEL 14 Translate V/SReplace • Translate(str, from_set, to_set) – Character of str that occur in from_set are translated to the corresponding char in to_set. – Translate(‘abc12efg3’,’1234’,xyzw) – abcxyefgz • Replace (str, from_set, to_set) – Replace to similar translate but it works on string rathar than character – Replace (‘abc12efg3’,’1234’,xyzw) – Abcxyzefg DBMS-3330703-2017
  • 15.
    HKPATEL 15 SUBSTRING • Substr(str,pos,length) •Returns a portion of str, beginning at pos and going up to length charcters • SUBSTR(‘KARM HARDIK PATEL’,6,4) – HARD DBMS-3330703-2017
  • 16.
    HKPATEL 16 Conversion functions •To_NUMBER( str) – convert value of character data type ,expressing a number to number data type • TO_CHAR(n, format) – convert numeric value n to a character data type using optional format. • TO_CHAR( date, format) – converting date value to character value using format. • TO_DATE( str, format) – converts character value to date value DBMS-3330703-2017
  • 17.
    HKPATEL 17 Date Functions •Add_months (date1, n) – Return new date after adding n months in date1 • Months_between(date1, date2) – Return number of months between date1 and date2. – Date1-Date2 • Last_Day (date1) – Returns the last date of the month specified by date1 • Next_Day(day, date1) – Return the next named week-day specified by day to relative date1. DBMS-3330703-2017
  • 18.
    HKPATEL 18 Continue • Round(date, format) – Returns rounded date according to format – Select(TO_DATE(‘31-DEC-18 03:30:45PM’,’DD_MON_YY HH:MM:SS PM’))from dual; – O/P: 01-JAN-19 • Trunc (date, format) – Returns truncated date according to format – Select(TO_DATE(‘31-DEC-18 03:30:45PM’,’DD_MON_YY HH:MM:SS PM’))from dual; – O/P: 31-DEC-18 • New_Time(date,Zone1,Zone2) – Returns the date after converting it from time zone 1 to time zone 2 DBMS-3330703-2017
  • 19.
    HKPATEL 19 Miscellaneous Function •UID – Returns the uid of the current user • USER – Returns the user name • GREATEST(exp1,exp2,exp3) – Returns the greatest exp • LEAST(exp1,exp2,exp3) – Returns the least exp • DECODE(Value,if1,then1,if2,then2,…,else) – It similar if-then-else • NVL(exp1,exp2) – Returns exp2, if exp1 is null--Returns exp1, if it is not null • VSIZE(exp) – Retuns the storage size of exp in oracle DBMS-3330703-2017
  • 20.