psql adding blank spaces to a text -
hi i'm new psql , have problem have varchar variable in function , want add blank spaces before , after it. this:
create function func(word varchar) returns varchar $word2$ declare word varchar; word2 varchar; begin word2 = ' ' word ' ' ; return word2; end; $word2$ language plpgsql ; is there quick , easy way? thanks.
you declare:
word varchar; and yet have in argument same name:
create function func(word varchar) what happens is: function ignores argument word, because redeclare it. docs:
if default clause not given variable initialized sql null value
you don't have := 'come value' after word varchar becomes null.
on other hand concatenation operator || not ignore null concat() function, result of ' ' word ' ' null, no matter what.
here example of working function:
t=# create or replace function func(word varchar) returns varchar $word2$ begin return ' '||word||' '; end; $word2$ language plpgsql ; create function time: 13.018 ms t=# select func('text'); func -------- text (1 row) time: 0.260 ms
Comments
Post a Comment