Tài liệu Oracle PL/SQL Language Pocket Reference- P16 ppt

50 466 0
Tài liệu Oracle PL/SQL Language Pocket Reference- P16 ppt

Đ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

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 Year The year spelled out. The S prefix places a negative sign in front of B.C. dates. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 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. 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. 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. 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. 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. S9,999,999 00009MI 999D99 9G999G999 L999.99 Previous: 13.3 Miscellaneous Function Descriptions Oracle PL/SQL Programming, 2nd Edition Next: 14.2 Conversion Function Descriptions 13.3 Miscellaneous Function Descriptions Book Index 14.2 Conversion Function 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. Previous: 14.1 Conversion Formats Chapter 14 Conversion Functions Next: 14.3 Conversion 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. 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. 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: ● 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' ● Use the FM fill mode element to suppress blanks and zeros: TO_CHAR (SYSDATE, 'FMMonth DD, YYYY') ==> 'February 5, 1994' ● 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. [...]... 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... accessing date masks in a PL/SQL table You can also store the date masks in a PL/SQL table (PL/SQL tables are explained fully in Chapter 10, PL/SQL Tables) The PL/SQL table structure, available only with PL/SQL Version 2, is similar to both a database table and a single-dimensional array There are two steps to using date masks from the PL/SQL table: Initialize the date masks in the PL/SQL table structure,... tool-specific memory structures A PL/SQL table in PL/SQL Version 2 Oracle' s version of a simple array structure is perfect for this application; unfortunately, PL/SQL tables are available only in the RDBMS (through stored procedures and triggers) and SQL*Plus scripts I provide the code for PL/SQL table-based alternatives in the following sections The technique based on Oracle Forms GLOBAL variables may... 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... table structure, and revamp dm_convert to rely on the PL/SQL table These are described below: 1 Initialize the date masks in the PL/SQL table structure The best way to do this in PL/SQL tables is to build a package (see Chapter 16, Packages, for more information about this structure) This package contains the code to initialize the masks in the PL/SQL table by transferring them from the database table,... will parse the entire parameter and leave behind a single quote around each value Previous: 14.1 Conversion Formats 14.1 Conversion Formats Oracle PL/SQL Programming, 2nd Edition Book Index Next: 14.3 Conversion Function Examples 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... default date format was hardcoded to be DD-MON-YY In the Oracle RDBMS Version 6, for example, if your company happened to use something else, you would have to make extensive use of the TO_DATE and TO_CHAR functions (and set the mask on all date fields in your forms applications, as well) q Oracle tools do not make it easy for users to enter dates Oracle Forms, for example, insists that when a user enters... figure out the rest It is possible with PL/SQL to build a "date manager" that satisfies the above moral imperatives for our users The rest of this section explores the implementation of a function, dm_convert, which converts a string entered by the user into an actual Oracle date value It liberates the user from having to know the default/enforced format in the Oracle7 database, because the input can... raising those nasty Oracle errors relating to invalid date formats when he tries to convert the string to a date The answer is that I use both the procedurality and the exception handling of PL/SQL In native SQL, I can use TO_DATE to convert a string to a date, but if the format doesn't match the string, I will get an error and the SQL statement will fail In fact, this is exactly what Oracle Forms does... 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 . string. The format of nls _language is as follows: 'NLS_DATE _LANGUAGE= < ;language& gt;' where < ;language& gt; is a language recognized by your. 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

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