How to replace escape character (\') from json data using sql query? - sql

json_data = {"jobId":"7f","created":"2020-05-24T00:22:55.705373Z","updated":"2020-05-24T00:31:03.716279Z","status":"DONE","sha265sum":"d3adf4466b5c88027478e4c","result":"FAILED","errors":[{"errorCode":"S_ERROR","errorDetail":"In: Can\'t create in vms : Error when sending ing s request:INTERNAL: PreparedStatementCallback; Duplicate entry \'Mob 66 The L\' for key \'uc\'; nested exception is Violation Exception: Duplicate entry \'Mob 66 The L\' for key \'uc\'","sId":47}
I want to replace (\') with empty char in the above data, using oracle sql query; i am trying to use
select replace(d.json_data,'\"', '"') from json_logs d where d.json_data like '%Error%'; but no luck
Thank you!

Your string has escaped single quotes, \'. But you are doung
replace(d.json_data,'\"', '"')
which is looking for escaped double quotes, \".
So you need to use:
replace(d.json_data,'\''', '''')
which also doubles-up the single quotes within the search and replacement strings, to perform Oracle's own escape of those.
You could also use the alternative quoting mechanism:
replace(d.json_data,q'[\']', q'[']')
but I'm not sure that's a lot clearer in this case.
db<>fiddle

Related

How to escape single quotes in RLIKE Hive? Double single quotes not working

I would like to escape the single quote in RLIKE input. I used double single quotes like so:
SELECT * FROM TABLE
WHERE column RLIKE 'o''brien'
But it returned the results with "obrien" rather than "o'brien". I tried "\\'" instead of double single quotes too, but that doesn't work either. So what is the correct escape character for single quote?
Three methods:
1 Put the whole regexp into double-quotes, single quote is shielded inside double-quotes:
where column rlike "o'brien"
See also: https://stackoverflow.com/a/66175603/2700344
2 Use unicode \u0027
where column rlike 'o\\u0027brien'
3 Use HEX \x27
where column rlike 'o\\x27brien'
Using \\x or \\u codes you can check any special character if you know it's code.
You can just use =:
WHERE column = 'o''brien'
I'm not sure why you are using RLIKE unless you intent:
WHERE column LIKE '%o''brien%'

How to escape '|' in SQL Server or T-SQL Query?

SELECT *
FROM performance_table
WHERE ad_group like '%|%'
I have no idea on how to escape the Pipe operator here.
You don't need to escape | in T-SQL as it has no special meaning inside like. However, if for example you would like to find texts containing % character, what you're looking for is:
SELECT *
FROM performance_table
WHERE ad_group like '%#%%' escape '#'
where escape defines escape character.
The pipe character does not need to be escaped.
Your query will find all records that contain a pipe character in the ad_group column.
When used inside a string literal ('|'), the character is not treated as an operator. Its function as an operator is bitwise OR, as for example in
select 8|3
will be 11.

insert string with " ' " to oracle

Hey I'm using oracle DB with Iron Python and I'm having trouble with strings that contains the char " ' " like in Mc'donalds. (I know it is looking for the closing ' )
The string is accepted from the user input and I want to add it to my DB as it is, meaning without omitting or changing any character.
How can I do it?
Try using the "q" (quote) function:
INSERT INTO restaurantTable (name)
VALUES (q'[O'Reilly and Conway's Irish Pub]');
You can also double-up the single apostrophes (O''Reilly and Conway''s Irish Pub). But in your case you'd have to parse them out, so either using the quote function or query parameters would work the best.
For more information: Q-quote operator introduced in Oracle 10g
Taken from PL/SQL, how to escape single quote in a string?
You can do it either using Q quoting like
q'[insert into MY_TBL (Col) values('ER0002')]';
OR you can use two quotes to denote a single quote
'insert into MY_TBL (Col) values(''ER0002'')';

Escaping a single quote in Oracle regex query

This is really starting to hurt!
I'm attempting to write a query in Oracle developer using a regex condition
My objective is to find all last names that contain charachters not commonly contained in names (non-alpha, spaces, hyphens and single quotes)
i.e.
I need to find
J00ls
McDonald "Macca"
Smithy (Smith)
and NOT find
Smith
Mckenzie-Smith
El Hassan
O'Dowd
My present query is
select * from dm_name
WHERE regexp_like(last_name, '([^A-Za-z -])')
and batch_id = 'ATEST';
which excludes everything expected except the single quote. When it comes to putting the single quote character, the Oracvel SQL Develoepr parser takes it as a literal.
I've tried:
\' -- but got a "missing right parenthesis" error
||chr(39)|| -- but the search returned nothing
'' -- negated the previous character in the matching group e.g. '([^A-Za-z -''])' made names with '-' return.
I'd appreciate anything you could offer.
Just double the single quote to escape your quote.
So
select *
from dm_name
where regexp_like(last_name, '[^A-Za-z ''-]')
and batch_id = 'ATEST'
See also this sqlfiddle. Note, I tried a similar query in SQL developer and that worked as well as the fiddle.
Note also, for this to work the - character has to be the last character in the group as otherwise it tries to find the group SPACE to ' rather than the character -.
The following works:
select *
from dm_name
WHERE regexp_like(last_name, '([^A-Za-z ''-])');
See this SQLFiddle.
Whether SQL Developer will like it or not is something I cannot attest to as I don't have that product installed.
Share and enjoy.

how to use a string that contain ' in SQL "IN" clause

i have a string value like 'Apple's'. i want to use this string in SQL "IN" clause like below query
select * from tbl_fruit where nm_fruit IN(''Apple's'','Orange');
how i can get the above query work correctly ?
Many Thanks,
Awais Afzal.
double the single quotes,
select * from tbl_fruit where nm_fruit IN ('Apple''s', 'Orange')
but if you do it on the application level, make sure you parameterized the query :)
I have found SQL correctly interprets the ASCII single-closed-quote (ALT 0146) as an apostrophe in searches while the "IN" treats it like any other character. Now I can search for 'Matt's Macintosh' using Matt(ASCII character 0146)s Macintosh" without messing up my list or the search.