You are on page 1of 34

Function of SQL

Character Function
LOWER (col|value)
Select LOWER(dname), LOWER(SQL COURSE) From DEPT
LOWER (dname) research sales operations accounting LOWER(SQL COURSE) sql course sql course sql course sql course

Character Functioncont.
UPPER(col|value)
Select ename From EMP Where ename = UPPER(smith)
ename SMITH

Character Functioncont.
INITCAP(col|value)
Forces first letter of each word of the string to be capitalised.

Select INITCAP(dname), INITCAP(loc) From DEPT


INITCAP(dname)
Accounting Research Sales Operations

INITCAP(loc)
New York Dallas Chicago Boston

Character Functioncont.
CONCAT(char1,char2)
Returns char1 concatenated with char2.

Select CONCAT(ename,job) job From EMP Where e# = 7902


job Jamesclerk

Character Functioncont.
LPAD(col|value,n,string)
pads the column or literal value from the left, to a total width of n character positions. The leading spaces are filled with string. If string is omitted value is padded with spaces.
Select LPAD(dname,15,*), LPAD(dname, 15), LPAD(d#, 10,.) From DEPT LPAD(dname,15,*) *******Research **********Sales *****Operations *****Accounting LPAD(dname,15) Research Sales Operations Accounting LPAD(d#,10,.) ..20 ..30 ..40 ..10

Character Functioncont.
RPAD(col|value,n,string)
pads the column or literal value to the right, to a total width of n character positions. The trailing spaces are filled with string. If string is omitted value is padded with blanks.
Select RPAD(dname,15,*), RPAD(dname, 15), RPAD(d#, 10,.) From DEPT RPAD(dname,15,*) Research ******* Sales ********** Operations ****** Accounting ****** RPAD(dname,15) Research Sales Operations Accounting RPAD(d#,10,.) 20.. 30.. 40.. 10..

Character Functioncont.
SUBSTR(col|value,pos,n)
Returns a string of n characters long from the column or literal value, starting at the position number pos. If n is omitted string is extracted from pos to end.
Select SUBSTR(Oracle,2,4), SUBSTR(dname,2), SUBSTR(dname,3,5) From DEPT SUBSTR(Oracle,2,4) racl racl racl racl SUBSTR(dname,2) esearch ales perations ccounting SUBSTR(dname,3,5) searc les erati count

Character Functioncont.
INSTR(col|value,string,pos,n)
finds the character position of nth occurrence of string in column or literal value starting at the position number pos.
Select dname, INSTR(dname,A), INSTR(dname,es),INSTR(dname,c,1,2) From DEPT Dname INSTR(dname,A) INSTR(dname,ES) INSTR(dname,c,1,2) ACCOUNTING 1 0 3 RESEARCH 5 2 0 SALES 2 4 0 OPERATIONS 5 0 0

Character Functioncont.
LTRIM(col|value,char/s)
Removes from the left leading occurrences of char (or combination of leading chars) specified. If char is not specified will trim any blanks from the left.
Select dname, LTRIM(dname,A), LTRIM(dname,AS), LTRIM(dname,ASOP) From DEPT Dname LTRIM(dname,A) LTRIM(dname,AS) LTRIM(dname,ASOP) RESEARCH RESEARCH RESEARCH RESEARCH SALES SALES LES LES OPERATIONS OPERATIONS OPERATIONS ERATIONS ACCOUNTING CCOUNTING CCOUNTING CCOUNTING

Character Functioncont.
RTRIM(col|value,char/s)
Removes from the right trailing occurrences of char (or combination of trailing chars) specified. If not char/s is specified , removes blanks .
Select dname, RTRIM(dname,G), RTRIM(dname,GHS), RTRIM(dname,N) From DEPT Dname RTRIM(dname,G) RTRIM(dname,GHS) RTRIM(dname,N) RESEARCH RESEARCH RESEARC RESEARCH SALES SALES SALE SALES OPERATIONS OPERATIONS OPERATION OPERATIONS ACCOUNTING ACCOUNTIN ACCOUNTIN ACCOUNTING

Character Functioncont.
SOUNDEX(col|value)
Returns a character string representing the sound of words for each column or literal value. This function returns a phonetic representation of each word and allows you to compare words that are spelt differently, but sound alike. Select ename, SOUNDEX(ename) From EMP Where SOUNDEX(ename)=SOUNDEX(FRED)
ename FORD SOUNDEX(ename) F630

Character Functioncont.
LENGTH(col|value)
Retuens the number of characters (or digits) in the column or literal value.
Select LENGTH(SQL COURSE), LENGTH(d#), LENGTH(dname) From DEPT LENGTH(SQL COURSE) LENGTH(d#) 10 2 10 2 10 2 10 2 LENGTH(dname) 8 5 10 10

Character Functioncont.
TRANSLATE(col|value, from, to)
Translates for output the character from to the character to. More than one character can be matched. All occurrences of from are replaced with the corresponding character in to. If a corresponding to character is not supplied, the from char is removed. Select ename, TRANSLATE(ename,C,P), job, TRANSLATE(job,AR,IT) From EMP Where d#=10 ename TRANSLATE(ename, C, P) job TRANLATE(job, AR, IT) CLARK PLARK MANAGER MINIGET KING KING PRESIDENT PTESIDENT MILIER MILIER CLERK CLETK

Character Functioncont.
REPLACE(col|value, string, replacement_string)
Returns col/value with every occurrence of string replaced with replacement_string. If replacement_string is omitted, all occurrences of search string are removed. If both string and replacement_string are not supplied, an error occurs.
Select job, REPLACE(job, SALESMAN, SALESPERSON) AS job1, ename REPLACE(ename,CO,PX) AS ename1 From EMP job job1 ANALYST ANALYST SALESMAN SALESPERSON SALESMAN SALESPERSON MANAGER MANAGER ename SCOTT TURNER ALLEN CLARK ename1 SPXTT TURNER ALLEN CLARK

Character Functioncont.
ex.
Select dname, LENGTH(dname),LENGTH(dname)LENGTH(TRANSLATE(dname,AS,A)) AS

namel From DEPT dname LENGTH(dname) RESEARCH 8 SALES 5 OPERATIONS 10 ACCOUNTING 10 namel 1 2 1 0

Number Function
ROUND(col|n) TRUNC(col|value,n) CEIL(col|value) FLOOR(col|value) POWER(col|value,n) EXP(n) SQRT(col|value) SIGN(col|value)

Number Function-Cont.
ABS(col|value) MOD(value1,value2) LOG(m,n) SIN(n) TAN(n) COS(n)

Date Function
Date storage
Oracle stores dates in an internal numeric format, representing:
Century Year Month Day Hours Minutes Seconds

Date Function Cont.


Sysdate
SYSDATE is a pseudo-column that returns the current date and time.

e.g.
Select SYSDATE From SYS.dual Note: Dual is a dummy table.

Date Function Cont.


operators that used on date type data
date + number
date number date date date +number/24 e.g. Select hiredate, hiredate+7, hiredate-7 From EMP Where hiredate Like %JUN% hiredate 13-JUN-93 11-JUN-94 04-JUN-94 25-JUN-95 hiredate+7 20-JUN-93 18-JUN-94 11-JUN-94 02-JUL-95 hiredate-7 06-JUN-93 04-JUN-94 28-MAY-94 18-JUN-95

Date Function Cont.


MONTHS_BETWEEN(date1,date2)
Select MONTHS_BETWEEN (SYSDATE,hiredate), MONTHS_BETWEEN(01-JAN-94,05-NOV98) From EMP
MONTHS-BETWEEN (SYSDATE,hiredate) 98) 65.0873622 60.5067171 MONTHS-BETWEEN(01-JAN-94,05-NOV-58.129032 -58.129032

Date Function Cont.


ADD_MONTHS(date,n)
Select hiredate, ADD_MONTHS(hiredate,3) newdate1, ADD_MONTHS(hiredate,-3) newdate2 From EMP Where d#=10 hiredate 14-MAY-94 31-OCT-93 04-JUN-94 newdate1 14-AUG-94 31-JAN-94 04-SEP-94 newdate2 14-FEB-94 31-JUL-93 04-MAR-94

Date Function Cont.


NEXT_DAY(date1,char)
Select hiredate, NEXT_DAY(hiredate,FRIDAY) newday1,NEXT_DAY(hiredate,6) newday2 From EMP hiredate 14-MAY-94 09-JUL-94 newday1 18-MAY-94 13-JUL-94 newday2 18-MAY-94 13-JUL-94

Date Function Cont.


LAST_DAY(date1)
Find the date of the last day of the month that contains date1. Select hiredate, LAST_DAY(hiredate), LAST_DAY(15-MAY98) From EMP

hiredate LAST_DAY(hiredate) LAST_DAY(15-MAY-97) 04-DEC-94 31-DEC-94 31-MAY-97 02-APR-94 30-APR-94 31-MAY-97

Date Function Cont.


TRUNC(date1,char)
Find the date of first day of the month containing in date1 when char=MONTH. If char=YEAR, it finds first day of year containing date1. Select SYSTADE,TRUNC(SYSDATE,MONTH) newdate1, TRUNC(SYSDATE,YEAR) newdate2 From SYS.DUAL

SYSDATE 04-DEC-99

newdate1 01-DEC-99

newdate2 01-JAN-99

Note: TRUNC is useful if you want to remove the time portion of the day. The time component of the day is in fact removed by default.

Conversion Function
TO_CHAR(date,date picture)
Select TO_CHAR(SYSDATE,DAY, DDTH MONTH YYYY) From SYS.DUAL

TO_CHAR(SYSDATE,DAY, DDTH MONTH YYYY) FRIDAY , 21TH MARCH 2003


Select TO_CHAR(SYSDATE,fmDAY, DDTH MONTH YYYY) From SYS.DUAL

TO_CHAR(SYSDATE,fmDAY, DDTH MONTH YYYY) FRIDAY , 21TH MARCH 2003

Conversion Function-cont.
More example
Select TO_CHAR(SYSDATE,HH:MI:SS) From SYS.DUAL TO_CHAR(SYSDATE,HH:MI:SS) 08:16:24 Select TO_CHAR(SAL,$9,999) From EMP

TO_CHAR(SAL,$9,999) $1,000 $2,975 $1,250

Conversion Function
TO_NUMBER
Select ename, ename, job, sal From EMP Where sal > TO_NUMBER(1500)

TO_DATE
Select e#, ename, hiredate From EMP Where hiredate = TO_DATE(JUN 4, 1994, MONTH dd, YYYY) e# 7844 ename TUENER hiredate 04-JUN-94

Conversion Function Cont.


DECODE(col/<exp>, search1,result, [search2,result2,,]default)
Select ename,job, DECODE(job,CLERK,WORKER, MANAGER,BOSS,UNDEFINED) decodejob From EMP ename smith ALLEN jones job CLERK SALESMAN MANAGER decodejob WORKER UNFEFINED BOSS

Conversion Function Cont.


more example
Select status, DECODE(status,20,20%,30,30% 40,40%,10%) decodestatus From S status 20 30 50 40 decodestatus 20% 30% 10% 40%

Conversion Function Cont.


other functions
NVL(col|value, val) GREATEST(col|vale1, col|value2,) LEAST(col|value1, col|value2,) VSIZE(col|value)
returns the number of bytes in ORACLEs internal representation of col|value.

SQLPLUS COMMAND
ACCEPT command
ACC[EPT] variable [number|char] [prompt|noprompt text] [HIDE] SQL> ACCEPT SALARY NUMBER PROMPT Salary figure: Salary figure: 30000 SQL> ACC PASSARD char PROMPT Password: HIDE Password : SQL> ACC COMM NUMBER NOPROMPT 500 SQL> DEFIN DEFINE SALARY = 30000(NUMBER) DEFINE PASSWORD = FREEBIES(CHAR) DEFINE COMM = 500(NUMBER)

Class Exercise (1)


Find all employees name whose salary greater than some managers salary .

Find the names of all employees whose salary greater than all managers salary . Find all departments name which have at least five employees whose salary grater than $5000.

You might also like