Beruflich Dokumente
Kultur Dokumente
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.
INITCAP(loc)
New York Dallas Chicago Boston
Character Functioncont.
CONCAT(char1,char2)
Returns char1 concatenated with char2.
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
e.g.
Select SYSDATE From SYS.dual Note: Dual is a dummy table.
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
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
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
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)
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.