數(shù)據(jù)庫(kù)技術(shù)與Oracle:sql-03 Single-Row Functions_第1頁(yè)
數(shù)據(jù)庫(kù)技術(shù)與Oracle:sql-03 Single-Row Functions_第2頁(yè)
數(shù)據(jù)庫(kù)技術(shù)與Oracle:sql-03 Single-Row Functions_第3頁(yè)
數(shù)據(jù)庫(kù)技術(shù)與Oracle:sql-03 Single-Row Functions_第4頁(yè)
數(shù)據(jù)庫(kù)技術(shù)與Oracle:sql-03 Single-Row Functions_第5頁(yè)
已閱讀5頁(yè),還剩45頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論