function of sql - ustcstaff.ustc.edu.cn/~llyue/function.pdf · 2017-03-06 · character...
TRANSCRIPT
Function of SQL
1
content
• Character function
• Number Function
• Date Function
• Conversion Function
2
Character Function
• LOWER (col|value)
Select LOWER(dname), LOWER(‘SQL
COURSE’)
From DEPT
LOWER (dname) LOWER(‘SQL COURSE’)
research sql course
sales sql course
operations sql course
accounting sql course
3
Character Function–cont.
• UPPER(col|value)
Select ename From EMP
Where ename = UPPER(‘smith’)
ename
SMITH
4
Character Function–cont.
• INITCAP(col|value)Forces first letter of each word of the string to be capitalised.
Select INITCAP(dname), INITCAP(loc)
From DEPT
INITCAP(dname) INITCAP(loc)
Accounting New York
Research Dallas
Sales Chicago
Operations Boston
5
Character Function–cont.
• CONCAT(char1,char2)Returns char1 concatenated with char2.
Select CONCAT(ename,job) job
From EMP Where e# = ‘7902’
job
Jamesclerk
6
Character Function–cont.
• 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,’*’) LPAD(dname,15) LPAD(d#,10,’.’)
*******Research Research ……..20
**********Sales Sales ……..30
*****Operations Operations ……..40
*****Accounting Accounting ……..10
7
Character Function–cont.
• 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,’*’) RPAD(dname,15) RPAD(d#,10,’.’)
Research ******* Research 20……..
Sales ********** Sales 30……..
Operations ****** Operations 40……..
Accounting ****** Accounting 10……..
8
Character Function–cont.
• 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) SUBSTR(dname,2) SUBSTR(dname,3,5)
racl esearch searc
racl ales les
racl perations erati
racl ccounting count
9
Character Function–cont.
• 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
10
Character Function–cont.
• 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 11
Character Function–cont.
• 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
12
Character Function–cont.
• 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 SOUNDEX(ename)
FORD F63013
Character Function–cont.
• 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#) LENGTH(dname)
10 2 8
10 2 5
10 2 10
10 2 10
14
Character Function–cont.
• 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
15
Character Function–cont.
• 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 ename ename1
ANALYST ANALYST SCOTT SPXTT
SALESMAN SALESPERSON TURNER TURNER
SALESMAN SALESPERSON ALLEN ALLEN
MANAGER MANAGER CLARK CLARK16
Character Function–cont.
• ex.
Select dname, LENGTH(dname),LENGTH(dname)-
LENGTH(TRANSLATE(dname,’AS’,’A’)) AS
namel
From DEPT
dname LENGTH(dname) namel
RESEARCH 8 1
SALES 5 2
OPERATIONS 10 1
ACCOUNTING 10 017
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)
18
Number Function-Cont.
• ABS(col|value)
• MOD(value1,value2)
• LOG(m,n)
• SIN(n)
• TAN(n)
• COS(n)
19
Date Function
• Date storage
Oracle stores dates in an internal numeric format, representing:– Century
– Year
– Month
– Day
– Hours
– Minutes
– Seconds
20
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.
21
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 hiredate+7 hiredate-7
13-JUN-03 20-JUN-03 06-JUN-03
11-JUN-04 18-JUN-04 04-JUN-04
04-JUN-04 11-JUN-04 28-MAY-04
25-JUN-05 02-JUL-05 18-JUN-05 22
Date Function – Cont.
• MONTHS_BETWEEN(date1,date2)
Select MONTHS_BETWEEN (SYSDATE,hiredate),
MONTHS_BETWEEN(‘01-JAN-94’,’05-NOV-98’)
From EMP
MONTHS-BETWEEN (SYSDATE,hiredate) MONTHS-BETWEEN(‘01-JAN-94’,’05-NOV-98’)
65.0873622 -58.129032
60.5067171 -58.129032
23
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 newdate1 newdate2
14-MAY-04 14-AUG-04 14-FEB-04
31-OCT-03 31-JAN-04 31-JUL-03
04-JUN-04 04-SEP-04 04-MAR-04
24
Date Function – Cont.
• NEXT_DAY(date1,char)
Select hiredate, NEXT_DAY(hiredate,’FRIDAY’)
newday1,NEXT_DAY(hiredate,6) newday2
From EMP
hiredate newday1 newday2
14-MAY-04 19-MAY-04 19-MAY-04
09-JUL-04 14-JUL-04 14-JUL-04
25
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-MAY-
98)
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
26
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 newdate1 newdate2
04-DEC-09 01-DEC-09 01-JAN-09
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.
27
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 200328
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 29
Conversion Function
• TO_NUMBERSelect 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# ename hiredate
7844 TUENER 04-JUN-94
30
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 job decodejob
smith CLERK WORKER
ALLEN SALESMAN UNFEFINED
jones MANAGER BOSS
31
Conversion Function – Cont.
• more exampleSelect status,
DECODE(status,’20’,’20%’,’30’,’30%’
’40’,’40%’,’10%’) decodestatus
From S
status decodestatus
20 20%
30 30%
50 10%
40 40%
32
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 ORACLE’s internal representation of
col|value.
33
作业
• 选择自己生活、学习中的需求,创建有2-3
个关系的数据库。
• 在创建的数据库上给出两个查询访问要求,并用SQL语句实现(要求有join/group等)
34
• Next Class
Integrity
text book chapter8 (version 7) & chapter 9 (version 8)
35