1
var searchValue = 'shahid';
var query = ("select * from students where name ilike '%"+searchValue+"%'");

This is my psql query, but it is not returning any values. So that I just console the query to know the execution.

The query is executing as:

select * from students where name ilike 'hahid%'

When I capitalize the first letter of search value (Shahid), it's executing perfectly.

2 Answers 2

1

If you want to pass in the upper case you should convert the variable searchValue eg.

var newSearchValue = (select initcatp(searchValue)) ;

This will convert 'shahid' to 'Shahid' Then use this in your query variable.

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

Comments

0

This, lacking a '%' on the left hand side, will only match thing that start with hahid

 select * from students where name ilike 'hahid%'

It's not the same as this

select * from students where name ilike 'Shahid%'

which will match only things that start with Shahid. Now if you want something that will match anything with hahid then you want

 select * from students where name ilike '%hahid%'

BTW, your example is extremely insecure if searchValue comes from I/O (user, file, network etc).

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.