Oracle TO_CHAR (PL/SQL) Function
The TO_CHAR function converts a date or number to a string.
Syntax:
TO_CHAR(исходное_значение, Формат, NLS_LANGUAGE);
--В качестве исходного значения может быть передана как дата DATE так и число NUMBER.
--Последний параметр NLS_LANGUAGE (как я понимаю для задания кодировки) мне не разу не пригодился.
Examples:
SQL> select to_char(sysdate,\'dd.mm.yyyy\') from dual;
TO_CHAR(SYSDATE,\'DD.MM.YYYY\')
-----------------------------
28.02.2017
SQL> select to_char(123,\'00009\') from dual;
TO_CHAR(123,\'00009\')
--------------------
00123
SQL> select to_char(123.1,\'00009.90\') from dual;
TO_CHAR(123.1,\'00009.90\')
-------------------------
00123.10
The format can be specified using combinations of the following values:
- YYYY — Four-digit year (e.g. 1980)
- YY — Two-digit year (e.g. 84)
- Q — Quarter (1, 2, 3, 4)
- MM — Month (01-12)
- WW — Week number in the year (1-53) where week 1 starts on the first day of the year and continues for 7 days
- W — Week number in the month (1-5) where week 1 starts on the first day of the month and continues for 7 days
- IW — Week number in the year (1-52 or 1-53) according to the ISO standard
- D — Day of the week (1-7)
- DD — Day of the month (1-31)
- DDD — Day of the year (1-366)
- J — Day according to the Julian calendar, number of days from January 1, 4712 BCE to CE
- H — Hour (1-12)
- HH12 — Hour (1-12)
- HH24 — Hour (0-23)
- MI — Minutes (0-59)
- SS — Seconds (0-59)
You can also use numeric masks to convert numbers, for example:
\'00009.90\' — Outputs numbers adding zeros to the left up to 5 digits, with two digits after the decimal point.
Or, for example, a very convenient mask for displaying monetary values: \'FM999990.00\'.
These are probably the main parameters that can be useful and have been used by me.
Comments (0)
No comments yet.