as attache of this mail is patch (to the main tree) with to_char's

family functions. Contain:

  conversion from a datetype to formatted text:

	to_char( datetime, 	text)
	to_char( timestamp,	text)
	to_char( int4,		text)
	to_char( int8,		text)
	to_char( float4,	text)
	to_char( float8,	text)
	to_char( numeric,	text)

  vice versa:

	to_date		( text, text)
	to_datetime	( text, text)
	to_timestamp	( text, text)
	to_number	( text, text)	   (convert to numeric)


  PostgreSQL to_char is very compatible with Oracle's to_char(), but not
total exactly (now). Small differentions are in number formating. It will
fix in next to_char() version.


! If will this patch aplly to the main tree, must be delete the current
  to_char version in contrib (directory "dateformat" and note in contrib's
  README), this patch not erase it (sorry Bruce).



The patch patching files:

	doc/src/sgml/func.sgml
                     ^^^^^^^^
   Hmm, I'm not sure if my English... :( Check it anyone (volunteer)?

   Thomas, it is right? SGML is not my primary lang  and compile
   the current PG docs tree is very happy job (hard variables setting in
   docs/sgml/Makefile --> HSTYLE= /home/users/t/thomas/....  :-)

   What add any definition to global configure.in and set Makefiles in docs
   tree via ./configure?

	src/backend/utils/adt/Makefile
	src/backend/utils/adt/formatting.c
	src/include/catalog/pg_proc.h
	src/include/utils/formatting.h
Karel Zak <zakkr@zf.jcu.cz>              http://home.zf.jcu.cz/~zakkr/
This commit is contained in:
Bruce Momjian 2000-01-25 23:53:56 +00:00
parent 90aaad06cf
commit b866d2e2d7
15 changed files with 3756 additions and 1877 deletions

View File

@ -14,10 +14,6 @@ bit -
Bit type
by Adriaan Joubert <a.joubert@albourne.com>
dateformat -
Date Formatting to/from character strings
by Karel Zak - Zakkr <zakkr@zf.jcu.cz>
datetime -
Date & time functions
by Massimo Dal Zotto <dz@cs.unitn.it>

View File

@ -1,71 +0,0 @@
#-------------------------------------------------------------------------
#
# Makefile --
#
# Makefile for TO-FROM_CHAR module.
#
#-------------------------------------------------------------------------
PGDIR = ../..
SRCDIR = $(PGDIR)/src
include $(SRCDIR)/Makefile.global
INCLUDE_OPT = -I ./ \
-I $(SRCDIR)/ \
-I $(SRCDIR)/include \
-I $(SRCDIR)/port/$(PORTNAME)
CFLAGS += $(INCLUDE_OPT) $(CFLAGS_SL)
MODNAME = to-from_char
SQLDEFS = $(MODNAME).sql
MODULE = $(MODNAME)$(DLSUFFIX)
MODDIR = $(LIBDIR)/modules
SQLDIR = $(LIBDIR)/sql
all: module sql
module: $(MODULE)
sql: $(SQLDEFS)
install: $(MODULE) $(SQLDEFS) $(MODDIR) $(SQLDIR)
cp -p $(MODULE) $(MODDIR)/
strip $(MODDIR)/$(MODULE)
cp -p $(SQLDEFS) $(SQLDIR)/
install-doc:
if [ -d "$(DOCDIR)" ]; then \
cp -p *.doc $(DOCDIR); \
else \
cp -p *.doc $(SQLDIR); \
fi
$(MODDIR):
mkdir -p $@
$(SQLDIR):
mkdir -p $@
%.sql: %.sql.in
sed "s|MODULE_PATHNAME|$(MODDIR)/$(MODULE)|" < $< > $@
.SUFFIXES: $(DLSUFFIX)
%$(DLSUFFIX): %.c
$(CC) $(CFLAGS) -shared -o $@ $<
depend dep:
$(CC) -MM $(INCLUDE_OPT) *.c >depend
clean:
rm -f *~ $(MODULE) $(MODNAME).sql
ifeq (depend,$(wildcard depend))
include depend
endif

