mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-08-31 13:37:26 +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/
59 lines
1.4 KiB
SQL
59 lines
1.4 KiB
SQL
|
|
---
|
|
--- Postgres DateStyle needs all tests which parsing 'now'::datetime string
|
|
---
|
|
SET DATESTYLE TO 'Postgres';
|
|
|
|
|
|
SELECT 'now'::datetime =
|
|
TO_CHAR('now'::datetime, 'Dy Mon DD HH24:MI:SS YYYY')::datetime
|
|
as "Now vs. to_char";
|
|
|
|
|
|
SELECT 'now'::datetime =
|
|
FROM_CHAR('now'::datetime, 'Dy Mon DD HH24:MI:SS YYYY')
|
|
as "Now vs. from_char";
|
|
|
|
|
|
SELECT FROM_CHAR('now'::datetime, 'Dy Mon DD HH24:MI:SS YYYY') =
|
|
TO_CHAR('now'::datetime, 'Dy Mon DD HH24:MI:SS YYYY')::datetime
|
|
as "From_char vs. To_char";
|
|
|
|
|
|
SELECT 'now'::datetime =
|
|
FROM_CHAR(
|
|
TO_CHAR('now'::datetime, '"Time: "HH24-MI-SS" Date: "Dy DD Mon YYYY'),
|
|
'"Time: "HH24-MI-SS" Date: "Dy DD Mon YYYY'
|
|
)
|
|
as "High from/to char test";
|
|
|
|
|
|
SELECT TO_CHAR('now'::datetime, 'SSSS')::int =
|
|
TO_CHAR('now'::datetime, 'HH24')::int * 3600 +
|
|
TO_CHAR('now'::datetime, 'MI')::int * 60 +
|
|
TO_CHAR('now'::datetime, 'SS')::int
|
|
as "SSSS test";
|
|
|
|
|
|
SELECT TO_CHAR('now'::datetime, 'WW')::int =
|
|
(TO_CHAR('now'::datetime, 'DDD')::int -
|
|
TO_CHAR('now'::datetime, 'D')::int + 7) / 7
|
|
as "Week test";
|
|
|
|
|
|
SELECT TO_CHAR('now'::datetime, 'Q')::int =
|
|
TO_CHAR('now'::datetime, 'MM')::int / 3 + 1
|
|
as "Quartal test";
|
|
|
|
|
|
SELECT TO_CHAR('now'::datetime, 'DDD')::int =
|
|
(TO_CHAR('now'::datetime, 'WW')::int * 7) -
|
|
(7 - TO_CHAR('now'::datetime, 'D')::int) +
|
|
(7 - TO_CHAR(('01-Jan-'||
|
|
TO_CHAR('now'::datetime,'YYYY'))::datetime,'D')::int)
|
|
+1
|
|
as "Week and day test";
|
|
|
|
|
|
|