标签:
SELECT UPPER(‘abc‘) FROM dual;
SELECT LOWER(‘ABC‘) FROM dual;
SELECT INITCAP(‘hello‘) FROM dual;
SELECT REPLACE(‘abcd efg‘,‘cd‘,‘XXX‘) FROM dual;
SELECT LENGTH(‘hello world‘) FROM dual;
SELECT SUBSTR(‘abcdef‘,0,3) FROM dual;
字符串截取(Oracle中下标都是从1开始,如果指定为0则也会默认为1)
l 从指定位置截取到结尾
l 截取部分的字符串
截取前三位:
SELECT SUBSTR(‘abcdefg’,0,3) FROM dual;
截取后三位:
SELECT SUBSTR(‘abcdefg’,LENGTH(‘abcdefg’)-2) FROM dual;
SELECT SUBSTR(‘abcdefg’,-3) FROM dual;
SELECT ASCII(‘A’) FROM dual;
SELECT CHR(100) FROM dual;
SELECT LTRIM(‘ AAA ‘) FROM dual;
SELECT RTRIM(‘ AAA ‘) FROM dual;
SELECT TRIM(‘ AAA ‘) FROM dual;
SELECT LPAD(‘abc‘,10,‘*‘) FROM dual;
SELECT RPAD(‘abc‘,10,‘*‘) FROM dual;
SELECT RPAD(LPAD(‘abc‘,5,‘*‘),10,‘*‘) FROM dual;
SELECT INSTR(‘Hello Wold‘,‘llo‘) FROM dual;
SELECT CONCAT(‘Hello‘,‘ World‘) FROM dual;
不保留小数:
SELECT ROUND(759.9965) FROM dual;
保留两位小数:
SELECT ROUND(759.9965,3) FROM dual;
处理整数进位:
SELECT ROUND(759.9965,-2) FROM dual;
SELECT TRUNC(759.9965,2) FROM dual;
SELECT TRUNC(759.9965,-2) FROM dual;
SELECT MOD(10,3) FROM dual;
SELECT MOD(10.5,3) FROM dual;
ALTER SESSION SET NLS_DATE_FORMAT = ‘yyyy-mm-dd hh24:mm:ss’
三天之后的日期:
SELECT SYSDATE + 3 FROM dual;
三天之前的日期:
SELECT SYSDATE -3 FROM dual;
三月之后的日期:
SELECT ADD_MONTHS(SYSDATE,3) FROM dual;
三月之前的日期:
SELECT ADD_MONTHS(SYSDATE,-3) FROM dual;
SELECT TRUNC(MONTHS_BETWEEN(SYSDATE,TO_DATE(‘1987-10-18‘,‘yyyy-mm-dd‘))/12) AS HIRE FROM dual;
SELECT EXTRACT(DAY FROM DATE ‘2016-07-25‘) FROM DUAL;
SELECT LAST_DAY(SYSDATE) FROM DUAL;
日期格式化标志
将日期转换为字符串:
SELECT TO_CHAR(SYSDATE,‘YYYY-MM-DD HH:MI:SS‘) FROM DUAL;
将数字转化为字符串:
SELECT
TO_CHAR(1562456845625.4512659, ‘999,999,999,999,999,999.9999999‘) AS FORMAT_A,
TO_CHAR(1562456845625.4512659, ‘000,000,000,000,000,000.0000000‘) AS FORMAT_B
FROM DUAL;
SELECT TO_DATE(‘1979/09/19‘,‘YYYY/MM/DD‘) FROM DUAL;
SELECT TO_NUMBER(‘23‘) * TO_NUMBER(‘55‘) AS RESULT FROM DUAL;
SELECT NVL(NULL,‘ABC‘) FROM DUAL;
SELECT NVL(‘HELLO‘,‘ABC‘) FROM DUAL;
SELECT NVL2(NULL,‘ABC‘,‘DEF‘) FROM DUAL;
SELECT NVL2(‘F‘,‘ABC‘,‘DEF‘) FROM DUAL;
SELECT NULLIF(1,1) FROM DUAL;
SELECT NULLIF(7,2) FROM DUAL;
找到:
SELECT DECODE(65, 60,‘BAD‘, 65,‘GOOD‘, 90,‘EXCELLENT‘ ) FROM DUAL; |
找不到:
SELECT DECODE(30, 60,‘BAD‘, 65,‘GOOD‘, 90,‘EXCELLENT‘ ) FROM DUAL; |
找不到时可以设置默认值:
SELECT DECODE(30, 60,‘BAD‘, 65,‘GOOD‘, 90,‘EXCELLENT‘, ‘UNKNOWN‘ ) FROM DUAL; |
值找到:
SELECT CASE 65 WHEN 60 THEN ‘BAD‘ WHEN 65 THEN ‘GOOD‘ WHEN 90 THEN ‘EXCELLENT‘ ELSE ‘UNKNOWN‘ END FROM DUAL; |
值找不到:
SELECT CASE 99 WHEN 60 THEN ‘BAD‘ WHEN 65 THEN ‘GOOD‘ WHEN 90 THEN ‘EXCELLENT‘ ELSE ‘UNKNOWN‘ END FROM DUAL; |
SELECT COALESCE(NULL,33,NULL) FROM DUAL; |
SELECT COALESCE(NULL,NULL,99) FROM DUAL; |
SELECT COALESCE(NULL,NULL,NULL) FROM DUAL; |
标签:
原文地址:http://www.cnblogs.com/kuillldan/p/5874975.html