View File

@ -1,25 +0,0 @@
PROGRAM = rand_datetime
OBJECTS = rand_datetime.o
CFLAGS = -Wall -fpic -O3
CC = gcc
RM = rm -f
LIBS =
INCLUDE =
COMPILE = $(CC) $(CPPFLAGS) $(CFLAGS) $(INCLUDE)
LINK = $(CC) $(CFLAGS) -o $@ $(LIBS)
all: $(PROGRAM)
$(PROGRAM): $(OBJECTS)
$(LINK) $(OBJECTS)
.c.o: $<
$(COMPILE) -c $<
clean:
$(RM) -f *~ $(OBJECTS) $(PROGRAM)

View File

@ -1,33 +0,0 @@
TO/FROM CHAR tests
~~~~~~~~~~~~~~~~~~
* rand_datetime
The program 'rand_datetime' output a random datetime strings
(with yaer range 0..9999), you can use this for datetime testing.
You can usage this (example) for table filling.
Usage:
./rand_datetime <randfile> <num> <prefix> <postfix>
Example:
./rand_datetime /dev/urandom 2 "INSERT INTO tab VALUES('" "'::datetime);"
INSERT INTO tab VALUES('Sat 27 Jul 13:08:57 19618'::datetime);
INSERT INTO tab VALUES('Wed 25 Aug 20:31:50 27450'::datetime);
* regress
psql < regress.sql (all answers, must be TRUE, for Posgres
datestyle)
--> TO_DATE() is simular as FROM_CHAR(), but convert full datetime
to date ==> needn't test (?).

View File

@ -1,71 +0,0 @@
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <stdlib.h>
char *month[] = {
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",NULL
};
char *day[] = { "Sun","Mon","Tue","Wed","Thu","Fri","Sat", NULL };
int num(FILE *f, int min, int max)
{
int x, y, one;
one = x = fgetc(f);
if (x < min)
x = min;
else if (x > max) {
while(x > max)
x /= 2;
return x;
}
do {
y = fgetc(f);
if ((x+y) > max)
return x;
x += y;
} while(--one > 0);
return x;
}
int main(int argc, char **argv)
{
FILE *f;
int count;
if (argc < 5) {
printf("\nUsage: %s <randfile> <num> <prefix> <postfix>\n", argv[0]);
printf("\n(C) Karel Zak - Zakkr 1999\n\n");
exit(1);
}
if ((f = fopen(argv[1], "r")) == NULL) {
perror(argv[1]);
exit(1);
}
count = atoi(argv[2]);
for(; count > 0; --count) {
fprintf(stdout, "%s%s %02d %s %02d:%02d:%02d %d%s\n",
argv[3],
day[ num(f, 0, 6) ],
num(f, 1, 28),
month[ num(f, 0, 11) ],
num(f, 0, 23),
num(f, 0, 59),
num(f, 0, 59),
num(f, 0, 9999),
argv[4]
);
}
exit(0);
}

View File

@ -1,58 +0,0 @@
---
--- 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";

File diff suppressed because it is too large Load Diff

View File

@ -1,183 +0,0 @@
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

View File

@ -1,18 +0,0 @@
#ifndef TO_FROM_CHAR_H
#define TO_FROM_CHAR_H
/*------
* For postgres
*------
*/
extern text *to_char(DateTime *dt, text *format);
extern DateTime *from_char(text *date_str, text *format);
extern DateADT to_date(text *date_str, text *format);
extern text *ordinal(int4 num, text *type);
extern char *months_full[]; /* full months name */
extern char *rm_months[]; /* roman numeral of months */
#endif

View File

