I use pg-promise#8.4.4 and I would like to have wildcard characters and the unaccent function of the PostgreSQL.
My pl/pgsql query has something like
WHERE unaccent(p.name) ILIKE (''%'' || unaccent($1) || ''%'')'
I want to make it into a pg-promise query like
uniqueQuery('select id, name from place WHERE unaccent(name) ILIKE \'% unaccent($1) #%\' ', [name])
Without the unaccent it works fine. When I add the unaccent , I get a synta error : error: syntax error at or near "ονομα μερους" (ονομα μερους is an the string I am searching for).
How can I combine the wildcards with the unaccent ?
Thanks
Related
How can I match a backslash with the like operator in Redshift?
I tried below, but they didn't work…
-- syntax error
select 'a\a' like '\';
-- false
select 'a\a' like '\\';
-- syntax error
select 'a\a' like '\\\';
-- syntax error
select 'a\a' like '\' escape '^';
-- syntax error
select 'a\a' like '^\' escape '^';
Envelope it with %:
select 'a\a' like '%\\%';
I want to execute query like this:
SELECT '{"a":[1,2,3],"b":[4,5,6]}'::json#>>'{a,2}'
But anorm can not prepare the query and raise the parse error.
How to escape the '{' and '}' symbols in SQL text for anorm?
Update:
the solution is a pretty simple (like RTFM:) )
SELECT '{"a":[1,2,3],"b":[4,5,6]}'::json#>>'{a,2}'
should be
SELECT '{"a":[1,2,3],"b":[4,5,6]}'::json->'a'->>2
there are no symbols '{', '}' so Anorm is happy.
You can pass the values as stings which contain curly braces
Look at this example
SELECT '{}'::jsonb;
so I replaced it with binding
SELECT {empty}::jsonb;
And passed this named param on executing the query
NamedParameter("empty", "{}")
I have a question about concat function in PostgreSQL.
This sentence works well in MySQL:
SELECT * FROM words
WHERE CONCAT (word,' ',gender,' ',semantic) LIKE '%"+value+"%'.
Value is a variable that is changed inside my Java program.
But I need the same working in postgresql. How kann I concatenate inside the WHERE clause, using postgres, considering the variable "value" that will have its value changed ?
In Postgresql you concatenate using the following syntax:
(users.first_name || ' ' || users.last_name)
Reference: http://www.postgresql.org/docs/9.1/static/functions-string.html
You can also use this for postgresql:
concat_ws(' ', campo1, campo2) like '%value%'
Note that there is a space between single quotes
I have a column url encoded with urlencode in php. I wish to make a select like this
SELECT some_mix_of_functions(...) AS Decoded FROM table
Replace is not a good solution because I will have to add all the decoding by hand. Any other solution to get the desire result ?
Yes you can:
CREATE OR REPLACE FUNCTION decode_url_part(p varchar) RETURNS varchar AS $$
SELECT convert_from(CAST(E'\\x' || string_agg(CASE WHEN length(r.m[1]) = 1 THEN encode(convert_to(r.m[1], 'SQL_ASCII'), 'hex') ELSE substring(r.m[1] from 2 for 2) END, '') AS bytea), 'UTF8')
FROM regexp_matches($1, '%[0-9a-f][0-9a-f]|.', 'gi') AS r(m);
$$ LANGUAGE SQL IMMUTABLE STRICT;
This creates a function decode_url_part, then you can use it like that:
SELECT decode_url_part('your%20urlencoded%20string')
Or you can just use the mix of functions and subqueries from the body of the above function.
This doesn't handle '+' characters (representing whitespace), but I guess adding this is quite easy (if you ever need it).
Also, this assumes utf-8 encoding for non-ascii characters, but you can replace 'UTF8' with your own encoding if you want.
It should be noted that the above code relies on undocumented postgresql feature, namely that the results of regexp_matches function are processed in the order they occur in the original string (which is natural, but not specified in docs).
As Pablo Santa Cruz notes, string_agg is a PostgreSQL 9.0 aggregate function. The equivalent code below doesn't use it (I hope it works for 8.x):
SELECT convert_from(CAST(E'\\x' || array_to_string(ARRAY(
SELECT CASE WHEN length(r.m[1]) = 1 THEN encode(convert_to(r.m[1], 'SQL_ASCII'), 'hex') ELSE substring(r.m[1] from 2 for 2) END
FROM regexp_matches($1, '%[0-9a-f][0-9a-f]|.', 'gi') AS r(m)
), '') AS bytea), 'UTF8');
Not out of the box. But you could create a pl/perl function that wraps the perl equivalent. (Or a pl/php function).
Tried select to_ascii('capo','LATIN1'), to_ascii('çapo','LATIN1') and the results are different....
Look here.
CREATE FUNCTION to_ascii(bytea, name)
RETURNS text STRICT AS 'to_ascii_encname' LANGUAGE internal;
and then just use it like this:
SELECT to_ascii(convert_to('Übermeier', 'latin1'), 'latin1');