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 '%\\%';
Related
I am trying to create a formatted string for a WHERE clause.
I need to insert LIKE syntax inside the FORMAT() function.
Here is my SQL code snippet:
FORMAT(' AND student_name LIKE %%L% ', CAST(jsnData->>'term' AS VARCHAR) );
As the documentation says,
In addition to the format specifiers described above, the special sequence %% may be used to output a literal % character.
So your expression should be
format($$ AND student_name LIKE '%%' || %L || '%%' $$, jsnData->>'term')
(The cast is unnecessary.)
For people who tried or needed a solution to escape every special character (even $) in a WHERE CLAUSE in POSTGRESQL, here is how it should be use
the documentation can be somehow hard to understand, and there is no proper example of it so here is mine
e.g : if you want to make a request looking as
SELECT
*
FROM
<TableName>
WHERE
<ColumnName> = 'string with ' character';
it will throw an error cause "character'" is outside the string
So here is how it should be written:
SELECT
*
FROM
<TableName>
WHERE
<ColumnName> = $$string with ' character$$;
The WHERE CONDITION will take the string literally; the interface may look broken but the following instruction will still be interpreted as expected.
SELECT
*
FROM
<TableName>
WHERE
<ColumnName> = $$string with ' character$$ AND <OtherColumnName> IS NOT NULL;
This could even be another escaped string with $$.
For details about dollar quoting, look at the documentation.
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
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 table containing 4 lines as follow:
Audit\\subfolder\System2.log
Audit\\subfolder/System1.log
Audit\\System2.log
Audit\\System1.log
Running the following query:
select * from table where file_location SIMILAR TO
'Audit\\[^/]*'
will yield:
Audit\\subfolder\System2.log
Audit\\System2.log
Audit\\System1.log
However running the following query:
select * from table where file_location SIMILAR TO
'Audit\\[^\\]*'
yields me nothing.
What would be the correct way to escape \\ in SIMILAR TO clause?
What would be the correct way to escape \ in SIMILAR TO clause?
Using ESCAPE might be a natural choice:
SELECT *
FROM table1
WHERE file_location SIMILAR TO 'Audit\\[^!\]*' ESCAPE '!'
Here is SQLFiddle demo