@ -1,29 +0,0 @@
-- to-from_char.sql datetime routines --
--
-- Copyright (c) 1999, Karel Zak "Zakkr" <zakkr@zf.jcu.cz>
--
-- This file is distributed under the GNU General Public License
-- either version 2, or (at your option) any later version.
-- Define the new functions
--
create function to_char(datetime, text) returns text
as 'MODULE_PATHNAME'
language 'c';
create function from_char(text, text) returns datetime
as 'MODULE_PATHNAME'
language 'c';
create function to_date(text, text) returns date
as 'MODULE_PATHNAME'
language 'c';
create function ordinal(int, text) returns text
as 'MODULE_PATHNAME'
language 'c';
-- end of file

View File

@ -420,6 +420,547 @@
</para>
</sect1>
<sect1>
<title id="formatting-funcs"> Formatting Functions </title>
<note>
<title>Author</title>
<para>
Written by
<ulink url="mailto:zakkr@zf.jcu.cz">Karel Zak</ulink>
on 2000-01-24.
</para>
</note>
<para>
Formatting functions provide a powerful set of tools for converting
various datetypes (date/time, int, float, numeric) to formatted strings
and reverse convert from formatted strings to original datetypes.
</para>
<para>
<table tocentry="1">
<title>Formatting Functions</title>
<tgroup cols="4">
<thead>
<row>
<entry>Function</entry>
<entry>Returns</entry>
<entry>Description</entry>
<entry>Example</entry>
</row>
</thead>
<tbody>
<row>
<entry> to_char(datetime, text) </entry>
<entry> text </entry>
<entry> convert datetime to string </entry>
<entry> to_char('now'::datetime, 'HH12:MI:SS') </entry>
</row>
<row>
<entry> to_char(timestamp, text) </entry>
<entry> text </entry>
<entry> convert timestamp to string </entry>
<entry> to_char( now(), 'HH12:MI:SS') </entry>
</row>
<row>
<entry> to_char(int, text) </entry>
<entry> text </entry>
<entry> convert int4/int8 to string </entry>
<entry> to_char(125, '999') </entry>
</row>
<row>
<entry> to_char(float, text) </entry>
<entry> text </entry>
<entry> convert float4/float8 to string </entry>
<entry> to_char(125.8, '999D9') </entry>
</row>
<row>
<entry> to_char(numeric, text) </entry>
<entry> text </entry>
<entry> convert numeric to string </entry>
<entry> to_char(-125.8, '999D99S') </entry>
</row>
<row>
<entry> to_datetime(text, text) </entry>
<entry> datetime </entry>
<entry> convert string to datetime </entry>
<entry> to_datetime('05 Dec 2000 13', 'DD Mon YYYY HH') </entry>
</row>
<row>
<entry> to_date(text, text) </entry>
<entry> date </entry>
<entry> convert string to date </entry>
<entry> to_date('05 Dec 2000', 'DD Mon YYYY') </entry>
</row>
<row>
<entry> to_timestamp(text, text) </entry>
<entry> date </entry>
<entry> convert string to timestamp </entry>
<entry> to_timestamp('05 Dec 2000', 'DD Mon YYYY') </entry>
</row>
<row>
<entry> to_number(text, text) </entry>
<entry> numeric </entry>
<entry> convert string to numeric </entry>
<entry> to_number('12,454.8-', '99G999D9S') </entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<para>
For all formatting functions is second argument format-picture.
</para>
<para>
<table tocentry="1">
<title>Format-pictures for datetime to_char() version.</title>
<tgroup cols="2">
<thead>
<row>
<entry>Format-picture</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry> HH </entry>
<entry> hour of day (01-12) </entry>
</row>
<row>
<entry> HH12 </entry>
<entry> hour of day (01-12) </entry>
</row>
<row>
<entry> MI </entry>
<entry> minute (00-59) </entry>
</row>
<row>
<entry> SS </entry>
<entry> socond (00-59) </entry>
</row>
<row>
<entry> SSSS </entry>
<entry> seconds past midnight (0-86399) </entry>
</row>
<row>
<entry> Y,YYY </entry>
<entry> year (4 and more digits) with comma </entry>
</row>
<row>
<entry> YYYY </entry>
<entry> year (4 and more digits) </entry>
</row>
<row>
<entry> YYY </entry>
<entry> last 3 digits of year </entry>
</row>
<row>
<entry> YY </entry>
<entry> last 2 digits of year </entry>
</row>
<row>
<entry> Y </entry>
<entry> last digit of year </entry>
</row>
<row>
<entry> MONTH </entry>
<entry> full month name (9-letters) - all characters is upper </entry>
</row>
<row>
<entry> Month </entry>
<entry> full month name (9-letters) - first character is upper </entry>
</row>
<row>
<entry> month </entry>
<entry> full month name (9-letters) - all characters is lower </entry>
</row>
<row>
<entry> MON </entry>
<entry> abbreviated month name (3-letters) - all characters is upper </entry>
</row>
<row>
<entry> Mon </entry>
<entry> abbreviated month name (3-letters) - first character is upper </entry>
</row>
<row>
<entry> mon </entry>
<entry> abbreviated month name (3-letters) - all characters is lower </entry>
</row>
<row>
<entry> MM </entry>
<entry> month (01-12) </entry>
</row>
<row>
<entry> DAY </entry>
<entry> full day name (9-letters) - all characters is upper </entry>
</row>
<row>
<entry> Day </entry>
<entry> full day name (9-letters) - first character is upper </entry>
</row>
<row>
<entry> day </entry>
<entry> full day name (9-letters) - all characters is lower </entry>
</row>
<row>
<entry> DY </entry>
<entry> abbreviated day name (3-letters) - all characters is upper </entry>
</row>
<row>
<entry> Dy </entry>
<entry> abbreviated day name (3-letters) - first character is upper </entry>
</row>
<row>
<entry> dy </entry>
<entry> abbreviated day name (3-letters) - all characters is upper </entry>
</row>
<row>
<entry> DDD </entry>
<entry> day of year (001-366) </entry>
</row>
<row>
<entry> DD </entry>
<entry> day of month (01-31) </entry>
</row>
<row>
<entry> D </entry>
<entry> day of week (1-7; SUN=1) </entry>
</row>
<row>
<entry> W </entry>
<entry> week of month </entry>
</row>
<row>
<entry> WW </entry>
<entry> week number of year </entry>
</row>
<row>
<entry> CC </entry>
<entry> century (2-digits) </entry>
</row>
<row>
<entry> J </entry>
<entry> julian day (days since January 1, 4712 BC) </entry>
</row>
<row>
<entry> Q </entry>
<entry> quarter </entry>
</row>
<row>
<entry> RM </entry>
<entry> month in roman numeral (I-XII; I=JAN) </entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<para>
All format-pictures allow use suffixes (postfix / prefix). The suffix is
always valid for near format-picture. The 'FX' is global prefix only.
</para>
<para>
<table tocentry="1">
<title>Suffixes for format-pictures for datetime to_char() version.</title>
<tgroup cols="3">
<thead>
<row>
<entry>Suffix</entry>
<entry>Description</entry>
<entry>Example</entry>
</row>
</thead>
<tbody>
<row>
<entry> FM </entry>
<entry> fill mode - prefix </entry>
<entry> FMMonth </entry>
</row>
<row>
<entry> TH </entry>
<entry> upper ordinal number - postfix </entry>
<entry> DDTH </entry>
</row>
<row>
<entry> th </entry>
<entry> lower ordinal number - postfix </entry>
<entry> DDTH </entry>
</row>
<row>
<entry> FX </entry>
<entry> FX - (Fixed format) global format-picture switch.
the TO_DATETIME / TO_DATA skip blank space if this option is
not use. Must by used as first item in formt-picture. </entry>
<entry> FX Month DD Day </entry>
</row>
<row>
<entry> SP </entry>
<entry> spell mode (not implement now)</entry>
<entry> DDSP </entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<para>
'\' - must be use as double \\, example '\\HH\\MI\\SS'
</para>
<para>
'"' - string between a quotation marks is skipen and not is parsed.
If you want write '"' to output you must use \\", exapmle '\\"YYYY Month\\"'.
</para>
<para>
text - the PostgreSQL's to_char() support text without '"', but string
between a quotation marks is fastly and you have guarantee, that a text
not will interpreted as a keyword (format-picture), exapmle '"Hello Year: "YYYY'.
</para>
<para>
<table tocentry="1">
<title>Format-pictures for number (int/float/numeric) to_char() version.</title>
<tgroup cols="2">
<thead>
<row>
<entry>Format-picture</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry> 9 </entry>
<entry> return value with the specified number of digits, and if digit is
not available use blank space </entry>
</row>
<row>
<entry> 0 </entry>
<entry> as 9, but instead blank space use zero </entry>
</row>
<row>
<entry> . (period) </entry>
<entry> decimal point </entry>
</row>
<row>
<entry> , (comma) </entry>
<entry> group (thousand) separator </entry>
</row>
<row>
<entry> PR </entry>
<entry> return negative value in angle brackets </entry>
</row>
<row>
<entry> S </entry>
<entry> return negatice value with minus sign (use locales) </entry>
</row>
<row>
<entry> L </entry>
<entry> currency symbol (use locales) </entry>
</row>
<row>
<entry> D </entry>
<entry> decimal point (use locales) </entry>
</row>
<row>
<entry> G </entry>
<entry> group separator (use locales) </entry>
</row>
<row>
<entry> MI </entry>
<entry> return minus sign on specified position (if number < 0) </entry>
</row>
<row>
<entry> PL </entry>
<entry> return plus sign on specified position (if number > 0) </entry>
</row>
<row>
<entry> RN </entry>
<entry> return number as roman number (number must be between 1 and 3999) </entry>
</row>
<row>
<entry> TH or th </entry>
<entry> convert number to ordinal number (not convert numbers under zero and decimal numbers) </entry>
</row>
<row>
<entry> V </entry>
<entry> arg1 * (10 ^ n); - return a value multiplied by 10^n (where 'n' is number of '9's after the 'V').
The to_char() not support use 'V' and decimal poin together, example "99.9V99". </entry>
</row>
<row>
<entry> EEEE </entry>
<entry> science numbers. Now not supported. </entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<para>
The PostgreSQL to_char() not support absurd to_char(0.1, '99.99')
--> <ProgramListing> ' .10' </ProgramListing> format.
</para>
<para>
<table tocentry="1">
<title> The to_char() examples. </title>
<tgroup cols="2">
<thead>
<row>
<entry>Input</entry>
<entry>Output</entry>
</row>
</thead>
<tbody>
<row>
<entry> to_char(now(), 'Day, HH12:MI:SS') </entry>
<entry><ProgramListing> 'Tuesday , 05:39:18' </ProgramListing></entry>
</row>
<row>
<entry> to_char(now(), 'FMDay, HH12:MI:SS') </entry>
<entry><ProgramListing> 'Tuesday, 05:39:18' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 0.1, '99.99') </entry>
<entry><ProgramListing> ' 0.10' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 0.1, '0.9') </entry>
<entry><ProgramListing> ' 0.1' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 0.1, '090.9') </entry>
<entry><ProgramListing> ' 000.1' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 485, '999') </entry>
<entry><ProgramListing> ' 485' </ProgramListing></entry>
</row>
<row>
<entry> to_char( -485, '999') </entry>
<entry><ProgramListing> '-485' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 485, '09999') </entry>
<entry><ProgramListing> ' 00485' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 485, 'FM09999') </entry>
<entry><ProgramListing> '00485' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 485, 'FM999') </entry>
<entry><ProgramListing> '485' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 485, '9 9 9') </entry>
<entry><ProgramListing> ' 4 8 5' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 1485, '9,999') </entry>
<entry><ProgramListing> ' 1,485' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 1485, '9G999') </entry>
<entry><ProgramListing> ' 1 485' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 148.5, '999.999') </entry>
<entry><ProgramListing> ' 148.500' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 148.5, '999D999') </entry>
<entry><ProgramListing> ' 148,500' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 3148.5,'9G999D999') </entry>
<entry><ProgramListing> ' 3 148,500' </ProgramListing></entry>
</row>
<row>
<entry> to_char( -485, '999S') </entry>
<entry><ProgramListing> '485-' </ProgramListing></entry>
</row>
<row>
<entry> to_char( -485, '999MI') </entry>
<entry><ProgramListing> '485-' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 485, '999MI') </entry>
<entry><ProgramListing> '485' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 485, 'PL999') </entry>
<entry><ProgramListing> '+485' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 485, 'SG999') </entry>
<entry><ProgramListing> '+485' </ProgramListing></entry>
</row>
<row>
<entry> to_char( -485, 'SG999') </entry>
<entry><ProgramListing> '-485' </ProgramListing></entry>
</row>
<row>
<entry> to_char( -485, '9SG99') </entry>
<entry><ProgramListing> '4-85' </ProgramListing></entry>
</row>
<row>
<entry> to_char( -485, '999PR') </entry>
<entry><ProgramListing> '<485>' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 485, 'L999') </entry>
<entry><ProgramListing> 'DM 485' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 485, 'RN') </entry>
<entry><ProgramListing> ' CDLXXXV' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 485, 'FMRN') </entry>
<entry><ProgramListing> 'CDLXXXV' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 5.2, 'FMRN') </entry>
<entry><ProgramListing> 'V' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 482, '999th') </entry>
<entry><ProgramListing> ' 482nd' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 485, '"Good number:"999') </entry>
<entry><ProgramListing> 'Good number: 485' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 485.8, '"Pre-decimal:"999" Post-decimal:" .999') </entry>
<entry><ProgramListing> 'Pre-decimal: 485 Post-decimal: .800' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 12, '99V999') </entry>
<entry><ProgramListing> ' 12000' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 12.4, '99V999') </entry>
<entry><ProgramListing> ' 12400' </ProgramListing></entry>
</row>
<row>
<entry> to_char( 12.45, '99V9') </entry>
<entry><ProgramListing> ' 125' </ProgramListing></entry>
</row>
</tbody>
</tgroup>
</table>
</para>
</sect1>
<sect1>
<title>Geometric Functions</title>

