Tài liệu single row functions pdf

66 297 0
Tài liệu single row functions pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Single Row Functions 3 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć2 Single Row Functions 3Ć3 Objectives Functions make the basic query block more powerful and are used to manipulate data values. This is the first of two lessons that explore functions. You will focus on single row character, number, and date functions, as well as those functions that convert data from one type to another, for example, character data to numeric. At the end of this lesson, you should be able to D Explain the various types of functions available in SQL. D Identify the basic concepts of using functions. D Use a variety of character, number, and date functions in SELECT statements. D Explain the conversion functions and how they might be used. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć4 Single Row Functions 3Ć5 Overview Functions are a very powerful feature of SQL and can be used to D Perform calculations on data. D Modify individual data items. D Manipulate output for groups of rows. D Alter date formats for display. D Convert column datatypes. There are two distinct types of functions: D Single row functions. D Multiple row functions. Single Row Functions These functions operate on single rows only, and return one result per row. There are different types of single row functions. We will cover those listed below. D Character D Number D Date D Conversion Multiple Row Functions These functions manipulate groups of rows to give one result per group of rows. For more information, see Oracle7 Server SQL Reference, Release 7.3 for the complete list of available functions and syntax. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć6 Single Row Functions 3Ć7 Single Row Functions Single row functions are used to manipulate data items. They accept one or more arguments and return one value for each row returned by the query. An argument may be one of the following: D A user-supplied constant D A variable value D A column name D An expression Features of Single Row Functions D They act on each row returned in the query. D They return one result per row. D They may return a data value of a different type than that referenced. D They may expect one or more user arguments. D You can nest them. D You can use them in SELECT, WHERE, and ORDER BY clauses. Syntax function_name (column|expression, [arg1, arg2, .]) where: function_name is the name of the function. column is any named database column. expression is any character string or calculated expression. arg1, arg2 is any argument to be used by the function. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć8 Single Row Functions 3Ć9 Character Functions Single row character functions accept character data as input and can return both character and number values. Function Purpose LOWER(column|expression) Converts alpha character values to lowercase. UPPER(column|expression) Converts alpha character values to uppercase. INITCAP(column|expression) Converts alpha character values to uppercase for the first letter of each word, all other letters in lowercase. CONCAT(column1|expression1, column2|expression2) Concatenates the first character value to the second character value. Equivalent to concatenation operator (||). SUBSTR(column|expression,m[,n]) Returns specified characters from character value starting at character position m, n characters long. If m is negative, the count starts from the end of the character value. LENGTH(column|expression) Returns the number of characters in value. NVL(column|expression1,column|ex pression2) Converts the the first value if null to the second value. Note: This list is a subset of the available character functions. For more information, see Oracle7 Server SQL Reference, Release 7.3, “Character Functions.” Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć10 [...]... remainder of m divided by n Note: This list is a subset of the available number functions For more information, see Oracle7 Server SQL Reference, Release 7.3, “Number Functions. ” Single Row Functions 3Ć17 3Ć18 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Number Functions continued The TRUNC and ROUND functions work with similar arguments If the second argument is 0 or is missing,... SUBSTR(name,1,3) = ’Ace’; NAME LENGTH(NAME) -Ace Ski Boot 12 Ace Ski Pole 12 Single Row Functions 3Ć15 3Ć16 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Number Functions Number functions accept numeric input and return numeric values This section describes some of the number functions Function Purpose ROUND(column|expression,n) Rounds the column, expression, or value... 230.083641 Note: SYSDATE is a SQL function that returns the current date and time Your results may differ from the examples Single Row Functions 3Ć25 3Ć26 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Date Functions Date functions operate on Oracle dates All date functions return a value of DATE datatype except MONTHS_BETWEEN, which returns a numeric value Function Purpose MONTHS_BETWEEN(date1,... date Note: This list is a subset of the available date functions The format models are covered later in this chapter Examples of format models are month or year For more information, see Oracle7 Server SQL Reference, Release 7.3, “Date Functions. ” Single Row Functions 3Ć27 3Ć28 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Date Functions continued Example For all employees employed... PRODUCT_ID -30433 40422 50532 NEXT_DAY( 11-SEP-92 12-FEB-93 16-APR-93 Single Row Functions LAST_DAY( 30-SEP-92 28-FEB-93 30-APR-93 3Ć29 3Ć30 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Date Functions continued The ROUND and TRUNC functions can be used for number and date values When using these functions with dates, they round or truncate to the specified format model Therefore,... TRUNC(STA 01-JUN-91 01-JAN-91 01-FEB-91 01-FEB-91 01-OCT-91 01-FEB-91 01-AUG-91 01-JUL-91 01-MAY-91 01-MAR-91 01-MAY-91 11 rows selected Single Row Functions 3Ć31 3Ć32 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Conversion Functions SQL provides three functions to convert a value from one datatype to another Function Purpose TO_CHAR(number|date,[‘fmt’]) Converts a number or date... date to a date value according to the fmt specified If fmt is omitted, format is DD-MON-YY Note: This list is a subset of the available conversion functions For more information, see Oracle7 Server SQL Reference, Release 7.3, “Conversion Functions. ” Single Row Functions 3Ć33 3Ć34 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder TO_CHAR Function with Date Formats Displaying a Date in a Specific...Character Functions continued Example Display the first and last name in lowercase, userid in initial capitalization, and title in uppercase for all vice presidents SQL> SELECT 2 3 4 FROM 5 WHERE LOWER(first_name||’ ’||last_name) VP, INITCAP(userid) USERID, UPPER(title) TITLE s_emp title LIKE ’VP%’; VP ladoris ngao midori nagayama mark quick-to-see audry ropeburn Single Row Functions USERID... The name is displayed as it was stored in the database To display the name in uppercase, the UPPER function must be used in the SELECT clause as well Single Row Functions 3Ć13 3Ć14 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Character Functions continued Example Display the name and country of all customers with a good credit rating Concatenate name and country SQL> SELECT 2 FROM 3... TRUNC(45.923,2) TRUNC(45.923) TRUNC(45.923,-1) - - -45.92 45 40 SYS.DUAL is a dummy table It will be covered in detail later in this lesson Single Row Functions 3Ć19 3Ć20 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Number Functions continued Example Calculate the remainder of the ratio of salary to commission for all employees whose salary is more than 1400 SQL> SELECT . functions. Single Row Functions These functions operate on single rows only, and return one result per row. There are different types of single row functions. . available functions and syntax. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć6 Single Row Functions 3Ć7 Single Row Functions Single row functions

Ngày đăng: 10/12/2013, 17:15

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan