mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-08-23 09:10:40 +02:00
TO_DATE() and PgSQL extension FROM_CHAR(). TO_CHAR() routine allow formating text output with a datetime values: SELECT TO_CHAR('now'::datetime, '"Now is: "HH24:MI:SS'); to_char ---------------- Now is: 21:04:10 FROM_CHAR() routine allow convert text to a datetime: SELECT FROM_CHAR('September 1999 10:20:30', 'FMMonth YYYY HH:MI:SS'); from_char ----------------------------- Wed Sep 01 10:20:30 1999 CEST TO_DATE() is equal with FROM_CHAR(), but output a Date only: SELECT TO_DATE('September 1999 10:20:30', 'FMMonth YYYY HH:MI:SS'); to_date ---------- 09-01-1999 In attache is compressed dir for the contrib. All is prepared, but I'am not sure if Makefile is good (probably yes). Comments & suggestions ? Thomas, thank you for your good advices. Karel ------------------------------------------------------------------------------ Karel Zak <zakkr@zf.jcu.cz> http://home.zf.jcu.cz/~zakkr/
184 lines
4.9 KiB
Plaintext
184 lines
4.9 KiB
Plaintext
|
|
|
|
TO_CHAR(datetime, text)
|
|
-----------------------
|
|
(returns text)
|
|
|
|
TO_CHAR - the DateTime function for formating date and time outputs.
|
|
This routine is inspire with the Oracle to_char().
|
|
|
|
SELECT TO_CHAR('now'::datetime, 'HH:MI:SS YYYY');
|
|
-------------
|
|
11:57:11 1999
|
|
|
|
|
|
FROM_CHAR(text, text)
|
|
---------------------
|
|
(returns DateTime)
|
|
|
|
FROM_CHAR - the PostgreSQL extension routine which read non-datetime
|
|
string and convert it to DateTime. This func. is inspire with the
|
|
Oracle to_date() routine, but in Oracle this func. return date only
|
|
and not support all keywords (format pictures).
|
|
|
|
SELECT FROM_CHAR('11:57:11 1999', 'HH:MI:SS YYYY');
|
|
----------------------------
|
|
Fri 01 Jan 11:57:11 1999 CET
|
|
|
|
|
|
TO_DATE(text, text)
|
|
-------------------
|
|
(returns Date)
|
|
|
|
TO_DATE - the Date function which read non-datetime (non-date) string
|
|
and convert it to date. All for thos func. is just as from_char().
|
|
This func. is inspire with the Oracle to_date() routine.
|
|
|
|
SELECT TO_DATE('11:57:11 1999', 'HH:MI:SS YYYY');
|
|
----------
|
|
01-01-1999
|
|
|
|
|
|
|
|
----------------------------------
|
|
String format-KeyWords and options:
|
|
----------------------------------
|
|
|
|
* TO_CHAR (..., 'format picture')
|
|
* FROM_CHAR (..., 'format picture')
|
|
* TO_DATE (..., 'format picture')
|
|
|
|
(Note: In Oracle manual is format-keyword called 'format pictures'.)
|
|
|
|
All keywords has suffixes (prefix or postfix), example for 2 hours:
|
|
keyword: HH (hour) 'HH' --> '02'
|
|
prefix: FM (fill mode) 'FMHH' --> '2'
|
|
postfix: TH (ordinal number) 'HHth' --> '02nd'
|
|
'FMHHth' --> '2nd'
|
|
|
|
Suffixes:
|
|
--------
|
|
FM - fill mode
|
|
02 --> FMHH --> 2
|
|
January , --> FMMonth --> January,
|
|
|
|
TH - upper ordinal number
|
|
02 --> HHTH --> 02ND
|
|
|
|
th - lower ordinal number
|
|
02 --> HHth --> 02th
|
|
|
|
|
|
KeyWords (format pictures):
|
|
--------------------------
|
|
|
|
HH - hour of day (01-12)
|
|
HH12 - -- // --
|
|
HH24 - hour (00-24)
|
|
MI - minute (00-59)
|
|
SS - socond (00-59)
|
|
SSSS - seconds past midnight (0-86399)
|
|
Y,YYY - year with comma (full PgSQL datetime range) digits)
|
|
YYYY - year (4 and more (full PgSQL datetime range) digits)
|
|
YYY - last 3 digits of year
|
|
YY - last 2 digits of year
|
|
Y - last digit of year
|
|
MONTH - full month name (upper) (9-letters)
|
|
Month - full month name - first character is upper (9-letters)
|
|
month - full month name - all characters is upper (9-letters)
|
|
MON - abbreviated month name (3-letters)
|
|
Mon - abbreviated month name (3-letters) - first character is upper
|
|
mon - abbreviated month name (3-letters) - all characters is upper
|
|
MM - month (01-12)
|
|
DAY - full day name (upper) (9-letters)
|
|
Day - full day name - first character is upper (9-letters)
|
|
day - full day name - all characters is upper (9-letters)
|
|
DY - abbreviated day name (3-letters) (upper)
|
|
Dy - abbreviated day name (3-letters) - first character is upper
|
|
Dy - abbreviated day name (3-letters) - all character is upper
|
|
DDD - day of year (001-366)
|
|
DD - day of month (01-31)
|
|
D - day of week (1-7; SUN=1)
|
|
WW - week number of year
|
|
CC - century (2-digits)
|
|
Q - quarter
|
|
RM - roman numeral month (I=JAN; I-XII)
|
|
W - week of month
|
|
J - julian day (days since January 1, 4712 BC)
|
|
|
|
|
|
AC / BC:
|
|
-------
|
|
|
|
TO-FROM CHAR routines support BC and AC postfix for years.
|
|
You can combine BC and AC with TH.
|
|
|
|
OTHER:
|
|
-----
|
|
'\' - must be use as double \\
|
|
|
|
'\\HH\\MI\\SS' --> 11\45\56
|
|
|
|
'"' - string berween a quotation marks is skipen and not
|
|
is parsed. If you wand write '"' to output you must
|
|
use \\"
|
|
|
|
'"Month: "Month' --> Month: November
|
|
'\\"YYYY Month\\"' --> "1999 November "
|
|
|
|
text - the PostgreSQL TO-FROM CHAR support text without '"',
|
|
but " text " is fastly and you have guarantee,
|
|
that this text not will interprete as keyword.
|
|
|
|
WARNING:
|
|
-------
|
|
|
|
You DON'T OMIT differention between fill mode (FM prefix)
|
|
and standard input in FROM_CHAR (TO_DATE), because this
|
|
routines can't scan your input string and conver it to
|
|
Datetime. See:
|
|
|
|
WRONG: FROM_CHAR('August 1999', 'Month YYYY');
|
|
|
|
RIGHT: FROM_CHAR('August 1999', 'Month YYYY');
|
|
or FROM_CHAR('August 1999', 'FMMonth YYYY');
|
|
|
|
(! Month is 9-letters string if you not set fill-mode !)
|
|
|
|
|
|
---------------------------
|
|
TODO / Now is not supported:
|
|
---------------------------
|
|
|
|
- spelled-out SP suffix ( 22 --> Twenty-two )
|
|
- AM/PM
|
|
|
|
- not supported number to character converting
|
|
|
|
TO_CHAR(number, 'format')
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
- secondary products :-) ------------------------------------------------------
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
ORDINAL(int4, text)
|
|
-------------------
|
|
|
|
* Translate number to ordinal number and return this as text
|
|
|
|
|
|
* Examples:
|
|
|
|
template1=> select ordinal(21212, 'TH');
|
|
ordinal
|
|
-------
|
|
21212ND
|
|
|
|
template1=> select ordinal(21212, 'th');
|
|
ordinal
|
|
-------
|
|
21212nd
|