版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、3Single-Row Functions3-2ObjectivesAfter completing this lesson, you should be able to do the following:Describe various types of functions availablein SQLUse character, number, and date functions in SELECT statementsDescribe the use of conversion functions 3-3SQL FunctionsInputFunction performs acti
2、onOutput3-4Two Types of SQL Functions3-5Single-Row FunctionsSingle row functions:Manipulate data itemsAccept arguments and return one valueAct on each row returnedReturn one result per rowMay modify the data typeCan be nestedAccept arguments which can be a column or an expressionfunction_name (arg1,
3、 arg2,.)3-6Single-row Functions3-7Character FunctionsLOWERUPPERINITCAPCONCATSUBSTRLENGTHINSTRLPAD | RPADTRIMREPLACE3-8Character FunctionsLOWERUPPERINITCAPCONCATSUBSTRLENGTHINSTRLPAD | RPADTRIMREPLACE3-9FunctionResultCase Manipulation FunctionsThese functions convert case for character strings.LOWER(
4、SQL Course)UPPER(SQL Course)INITCAP(SQL Course)sql courseSQL COURSESql Course3-10Using Case Manipulation FunctionsDisplay the employee number, name, and department number for employee Higgins:SELECT employee_id, last_name, department_idFROM employeesWHERE last_name = higgins;SELECT employee_id, last
5、_name, department_idFROM employeesWHERE LOWER(last_name) = higgins;3-11CONCAT(Hello, World)SUBSTR(HelloWorld,1,5)LENGTH(HelloWorld)INSTR(HelloWorld, W)LPAD(salary,10,*)RPAD(salary, 10, *)TRIM(H FROM HelloWorld)HelloWorldHello106*2400024000*elloWorldFunctionResultCharacter Manipulation FunctionsThese
6、 functions manipulate character strings:3-12SELECT employee_id, CONCAT(first_name, last_name) NAME, job_id, LENGTH (last_name), INSTR(last_name, a) Contains a?FROM employeesWHERE SUBSTR(job_id, 4) = REP;Using the Character Manipulation Functions1231233-13Number FunctionsROUND: Rounds value to specif
7、ied decimalROUND(45.926, 2)45.93TRUNC: Truncates value to specified decimalTRUNC(45.926, 2) 45.92MOD: Returns remainder of divisionMOD(1600, 300)1003-14SELECT ROUND(45.923,2), ROUND(45.923,0), ROUND(45.923,-1)FROM DUAL;Using the ROUND FunctionDUAL is a dummy table you can use to view results from fu
8、nctions and calculations.1233123-15SELECT TRUNC(45.923,2), TRUNC(45.923), TRUNC(45.923,-2)FROM DUAL;Using the TRUNC Function3121233-16SELECT last_name, salary, MOD(salary, 5000)FROM employeesWHERE job_id = SA_REP;Using the MOD FunctionCalculate the remainder of a salary after it is divided by 5000 f
9、or all employees whose job title is sales representative.3-17Working with DatesOracle database stores dates in an internal numeric format: century, year, month, day, hours, minutes, seconds.The default date display format is DD-MON-RR.Allows you to store 21st century dates in the 20th century by spe
10、cifying only the last two digits of the year. Allows you to store 20th century dates in the 21st century in the same way. SELECT last_name, hire_dateFROM employeesWHERE last_name like G%3-18Working with DatesSYSDATE is a function that returns:Date Time3-19Arithmetic with DatesAdd or subtract a numbe
11、r to or from a date for a resultant date value.Subtract two dates to find the number of days between those dates.Add hours to a date by dividing the number of hours by 24.3-20Using Arithmetic Operatorswith DatesSELECT last_name, (SYSDATE-hire_date)/7 AS WEEKSFROM employeesWHERE department_id = 90;3-
12、21Date FunctionsNumber of monthsbetween two datesMONTHS_BETWEENADD_MONTHSNEXT_DAYLAST_DAYROUNDTRUNC Add calendar months to dateNext day of the date specifiedLast day of the monthRound date Truncate dateFunctionDescription3-22 MONTHS_BETWEEN (01-SEP-95,11-JAN-94)Using Date Functions ADD_MONTHS (11-JA
13、N-94,6) NEXT_DAY (01-SEP-95,FRIDAY) LAST_DAY(01-FEB-95) 19.677419411-JUL-9408-SEP-9528-FEB-953-23 ROUND(SYSDATE,MONTH) 01-AUG-95 ROUND(SYSDATE ,YEAR) 01-JAN-96 TRUNC(SYSDATE ,MONTH) 01-JUL-95 TRUNC(SYSDATE ,YEAR) 01-JAN-95Using Date FunctionsAssume SYSDATE = 25-JUL-95:3-24Conversion Functions3-25Imp
14、licit Data Type ConversionFor assignments, the Oracle server can automaticallyconvert the following:VARCHAR2 or CHARFromToVARCHAR2 or CHARNUMBERDATENUMBERDATEVARCHAR2VARCHAR23-26Implicit Data Type ConversionFor expression evaluation, the Oracle Server can automatically convert the following:VARCHAR2
15、 or CHARFromToVARCHAR2 or CHARNUMBERDATE3-27Explicit Data Type ConversionNUMBERCHARACTERTO_CHARTO_NUMBERDATETO_CHARTO_DATE3-28Explicit Data Type ConversionNUMBERCHARACTERTO_CHARTO_NUMBERDATETO_CHARTO_DATE3-29Explicit Data Type ConversionNUMBERCHARACTERTO_CHARTO_NUMBERDATETO_CHARTO_DATE3-30Using the
16、TO_CHAR Function with DatesThe format model:Must be enclosed in single quotation marks and is case sensitiveCan include any valid date format elementHas an fm element to remove padded blanks or suppress leading zerosIs separated from the date value by a commaTO_CHAR(date, format_model)3-31YYYYElemen
17、ts of the Date Format ModelYEARMMMONTHDYDAYFull year in numbersYear spelled outTwo-digit value for monthThree-letter abbreviation of the day of the weekFull name of the day of the weekFull name of the monthMONThree-letter abbreviation of the monthDDNumeric day of the month3-32Using the TO_CHAR Funct
18、ion with NumbersThese are some of the format elements you can use with the TO_CHAR function to display a number value as a character:TO_CHAR(number, format_model)90$L.,Represents a numberForces a zero to be displayedPlaces a floating dollar signUses the floating local currency symbolPrints a decimal
19、 pointPrints a thousand indicator3-33SELECT TO_CHAR(salary, $99,999.00) SALARYFROM employeesWHERE last_name = Ernst;Using the TO_CHAR Function with Numbers3-34Using the TO_NUMBER and TO_DATE Functions Convert a character string to a number format using the TO_NUMBER function:Convert a character stri
20、ng to a date format using the TO_DATE function:These functions have an fx modifier. This modifier specifies the exact matching for the character argument and date format model of a TO_DATE functionTO_NUMBER(char, format_model)TO_DATE(char, format_model)3-35Using the TO_NUMBER and TO_DATE Functions C
21、onvert a character string to a number format using the TO_NUMBER function:Convert a character string to a date format using the TO_DATE function:These functions have an fx modifier. This modifier specifies the exact matching for the character argument and date format model of a TO_DATE functionTO_NU
22、MBER(char, format_model)TO_DATE(char, format_model)3-36RR Date FormatCurrent Year1995199520012001Specified Date27-OCT-9527-OCT-1727-OCT-1727-OCT-95RR Format1995201720171995YY Format1995191720172095If two digits of the current year are:04904950995099The return date is in the current centuryThe return
23、 date is in the century after the current oneThe return date is in the century before the current oneThe return date is in the current centuryIf the specified two-digit year is:3-37Example of RR Date FormatTo find employees hired prior to 1990, use the RR format, which produces the same results whet
24、her the command is run in 1999 or now:SELECT last_name, TO_CHAR(hire_date, DD-Mon-YYYY)FROM employeesWHERE hire_date TO_DATE(01-Jan-90, DD-Mon-RR);3-38Nesting FunctionsSingle-row functions can be nested to any level.Nested functions are evaluated from deepest level to the least deep level.F3(F2(F1(c
25、ol,arg1),arg2),arg3)Step 1 = Result 1Step 2 = Result 2Step 3 = Result 33-39SELECT last_name, NVL(TO_CHAR(manager_id), No Manager)FROM employeesWHERE manager_id IS NULL;Nesting Functions3-40General FunctionsThese functions work with any data type and pertainto using nulls.NVL (expr1, expr2)NVL2 (expr
26、1, expr2, expr3)NULLIF (expr1, expr2)COALESCE (expr1, expr2, ., exprn)3-41NVL FunctionConverts a null to an actual value.Data types that can be used are date, character, and number.Data types must match:NVL(commission_pct,0)NVL(hire_date,01-JAN-97)NVL(job_id,No Job Yet)3-42SELECT last_name, salary,
27、NVL(commission_pct, 0), (salary*12) + (salary*12*NVL(commission_pct, 0) AN_SALFROM employees;Using the NVL Function12123-43SELECT last_name, salary, commission_pct, NVL2(commission_pct, SAL+COMM, SAL) incomeFROM employees WHERE department_id IN (50, 80);Using the NVL2 Function12123-44Conditional Exp
28、ressionsProvide the use of IF-THEN-ELSE logic within a SQL statementUse two methods:CASE expressionDECODE function3-45The CASE ExpressionFacilitates conditional inquiries by doing the work of an IF-THEN-ELSE statement:CASE expr WHEN comparison_expr1 THEN return_expr1 WHEN comparison_expr2 THEN retur
29、n_expr2 WHEN comparison_exprn THEN return_exprn ELSE else_exprEND3-46SELECT last_name, job_id, salary, CASE job_id WHEN IT_PROG THEN 1.10*salary WHEN ST_CLERK THEN 1.15*salary WHEN SA_REP THEN 1.20*salary ELSE salary END REVISED_SALARYFROM employees;Using the CASE ExpressionFacilitates conditional inquiries by doing the work of an IF-THEN-EL
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度建筑企業(yè)內(nèi)部承包合同爭(zhēng)議解決機(jī)制
- 2025年度個(gè)人信用貸款合同模板匯編
- 2025年度建筑抗震加固安裝工程承包合同(安全耐久)
- 鹽城江蘇鹽城市大豐區(qū)住房和城鄉(xiāng)建設(shè)局招聘勞務(wù)派遣工作人員4人筆試歷年參考題庫附帶答案詳解
- 滁州2025年安徽滁州來安縣司法局招聘5名司法協(xié)理員筆試歷年參考題庫附帶答案詳解
- 湖北2025年湖北理工學(xué)院專項(xiàng)招聘97人筆試歷年參考題庫附帶答案詳解
- 浙江浙江省衛(wèi)生健康綜合保障中心招聘編外人員筆試歷年參考題庫附帶答案詳解
- 洛陽2024年河南洛陽市孟津區(qū)引進(jìn)研究生學(xué)歷人才50人筆試歷年參考題庫附帶答案詳解
- 承德2025年河北承德市中心醫(yī)院招聘筆試歷年參考題庫附帶答案詳解
- 2025年中國五礦機(jī)械市場(chǎng)調(diào)查研究報(bào)告
- 數(shù)學(xué)-河南省三門峽市2024-2025學(xué)年高二上學(xué)期1月期末調(diào)研考試試題和答案
- 2025年春新人教版數(shù)學(xué)七年級(jí)下冊(cè)教學(xué)課件
- 《心臟血管的解剖》課件
- 心肺復(fù)蘇課件2024
- 2024-2030年中國并購基金行業(yè)發(fā)展前景預(yù)測(cè)及投資策略研究報(bào)告
- 河道清淤安全培訓(xùn)課件
- 2024各科普通高中課程標(biāo)準(zhǔn)
- 7.3.1印度(第1課時(shí))七年級(jí)地理下冊(cè)(人教版)
- 教師培訓(xùn)校園安全
- 北師大版語文四年級(jí)下冊(cè)全冊(cè)教案
- 《湖南師范大學(xué)》課件
評(píng)論
0/150
提交評(píng)論