midhun k
midhun k

Reputation: 1068

psql query not returning proper results?

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.

Upvotes: 1

Views: 65

Answers (2)

Gurmokh
Gurmokh

Reputation: 2081

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.

Upvotes: 1

Evan Carroll
Evan Carroll

Reputation: 1

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).

Upvotes: 0

Related Questions