text formatting in a query, a la sprintf - Mailing list pgsql-general

From Reece Hart
Subject text formatting in a query, a la sprintf
Date
Msg-id 1156021157.4158.496.camel@tallac.gene.com
Whole thread Raw
Responses Re: text formatting in a query, a la sprintf  (Michael Fuhr <mike@fuhr.org>)
List pgsql-general
I often desire text and number formatting that's not available with functions like to_char. I'd really like to have functionality akin to sprintf, such as:

$ select no_hits,term,sprintf('http://google.com?q=%s',replace(queryterm,' ','+')) as url from queries;
$ select event,sprintf("%.2g",probability) as prob from event_probs;
$ select sprintf('<td color="%s">%s</td>',color,content) as td_cell from cells;

I considered a plperl function to do this, but plperl can't take anyarray and that means that I'd have to resort to ugliness like coding for specific numbers of args or encoding args them within in a single string... ick!

Is there a way to get more flexible formatting as in these examples within a backend query, even if not with sprintf per se?

Thanks,
Reece

-- 
Reece Hart, http://harts.net/reece/, GPG:0x25EC91A0

pgsql-general by date:

Previous
From: Andrew Sullivan
Date:
Subject: Re: problem with postgres SQL db
Next
From: John Gunther
Date:
Subject: Locale, encoding, sort order confusion