# Oracle PLSQL Language- P16

Chia sẻ: Thanh Cong | Ngày: | Loại File: PDF | Số trang:50

0
49
lượt xem
4

## Oracle PLSQL Language- P16

Mô tả tài liệu

Tham khảo tài liệu 'oracle plsql language- p16', công nghệ thông tin, cơ sở dữ liệu phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Chủ đề:

Bình luận(0)

Lưu

## Nội dung Text: Oracle PLSQL Language- P16

1. The default date format is also set implicitly with another initialization parameter, NLS_TERRITORY. When you specify an NLS_TERRITORY value, you set conventions for date format, date language, numeric formats, currency symbols, and week start day. Even with this flexibility, the database still supports only a single default date format in a given instance. Both developers and users must be aware of this format when working with dates. Later sections of this chapter explore approaches in PL/SQL that give the user much more flexibility when entering dates in their applications. As you can see, format masks (such as MMDDYY and Month DD, YYYY) play an important role in the conversion of date and character data. Table 14.2 provides the full set of date format masks and explains how to use them in all their variations. You can use the format elements in any combination, in any order. You can even use the same format element more than once in your format mask. Following the table are examples showing these variations. Table 14.2: Date Format Model Elements Mask Description SCC or CC The century. If the SCC format is used, any B.C. dates are prefaced with a hyphen (-). SYYYY or YYYY The four-digit year. If the SYYYY format is used, any B.C. dates are prefaced with a hyphen (-). IYYY The four-digit ISO standard year. YYY or YY or Y The last three, two, or one digits of the year. The current century is the default. IYY or IY or I The last three, two, or one digits of the ISO standard year. The current century is the default. Y,YYY The four-digit year with a comma. SYEAR or YEAR or SYear or The year spelled out. The S prefix places a negative sign in front Year of B.C. dates. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
2. RR The last two digits of the year. This format is used to display years in centuries other than our own. See Section 14.3.3, "RR: Changing Millenia". BC or AD The B.C. or A.D. indicator, without periods. B.C. or A.D. The B.C. or A.D. indicator, with periods. Q The quarter of the year, from 1 through 4. January through March are in the first quarter, April through June in second quarter, etc. MM The number of the month in the year, from 01 through 12. January is month number 01, September is 09, etc. RM The Roman numeral representation of the month number, from I through XII. January is I, September is IX, etc. MONTH or Month The name of the month, either in upper- or mixed-case format. MON or Mon The abbreviated name of the month, as in JAN for January. WW The week in the year, from 1 through 53. IW The week in the year, from 1 through 52 or 1 through 53, based on the ISO standard. W The week in the month, from 1 through 5. Week 1 starts on the first day of the month and ends on the seventh. DDD The day in the year, from 1 through 366. DD The day in the month, from 1 through 31. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
3. D The day in the week, from 1 through 7. The day of the week that is decreed the first day is specified implicitly by the NLS_TERRITORY initialization parameter for the database instance. DAY or Day The name of the day in upper- or mixed-case format. DY The abbreviated name of the day, as in TUE for Tuesday. J The Julian day format of the date (counted as the number of days since January 1, 4712 B.C., the earliest date supported by the Oracle RDBMS). AM or PM The meridian indicator (morning or evening) without periods. A.M. or P.M. The meridian indicator (morning or evening) with periods. HH or HH12 The hour in the day, from 1 through 12. HH24 The hour in the day, from 0 through 23. MI The minutes component of the date's time, from 0 through 59. SS The seconds component of the date's time, from 0 through 59. SSSSS The number of seconds since midnight of the time component. Values range from 1 through 86399, with each hour comprising 3600 seconds. TH Suffix which converts a number to its ordinal format; for example, 4 becomes 4th and 1 becomes 1st. This element can appear only at the end of the entire format mask. The return value is always in English, regardless of the date language. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
4. SP Suffix which converts a number to its spelled format; for example, 4 becomes FOUR, 1 becomes ONE, and 221 becomes TWO HUNDRED TWENTY-ONE. This element can appear only at the end of the entire format mask. The return value is always in English, regardless of the date language. SPTH Suffix which converts a number to its spelled and ordinal format; for example, 4 becomes FOURTH and 1 becomes FIRST. This element can appear only at the end of the entire format mask. The return value is always in English, regardless of the date language. FX Element which requires exact pattern matching between data and format model. (FX stands for Format eXact.) FM Element which toggles suppression of blanks in output from conversion. (FM stands for Fill Mode.) Other text Any punctuation, such as a comma (,) or slash (/) or hyphen (-), will be reproduced in the formatted output of the conversion. You can also include text within double quotes (") and this text will then be represented as entered in the converted value. See examples in TO_CHAR for an illustration of this element. Note that whenever a date format returns a spelled value (words rather than numbers, as with MONTH, MON, DAY, DY, AM, and PM), the language used to spell these words is determined by the National Language Support parameters, NLS_DATE_LANGUAGE and NLS_LANGUAGE, or by the optional date language argument you can pass to both TO_CHAR and TO_DATE. Here are some examples of date format masks composed of the above format elements: 'Month DD, YYYY' 'MM/DD/YY Day A.M.' 'Year Month Day HH24:MI:SS' 'J' 'SSSSS-YYYY-MM-DD' '"A beautiful summer morning on the" DDth" day of "Month' See the description of the TO_CHAR and TO_DATE functions for more examples of the use and resulting values of these masks. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
5. 14.1.2 Number Format Models The number formats are used in both TO_CHAR and TO_NUMBER. The number format in TO_CHAR translates a numeric value to a VARCHAR2 datatype. The number format in TO_NUMBER translates a VARCHAR2 value to a numeric datatype. A number format mask can comprise one or more elements from Table 14.3. The resulting format of the character string (or the converted numeric value) will reflect the combination of the format elements. You will find examples of different applications of the format models in the descriptions of both the TO_CHAR and TO_NUMBER functions. Format elements with a description starting with "Prefix:" can be used only at the beginning of the complete format mask. Format elements with a description starting with "Suffix:" can be used only at the end of the complete format mask. Table 14.3: Number Format Model Elements Format Elements Description 9 Each 9 represents a significant digit to be returned. Leading zeros in a number are displayed or treated as blanks. 0 Each represents a significant digit to be returned. Leading zeros in a number are displayed or treated as zeros. $Prefix: puts a dollar sign in front of the number. B Prefix: returns a zero value as blanks, even if the format element was used to show a leading zero. MI Suffix: places a minus sign (-) after the number if it is negative. For positive values it returns a trailing space, which is different from NULL. S Prefix: places a plus sign (+) in front of a positive number and a minus sign (-) before a negative number. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 6. PR Suffix: places angle brackets (< and >) around a negative value. For positive values it places leading and trailing spaces around the number. D Specifies the location of the decimal point in the returned value. All format elements to the left of the D will format the integer component of the value. All format elements to the right of the D will format the fractional part of the value. The character used for the decimal character is determined by the database initialization parameter NLS_NUMERIC_CHARACTERS. G Specifies the location of the group separator (for example, a comma to separate thousands as in 6,734) in the returned value. The character used for the group separator is determined by the database initialization parameter NLS_NUMERIC_CHARACTERS. C Specifies the location of the ISO currency symbol in the returned value. L Specifies the location of the local currency symbol (such as$) in the returned value. , (comma) Specifies that a comma be returned in that location in the return value. . (period) Specifies that a period be returned in that location in the return value. V Multiplies the number to the left of the V in the format model by 10 raised to the nth power, where n is the number of 9s found after the V in the format model. EEEE Suffix: specifies that the value be returned in scientific notation. RN or rn Specifies that the return value be converted to upper- or lowercase Roman numerals. The range of valid numbers for conversion to Roman numerals is between 1 and 3999. The value must be an integer. Here are some examples of numeric format masks built from these elements: 9.999EEEE 00V99 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
7. S9,999,999 00009MI 999D99 9G999G999 L999.99 Previous: 13.3 Oracle PL/SQL Next: 14.2 Conversion Miscellaneous Function Programming, 2nd Edition Function Descriptions Descriptions 13.3 Miscellaneous Function Book Index 14.2 Conversion Function Descriptions Descriptions The Oracle Library Navigation Copyright (c) 2000 O'Reilly & Associates. All rights reserved. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
8. Previous: 14.1 Conversion Chapter 14 Next: 14.3 Conversion Formats Conversion Functions Function Examples 14.2 Conversion Function Descriptions This section describes the various conversion functions provided by PL/SQL. 14.2.1 The CHARTOROWID function The CHARTOROWID function converts a string of either type CHAR or VARCHAR2 to a value of type ROWID. The specification of the CHARTOROWID function is: FUNCTION CHARTOROWID (string_in IN CHAR) RETURN ROWID FUNCTION CHARTOROWID (string_in IN VARCHAR2) RETURN ROWID In order for CHARTOROWID to successfully convert the string, it must be of the format: BBBBBBBB.RRRR.FFFF where BBBBBBBB is the number of the block in the database file, RRRR is the number of the row in the block, and FFFF is the number of the database file. All three numbers must be in hexadecimal format. If the input string does not conform to the above format, PL/SQL raises the VALUE_ERROR exception. 14.2.2 The CONVERT function The CONVERT function converts strings from one character set to another character set. The specification of the CONVERT function is: FUNCTION CONVERT (string_in IN VARCHAR2, new_char_set VARCHAR2 [, old_char_set VARCHAR2]) RETURN VARCHAR2 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
9. The old_char_set is an optional argument. If this third argument is not specified, then the default character set for the database instance is used. The CONVERT function does not translate words or phrases from one language to another! CONVERT simply substitutes the letter or symbol in one character set with the corresponding letter or symbol in another character set. (A character set is not the same thing as a human language.) Two commonly used character sets are US7ASCII (U.S. 7-bit ASCII character set) and F7DEC (DEC French 7-bit character set). 14.2.3 The HEXTORAW function The HEXTORAW function converts a hexadecimal string from type CHAR or VARCHAR2 to type RAW. The specification of the HEXTORAW function is: FUNCTION HEXTORAW (string_in IN CHAR) RETURN RAW FUNCTION HEXTORAW (string_in IN VARCHAR2) RETURN RAW 14.2.4 The RAWTOHEX function The RAWTOHEX function converts a value from type RAW to a hexadecimal string of type VARCHAR2. The specification of the RAWTOHEX function is: FUNCTION RAWTOHEX (binary_value_in IN RAW) RETURN VARCHAR2 RAWTOHEX always returns a variable-length string value, even if its mirror conversion function is overloaded to support both types of input. 14.2.5 The ROWIDTOCHAR function The ROWIDTOCHAR function converts a binary value of type ROWID to a string of type VARCHAR2. The specification of the ROWIDTOCHAR function is: FUNCTION ROWIDTOCHAR (row_in IN ROWID ) RETURN VARCHAR2 The string returned by this function has the format: BBBBBBBB.RRRR.FFFF where BBBBBBBB is the number of the block in the database file, RRRR is the number of the row in the block, and FFFF is the number of the database file. All three numbers are in hexadecimal format. 14.2.6 The TO_CHAR function (date conversion) Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
11. 1994' q Show the day of year, the month, and the week for the date: TO_CHAR (SYSDATE, 'DDD DD D ') ==> '036 05 7' TO_CHAR (SYSDATE, 'fmDDD DD D ') ==> '36 5 7' q Some fancy formatting for reporting purposes: TO_CHAR (SYSDATE, '"In month "RM" of year "YEAR') ==> 'In month II of year NINETEEN NINETY FOUR' 14.2.7 The TO_CHAR function (number conversion) The TO_CHAR function converts numbers as well as dates. The specification of the TO_CHAR (number) function is: FUNCTION TO_CHAR (number_in IN NUMBER [, format_mask IN VARCHAR2 [, nls_language IN VARCHAR2]]) RETURN VARCHAR2; where number_in is the number to be converted to character format, the format_mask is the mask made up of one of more of the number format elements, and nls_language is a string specifying one or more of the NLS parameters which affect the way numbers are displayed. Both the format mask and the NLS language parameters are optional. If the format mask is not specified, then the default number format for the database instance is used. Here are some examples of TO_CHAR for number conversion: TO_CHAR (564.70, '$999.9') ==>$564.7 TO_CHAR (564.70, '$0000999.9') ==>$0000564.7 14.2.8 The TO_DATE function The TO_DATE function converts a character string to a true DATE datatype. The specification of the TO_DATE function is overloaded for string and number input: FUNCTION TO_DATE (string_in IN VARCHAR2 [, format_mask IN VARCHAR2 [, nls_language IN VARCHAR2 ]] Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
13. For example, the format mask YYYY-YYY-DD-MM is illegal because it includes two year elements, YYYY and YYY. q You cannot use the 24-hour time format (HH24) and a meridian element (e.g., AM) in the same mask. 14.2.9 The TO_NUMBER function The TO_NUMBER function converts both fixed- and variable-length strings to numbers using the associated format mask. The specification of the TO_NUMBER function is as follows: FUNCTION TO_NUMBER (string_in IN CHAR [, format_mask VARCHAR2 [, nls_language VARCHAR2 ]]) RETURN NUMBER; FUNCTION TO_NUMBER (string_in IN VARCHAR2 [, format_mask VARCHAR2 [, nls_language VARCHAR2 ]]) RETURN NUMBER; where string_in is the string containing a sequence of characters to be converted to a number, format_mask is the optional string directing TO_NUMBER how to convert the character bytes to a number, and nls_language is a string containing up to three specifications of National Language Support parameters, as follows: NLS_NUMERIC_CHARACTERS The characters used to specify the decimal point and the group separator in a number. The decimal point character for the American language is a dot (.) while the group separator is a comma (,). NLS_CURRENCY The character(s) used to specify the local currency symbol. The currency character for the American language is a dollar sign ($). NLS_ISO_CURRENCY The character(s) used to specify the international currency symbol in the string. The format for nls_language in the call to TO_NUMBER is as follows: 'NLS_NUMERIC_CHARACTERS = ''string''' 'NLS_CURRENCY = ''string''' 'NLS_ISO_CURRENCY = ''string''' Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 14. Two contiguous single quotes are needed before and after the values for each string value so that PL/ SQL will parse the entire parameter and leave behind a single quote around each value. Previous: 14.1 Conversion Oracle PL/SQL Next: 14.3 Conversion Formats Programming, 2nd Edition Function Examples 14.1 Conversion Formats Book Index 14.3 Conversion Function Examples The Oracle Library Navigation Copyright (c) 2000 O'Reilly & Associates. All rights reserved. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 15. Previous: 14.2 Conversion Chapter 14 Next: IV. Modular Code Function Descriptions Conversion Functions 14.3 Conversion Function Examples This section shows how you can use the conversion functions we've described in actual PL/SQL examples. 14.3.1 FM: Suppressing Blanks and Zeros PL/SQL offers the FM element as a modifier to a format mask. FM (fill mode) controls the suppression of padded blanks and leading zeros in values returned by the TO_CHAR function. By default, the following format mask results in both padded blanks and leading zeros (there are five spaces between the month name and the day number): TO_CHAR (SYSDATE, 'Month DD, YYYY') ==> 'April 05, 1994' With the FM modifier at the beginning of the format mask, however, both the extra blank and the leading zeros disappear: TO_CHAR (SYSDATE, 'FMMonth DD, YYYY') ==> April 5, 1994' The modifier can be specified in upper-, lower-, or mixed-case; the effect is the same. The FM modifier is a toggle, and can appear more than once in a format model. Each time it appears in the format, it changes the effect of the modifier. By default (that is, if FM is not specified anywhere in a format mask), blanks are not suppressed and leading zeros are included in the result value. So the first time that FM appears in the format it indicates that blanks and leading zeros are suppressed for any following elements. The second time that FM appears in the format, it indicates that blanks and leading zeros are not suppressed for any following elements, and so on. In the following example I suppress the padded blank at the end of the month name, but preserve the leading zero on the day number with a second specification of FM: Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 16. TO_CHAR (SYSDATE, 'fmMonth FMDD, YYYY') ==> April 05, 1994' If you do not use FM in your mask, a converted date value is always right-padded with blanks to a fixed length (that length is dependent on the different format elements you use). When you do use FM, on the other hand, the length of your return value may vary depending on the actual values returned by the different format elements. When you do not use FM to convert a number to a character string, the resulting value is always left- padded with blanks so that the number is right-justified to the length specified by the format (or declaration of the variable). When you do use FM, the left-padded blanks are suppressed and the resulting value is left-justified. Here are some examples of the impact of FM on numbers converted with TO_CHAR: TO_CHAR (8889.77, 'L9999D99') ==> '$8889.77' TO_CHAR (8889.77, 'fmL9999D99') ==> '\$8889.77' The FM modifier can also be used in the format model of a call to the TO_DATE function to fill a string with blanks or zeros to match the format model. This variation of FM is explored in the discussion of FX. 14.3.2 FX: Matching Formats Exactly PL/SQL offers the FX element as a modifier to a format mask. FX (format exact) specifies that an exact match must be performed for a character argument and date format mask in a call to the TO_DATE function. If FX is not specified, the TO_DATE function does not require that the character string match the format precisely. It makes the following allowances: q Extra blanks in the character string are ignored. Blanks are not significant data in any of the parts of a date value, except to delimit separate parts of the date and time: TO_DATE ('Jan 15 1994', 'MON DD YYYY') ==> 15-JAN-1994 q Numeric values, such as the day number or the year, do not have to include leading zeros to fill out the mask. As long as the numbers are in the right place in the string (as determined, usually, by the delimiter characters in the string), TO_DATE can convert the numeric values properly: TO_DATE ('1-1-4', 'DD-MM-YYYY') ==> 01-JAN-0004 TO_DATE ('7/16/94', 'MM/DD/YY') ==> 14-JUL-1994 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.