關於oracle內建函式的使用

lixiang114發表於2011-09-05

一些基本的oracle內建函式使用方法,時間長了好多都忘記了,予以記錄:

轉自:http://blog.blueshop.com.tw/pili9141/articles/52418.aspx

1字串常用函式
21. 字串的開始位置是1
32. 函式中最後一個字是'B'的,表示該函式,有能力處理單位元資料與處理雙位元資料
4
5A.字串函式傳回字串值
6
7 CHR(n)
8 --可將ASCII CODE 轉成database character set
9
10 ex.
11 select CHR(67)||CHR(65) AA from dual;
12
13 AA
14 ----------
15 CA
16
17 select ASCII('屋') from dual;
18
19 ASCII('屋')
20 ----------
21 43982
22
23 select CHR(43982) from dual;
24
25 CHR(43982)
26 ----------
27
28
29 CONCAT(char1,char2)
30 --◎ 可連結char1及char2兩個字串
31 --◎ char1、char2可以是資料欄名稱
32 --◎ 與「||」之字串連結符號作用相同 concat('AA','BB') 可改成 'AA'||'BB'
33
34 ex.
35 select concat('AA','BB') AA from dual;
36
37 AA
38 ----------
39 AABB
40
41 INITCAP(chars)
42 --◎ 將每一個字的字首轉成大寫
43 --◎ 隻影響英文,不影響中文
44
45 ex.
46 select INITCAP('aa') AA from dual;
47
48 AA
49 ----------
50 Aa
51
52 LOWER(chars)
53 --◎ 將大寫的字串轉成小寫
54 --◎ 隻影響英文,不影響中文
55
56 ex.
57 select LOWER('AAAA') AA from dual;
58
59 AA
60 ----------
61 aaaa
62
63 UPPER(chars)
64 --◎ 將小寫的字串轉成大寫
65 --◎ 隻影響英文,不影響中文
66
67 ex.
68 select UPPER('aaaa') AA from dual;
69
70 AA
71 ----------
72 AAAA
73
74 LPAD(char1,n[,char2])
75 --◎ 將字串char1往右靠,不足n個字元則補字串char2
76 --◎ char2預設值為一個空白
77
78 ex.
79 select LPAD('aa',5,'*') AA from dual;
80
81 AA
82 ----------
83 ***aa
84
85 RPAD(char1,n[,char2])
86 --◎ 將字串char1往左靠,不足n個字元則補字串char2
87 --◎ char2預設值為一個空白
88
89 ex.
90 select RPAD('aa',5,'*') AA from dual;
91
92 AA
93 ----------
94 aa***
95
96 LTRIM(char1[,set])
97 --◎ 將字串char1最左邊開始去除set包含的字元
98 --◎ set預設值為一個空白
99 --◎ 對數字無效
100
101 ex.
102 select LTRIM('aaBBBBaa','a') AA from dual;
103
104 AA
105 ----------
106 BBBBaa
107
108 RTRIM(char1[,set])
109 --◎ 將字串char1最右邊開始去除set包含的字元
110 --◎ set預設值為一個空白
111 --◎ 對數字無效
112
113 ex.
114 select RTRIM('aaBBBBaa','a') AA from dual;
115
116 AA
117 ----------
118 aaBBBB
119
120 REPLACE(char,search_string[,replacement_string])
121 --◎ 在char字串中找尋search_string,找到後將其轉換成replacement_string
122 --◎ 若省略replacement_string,則將removed search_string
123
124 ex.
125 select REPLACE('aaBBBBaa','a','c') AA from dual;
126
127 AA
128 ----------
129 ccBBBBcc
130
131 select REPLACE('aaBBBBaa','a') AA from dual;
132
133 AA
134 ----------
135 BBBB
136
137 TRANSLATE(char,from,to)
138 --◎ 將字串char由from改為to
139
140 ex.
141 select TRANSLATE(12345.12,'12345','ABCDE') AA from dual;
142
143 AA
144 ----------
145 ABCDE.AB
146
147 select TRANSLATE('abaaa','abcde','ABCDE') AA from dual;
148
149 AA
150 ----------
151 ABAAA
152
153 SUBSTR(char,m[,n])
154 --◎ 將字串char由第m個字元開始擷取n個字元
155 --◎ 若m設成0,仍由第一個字元開始擷取;若m設成負數,則會由字串的最後開始找起
156 --◎ 若省略n,則會傳回m後的所有字元;若n為負數,則傳回null值
157
158 ex.
159 select SUBSTR('abcdef',-3,2) AA from dual;
160
161 AA
162 ----------
163 de
164
165 select SUBSTR('abcdef',3,-2) AA from dual;
166
167 AA
168 ----------
169 -- 傳回null
170
171 SUBSTRB(char,m[,n])
172 -- 用法和substr相同,但substrb可處理multi-byte的資料
173 -- 若資料庫只能處理single-byte的資料,則substr和substrb執行的結果會相同
174
175 ex.
176 select SUBSTRB('測試',3,2) AA from dual;
177
178 AA
179 ----------
180
181
182
183B.字串函式傳回數字值
184
185 INSTR(char1,char2[,n[,m]])
186 --◎ 在char1第n個字元開始尋找,第m次遇到字串char2的位置
187 --◎ n、m預設值為1,可省略
188 --◎ 若要尋找中文可使用INSTRB
189
190 ex.
191 select INSTR('aaBBBBaa','B') AA from dual;
192
193 AA
194 ----------
195 3
196
197 select INSTR('臺北 aaa','北') AA from dual;
198
199 AA
200 ----------
201 2
202
203 INSTRB(char1,char2[,n[,m]])
204 --◎ 在char1第n個字元開始尋找,第m次遇到字串char2的位置
205 --◎ n、m預設值為1,可省略
206 --◎ 用法和INSTR相同,可處理multi-byte的資料
207
208 ex.
209 select INSTRB('中秋秋月圓','秋',1,2) AA from dual;
210
211 AA
212 ----------
213 5
214
215 LENGTH(char)
216 --◎ 可計算出字串char的長度
217
218 LENGTHB(char)
219 --◎ 可計算出字串char的長度
220 --◎ 欄位宣告為char的資料型態,且欄位資料包含中文時使用
[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7417660/viewspace-1054760/,如需轉載,請註明出處,否則將追究法律責任。

相關文章