Noob question for plpgsql. How to format a string...
# sql
p
Noob question for plpgsql. How to format a string using printf-like semantics in a script? Basically I want the equivalent of
newstr := fmt.Sprintf("%s-%s", "foo", "bar")
This has been surprisingly difficult to google.
t
there is a format function
p
Hmm I thought I tried this, but I guess it's working:
Copy code
CREATE OR REPLACE FUNCTION str_cat(
  a text, 
  b text) 
RETURNS text 
AS $$
declare
    out text;
begin
    out := a || b;
    out := a || '-' || b;
    out := format('%s | %s', a, b);
    return out;
END;
$$ LANGUAGE plpgsql;
t
obviously only the most recent
out
would do anything. I'd look up
out
to check that it is a reserved word if your having any odd issues. https://www.postgresql.org/docs/current/sql-keywords-appendix.html