6-4. Converting a Date to a String

Problem

You want to convert a date into a nicely formatted string value. For example, you are converting a legacy application from another database vendor into a web-based Oracle application. A few of the fields on the web form are dates. The users of the application expect to see the dates in a specific format, so you need the dates to be formatted in a particular manner for display.

Solution

Use the TO_CHAR function using the date masks. The TO_CHAR function offers many formatting options for returning a string from a DATE value. The following function accepts an EMPLOYEE_ID value and returns a representation of the HIRE_DATE spelled out.

CREATE OR REPLACE PROCEDURE obtain_emp_hire_date(emp_id IN NUMBER)
 AS
 emp_hire_date    employees.hire_date%TYPE;
 emp_first        employees.first_name%TYPE;
 emp_last         employees.last_name%TYPE;
BEGIN
  SELECT hire_date, first_name, last_name
  INTO emp_hire_date, emp_first, emp_last
  FROM employees
  WHERE employee_id = emp_id;

  DBMS_OUTPUT.PUT_LINE(emp_first || ' ' || emp_last ||
        ' was hired on: ' ||
        TO_CHAR(emp_hire_date, 'DAY MONTH DDTH YYYY'));
EXCEPTION
  WHEN NO_DATA_FOUND THEN
    DBMS_OUTPUT.PUT_LINE('No employee found for the given ID'),
END;

If you pass the employee ID of 200 to this function, then it will return a result in the following format:

Jennifer Whalen was hired on: THURSDAY   SEPTEMBER 17TH 1987

PL/SQL procedure successfully completed.

How It Works

As shown in the previous recipe, the TO_CHAR function accepts a NUMBER or DATE value and returns a nicely formatted string. Using the many formatting masks that are available, you can return a string-based representation in a number of ways. As demonstrated in the solutions to this recipe and the previous one, the TO_CHAR function works a bit differently than the other conversion functions because the formatting mask is used to help produce the final string. Other conversion functions use the formatting mask to represent the format of the string you are passing in. In other words, TO_CHAR produces the formatted strings, whereas the other conversion functions accept them and produce a different datatype.

Table 6-4 lists some of the most commonly used characters for converting dates into strings.

Table 6-4. Date Formatting Mask Characters

Characters Description
YYYY Represents the four-digit year
YEAR Represents the spelled-out year
YYY Represents the last three digits of the year
YY Represents the last two digits of the year
Y Represents the last digit of the year
IYY Represents the last three digits of the ISO year
IY Represents the last two digits of the ISO year
I Represents the last digit of the ISO year
Q Represents the quarter of the year
MM Represents the month of the year
MON Represents the abbreviated month name
MONTH Represents the spelled-out month name padded with blanks
RM Represents the Roman numeral month
WW Represents the week of the year
W Represents the week of the month
IW Represents the ISO week of the year
D Represents the day of the week
DAY Represents the name of the day
DD Represents the day of the month
DDD Represents the day of the year
DY Represents the abbreviated name of the day
J Represents the Julian day
HH Represents the hour of the day (1–12)
HH12 Represents the hour of the day (1–12); same as HH
HH24 Represents the hour of the day (0–23)
MI Represents the minute of the hour (0–59)
SS Represents the second (0–59)
SSSSS Represents the seconds past midnight (0–86399)
FF Represents the fractional seconds

There are several formatting options, as you can see. It is best to spend some time with each of the different combinations to decide upon which one works best for your solution.

PL/SQL can make date formatting easy, because it is possible to create your own function that returns a date formatted per your application's requirements. Sometimes it is difficult to remember all the different formatting options that are available for dates. It can also be quite painful to reference a table such as Table 6-4 each time you want to format a date string. You can instead create your own conversion function to support just the formats that you use, and no others. Such a function greatly reduces the possibility for error, thus enhancing consistency in how your application formats dates.

The function in the following example accepts two parameters: the date to be converted and a string that specifies the output format. The second argument is limited to only four, easy-to-remember values: LONG, SHORT, STD, and DASH.

-- Returns a date string formatted per the style
-- that is passed into it.  The possible style strings
-- are as follows:
--   LONG => The spelled out date
--   SHORT => The abbreviated date
--   STD or blank => The standard date format mm/dd/yyyy
--   DASH => The standard format with dashes mm-dd-yyyy
CREATE OR REPLACE FUNCTION FORMAT_DATE(in_date IN DATE,
                                       style IN VARCHAR2)
 RETURN VARCHAR2 AS
 formatted_date    VARCHAR2(100);
BEGIN
  CASE style
    WHEN 'LONG' THEN
        formatted_date := TO_CHAR(in_date, 'DAY MONTH DDTH YYYY'),
    WHEN 'SHORT' THEN
        formatted_date := TO_CHAR(in_date, 'DY MON DDTH YYYY'),
    WHEN 'DASH' THEN
        formatted_date := TO_CHAR(in_date, 'MM-DD-YYYY'),
    ELSE
        formatted_date := TO_CHAR(in_date, 'MM/DD/YYYY'),
  END CASE;
  RETURN formatted_date;
END;

This function is nice because you only need to remember a short string that is used to represent the date format that you'd like to return.

It is also possible to convert dates to strings using the CAST function. For more information on the use of the CAST function, please see Recipe 6-5.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.83.57