The data in database are the basis of critical business information. Generating information from data often requires many data manipulations. Sometimes such data manipulation involves the decomposition of data elements.
SQL functions are very useful tools. There are many types of SQL functions, such as arithmetic, trigonometric, string, date, and time functions.
NOTE: DUAL is oracles pseudo table used only for case where a table is not really needed
· SYSDATE: Returns today’s date.
SQL> select sysdate
2 from dual;
SYSDATE
--------------
04-MAR-12
Syntax: ADD_MONTHS(DATE_VALUE,N)
SQL> select add_months(sysdate,4)
2 from dual;
ADD_MONTH
------------------
04-JUL-12
SQL> select add_months(sysdate,4)"addmonths"
2 from dual;
addmonths
-------------
04-JUL-12
SQL> select add_months(sysdate,4) as addmonths
2 from dual;
ADDMONTHS
-----------------
04-JUL-12
Syntax: LAST_DAY(DATE_VALUE)
SQL> select sysdate,last_day(sysdate)"lastday"
2 from dual;
SYSDATE lastday
-------------- ------------
04-MAR-12 31-MAR-12
NUMERIC FUNCTIONS:
Numeric functions can be grouped in many different ways, such as algebraic, trigonometric and logarithmic. The following are selected group of numeric functions.
· ABS: Returns the absolute value of a number
Syntax: ABS(NUMERIC_VALUE)
SQL> select 1.93,-1.93, abs (1.93), abs (-1.93)
2 from dual;
1.93 -1.93 ABS (1.93) ABS (-1.93)
----------- ----------- ------------- -------------
1.93 -1.93 1.93 1.93
Syntax: POWER(m,n)
Eg. Find 52
SQL> select power(5,2)
2 from dual;
POWER(5,2)
---------------
25
· ROUND: Returns n, rounded to m places to the right of a decimal point. If m is omitted , n is rounded to zero places
Syntax: ROUND(n,m)
SQL> select round(15.193,1)
2 from dual;
ROUND(15.193,1)
---------------------
15.2
SQL> select round(15.193)
2 from dual;
ROUND(15.193)
-------------------
15
Syntax: SQRT(n)
Eg. Find the square root of 625
SQL> select sqrt(625)
2 from dual;
SQRT(625)
--------------
25
· MOD: Returns the remainder of a first number divided by second number passed a parameter. If the second number is zero, the result is the same as the first number.
Syntax: MOD(m,n)
Eg. Find 15 mod 4
SQL> select mod(15,4)
2 from dual;
MOD(15,4)
-------------
3
· FLOOR: Returns the largest integer value that is equal to or less than a number.
Syntax: FLOOR(n)
Eg. Find floor(24.8)
SQL> select floor(24.8)
2 from dual;
FLOOR(24.8)
---------------
24
Syntax: CEIL(n)
Eg. Find ceil(24.8)
SQL> select ceil(24.8)
2 from dual;
CEIL(24.8)
-------------
25
STRING FUNCTIONS:
· CONCATENATION: (||) Concatenates data from two different character columns and returns a single column
· LOWER: Returns char , with all letters in lower case.
Syntax: LOWER(char)
SQL> select lower('KNREDDY') as lower
2 from dual;
LOWER
----------
knreddy
· INITCAP: returns a string with the first letter of each word in upper case
Syntax: INITCAP(char)
SQL> select initcap('nageswarareddy') as initcap
2 from dual;
INITCAP
--------------
Nageswarareddy
· UPPER: Returns a string with all letters forced to upper
Syntax: UPPER(char)
SQL> select upper('knreddy') as upper
2 from dual;
UPPER
-----------
KNREDDY
· SUBSTR: Returns a portion of characters, beginning at character m, and going upto characters n. If n is omitted, the result returned is upto the last character in the string. The first position of character is 1.
Syntax: SUBSTR(<string>,<startposition>,<length>)
Where string - source string
Start position – position for extraction
Length – number of characters to extract
SQL> select substr('databasemanagementsystems',5,4)
2 from dual;
SUBS
--------
base
SQL> select substr('databasemanagementsystems',5) as substr
2 from dual;
SUBSTR
------------------------------------
basemanagementsystems
· 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 of the characters after the first.
Syntax: ASCII(<single- character>)
SQL> select ascii('a')
2 from dual;
ASCII('A')
----------
97
SQL> select ascii('A')
2 from dual;
ASCII('A')
----------
65
SQL> select ascii('APPLE')
2 from dual;
ASCII('APPLE')
-----------------
65
· LENGTH: Returns the length of a word
Syntax: LENGTH(word)
SQL> select length('database')
2 from dual;
LENGTH('DATABASE')
-----------------------------
8
SQL> select length('data base')
2 from dual;
LENGTH('DATABASE')
----------------------------
9
· LTRIM: Removes characters from the left of character with initial characters removed upto the first character not in set.
Syntax: LTRIM(char,set)
SQL> select ltrim('dbms','d')
2 from dual;
LTR
-----
bms
SQL> select ltrim('ddddbms','d')
2 from dual;
LTR
------
bms
SQL> select ltrim('dbms','d''b')
2 from dual;
LT
---
ms
· RTRIM: Returns char, eith final characters removed after the last character not in the set.
Syntax: RTRIM(char,set)
SQL> select rtrim('dbms','s')
2 from dual;
RTR
-----
dbm
SQL> select rtrim('dbms','s''m')
2 from dual;
RT
---
Db
· TRIM: Removes all specified characters either from the beginning or the ending of a string
Syntax: TRIM([leading|trailing|both{<trim-characters> from]] <string>)
Where leading -remove trim string from the front of the string
Trailing -remove trim string from end of string
Both -remove trim string from the front and end of string
If none of the above option is chosen, the TRIM function will remove trim string from both the front and end of string.
Trim_character is the character that will be removed from string. If this parameter is omitted, the trim function will remove all leading and trailing spaces from string
String - string to trim
SQL> select trim(' dbms ')
2 from dual;
TRIM
-------
dbms
SQL> select trim(leading 'x' from 'xxxxxdbmsxxxxx') as leading
2 from dual;
LEADING
---------------
dbmsxxxxx
SQL> select trim(trailing 'x' from 'xxxxxdbmsxxxxx') as trailing
2 from dual;
TRAILING
--------------
xxxxxdbms
SQL> select trim(both 'x' from 'xxxxxdbmsxxxxx')
2 from dual;
TRIM
-------
dbms
SQL> select trim(both '1' from '1123dbms2311') as both
2 from dual;
BOTH
------------
23dbms23
· LPAD: Return char-1 left padded to length n with the sequence of characters specified in char-2. If char-2 is not specified oracle uses blanks by default
Syntax: LPAD(char-1,n,char-2)
SQL> select lpad('dbms',15,'@') as lpad
2 from dual;
LPAD
----------------------------------
@@@@@@@@@@@dbms
· RPAD: Return char-1 right padded to length n with the sequence of characters specified in char-2. If char-2 is not specified oracle uses blanks by default
Syntax: LPAD(char-1,n,char-2)
SQL> select rpad('dbms',15,'@') as rpad
2 from dual;
RPAD
-----------------------------------
dbms@@@@@@@@@@@