Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming PL SQL.doc
Скачиваний:
2
Добавлен:
01.07.2025
Размер:
5.06 Mб
Скачать

10.4.4 The from_tz Function

The FROM_TZ function adds time zone information to aTIMESTAMP value, thus converting the TIMESTAMP value into a TIMESTAMP WITH TIME ZONE value. The specification is:

FUNCTION FROM_TZ (timestamp_value IN TIMESTAMP,

time_zone IN VARCHAR2)

You can specify the time zone using either an offset from UTC or a time zone region name. The result returned by the function is the same date and time passed as input, but with the addition of the time zone information that you specify.

Following are two examples of using FROM_TZ:

DECLARE

w TIMESTAMP := CAST('24-Jun-2002 3.41.00.00 PM' AS TIMESTAMP);

x TIMESTAMP WITH TIME ZONE;

y TIMESTAMP WITH TIME ZONE;

BEGIN

x := FROM_TZ(w,'-5:00');

y := FROM_TZ(w,'US/Eastern');

DBMS_OUTPUT.PUT_LINE(x);

DBMS_OUTPUT.PUT_LINE(y);

END;

The output is:

24-JUN-02 03.41.00.000000 PM -05:00

24-JUN-02 03.41.00.000000 PM US/EASTERN

You can see that the date and time fields remain unchanged from those passed as input to FROM_TZ. The only difference is that the time zone information has been tacked onto the original TIMESTAMP values, making them into TIMESTAMP WITH TIME ZONE values.

10.4.5 The last_day Function

The LAST_DAY function returns the date of the last day of the month for a given date. The specification is:

FUNCTION LAST_DAY (date_in IN DATE) RETURN DATE

This function is useful because the number of days in a month varies throughout the year. With LAST_DAY, for example, you do not have to try to figure out if February of this or that year has 28 or 29 days. Just let LAST_DAY figure it out for you.

Here are some examples of LAST_DAY in action:

BEGIN

--Go to the last day in the month:

DBMS_OUTPUT.PUT_LINE(LAST_DAY ('12-JAN-99'));

--If already on the last day, just stay on that day:

DBMS_OUTPUT.PUT_LINE(LAST_DAY ('31-JAN-99'));

--Get the last day of the month three months ago:

DBMS_OUTPUT.PUT_LINE(LAST_DAY (ADD_MONTHS (SYSDATE, 3)));

--Tell me the number of days until the end of the current month:

DBMS_OUTPUT.PUT_LINE(LAST_DAY (SYSDATE) - SYSDATE);

END;

The output is:

31-JAN-0099

31-JAN-0099

31-MAY-2002

4

Getting the First Day of the Month

Oracle has a LAST_DAY function, so why not a FIRST_DAY? Believe it or not, such a function would have its use. We've written quite a few queries where we've had to determine both the first and last day of the month for any arbitrary date a user enters. A convenient incantation to return the first day of the month for any date is:

ADD_MONTHS(LAST_DAY(x),-1)+1

This discussion depends on the behavior of ADD_MONTHS with respect to month-end dates. First, we use LAST_DAY to compute the last day of the current month. Then we add -1 months (using ADD_MONTHS), which is guaranteed to get us the last day of the previous month. Finally, we add 1 to advance the day to the first of the current month.

While this approach may seem a tad convoluted, it avoids the need for string manipulation and preserves any time-of-day component that is part of the input value.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]