This question already has answers here:
how to make a like search in postgresql and node js
(2 answers)
Closed 5 years ago.
Suppose you have a query like this:
SELECT * FROM the_table WHERE (name LIKE %$1%) LIMIT 10
And a values array like this:
[tyrone]
The query works without the %% syntax used with LIKE operator. Is it possible to combine these ideas? Any reference to the LIKE operator I can find abandons the use of placeholders.
I am receiving the following error when attempting the query above:
{"name":"error","length":90,"severity":"ERROR","code":"42601","position":"38","file":"scan.l","line":"1086","routine":"scanner_yyerror"}
SQL doesn't have string interpolation so you cannot use placeholders in string literals. You must instead use concatenation, e.g.
SELECT * FROM the_table WHERE (name LIKE '%' || $1 || '%') LIMIT 10
Note that the result of '%' || NULL is NULL.
Related
This question already has answers here:
How to make "case-insensitive" query in Postgresql?
(14 answers)
PostgreSQL accent + case insensitive search
(2 answers)
Case Insensitive searches/queries
(2 answers)
How to make LIKE clause case-insensitive?
(2 answers)
PostgreSQL citext index vs lower expression index performance
(1 answer)
Closed 3 years ago.
I have a lot of sql query command that use "WHERE" clause, I have just wondered that my postgresql searching was case sensitive.
for example:
Select * From myarea Where area_name = 'Jawa Barat1' --> not found
Select * from myarea Where area_name = 'jawa barat1' --> found
How to turn off the case sensitive searching in postgresql?
Please don't suggest me to change the sql command to set to lowercase at both side.
expect postgresql to in-case-sensitive searching (Mother = mother)
demo:db<>fiddle
Using ILIKE comparator, which (as the LIKE comparator) works also with wildcards (see fiddle)
Select * From myarea Where area_name ILIKE 'Jawa Barat1'
Or normalize string with lower() which converts all letters to non-capitals. You have to use it for both comparing literals:
Select * From myarea Where lower(area_name) = lower('Jawa Barat1')
This question already has answers here:
How to find a table having a specific column in postgresql
(6 answers)
Closed 5 years ago.
I want to search for a particular variable name in ‘PostgreSQL’ database. Similar to the following ‘Teradata’ query
Select TableName, ColumnName from
DBC.Columns
Where ColumnName like (‘%profile%’)
Is there a similar query in PostgreSQL?
Postgres documentation
SELECT table_name,column_name
FROM information_schema.columns
WHERE column_name like '%profile%'
For detecting the existence of a key in a hstore, I need to run a query like this:
SELECT * FROM tbl WHERE hst ? 'foo'
However, that gives me a PDOException:
PDOException: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound: SELECT * FROM tbl WHERE hst ? 'foo'
Is there any way to escape the question mark so PDO won't pick it up as a placeholder? I've tried with up to four backslashes, as well as a double question mark (??), but nothing seems to persuade PDO to leave the question mark alone.
Use the function call form. According to the system catalogs, the hstore ? operator uses the exist function:
regress=# select oprname, oprcode from pg_operator where oprname = '?';
oprname | oprcode
---------+---------
? | exist
(1 row)
so you can write:
SELECT * FROM tbl WHERE exist(hst,'foo');
(Personally I'm not a big fan of hstore's operator-centric design and documentation, I think it discards the useful self-documenting properties of a function based interface without any real benefit and I usually use its function calls rather than its operators. Just because you can define operators doesn't mean you should.)
I had the same problem when searching on JSONB data. The full question is here
SELECT * FROM post WHERE locations ? :location;
The workaround on PostgreSQL 9.5 is similar:
SELECT * FROM post WHERE jsonb_exists(locations, :location);
I also opened a ticket at PHP bug tracing system
Update
As Diabl0 mentioned, the proposed solution work but does not use the index.
Tested with:
CREATE INDEX tempidxgin ON post USING GIN (locations);
I suggest you disable PDO native prepared statement so question marks will be ignored:
$pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
I want to select a column value and trim away a suffix matching a regular expression, all inside PostgreSQL, how can this be done?
-- somewhat like performing s/_bar$// on "foo_bar"
select regexp_replace('foo_bar', '_bar$', '')
I cannot get the like statement to work with space and trailing wildcard.
My query goes as follows:
select * from Table where Field like 'Desc_%'
The data is space-delimited such as, Desc Top, Desc Bottom and so on. The query works when I use the pattern 'Desc_%' but not when I use the pattern 'Desc %'. The field is nvarchar(255).
Any ideas?
EDIT
Turns out the data was tab-delimited and when I copied a value from the 2008 Management Studio it converted the tab to space. Dumb mistake. I did like the [ ] tip so I marked it the answer. Thanks everyone, I'll remember not to trust the copy from the grid results.
Use brackets '[' & ']' to set up a single-character class to match. In your case the SQL should look like this: "select * from Table where Field like 'Desc[ ]%'"
EDIT: add sample, link
CREATE TABLE #findtest (mytext varchar(200) )
insert #findtest VALUES ('Desc r')
insert #findtest VALUES ('Descr')
select * from #findtest where mytext like 'Desc[ ]%'
DROP TABLE #findtest
(1 row(s) affected)
(1 row(s) affected)
mytext
--------
Desc r
(1 row(s) affected)
See this article.
Since an underscore is a single character wildcard, and percent is the multi-char wildcard, they are the same ( "%" and "_%" ). It is as if you are asking for two consecutive wildcards. Not sure if I understand your question, but I am not surprised it does not behave the way you expect.
Consider explicitly stating that you want a space, using its ASCII value?
SELECT * FROM Table WHERE Field Like 'Desc' + CHAR(32) + '%'