Oracle PLSQL Language- P16

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

0
44
lượt xem
4
download

Oracle PLSQL Language- P16

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

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ủ đề:
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.
  10. The TO_CHAR function can be used to convert both dates and numbers to a variable-length string. The following specification describes TO_CHAR for dates: FUNCTION TO_CHAR (date_in IN DATE [, format_mask IN VARCHAR2 [, nls_language IN VARCHAR2]]) RETURN VARCHAR2 where date_in is the date to be converted to character format, the format_mask is the mask made up of one or more of the date format elements, and nls_language is a string specifying a date language. Both the format mask and the NLS language parameters are optional. If the format mask is not specified, then the default date format for the database instance is used. This format is DD-MON-YY, unless the initialization parameter NLS_DATE_FORMAT is included in the initialization file. The format of the specification of an alternative date mask is: NLS_DATE_FORMAT = 'MM/DD/YYYY' If the NLS language parameter is not specified, then the default date language for the instance is used. This is either the language for the instance specified by the NLS_LANGUAGE parameter, or the date language specified in the initialization file with the parameter NLS_DATE_LANGUAGE. Note that if you want to specify a date language, you also must include a format mask. You cannot skip over the intervening parameters. Here are some examples of TO_CHAR for date conversion: q Notice that there are two blanks between month and day and a leading zero for the fifth day: TO_CHAR (SYSDATE, 'Month DD, YYYY') ==> 'February 05, 1994' q Use the FM fill mode element to suppress blanks and zeros: TO_CHAR (SYSDATE, 'FMMonth DD, YYYY') ==> 'February 5, 1994' q Note the case difference on the month abbreviations of the next two samples. You get exactly what you ask for with Oracle date formats! TO_CHAR (SYSDATE, 'MON DDth, YYYY') ==> 'FEB 05th, 1994' TO_CHAR (SYSDATE, 'fmMon DDth, YYYY') ==> 'Feb 5th, 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.
  12. ) RETURN DATE; FUNCTION TO_DATE (number_in IN NUMBER [, format_mask IN VARCHAR2 [, nls_language IN VARCHAR2 ]]) RETURN DATE; The second version of TO_DATE can be used only with the format mask of J for Julian date. The Julian date is the number of days which have passed since January 1, 4712 B.C. Only in this use of TO_DATE can a number be passed as the first parameter of TO_DATE. For all other cases, string_in is the string variable, literal, named constant, or expression to be converted, format_mask is the format mask TO_DATE will use to convert the string, and nls_language is a string which specifies the language which is to be used to interpret the names and abbreviations of both months and days in the string. The format of nls_language is as follows: 'NLS_DATE_LANGUAGE=' where is a language recognized by your instance of the database. You can usually determine the acceptable languages by checking your installation guide. Here are some examples of the TO_DATE function: q Convert the string `123188' to a date: TO_DATE ('123188', 'MMDDYY') ==> 31-DEC-1988 q Convert a date using the Spanish language: TO_DATE ('Abril 12 1991', 'Month DD YYYY', 'NLS_DATE_LANGUAGE=Spanish') ==> 12-APR-1991 Any Oracle errors between ORA-01800 and ORA-01899 are related to the internal Oracle date function and can arise when you encounter date conversion errors. You can learn additional nuances of date conversion rules by perusing the different errors and reading about the documented causes of these errors. Some of these rules are: q A date literal passed to TO_CHAR for conversion to a date cannot be longer than 220 characters. q You cannot include both a Julian date element (J) and the day of year element (DDD) in a single format mask. q You cannot include multiple elements for the same component of the date/time in the mask. 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.
  17. q Punctuation and literals in the string to be converted can simply match the length and position of punctuation and quoted text in the format. In the following example, my format mask specifies hyphen (-) delimiters and the text "WhatIsaynotdo" between the day and the year. The string that I pass it, on the other hand, uses caret (^) delimiters and "the year of" for the embedded text. Because both of the two literals inside quotes have the same number of characters, TO_DATE has no problem making the match. TO_DATE ('JANUARY^1^ the year of 94', 'Month- dd-"WhatIsaynotdo"yy') ==> 01-JAN-1994 This kind of flexibility is great -- until you want to actually restrict a user or even a batch process from entering data in a nonstandard format. In some cases, it simply is not a reflection of everything being OK when a date string has a pound sign (#) instead of a hyphen (-) between the day and month numbers. For these situations, you can use the FX modifier to enforce an exact match between string and format model. With FX, there is no flexibility for interpretation of the string. It cannot have extra blanks if none are found in the model. Its numeric values must include leading zeros if the format model specifies additional digits. And the punctuation and literals must exactly match the punctuation and quoted text of the format mask (except for case, which is always ignored). In all of the following examples, PL/ SQL raises one of the following errors: ORA-01861: literal does not match format string ORA-01862: wrong number of digits for this format item TO_DATE ('Jan 15 1994', 'fxMON DD YYYY') TO_DATE ('1-1-4', 'fxDD-MM-YYYY') TO_DATE ('7/16/94', 'FXMM/DD/YY') TO_DATE ('JANUARY^1^ the year of 94', 'FXMonth- dd-"WhatIsaynotdo"yy') The FX modifier can be specified in upper-, lower-, or mixed-case; the effect is the same. The FX 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 FX is not specified anywhere in a format mask), an exact match is not required in any part of the string (as described above). So the first time that FX appears in the format it turns on exact matching for any following elements. The second time that FX appears in the format it indicates that an exact match is not required for any following elements, and so on. In the following example I specify FX three times. As a result, an exact match is required for the day number and the year number, but not the month number: TO_DATE ('07-1-1994', 'FXDD-FXMM-FXYYYY') ==> 07-JUL-1994 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  18. This next attempt at date conversion will raise ORA-01862 because the year number is not fully specified: TO_DATE ('07-1-94', 'FXDD-FXMM-FXYYYY') -- Invalid string for format! You saw in the previous section how the FM modifier would strip leading blanks and zeros from the output of a call to TO_CHAR. You can also use FM in the format model of a call to the TO_DATE function to fill a string with blanks or zeros. This action matches the format model (the opposite of the suppression action). You can, in other words, use FM to guarantee that a format exact match required by FX will succeed. The following call to TO_DATE will return a date because the fm at the beginning of the format mask turns on fill mode for the entire string, thus changing the 1 to 01 and 94 to 1994: TO_DATE ('07-1-94', 'FXfmDD-FXMM-FXYYYY') You can also include multiple references to both FM and FX in the same format string, to toggle both or either of these modifiers. 14.3.3 RR: Changing Millenia We are coming up fast on the end of the 20th century. How many of your programs will still work when the clock ticks over midnight on December 31, 1999? Many of your Oracle-based applications should be well protected since you have been able to take advantage of a true date datatype. In other words, you haven't had to write any special programs to manually manipulate dates, thereby leaving yourself vulnerable. On the other hand, most everyone has been using a two-digit year in their date format masks, either inherited from the default DD-MON-YY or with common substitutes like MM/ DD/YY. The two-digit year format elements could give you problems when the century and millenium are close to changing. The YY format element always defaults to the current century. So when it is November 1999 and your user enters 1/1/1 or 1-JAN-1, they will enter into the database the date of January 1, 1901 -- not January 1, 2001, as they might have been thinking. What's an IS manager to do? One solution is to go into all your screens and change or add trigger logic so that if the user enters a year number less than ten (or whatever you decide the cutoff to be), then the next century will be assumed. That will work, but it surely must be a most undesirable prospect. Fortunately, Oracle7 provides a new format element to take care of this problem: the RR format model. With RR you can enter dates from the 21st century before the year 2000 and you can enter dates from the 20th century after the year 2000 (like the birthdays of employees and customers). Here is how RR works: Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  19. If the current year is in the first half of the century (years through 49) then: q If you enter a date in the first half of the century, RR returns the current century. q On the other hand, if you enter a date in the latter half of the century, RR returns the previous century. q If the current year is in the latter half of the century (years 50 through 99) then: r If you enter a date in the first half of the century, RR returns the next century. r If you enter a date in the latter half of the century, RR returns the current century. Here are some examples of the impact of RR. Notice that the same year numbers are returned for Year 88 and Year 18, even though SYSDATE returns a current date in the 20th and 21st centuries, respectively: SELECT TO_CHAR (SYSDATE, 'MM/DD/YYYY') "Current Date", TO_CHAR (TO_DATE ('14-OCT-88', 'DD-MON-RR'), 'YYYY') "Year 88", TO_CHAR (TO_DATE ('14-OCT-18', 'DD-MON-RR'), 'YYYY') "Year 18" FROM dual; Current Date Year 88 Year 18 ------------ ------- ------- 11/14/1994 1988 2018 SELECT TO_CHAR (SYSDATE, 'MM/DD/YYYY') "Current Date", TO_CHAR (TO_DATE ('10/14/88', 'MM/DD/RR'), 'YYYY') "Year 88", TO_CHAR (TO_DATE ('10/14/18', 'MM/DD/RR'), 'YYYY') "Year 18" FROM dual; Current Date Year 88 Year 18 ------------ ------- ------- 11/14/2001 1988 2018 Of course, if you use the RR format after the year 2000 and want to enter a date that falls in the latter half of the 21st century, you will need to add special logic. Masks with the RR format model will always convert such two-digit years into the previous century. There are a number of ways you can activate the RR logic in your current applications. The cleanest and simplest way is to change the default format mask for dates in your database instance(s). You can do this by changing the NLS_DATE_FORMAT initialization parameter as follows: Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  20. NLS_DATE_FORMAT = 'MM/DD/RR' or: NLS_DATE_FORMAT = 'DD-MON-RR' depending on what the previous format was. Then, if you have not hardcoded the date format mask anywhere else in your screens or reports, you are done. Bring down and restart the database and then your application will allow users to enter dates in the 21st century. If you do have date format masks in the format property for an Oracle Forms item or in an Oracle Reports query or field, you will need to change those modules to reflect the new approach embodied by RR. 14.3.4 Using TO_CHAR to Create a Date Range At times, users want information about activity on a specific date. In other situations, however, their interest lies in a range of dates. The user might enter the two dates and then expect to view all data that falls between them. Suppose, for example, that in an Oracle Forms application the user enters 12/4/93 in the start date field and 4/8/96 in the end date field. The query that Oracle Forms executes against the database would need to have logic in it as follows: hire_date BETWEEN '04-DEC-93' AND '08-APR-96' or, more generally: hire_date BETWEEN :criteria.start_date AND :criteria. end_date where criteria is the name of the block containing the start_date and end_date fields. The colons (:) in front of the field names indicate to PL/SQL that these are bind variables from the host environment. Sometimes this general logic can be passed directly to the SQL layer. In other situations, programmers must use the Pre-Query trigger or the SET_BLOCK_PROPERTY built-in to alter the SQL statement directly. In this case, they will need to create a string date range from the input dates. Rather than write the application-specific code to handle this each time, you can build a generic utility, using TO_CHAR and TO_DATE conversion functions. I offer below the date_range function. Its specification is as follows: FUNCTION date_range (start_date_in IN DATE, end_date_in IN DATE, Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Đồng bộ tài khoản