Presentation is loading. Please wait.

Presentation is loading. Please wait.

Gollis University Faculty of Computer Engineering Chapter Five: Retrieval, Functions Instructor: Mukhtar M Ali “Hakaale” BCS.

Similar presentations


Presentation on theme: "Gollis University Faculty of Computer Engineering Chapter Five: Retrieval, Functions Instructor: Mukhtar M Ali “Hakaale” BCS."— Presentation transcript:

1 Gollis University Faculty of Computer Engineering Chapter Five: Retrieval, Functions Instructor: Mukhtar M Ali “Hakaale” BCS

2 Overview of the Functions The main topic is still retrieval. It introduces functions and regular expressions, which enable you to formulate more powerful and complicated queries in an easy way. The chapter begins with an overview of the seven categories of functions: arithmetic, text, regular expression, date, general, conversion, and group. The last section of this chapter briefly explains how you can define your own SQL functions in Oracle using the PL/SQL programming language.

3 Overview of the Functions Cont … In Chapter 2, you saw that SQL supports the following standard SQL operators: a)Arithmetic operators: +, -, *, and / b)Alphanumeric operator: || (concatenation) Besides using these operators, you can also perform many operations on your data using functions. You can use functions virtually anywhere within queries: in the SELECT, WHERE, HAVING, and ORDER BY clauses.

4 Overview of the Functions Cont … if you specify a number as an argument for a function that expects a string instead, the number will be interpreted alphanumerically. However, if you ask for the square root of an employee name, you will get the error message ORA-01722: invalid number.” Here are some examples of calls to functions to illustrate the above. A number given to a string function (incorrect usage)—number interpreted as string:

5 Overview of the Functions Cont …

6 DUAL seems to indicate something to do with “two,” and the original table did indeed have two rows. Now it has only one row, and that is the important thing about it, but the name remained unchanged.

7 Arithmetic Functions The most popular arithmetic functions of Oracle are listed in Table 5-2.

8 Arithmetic Functions Cont … As Table 5-2 shows, the ROUND and TRUNC functions have an optional argument m; the default value for m is zero. Note that you can also use negative values for m, as you can see from the second example in Listing 5-1. Listings 5-1 through 5-4 show some self-explanatory examples of using the following arithmetic functions: ROUND, CEIL, FLOOR, ABS, SIGN, POWER, and MOD.

9 Text Functions The most important Oracle text functions are listed in Table 5-3

10 Text Functions Cont … Several text functions have a corresponding function with a B suffix, such as SUBSTRB, INSTRB, and LENGTHB. These special functions express their results in bytes instead of characters. This distinction is relevant only if you are using multibyte character sets. See Oracle SQL Reference for more details. Listing 5-7 shows some examples of the LOWER, UPPER, INITCAP, and LENGTH text functions; the results are self-explanatory.

11 Text Functions Cont … The first two column headings in Listing 5-8 are very confusing, because SQL*Plus converts all SELECT clause expressions to uppercase, including your function arguments. If you want lowercase characters in your column headings, you must add column aliases and specify them between double quotes. For example, the first line of Listing 5-8 would look like this: select ascii('a') as "ASCII('a')", ascii('z') as "ASCII('z')" Listings 5-9 and 5-10 show some examples of using the INSTR, SUBSTR, LTRIM, and RTRIM text functions. (The layout in Listing 5-9 is formatted to increase readability.)

12 Text Functions Cont …

13 Regular Expressions The previous chapter introduced the LIKE operator, and the previous section of this chapter introduced the INSTR, SUBSTR, and REPLACE functions. All of these SQL functions search for text. The LIKE operator offers the two wildcard characters % and _, which allow you to perform more advanced searches. Regular Expression Operators and Metasymbols Table 5-4 shows the most important regular expression metasymbols and their meanings. The Type column in Table 5-4 may contain the following: Postfix, which means that the operator follows its operand Prefix, which means that the operator precedes its operand Infix, which means that the operator separates its operands Nothing (empty), which means that the operator has no operands

14 Regular Expressions Cont …

15 If the square brackets notation does not give you enough precision or flexibility, you can use multicharacter collation elements, character classes, and equivalence classes, as follows: 1.Multicharacter collation elements are relevant for certain languages. Valid values are predefined and depend on the NLS_SORT setting. Use [. and.] to enclose collation elements. 2.Character classes give you more flexibility than the dash symbol between square brackets; for example, you can refer to alphabetic characters, numeric digits, alphanumeric characters, blank spaces, punctuation, and so on. Use [: and :] to enclose character classes. 3.Equivalence classes allow you to match all accented and unaccented versions of a letter. Use [= and =] to enclose equivalence classes.

16 Regular Expression Function Syntax The four regular expression functions have the following syntax. You can specify regular expressions in their pattern argument. a)REGEXP_LIKE(text, pattern[, options]) b)REGEXP_INSTR(text, pattern[, pos[, occurrence[, return[, options]]]]) c)REGEXP_SUBSTR(text, pattern[, pos[, occurrence[, options]]]) d)REGEXP_REPLACE(text, pattern[, replace [, pos[, occurrence[, options]]]]) For all four functions, the first two arguments (text and pattern) are mandatory. These arguments provide the source text and the regular expression to search for, respectively. All of the remaining arguments are optional.

17 Influencing Matching Behavior You can influence the matching behavior of the regular expression functions with their options argument. Table 5-5 shows the values you can specify in the options function argument.

18 REGEXP_INSTR Return Value

19 REGEXP_LIKE

20 REGEXP_INSTR

21 REGEXP_SUBSTR

22 REGEXP_REPLACE

23 A Q & Q U E S T I O N S A N S W E R S

24


Download ppt "Gollis University Faculty of Computer Engineering Chapter Five: Retrieval, Functions Instructor: Mukhtar M Ali “Hakaale” BCS."

Similar presentations


Ads by Google