



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Page 1 of 5
Note: Math Functions, Mathematical manipulation of dates, MONTHS_BETWEEN, ADD_MONTHS, LAST_DAY, NEXT_DAY – These have all been covered in the lectures with examples. I will not be repeating them here unless someone has a specific question.
Instead today we will cover TO_CHAR, TO_DATE, TO_NUMBER, NVL, TRANSLATE, DECODE and the creation of formatted reports in SQL*Plus.
The TO_CHAR function. This function converts a number or date value to a character string.
The TO_CHAR function is often used to display a date in a particular format. It is applied in the following way...
TO_CHAR(DATE, DATE_PICTURE)
For Example...
SELECT TO_CHAR(SYSDATE, 'DAY, MONTH') FROM DUAL;
This will lead to the output ...
MONDAY , NOVEMBER
Alternatively..
SELECT TO_CHAR(SYSDATE, 'DDTH MONTH YYYY') FROM DUAL;
Or...
SELECT TO_CHAR(SYSDATE, 'YEAR, MON, DY') FROM DUAL;
And so on ...
TO_CHAR with dates - Points to note: The date picture, which must be enclosed in single quotes, can include any date format elements (A list follows). The column and date picture must be separated by a comma. The names of days and months in the output are automatically padded with blanks to a length of 9. To remove the blank padding and suppress leading zeros for number formats, use the FM (Fill Mode) prefix. To change the case of characters specify the format in the picture.
SELECT TO_CHAR(sysdate, 'FMddth Month YYYY') FROM dual;
Converting a date to display time information. TO_CHAR can be used to extract the time of day from a date, and display it in a specific format.
SELECT TO_CHAR(sysdate, 'HH:MI:SS')
Page 2 of 5
FROM dual;
Date formats. Picture: Meaning: SCC or CC Century; 'S' prefixes BC date with '-' Years in Dates YYYY or SYYYY Year; 'S' prefixes BC date with '-' YYY or YY or Y Last 3, 2 or 1 digit(s) of the year. Y,YYY Year with Comma in this Position. IYYY, IYY, IY, I ISO standard for years. SYEAR or YEAR Year spelled out; 'S' prefixes... BC or AD BC/AD indicator. B.C. or A.D. Same with periods. Q Quarter of Year. MM Month MONTH Name of Month padded to 9 Characters. MON Name of Month - 3 letter abbreviation. RM Roman Numeral Month. WW or W Week of Year or Month DDD or DD or D Day of week, year, month. DAY Name of day, padded with blanks to 9 characters. DY Name of Day, 3 letter abbreviation. J Julian Day.
Time Formats with TO_CHAR: AM or PM Meridian Indicator. A.M. or P.M. Same with periods. HH or HH12 or HH24 Hour of day, 12 hour or 24 hour. MI Minute SS Second SSSSS Seconds past midnight
Specifying Suffixes to Influence Number Display TH Ordinal Number (For Example 4th) SP Spelled out Number SPTH or THSP Spelled out Ordinal Numbers.
Converting a number to a character string TO_CHAR(number, 'NUMBER PICTURE’) Converts a number to a number string.
SELECT TO_CHAR(temperature, '999.00') FROM weather;
Note that 9 represents any number while 0 forces a zero to be displayed.
Page 4 of 5
This is used to convert a null value to a specified value or string. Note that the datatypes must match.
Example:
NVL (temperature, ‘0’)
A function that does an orderly character by character substitution in a string. The format is as follows:
SELECT TRANSLATE( String or column, if, then) FROM table ;
Example:
SELECT TRANSLATE(12345, 15729034, ‘abcdefgh’) FROM dual;
SELECT TRANSLATE(‘HAL’, ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’, ‘BCDEFGHIJKLMNOPQRSTUVWXYZA’) FROM dual;
DECODE This function acts very much like TRANSLATE except that instead of character by character substitution, it uses Value by Value substitution.
SELECT DECODE( value, if1, then1, if2, then 2, if3, then3…., else) FROM table ;
Example:
SELECT province, DECODE(province, ‘MUNSTER’, ‘HEAVEN’, ‘LEINSTER’, ‘HELL’, ‘?’) FROM region;
Formatting basic reports in SQLPlus.* The normal output from a query in SQL plus is as we have seen very basic, and often unclear. We have seen one method for the enhancement of these reports using column aliases within the queries. Here we will add a few more commands that are part of SQL*Plus, not SQL.
Here is an example based on a new weather table…
SET HEADSEP!
TTITLE 'THE WEATHER!REPORT!FROM MET-EIRE' BTITLE 'COPYRIGHT MET-EIRE 1998'
COLUMN CITY HEADING 'CITY!NAME' COLUMN CITY FORMAT A COLUMN CITY TRUNC
COLUMN DAY HEADING 'DATE'
Page 5 of 5
FROM WEATHER W -- Note the use of a Table Alias. --WHERE WEATHER.DAY = '12-FEB-98' ORDER BY W.CITY / SPOOL OFF
--EOF--
Explanations…
SET HEADSEP! – The purpose of this command is to tell SQL*Plus that every time it meets a ‘!’, force a split onto two lines. (Heading Separator).
TTITLE - Top Title for each page of the report. BTITLE – Bottom Title for each page of the report.
COLUMN – Gives SQL*Plus a variety of instructions on the heading, format and treatment of a column. Specify the column name next and then a parameter. FORMAT - Specifies the data type and the picture you want imposed on it, as in the TO_CHAR picture. A10 – String data 10 characters in width. 999.00 – Up to three numbers followed by ‘.00’ HEADING – Like the column Alias. WORD_WRAPPED and TRUNCATED as you would expect.
SET PAGESIZE and SET LINESIZE – Have already been explained.
BREAK ON – Tells SQLPlus where to put spaces between sections of a report or where to break for subtotals or totals. Note that the column you break on should be the same as the column specified in the ORDER BY clause. SKIP 2 tells SQLPlus to skip two lines at the break.
SPOOL – Tells SQLPlus to spool a file with the output to the specified location. SPOOL OFF tells SQLPlus when to stop writing to the file.