#select lower(100+200),lower('SQL'),lower('sysdate') from dual;select first_name,last_name,lower(last_name) from hr.employees where lower(last_name) like '%ur%';# UPPERselect * from hr.countries where upper(country_name) like '%U%S%A%';#INITCAP 首字母大寫,其它字母小寫。 SQL> select initcap(' init CAP or init_cap or init%cap') as initcap from dual;INITCAP---------------------------------------------------------------------- Init Cap Or Init_Cap Or Init%Cap
8.1.4 字符串操作函數(shù)
Trim : TRIM只能截取一個字符
sql
復制代碼
#CONCATSQL> select concat('Today is:',sysdate) from dual;CONCAT('TODAYIS:',SYSDATE)--------------------------------------------------------Today is:2020-01-21 03:43:22#LENGTHselect * from hr.countries where length(country_name) > 10;# LPAD/RPAD 函數(shù) ,填充到指定位數(shù)。若是大于指定位數(shù),就會截止select rpad(first_name || ' ' || last_name,18) || ' earns ' || lpad(salary,6,' ' ) from hr.employees where department_id=100;# TRIM函數(shù)# TRIM只能截取一個字符。SQL> select trim(trailing 'son' from 'Anderson') from dual;ORA-30001: trim set should have only one characterSQL> select trim(both '*' from '**Hiden**'), trim(leading '*' from '**Hiden**') , trim(trailing '*' from '**Hiden**') from dual;TRIM(BOTH' TRIM(LEADING'* TRIM(TRAILING'---------- -------------- --------------Hiden Hiden** **Hiden#INSTR 搜索字符串在給定字符串的位置,返回數(shù)字位置#instr( string1, string2 [, start_position [, nth_appearance ] ] ) // instr(源字符串, 目標字符串, 起始位置, 匹配序號)select instr('1#3#5#7#9#','#') from dual;select instr('1#3#5#7#9#','#',5) from dual;select instr('1#3#5#7#9#','#',3,4) from dual; -- 第三個字符開始,第4次出現(xiàn)的位置。INSTR('1#3#5#7#9#','#',3,4)--------------------------- 10#SUBSTR 指定位置取子字符串,substr( string, start_position, [ length ] )SQL> select substr('1#3#5#7#9#',5) from dual;SUBSTR('1#3#------------5#7#9#select substr('1#3#5#7#9#',5,3) from dual; --第5個字符開始取3個。SUBSTR------5#7select substr('1#3#5#7#9#',-3,2) from dual; --倒數(shù)第3個字符開始,取2個字符。SUBS----#9## replace(原字段,“原字段舊內(nèi)容“,“原字段新內(nèi)容“)update demo set name = replace(name,'醫(yī)院','醫(yī)院住院部')SQL> select replace('1#3#5#7#9#','#','->') from dual;REPLACE('1#3#5#7#9#','#','->')------------------------------1->3->5->7->9->SQL> select replace('1#3#5#7#9#','#') from dual; -- 用null替換#,即刪除#號REPLACE('1----------13579
字符串綜合示例:
csharp
復制代碼
#到出hostname信息。define email='thx@hostname.163.com';select instr('&email','@') from dual; #pos=4select substr('&email',instr('&email','@')+1) from dual; # hostname.163.com SUBSTR('THX@HOSTNAME.163.COM',IN--------------------------------hostname.163.comselect substr('&email',instr('&email','@')+1, instr('&email','.163.com')-instr('&email','@')-1) from dual; #到hostnameSUBSTR('THX@HOST----------------hostname
8.1.5 數(shù)字函數(shù)
ROUND:
scss
復制代碼
#數(shù)字函數(shù)SQL> SELECT TRUNC(45.923,2), TRUNC(45.923), TRUNC(45.923,-1) FROM DUAL;TRUNC(45.923,2) TRUNC(45.923) TRUNC(45.923,-1)--------------- ------------- ---------------- 45.92 45 40#ROUND 函數(shù),對n+1位4舍5入.SQL> select round(1601.916,1) from dual;ROUND(1601.916,1)----------------- 1601.9SQL> select round(-1601.916,1) from dual;ROUND(-1601.916,1)------------------ -1601.9SQL> select round(1601.916,2) from dual;ROUND(1601.916,2)----------------- 1601.92SQL> select round(1601.916,-3) from dual;ROUND(1601.916,-3)------------------ 2000
TRUNC:
sql
復制代碼
#TruncateSQL> select trunc(1601.916,1) from dual; -- 1601.9SQL> select trunc(1601.916,2) from dual; -- 1601.91SQL> select trunc(1601.916,-3) from dual;TRUNC(1601.916,-3)------------------ 1000SQL> SELECT TRUNC (ROUND(156.00,-2),-1) FROM DUAL;TRUNC(ROUND(156.00,-2),-1)-------------------------- 200
MOD
sql
復制代碼
#ModulusSQL> select mod(6,2) from dual; MOD(6,2)---------- 0SQL> select mod(5,3) from dual; MOD(5,3)---------- 2
#數(shù)字轉(zhuǎn)字符,日期轉(zhuǎn)字符select length(12345678) from dual;select sysdate from dual; #select length(sysdate) from dual;#字符轉(zhuǎn)數(shù)字select mod('123',2) from dual; -- 1SQL> select mod('11.123',2) from dual; MOD('11.123',2)--------------- 1.123>select mod('11.123.123',2) from dual; --會出錯。#字串到日期select add_months('24-JAN-09',1) from dual;ADD_MONTHS('24-JAN--------------------0024-02-09 00:00:00SQL> select add_months(to_date('2015.12.29','YYYY/MM/DD'),1) from dual;ADD_MONTHS(TO_DATE(-------------------2016-01-29 00:00:00
2)顯式數(shù)據(jù)類型轉(zhuǎn)換
python
復制代碼
#TO_CHARSQL> select to_char(0001), to_char(0001,'099999') from dual;TO TO_CHAR(0001,'-- --------------1 000001#||后要接單引號SQL> select to_char(0001) || " is a special number" from dual; --errorORA-00904: " is a special number": invalid identifierSQL> select to_char(0001) || ' is a special number' from dual; --okTO_CHAR(0001)||'ISASPECIALNUMBER'------------------------------------------1 is a special number#SQL> select 1 || 'is a special number' from dual; --ok1||'ISASPECIALNUMBER'----------------------------------------1is a special number#SQL> select to_char(14,'L0999') from dual;TO_CHAR(14,'L0999')------------------------------ $0014
#nls_session_parametersselect * from nls_session_parameters;select * from nls_session_parameters where PARAMETER='NLS_DATE_FORMAT';alter session set NLS_DATE_FORMAT='DD-MON-RR'; --RR是相當于YY,yearSQL> select to_char(sysdate,'fmDD Month YYYY') as today from dual;TODAY------------------------------------------------------------24 January 2020# FM去除前導0,即01-->1SQL> select to_char(to_date('2020-01-21','YYYY-MM-DD'),'DD-MM-YYYY') from dual;TO_CHAR(TO_DATE('202--------------------21-01-2020SQL> select to_char(to_date('2020-01-21','YYYY-MM-DD'),'fm DD-MM-YYYY') from dual;TO_CHAR(TO_DATE('202-------------------- 21-1-2020SQL> select to_char(30.1,'$99,999.99') from dual;TO_CHAR(30.1,'$99,999.---------------------- $30.10SQL> SELECT last_name, TO_CHAR(hire_date, 'DD-Mon-YYYY') FROM employees WHERE hire_date < TO_DATE('01-Jan-90','DD-Mon-RR');LAST_NAME TO_CHAR(HIRE_DATE,'DD-MON-YYYY')-------------------------------------------------- ----------------------------------------Whalen 17-Sep-1987King 17-Jun-1987Kochhar 21-Sep-1989
日期轉(zhuǎn)換操作:
TO_CHAR
python
復制代碼
#TO_CHARselect to_char(sysdate,'YYYY-MM-DD') from dual;select to_char(sysdate,'year') from dual;select to_char(sysdate,'day') from dual;SQL> select to_char(sysdate,'HH24:MI:SS AM') from dual;TO_CHAR(SYSDATE,'HH24:----------------------11:52:53 AMSQL> select to_char(sysdate) from dual;TO_CHAR(SYSDATE)--------------------------------------2020-01-21 16:02:32SQL> select to_char(sysdate,'Month') from dual;TO_CHAR(SYSDATE,'MONTH')------------------------------------------------------------------------January#SQL> select TO_CHAR(TO_DATE('01-JAN-00','DD-MON-RR'),'Day') from dual;TO_CHAR(TO_DATE('01-JAN-00','DD-MON-RR'),'DAY')------------------------------------------------------------------------SaturdaySQL> select to_char(sysdate,'YYYY-MM-DD HH:MI:SS') from dual;TO_CHAR(SYSDATE,'YYYY-MM-DDHH:MI:SS')--------------------------------------2020-01-21 04:09:10SQL> SELECT last_name, TO_CHAR(hire_date, 'fmDD Month YYYY') AS HIREDATE FROM employees;LAST_NAME HIREDATE------------------------------------- -----------------------------------------------------------------------OConnell 21 June 1999Grant 13 January 2000
TO_DATE
sql
復制代碼
SQL> select * from nls_session_parameters where PARAMETER='NLS_DATE_FORMAT';SQL> select to_date('01-DEC-2010') from dual;TO_DATE('01-DEC-201-------------------0001-12-20 10:00:00SQL> select to_date('25-DEC','DD-MON') from dual; TO_DATE('25-DEC','D-------------------2020-12-25 00:00:00SQL> select to_date('25-DEC') from dual; --出錯ORA-01858: a non-numeric character was found where a numeric was expectedSQL> select to_date('25-DEC-10','fxDD-MON-YYYY') from dual; --出錯ORA-01862: the numeric value does not match the length of the format itemSQL> select to_date('01-DEC-2010','DD-MON-YYYY') from dual;TO_DATE('01-DEC-201-------------------2010-12-01 00:00:00
TO_NUMBER
sql
復制代碼
select to_number('$100.55') from dual; --錯誤ORA-01722: invalid numberSQL>select to_number('$1,000.55','$99,999.99') from dual; --okTO_NUMBER('$1,000.55','$99,999.99')----------------------------------- 1000.55SQL> select to_number(123.56,'999.9') from dual; --error 。select to_number(123.56,'999.999') from dual; --right 123.56
8.4 SELECT中應(yīng)用條件表達式
8.4.1 嵌套函數(shù)
sql
復制代碼
SQL> select next_day(last_day(sysdate) - 7 ,'tue') from dual;NEXT_DAY(LAST_DAY(S-------------------2020-01-28 20:29:41