postgresql - Postgres LIKE operator question -


so let's have function takes in 1 parameter

create function(i_param varchar) 

i want select 1 table where col_name 'i_param%'

i tried doing like i_param||'%' think literally returns string 'i_param' not value put function.

it doesn't give me errors returns 0 rows. how like on input parameter?

pretty new stuff, great! thanks!

as note, starting in 9.2, can things way are. in previous versions, should error instead.

the fact didn't error suggests use of i_param in colliding column name. recommend prefixing input variables reason (i use "in_" in work).

for example, if you:

create table foo (    foo text,    bar text ); 

then following not generate error:

create function search_foo(bar text) returns setof foo language sql $$  select * foo foo ilike bar || '%';  $$; 

however in fact comparing column foo against column bar. following give errors:

create function search_foo(in_bar text) returns setof foo language sql $$  select * foo foo ilike in_bar ||'%';  $$; 

i highly recommend avoiding variables same names arguments. leads problems in many places.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -