1. Trang chủ
  2. » Công Nghệ Thông Tin

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

50 474 0
Tài liệu đã được kiểm tra trùng lặp

Đ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

Thông tin cơ bản

Tiêu đề Tài Liệu Oracle Pl/Sql Language Pocket Reference- P16 Ppt
Thể loại Tài liệu
Định dạng
Số trang 50
Dung lượng 183,7 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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.. Note that whenever a date format returns a spelled va

Trang 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

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 (-)

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

SYEAR or YEAR or SYear or

Year

The year spelled out The S prefix places a negative sign in front

of B.C dates

Trang 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

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

Trang 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

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

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

Trang 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

Trang 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

Trang 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

Trang 7

Next: 14.2 Conversion Function Descriptions

Trang 8

Previous: 14.1 Conversion

Formats

Chapter 14Conversion 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:

Trang 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:

Trang 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:

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

Trang 11

● 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'

● 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:

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 ]]

Trang 12

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=<language>'

where <language> 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:

● Convert the string `123188' to a date:

TO_DATE ('123188', 'MMDDYY') ==> 31-DEC-1988

● Convert a date using the Spanish language:

TO_DATE ('Abril 12 1991', 'Month DD YYYY', 'NLS_DATE_LANGUAGE=Spanish')

Trang 13

For example, the format mask YYYY-YYY-DD-MM is illegal because it includes two year elements, YYYY and YYY

● 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:

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'''

Trang 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

Formats

Oracle PL/SQL Programming, 2nd Edition

Next: 14.3 Conversion Function Examples14.1 Conversion Formats Book Index 14.3 Conversion Function

Trang 15

Previous: 14.2 Conversion

Function Descriptions

Chapter 14Conversion Functions

Next: IV Modular Code

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:

Trang 16

TO_CHAR (SYSDATE, 'fmMonth FMDD, YYYY') ==> April 05,

left-Here are some examples of the impact of FM on numbers converted with TO_CHAR:

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 ('1-1-4', 'DD-MM-YYYY') ==> 01-JAN-0004TO_DATE ('7/16/94', 'MM/DD/YY') ==> 14-JUL-1994

Trang 17

● 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

Trang 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:

Trang 19

If the current year is in the first half of the century (years through 49) then:

● If you enter a date in the first half of the century, RR returns the current century

● On the other hand, if you enter a date in the latter half of the century, RR returns the previous century

● If the current year is in the latter half of the century (years 50 through 99) then:

❍ If you enter a date in the first half of the century, RR returns the next century

❍ 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'),

SELECT TO_CHAR (SYSDATE, 'MM/DD/YYYY') "Current Date",

TO_CHAR (TO_DATE ('10/14/88', 'MM/DD/RR'), 'YYYY')

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:

Trang 20

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,

Trang 21

check_time_in IN VARCHAR2 := 'NOTIME')

Flag indicating whether or not to include the time component of the dates in the range check

If TIME then use the time component of the dates as part of the comparison If NOTIME then strip off the time

If the start date is NULL or the end date is NULL, the string returned by date_range uses the <= and

>= operators rather than the BETWEEN operator

Here are some examples of the output from date_range Note that date_range places two contiguous quote marks around each date because these are actually string literals At runtime these two quotes are resolved into a single quote by PL/SQL:

date_range ('04-DEC-93', '08-APR-96')

If you do want to include the time component, then date_range will generate a string in this form:

date_range ('04-DEC-93', '08-APR-96', 'time')

==>

BETWEEN TO_DATE (''04-DEC-93'', ''HHMMYYYY HHMISS'')

AND

Trang 22

TO_DATE (''08-APR-96'', ''HHMMYYYY HHMISS'')

Let's take a look at how you might use date_range in query processing in Oracle Forms The Query trigger modifies the Default Where clause of a base table block In Pre-Query, an assignment actually results in the addition of a WHERE clause in the SQL query for that block Let's look at a simple example first If Pre-Query contains a statement like this:

Pre-:customer.contact_date := '12-JAN-95';

then the query executed by the forms tool to fill the block contains a WHERE clause that looks like this:

WHERE <other clauses> AND (contact_date = '12-JAN-95')

So a simple assignment results in a straightforward comparison/restriction in the WHERE clause You can also place complex SQL statements in the WHERE clause by appending a pound sign (#) at the beginning of the assignment string When the forms tools detect the #, they know to substitute completely the equals sign (=) comparison with the text following the symbol So if you place a statement like this in Pre-Query:

WHERE <other-where-clauses> AND

customer_id IN (SELECT customer_id

FROM invoice WHERE invoice_total

> 1000)

This very useful feature is documented in the Advanced Oracle Forms Techniques manual from

Oracle Corporation The syntax of a # assignment in Pre-Query for a date range would be like this:

:employee.hire_date :=

'# BETWEEN ' || TO_CHAR (:criteria.start_date) || '

AND ' ||

TO_CHAR (:criteria.end_date);

If I were to place literal dates inside this string assignment, then I would need to put two single

quotes together in the string wherever I needed one single quote to appear in the actual value placed

in that field, as follows:

Trang 23

:employee.hire_date := '# BETWEEN JAN-93'' AND

In the first call to date_range, I rely on the default value of the check_time_in parameter of NOTIME

to ignore the time component of the dates In the second call, I explicitly request that the time

component be included

The SET_BLOCK_PROPERTY built-in in Oracle Forms offers another method of modifying the DEFAULT WHERE clause of a base table block It allows you to directly pass a string of SQL

syntax, which then replaces the DEFAULT WHERE clause specified at design time in the form It is

a much more structured approach than using the # syntax In the following two calls to the built-in, I call date_range to generate a date range and attach that date range syntax to the contact_date column

In the second example I apply TRUNC to the contact date so that the time at which the contact_date was entered does not become a factor in the range check:

Here is the code for the date_range function:

/* Filename on companion disk: daternge.sf */

Trang 24

/* Date mask for date<->character conversions */

mask_int VARCHAR2(15) := 'MMDDYYYY';

/* Version of date mask which fits right into date

range string */

mask_string VARCHAR2(30) := NULL;

/* The return value for the function */

return_value VARCHAR2(1000) := NULL;

|| Convert mask Example:

|| If mask is: MMDDYYYY HHMISS

|| then mask string is: ', 'MMDDYYYY HHMISS')

/* If both start and end are NULL, then return NULL */

IF start_date_int IS NULL AND end_date_int IS NULL

Trang 25

THEN

return_value := NULL;

/* If no start point then return "<=" format */

ELSIF start_date_int IS NULL

THEN

return_value := '<= TO_DATE (''' || end_date_int ||

mask_string;

/* If no end point then return ">=" format */

ELSIF end_date_int IS NULL

14.3.5 Building a Date Manager

The Oracle Server offers the ability to set a default date format for each instance of a database with the NLS_DATE_FORMAT initialization parameter.[2] Oracle provides a ruthlessly efficient

gatekeeper for its RDBMS: there is no way you will ever be able to enter an invalid date into the database And there are lots of functions that enable you to perform arithmetic on dates once they are

in the database There are, however, obstacles to entering dates efficiently:

[2] This is a big improvement over earlier versions, in which the 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)

● Oracle tools do not make it easy for users to enter dates Oracle Forms, for example, insists that when a user enters a date, it must conform to a single date format mask either the

Ngày đăng: 15/12/2013, 04:15

TỪ KHÓA LIÊN QUAN