Re: Format Function

From: "Yudie" <yudie(at)axiontech(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Format Function
Date: 2003-02-18 00:21:28
Message-ID: 014d01c2d6e3$aff25610$8401a8c0@yudie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

OK thanks for the answer.
I made my function with plpgsql to do formating,
It's not perfect but at least make it easy to do simple formating
==============================================
CREATE FUNCTION myformat(text,text) returns text as'
DECLARE
i integer;
n integer;
strtemp text;

BEGIN
i := length($2);
n := length($1);
strtemp='''';

WHILE i > 0 AND n > 0 LOOP
IF substr($2,i,1) = ''#'' THEN
strtemp := substr($1,n,1) || strtemp;
n := n - 1;
ELSE
strtemp := substr($2,i,1) || strtemp;
END IF;

i := i - 1;
END LOOP;

return strtemp;
END
' language 'PLPGSQL'

----- Original Message -----
From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: "Yudie" <yudie(at)axiontech(dot)com>; <pgsql-sql(at)postgresql(dot)org>
Sent: Monday, February 17, 2003 2:56 PM
Subject: Re: [SQL] Format Function

Yudie,

> Is there any default function for formating string in postgre sql?
> for instance:
> Format('123ABCDE', '####-###-###') => '12-3AB-CDE'
>
> The closest function I know is the to_char() function but it only works
for
numbers

No, there isn't. You could write one, though. For example, you could
write:

(7.3 syntax)

CREATE FUNCTION yudie_format(text) RETURNS text AS
'SELECT SUBSTR($1, 1, 4) || ''-'' || SUBSTR($1,5,3) || ''-'' ||
SUBSTR($1,9,4);
' LANGUAGE SQL IMMUTABLE STRICT;

As a simple formatting function.

For that matter, it would be the work of a weekend for someone to write a
function in PL/Perl which would take a format mask and apply it to any text
string.

--
-Josh Berkus
Aglio Database Solutions
San Francisco

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Will Trillich 2003-02-18 03:19:46 Re: DATABASE EXAMPLES?
Previous Message Frank Bax 2003-02-17 21:43:01 Re: convert from an integer to a date