View File

@ -4,7 +4,7 @@
# Makefile for utils/adt
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/backend/utils/adt/Makefile,v 1.32 2000/01/19 02:58:56 petere Exp $
# $Header: /cvsroot/pgsql/src/backend/utils/adt/Makefile,v 1.33 2000/01/25 23:53:51 momjian Exp $
#
#-------------------------------------------------------------------------
@ -31,7 +31,7 @@ OBJS = acl.o arrayfuncs.o arrayutils.o bool.o cash.o char.o chunk.o \
regexp.o regproc.o ruleutils.o selfuncs.o sets.o \
tid.o timestamp.o varchar.o varlena.o version.o \
network.o mac.o inet_net_ntop.o inet_net_pton.o \
ri_triggers.o pg_lzcompress.o pg_locale.o
ri_triggers.o pg_lzcompress.o pg_locale.o formatting.o
all: SUBSYS.o

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,7 @@
*
* Copyright (c) 1994, Regents of the University of California
*
* $Id: pg_proc.h,v 1.116 2000/01/24 07:16:52 tgl Exp $
* $Id: pg_proc.h,v 1.117 2000/01/25 23:53:52 momjian Exp $
*
* NOTES
* The script catalog/genbki.sh reads this file and generates .bki
@ -2343,6 +2343,30 @@ DESCR("larger of two numbers");
DATA(insert OID = 1769 ( numeric_cmp PGUID 11 f t t 2 f 23 "1700 1700" 100 0 0 100 numeric_cmp - ));
DESCR("compare two numbers");
/* formatting */
DATA(insert OID = 1770 ( to_char PGUID 11 f t f 2 f 25 "1184 25" 100 0 0 100 datetime_to_char - ));
DESCR("convert / formatting datetime to text");
DATA(insert OID = 1771 ( to_char PGUID 11 f t f 2 f 25 "1296 25" 100 0 0 100 timestamp_to_char - ));
DESCR("convert / formatting timestamp to text");
DATA(insert OID = 1772 ( to_char PGUID 11 f t f 2 f 25 "1700 25" 100 0 0 100 numeric_to_char - ));
DESCR("convert / formatting numeric to text");
DATA(insert OID = 1773 ( to_char PGUID 11 f t f 2 f 25 "23 25" 100 0 0 100 int4_to_char - ));
DESCR("convert / formatting int4 to text");
DATA(insert OID = 1774 ( to_char PGUID 11 f t f 2 f 25 "20 25" 100 0 0 100 int8_to_char - ));
DESCR("convert / formatting int8 to text");
DATA(insert OID = 1775 ( to_char PGUID 11 f t f 2 f 25 "700 25" 100 0 0 100 float4_to_char - ));
DESCR("convert / formatting float4 to text");
DATA(insert OID = 1776 ( to_char PGUID 11 f t f 2 f 25 "701 25" 100 0 0 100 float8_to_char - ));
DESCR("convert / formatting float8 to text");
DATA(insert OID = 1777 ( to_number PGUID 11 f t f 2 f 1700 "25 25" 100 0 0 100 numeric_to_number - ));
DESCR("convert text to numeric");
DATA(insert OID = 1778 ( to_datetime PGUID 11 f t f 2 f 1184 "25 25" 100 0 0 100 to_datetime - ));
DESCR("convert text to datetime");
DATA(insert OID = 1779 ( to_timestamp PGUID 11 f t f 2 f 1296 "25 25" 100 0 0 100 to_timestamp - ));
DESCR("convert text to datetime");
DATA(insert OID = 1780 ( to_date PGUID 11 f t f 2 f 1082 "25 25" 100 0 0 100 to_date - ));
DESCR("convert text to date");
/*
* prototypes for functions pg_proc.c

View File

@ -0,0 +1,31 @@
/* -----------------------------------------------------------------------
* formatting.h
*
* $Id: formatting.h,v 1.1 2000/01/25 23:53:56 momjian Exp $
*
*
* The PostgreSQL routines for a DateTime/int/float/numeric formatting,
* inspire with Oracle TO_CHAR() / TO_DATE() / TO_NUMBER() routines.
*
* 1999 Karel Zak "Zakkr"
*
* -----------------------------------------------------------------------
*/
#ifndef _FORMATTING_H_
#define _FORMATTING_H_
extern text *datetime_to_char(DateTime *dt, text *fmt);
extern text *timestamp_to_char(time_t dt, text *fmt);
extern DateTime *to_datetime(text *date_str, text *fmt);
extern time_t to_timestamp(text *date_str, text *fmt);
extern DateADT to_date(text *date_str, text *fmt);
extern Numeric numeric_to_number(text *value, text *fmt);
extern text *numeric_to_char(Numeric value, text *fmt);
extern text *int4_to_char(int32 value, text *fmt);
extern text *int8_to_char(int64 *value, text *fmt);
extern text *float4_to_char(float32 value, text *fmt);
extern text *float8_to_char(float64 value, text *fmt);
#endif