Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8478829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:51:19+00:00 2026-06-10T18:51:19+00:00

I need a function like SELECT vformat(‘Hello %%! Bye %%…’, array[‘John’,’Maria’], ‘%%’); — ‘Hello

  • 0

I need a function like

SELECT vformat('Hello %%! Bye %%...', array['John','Maria'], '%%');
-- 'Hello John! Bye Maria...'

Supplying a string with placeholders (a template), and corresponding inputs into a vector, it returns a text. The “placeholder mark” is a free string.

I can’t use the pg9.1 format (like sprintf) function, because it does not allow other mark than ‘%’ (see ex. Python formatter) and not allow array parameter (see ex. vsprintf).
The SIMPLEST solution with PostgreSQL 9.0 (I think 8.4+) is

CREATE FUNCTION array_zipper_string(anyarray,anyarray) RETURNS text AS $$ 
  -- use with bigger array at left
  SELECT string_agg(t.val||coalesce($2[t.idx],''),'')  
  FROM  (SELECT generate_subscripts($1, 1) AS idx, unnest($1) AS val) AS t;
$$ LANGUAGE SQL IMMUTABLE;

CREATE FUNCTION tpl_positional(text,anyarray,varchar DEFAULT '%%') 
RETURNS text AS $$ 
  SELECT array_zipper_string(string_to_array($1,$3),$2);
$$ LANGUAGE SQL IMMUTABLE;
CREATE FUNCTION tpl_positional(
   text, text, varchar DEFAULT '%%',varchar DEFAULT '|' 
) RETURNS text AS $$ 
  SELECT tpl_positional($1,string_to_array($2,$4),$3);
$$ LANGUAGE SQL IMMUTABLE;

-- TESTING:
SELECT tpl_positional('Hello %%! Bye %%...', 'John|Maria');
SELECT tpl_positional('Hello %%!',array['John']);

There are well-knowed (open source) function or library for do the same?

PS: that do this and also something more (!) … My wishes list:

  • open source from a standard library
  • option to indexable placeholders, ‘Hello %%{1}! Bye %%{2}…’
  • option to use formatters, ‘There are %%2d monkeys.’ or ‘There are %%{1|2d} monkeys.’;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:51:21+00:00Added an answer on June 10, 2026 at 6:51 pm

    If Python’s formatting function does what you need, wrap it via PL/Python, and use that. You’ll need to:

    CREATE LANGUAGE plpythonu;
    

    … which may require you to install an additional package containing the python language support or re-compile using --with-python, depending on how you installed Pg or the packages you’re using. For me using the Fedora 17 packages it was yum install postgresql-plpython.

    You’ll need a signature like:

    CREATE OR REPLACE FUNCTION my_format(formatstr text, placeholder text, args VARIADIC text) RETURNS text LANGUAGE 'plpythonu' IMMUTABLE AS $$
     .... body here ....
     $$;
    

    Note the usage plpythonu. Here u means “untrusted”, ie “has no security model”. Code running in untrusted languages can’t be restricted, so the server only permits the superuser to create functions in such languages. They must be written so that they can’t be tricked into doing things other than the author intended if the database is exposed to less-trusted users.

    Unfortunately the procedural languages’ handling of arrays was, last I checked, less than wonderful. See the depesz article at the bottom of this post, and its comments. It’s workable, just clumsier than would be nice.

    If Python’s format functionality doesn’t do what you want, consider Perl. PL/Perl is very popular, partly because unlike Python it has a security model and there’s a “trusted” version of plperl. I’ll be very surprised if you can’t find a suitable CPAN module to do what you want. The function signature would be much the same, just with plperl.

    See this excellent-as-usual depesz article on plperl and sprintf.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that works like this: select score from comparestrings(@String1,@string2) I need
I need a function like var_dump($object) in php The problem is that the Var_dump
I need a function just like preg_replace but instead of strings I need it
I really need some help in Regular Expressions, i'm working on a function like
I have HTML + CSS text. I need some lib with simple function like
I have something like this: function showFunction () { // need position and place
I need to export my nodes like this: function recursive_simplify(node){ if(node.children){ for(var i =0;i<node.children.length;i++){
I need to create a function which rounds decimal numbers like this: Round($32.95, 0)
I need to pass multiple arguments to a function that I would like to
I'd like to run a preg_replace PHP function on a string but I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.