0

I have a requirement to search for the strings starting with char provided by user. I am using a postgresql function and returning SETOF-

CREATE OR REPLACE FUNCTION public._A1_loc_search15(in_pattern character varying) 
RETURNS setof character varying
AS
$$
SELECT coord FROM table1 WHERE loc_lb ~* '^in_pattern';
$$ 
LANGUAGE sql;

SELECT public._A1_loc_search15('A');

While the select query runs fine if hard coded '^A' is used, with user input it's not working. Any insight on how to use it correctly would help. Thanks.

2 Answers 2

1

It looks as if you're not actually using in_pattern as provided by the user, but just the literal string "in_pattern". So you're searching for /^in_pattern/.

Sign up to request clarification or add additional context in comments.

2 Comments

:using the suggested pattern does not works too..I am using postgresql 9.x,if this helps..earlier I also used '||^in_pattern||' simialr to a sql expression regexp_like but this doesnt works as well..
I'm sorry, I wasn't suggesting a pattern, I was merely suggesting that you're not using one.
0

You need to use the actual variable:

SELECT coord FROM table1 WHERE loc_lb ~* ('^' || in_pattern);

You could also use like (or ilike):

SELECT coord FROM table1 WHERE loc_lb like (in_pattern || '%');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.