Dưới đây mình liệt kê khá đầy đủ các hàm sử dụng trong hệ quản trị CSDL Oracle cùng với ví dụ.Hi vọng giúp ích cho những bạn quan tâm Lower(kiểu char) Select lower(description) from course Biến thành chữ thường Upper(kiểu char) Select upper(description) from course Biến thành chữ hoa Initcap(kiểu char) Select initcap(description) from course Chữ cái đầu tiên của mỗi từ sẽ viết hoa Lpad(record,n[,char]) Select lpad(cost,6,'*') from course Select lpad('trung',10,'*') from dual **1195 *****trung Rpad(record,n[,char]) Select rpad(cost,6,'*') from course Select rpad('trung',10,'*') from dual 1195** trung***** Ltrim(record or char, char) Select ltrim('0012300','0') from dual 12300 Bên trái sang những từ ‘0’ sẽ bỏ Rtrim(record or char, char) Select rtrim('0012300','0') from dual 00123 Bên phải sang những từ ‘0’ sẽ bỏ Trim([Leading or traling or both] char1 from char2) Select trim(leading '0' from '0012300') from dual 12300 Bên trái sang những từ ‘0’ sẽ bỏ Select trim(trailing '0' from '0012300') from dual 00123 Bên phải sang những từ ‘0’ sẽ bỏ Select trim(both '0' from '0012300') from dual 123 Những từ ‘0’ sẽ bỏ ở 2 bên Substr(record or char,n[,n]) Vị trí được bắt đầu đếm từ 1 Select substr('0012300',3) from dual 12300 Bên trái sang từ vị trí thứ ba(1) lấy hết Select substr('0012300',3,4) from dual 1230 Bên trái sang, Lấy từ vị trí thứ ba(1) bắt đầu đếm 4 kí tự tiếp theo Select substr('0012300',-3) from dual 300 Bên phải sang từ vị trí thứ ba(3), bắt đầu đếm sang phải lấy hết Select substr('0012300',-3,2) from dual 30 Bên phải sang từ vị trí thứ ba(3), bắt đầu đếm sang phải 2 kí tự Instr(record or char,char[,n1[,n2]]) (Đếm cả khỏang trắng) Select instr('hello world','l')from dual; 3 Chữ ‘L’ đầu tiên nằm tại vị trí thứ 3 Select instr('hello world','l',7)from dual 10 Đếm từ vị trí thứ 7(w) trở đi Select instr('hello world','l',1,2)from dual 4 Đếm từ vị trí thứ 1(h),lấy chữ ‘L’ xuất hiện lần thứ 2 Length(char) Đếm cả khỏang trắng Select length('hello world') from dual 11 Đếm chuỗi Concat(char1,char2) Select concat('1','2') from dual; 12 Nối chuỗi Select 'hello'||' '||'world' from dual Hello world Nối chuỗi Replace(record or char,tên cần thay,tên thay) Select replace('hello world','hello','Bye') from dual; Bye world Tìm hello thay = Bye Translate(record or char, if, then) select translate(11145,'12345','hello')from dual Số có thể để trong dấu nháy hoặc không hhhlo 1=h; 2=e; 3=l; 4=l; 5=o =>11145=hhhlo ABS(value) Select abs(-14) from dual 14 Trị tuyệt đối Sign(value) Select sign(-14) from dual -1 Số âm Select sign(14) from dual 1 Số dương Select sign(0) from dual 0 Mod(value,số chia) Select mod(20,7) from dual 6 Sysdate Select sysdate from dual Trả về ngày tháng hiện tại Sysdate +3/ Sysdate +3/24 Sysdate +0.5 select sysdate+3 from dual; select sysdate+3/24 from dual; select to_char(to_date('1-1-2008 00:00','mm-dd-yyyy hh24:mi')+0.5,'mm-dd-yyyy hh24:mi') from dual Không có to_char thì kiểu ngày xuất ra mặc định là dd-MON-yy +3: Cộng 3 ngày vào ngày hiện tại +3/24: Cộng 3h vào ngày hiện tại +0.5: Cộng nửa ngày vào ngày hiện tại Round(number) select round(123.456) from dual 123 Làm tròn xuống vì ‘.456’ < ’.500’ select round(123.511) from dual 124 Làm tròn lên vì ‘.511’ > ’.500’ select round(123.456,2) from dual 123.46 Sau dấu phẩy đếm 2 giá trị là 45 và làm tròn thành 46 vì 5>=5 select round(123.456,-2) from dual 100 Đếm ngc 2 giá trị trước dấu phẩy, 2<5 Round(date) Round(sysdate) select to_char(round(to_date('12-12-2008 9:30','dd-mm-yyyy hh24:mi')),'dd-mm-yyyy hh24:mi') from dual 9:30=>12-12-2008 00:00 13:30=>13-12-2008 00:00 Ngày: 0giờ-11giờ:59: làm tròn xuống Ngày: 12giờ-23giờ:59: làm tròn lên. Qua ngày tiếp theo Round(sysdate,MM) select to_char(round(to_date('12-12-2008','dd-mm-yyyy'),'MM'),'dd-mm-yyyy') from dual 12-12-2008=>1-12-2008 16-12-2008=>1-1-2009 Tháng: ngày 1-ngày 15 làm tròn xuống Tháng: ngày 16-ngày 31 làm tròn lên một tháng Trunc select trunc(123.456) from dual 123 Làm tròn số nhưng kô lấy số dư select trunc(123.556,2) from dual 123.55 select trunc(123.556,-2) from dual 100 Distinct select distinct cost from course Rút gọn những giá trị giống nhau To_char (number) select to_char(1925,'$9999') from dual $1925 1925: kiểu char $9999:format To_char (date) select to_char(sysdate, 'mon dd-mm-yyyy') from dual jan 06-01-2008 select to_char(to_date('12-11-2008','dd-mm-yyyy'),'mon dd-mm-yyyy') from dual nov 12-11-2008 To_date select to_date('12-2-2008','MM-DD-YYYY') from dual 02-DEC-08 To_number Select to_number(1995) from dual 1995(kiểu number) Giá trị nhập vào ban đầu là kiểu char NVL(record or char,giá trị sẽ thay vào) select nvl(cost,to_number(1000)) from course Tìm giá trị rỗng ở trong cost thay bằng giá trị 1000 Ở trong hàm nvl phải cùng kiểu Select nvl(to_char(prerequisite),'None') from course NVL2(record, not null,null) Select nvl2(cost,'exists','none') from course Tìm giá trị ở cost Nếu có thay = exists Nếu kô thay = none Coalesce (kết hợp à chọn 1) Select coalesce(midterm_grade,finalexam_grade, quiz_grade) from grade_summary Nếu mid có value thì sẽ lấy mid Nếu kô có thì sẽ thay = final Nếu kô có thì sẽ thay = quiz Select coalesce(to_char(cost),'None') from course Tìm giá trị rỗng ở Cost nếu kô có thay = none Nullif(char1, char2) select nullif(1,2) from dual 1 Char1=char2 trả về giá trị đầu tiên select nullif(1,1) from dual Null Char1 khác char2 trả về giá trị NULL Decode select decode(cost,1195,2008) from course; Tại Cost record. Tìm 1195 nếu có thay bằng 2008 Nếu ko có thay = null select decode(cost,1195,2008,2007) from course; Tìm 1195 nếu có thay bằng 2008 Nếu ko có thay = 2007 select next_day(sysdate,'sunday') from dual; chủ nhật tiếp theo là ngày thứ mấy vậy ta? select next_day(sysdate,1) from dual; 1:chủ nhật ………… 7: thứ bảy select last_day(sysdate) from dual Ngày cuối cùng của tháng hiện tại Select Trích thành phần ngày/ thành phần tháng/ thành phần năm của ngày hôm nay Select 11.9 Tính số tháng giữa hai ngày cụ thể Next_date(kiểu date,day of week) Next_date(kiểu date,number) select next_day(sysdate,2) from dual; Last_day(kiểu date) Extract extract(day from sysdate), extract(month from sysdate), extract(year from sysdate) from dual; Months_between(date2, date1) months_between (to_date('31-12-2008','dd-mm-yyyy'), to_date('1-1-2008','dd-mm-yyyy')) from dual;