Tài liệu OCA: Oracle Database 11g Administrator Certified Associate Study Guide- P4 doc

50 1.1K 3
Tài liệu OCA: Oracle Database 11g Administrator Certified Associate Study Guide- P4 doc

Đ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

Using Single-Row Numeric Functions 81 Function Description ATAN2 Returns the arc tangent; takes two inputs BITAND Returns the result of a bitwise AND on two inputs CEIL Returns the next higher integer COS Returns the cosine COSH Returns the hyperbolic cosine EXP Returns the base of natural logarithms raised to a power FLOOR Returns the next smaller integer LN Returns the natural logarithm LOG Returns the logarithm MOD Returns the modulo (remainder) of a division operation NANVL Returns an alternate number if the value is Not a Number (NaN) for BINARY_FLOAT and BINARY_DOUBLE numbers POWER Returns a number raised to an arbitrary power REMAINDER Returns the remainder in a division operation ROUND Rounds a number SIGN Returns an indicator of sign: negative, positive, or zero SIN Returns the sine SINH Returns the hyperbolic sine SQRT Returns the square root of a number TAN Returns the tangent TANH Returns the hyperbolic tangent TRUNC Truncates a number WIDTH_BUCKET Creates equal-width histograms TABLE 2.2 Numeric Function Summary (continued) 95127c02.indd 81 2/18/09 6:46:40 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 82 Chapter 2 N Using Single-Row Functions Numeric Function Descriptions Numeric functions have numeric arguments and return numeric values. The trigonometric functions all operate on radians, not degrees. The numeric functions are arranged in alphabetical order, with descriptions and exam- ples of each one. SIGN , ROUND , and TRUNC are most commonly used numeric functions—pay particular attention to them. FLOOR , CEIL , MOD , and REMAINDER are also important functions that can appear in the test. TRUNC and ROUND functions can take numeric input or a datetime input. These two functions are discussed in the “Using Single-Row Date Functions” section to illustrate their behavior with a datetime datatype input. ABS ABS(n) takes a single argument, where n is a numeric datatype (NUMBER, BINARY_ FLOAT or BINARY_DOUBLE). This function returns the absolute value of n . SELECT ABS(-52) negative, ABS(52) positive FROM dual; NEGATIVE POSITIVE ---------- ---------- 52 52 ACOS ACOS(n) takes a single argument, where n is a numeric datatype between –1 and 1. This function returns the arc cosine of n expressed in radians, accurate to 30 digits of precision. SELECT ACOS(-1) PI, ACOS(0) ACOSZERO, ACOS(.045) ACOS045, ACOS(1) ZERO FROM dual; PI ACOSZERO ACOS045 ZERO ---------- ---------- ---------- ---------- 3.14159265 1.57079633 1.52578113 0 ASIN ASIN(n) takes a single argument, where n is a numeric datatype between –1 and 1. This function returns the arc sine of n expressed in radians, accurate to 30 digits of precision. SELECT ASIN(1) high, ASIN(0) middle, ASIN(-1) low FROM dual; 95127c02.indd 82 2/18/09 6:46:40 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Using Single-Row Numeric Functions 83 HIGH MIDDLE LOW ---------- ---------- ---------- 1.57079633 0 -1.5707963 ATAN ATAN(n) takes a single argument, where n is a numeric datatype. This function returns the arc tangent of n expressed in radians, accurate to 30 digits of precision. SELECT ATAN(9E99) high, ATAN(0) middle, ATAN(-9E99) low FROM dual; HIGH MIDDLE LOW ---------- ---------- ---------- 1.57079633 0 -1.5707963 ATAN2 ATAN2(n1, n2) takes two arguments, where n1 and n2 are numbers. This function returns the arc tangent of n1 and n2 expressed in radians, accurate to 30 digits of precision. ATAN2(n1,n2) is equivalent to ATAN(n1/n2) . SELECT ATAN2(9E99,1) high, ATAN2(0,3.1415) middle, ATAN2(-9E99,1) low FROM dual; HIGH MIDDLE LOW ---------- ---------- ---------- 1.57079633 0 -1.5707963 BITAND BITAND(n1, n2) takes two arguments, where n1 and n2 are positive integers or zero. This function performs a bitwise AND operation on the two input values and returns the results, also an integer. It is used to examine bit fields. Here are two examples of BITAND . The first one performs a bitwise AND operation on 6 (binary 0110) and 3 (binary 0011). The result is 2 (binary 0010). Similarly, the bitwise AND between 8 (binary 1000) and 2 (binary 0010) is 0 (0000). SELECT BITAND(6,3) T1, BITAND(8,2) T2 FROM dual; T1 T2 ---------- ---------- 2 0 95127c02.indd 83 2/18/09 6:46:40 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 84 Chapter 2 N Using Single-Row Functions CEIL CEIL(n) takes a single argument, where n is a numeric datatype. This function returns the smallest integer that is greater than or equal to n . CEIL rounds up to a whole number. See also FLOOR . SELECT CEIL(9.8), CEIL(-32.85), CEIL(0), CEIL(5) FROM dual; CEIL(9.8) CEIL(-32.85) CEIL(0) CEIL(5) ---------- ------------ ---------- ---------- 10 -32 0 5 COS COS(n) takes a single argument, where n is a numeric datatype in radians. This function returns the cosine of n , accurate to 36 digits of precision. SELECT COS(-3.14159) FROM dual; COS(-3.14159) ------------- -1 COSH COSH(n) takes a single argument, where n is a numeric datatype. This function returns the hyperbolic cosine of n , accurate to 36 digits of precision. SELECT COSH(1.4) FROM dual; COSH(1.4) ---------- 2.15089847 EXP EXP(n) takes a single argument, where n is a numeric datatype. This function returns e (the base of natural logarithms) raised to the n power, accurate to 36 digits of precision. SELECT EXP(1) “e” FROM dual; e ---------- 2.71828183 95127c02.indd 84 2/18/09 6:46:40 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Using Single-Row Numeric Functions 85 FLOOR FLOOR(n) takes a single argument, where n is a numeric datatype. This function returns the largest integer that is less than or equal to n . FLOOR rounds down to a whole number. See also CEIL . SELECT FLOOR(9.8), FLOOR(-32.85), FLOOR(137) FROM dual; FLOOR(9.8) FLOOR(-32.85) FLOOR(137) ---------- ------------- ---------- 9 -33 137 LN LN(n) takes a single argument, where n is a numeric datatype greater than 0. This function returns the natural logarithm of n , accurate to 36 digits of precision. SELECT LN(2.7) FROM dual; LN(2.7) ---------- .993251773 LOG LOG(n1, n2) takes two arguments, where n1 and n2 are numeric datatypes. This function returns the logarithm base n1 of n2 , accurate to 36 digits of precision. SELECT LOG(8,64), LOG(3,27), LOG(2,1024), LOG(2,8) FROM dual; LOG(8,64) LOG(3,27) LOG(2,1024) LOG(2,8) ---------- ---------- ----------- ---------- 2 3 10 3 MOD MOD(n1, n2) takes two arguments, where n1 and n2 are any numeric datatype. This func- tion returns n1 modulo n2 , or the remainder of n1 divided by n2 . If n1 is negative, the result is negative. The sign of n2 has no effect on the result. If n2 is zero, the result is n1 . See also REMAINDER . SELECT MOD(14,5), MOD(8,2.5), MOD(-64,7), MOD(12,0) FROM dual; 95127c02.indd 85 2/18/09 6:46:41 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 86 Chapter 2 N Using Single-Row Functions MOD(14,5) MOD(8,2.5) MOD(-64,7) MOD(12,0) ---------- ---------- ---------- --------- 4 .5 -1 12 NANVL This function is used with BINARY_FLOAT and BINARY_DOUBLE datatype numbers to return an alternate value if the input is NaN. The following example defines the NULL display as ? to show NULL value. The TO_BINARY_ FLOAT function (discussed later in the chapter) is used to convert input to a BINARY_ FLOAT datatype number. SET NULL ? SELECT NANVL(TO_BINARY_FLOAT(‘NaN’), 0) T1, NANVL(TO_BINARY_FLOAT(‘NaN’), NULL) T2 FROM dual; T1 T2 ---------- ---------- 0 ? POWER POWER(n1, n2) takes two arguments, where n1 and n2 are numeric datatypes. This function returns n1 to the n2 power. SELECT POWER(2,10), POWER(3,3), POWER(5,3), POWER(2,-3) FROM dual; POWER(2,10) POWER(3,3) POWER(5,3) POWER(2,-3) ----------- ---------- ---------- ----------- 1024 27 125 .125 REMAINDER REMAINDER(n1, n2) takes two arguments, where n1 and n2 are any numeric datatype. This function returns the remainder of n1 divided by n2 . If n1 is negative, the result is negative. The sign of n2 has no effect on the result. If n2 is zero and the datatype of n1 is NUMBER, an error is returned; if the datatype of n1 is BINARY_FLOAT or BINARY_DOUBLE, NaNis returned. See also MOD. SELECT REMAINDER(13,5), REMAINDER(12,5), REMAINDER(12.5, 5) FROM dual; 95127c02.indd 86 2/18/09 6:46:41 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Using Single-Row Numeric Functions 87 REMAINDER(13,5) REMAINDER(12,5) REMAINDER(12.5,5) --------------- --------------- ----------------- -2 2 2.5 The difference between MOD and REMAINDER is that MOD uses the FLOOR function, whereas REMAINDER uses the ROUND function in the formula. If you apply MOD function to the previous example, the results are the same except for the first column: SELECT MOD(13,5), MOD(12,5), MOD(12.5, 5) FROM dual; MOD(13,5) MOD(12,5) MOD(12.5,5) ---------- ---------- ----------- 3 2 2.5 Here is another example of using REMAINDER with a BINARY_FLOAT number, having n2 as zero: SELECT REMAINDER(TO_BINARY_FLOAT(‘13.0’), 0) RBF from dual; RBF ---------- Nan ROUND ROUND(n1 [,n2]) takes two arguments, where n1 is a numeric datatype and n2 is an integer. This function returns n1 rounded to n2 digits of precision to the right of the decimal. If n2 is negative, n1 is rounded to the left of the decimal. If n2 is omitted, the default is zero. This function is similar to TRUNC . SELECT ROUND(123.489), ROUND(123.489, 2), ROUND(123.489, -2), ROUND(1275, -2) FROM dual; ROUND(123.489) ROUND(123.489,2) ROUND(123.489,-2) ROUND(1275,-2) -------------- ---------------- ----------------- -------------- 123 123.49 100 1300 95127c02.indd 87 2/18/09 6:46:41 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 88 Chapter 2 N Using Single-Row Functions SIGN SIGN(n) takes a single argument, where n is a numeric datatype. This function returns –1 if n is negative, 1 if n is positive, and 0 if n is 0. SELECT SIGN(-2.3), SIGN(0), SIGN(47) FROM dual; SIGN(-2.3) SIGN(0) SIGN(47) ---------- ---------- ---------- -1 0 1 SIN SIN(n) takes a single argument, where n is a number in radians. This function returns the sine of n , accurate to 36 digits of precision. SELECT SIN(1.57079) FROM dual; SIN(1.57079) ------------ 1 SINH SINH(n) takes a single argument, where n is a number. This function returns the hyperbolic sine of n , accurate to 36 digits of precision. SELECT SINH(1) FROM dual; SINH(1) ---------- 1.17520119 SQRT SQRT(n) takes a single argument, where n is a numeric datatype. This function returns the square root of n. SELECT SQRT(64), SQRT(49), SQRT(5) FROM dual; SQRT(64) SQRT(49) SQRT(5) ---------- ---------- ---------- 8 7 2.23606798 95127c02.indd 88 2/18/09 6:46:41 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Using Single-Row Numeric Functions 89 TAN TAN(n) takes a single argument, where n is a numeric datatype in radians. This function returns the tangent of n , accurate to 36 digits of precision. SELECT TAN(1.57079633/2) “45_degrees” FROM dual; 45_Degrees ---------- 1 TANH TANH(n) takes a single argument, where n is a numeric datatype. This function returns the hyperbolic tangent of n , accurate to 36 digits of precision. SELECT TANH( ACOS(-1) ) hyp_tan_of_pi FROM dual; HYP_TAN_OF_PI ------------- .996272076 TRUNC TRUNC(n1 [,n2]) takes two arguments, where n1 is a numeric datatype and n2 is an integer. This function returns n1 truncated to n2 digits of precision to the right of the decimal. If n2 is negative, n1 is truncated to the left of the decimal. See also ROUND . SELECT TRUNC(123.489), TRUNC(123.489, 2), TRUNC(123.489, -2), TRUNC(1275, -2) FROM dual; TRUNC(123.489) TRUNC(123.489,2) TRUNC(123.489,-2) TRUNC(1275,-2) -------------- ---------------- ----------------- -------------- 123 123.48 100 1200 WIDTH_BUCKET You can use WIDTH_BUCKET(n1, min_val, max_val, buckets) to build histograms of equal width. The first argument n1 can be an expression of a numeric or datetime datatype. The second and third arguments, min_val and max_val , indicate the end points for the histo- gram’s range. The fourth argument, buckets , indicates the number of buckets. 95127c02.indd 89 2/18/09 6:46:41 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 90 Chapter 2 N Using Single-Row Functions The following example divides the salary into a 10-bucket histogram within the range 2,500 to 11,000. If the salary falls below 2500, it will be in the underflow bucket (bucket 0), and if the salary exceeds 11,000, it will be in the overflow bucket ( buckets + 1). SELECT first_name, salary, WIDTH_BUCKET(salary, 2500, 11000, 10) hist FROM employees WHERE first_name like ‘J%’; FIRST_NAME SALARY HIST -------------------- ---------- ---------- Jennifer 4400 3 John 8200 7 Jose Manuel 7800 7 Julia 3200 1 James 2400 0 James 2500 1 Jason 3300 1 John 2700 1 Joshua 2500 1 John 14000 11 Janette 10000 9 Jonathon 8600 8 Jack 8400 7 Jean 3100 1 Julia 3400 2 Jennifer 3600 2 Using Single-Row Date Functions Single-row date functions operate on datetime datatypes. A datetime is a coined word to identify datatypes used to define dates and times. The datetime datatypes in Oracle 11g are DATE, TIMESTAMP, and INTERVAL. Most have one or more date arguments, and most return a datetime value. Date data is stored internally as numbers. The whole-number por- tion is the number of days since January 1, 4712 BC, and the decimal portion is the frac- tion of a day (for example, 0.5=12 hours). 95127c02.indd 90 2/18/09 6:46:41 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... “Introducing Oracle 11g Components and Architecture,” for more information Oracle provides the ORA_ROWSCN pseudocolumn to identify the SCN when the block containing the row was last modified Using the ORA_ROWSCN pseudocolumn, you can identify the approximate time when the row was last modified I say approximate because the SCN is associated with a block, and all the rows in the block will have the same SCN associated... value to a character or a character value to a numeric or datetime value Character datatypes in Oracle 11g are CHAR, VARCHAR2, NCHAR, NVARCHAR2, and CLOB Numeric datatypes in Oracle 102  Chapter 2    Using Single-Row Functions n 11g are NUMBER, BINARY_DOUBLE, and BINARY_FLOAT Datetime datatypes in Oracle 11g are DATE, TIMESTAMP, and INTERVAL Datatype conversion are required and used extensively in day-to-day... 31-Mar-2008 12:23:43 31-MAR-08 12.23.43.305000 PM US/EASTERN DBTIMEZONE DBTIMEZONE returns the database s time zone, as set by the latest CREATE DATABASE or ALTER DATABASE SET TIME_ZONE statement Note that after changing the database time zone with the ALTER DATABASE statement, the database must be bounced (restarted) for the change to take effect The time zone is a character string specifying... system change number (SCN) in the database This function returns the timestamp associated with the SCN The return datatype is TIMESTAMP SELECT SCN_TO_TIMESTAMP(8569432113130) UPD_TIME from dual; UPD_TIME -25-MAR-08 12.16.49.000000000 PM An SCN is a number that gets incremented when a commit occurs in the database The SCN identifies the state of the database uniquely, is recorded... alphabetical order, with descriptions and examples of each one Oracle 11g includes functions to convert from one datatype to another datatype Most of the functions have only one argument Many functions used to convert 104  Chapter 2    Using Single-Row Functions n to/from numeric or datetime datatypes have three arguments; the second argument will tell Oracle what format the input given in the first argument... last_name FROM employees WHERE rowid = CHARTOROWID(‘AAARAgAAFAAAABYAA9’); FIRST_NAME LAST_NAME Sarath Sewall Each row in the database is uniquely identified by a ROWID ROWID shows the physical location of the row stored in the database The pseudocolumn ROWID shows the address of the row COMPOSE COMPOSE(c) takes a single argument, where c is a character string This function returns c... Date Functions  91 Date-Format Conversion National-language support (NLS) parameters and arguments allow you to internationalize your Oracle database system NLS internationalizations include date representations, character sets, alphabets, and alphabetical ordering Oracle will implicitly or automatically convert its numeric date data to and from character data using the format model specified with... one of the most commonly used Oracle functions There’s a good chance you’ll see it on the exam Since the SYSDATE value is returned based on the time of the host server where the database resides, the result will be the same for a user sitting in New York or one in Hong Kong SYSTIMESTAMP SYSTIMESTAMP takes no arguments and returns a TIMESTAMP WITH TIME ZONE for the current database date and time (the... time zone This function is similar to SYSDATE, whereas SYSDATE returns the current date for the database s (host’s) time zone You can set the client time zone using the ALTER SESSION SET TIME_ZONE command The following example illustrates CURRENT_DATE and how to change the time zone for the session The database is in U.S./Central time zone, and the client is in U.S./Mountain time zone ALTER SESSION... mean time) or a time zone region name The valid time zone region names can be found in the TZNAME column of the view V$TIMEZONE_NAMES The default time zone for the database is UTC (00:00) if you do not explicitly set the time zone during database creation SQL> SELECT DBTIMEZONE FROM dual; DBTIME -+00:00 EXTRACT EXTRACT(c FROM dt) extracts and returns the specified component c of date/time or interval . returns the database s time zone, as set by the latest CREATE DATABASE or ALTER DATABASE SET TIME_ZONE statement. Note that after changing the database time. internationalize your Oracle database system. NLS internationalizations include date representations, char- acter sets, alphabets, and alphabetical ordering. Oracle will

Ngày đăng: 15/12/2013, 05: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