Remove single quotes in Oracle - sql

I have a string like (''acc','xyz''), I need the output as ('acc','xyz').
What will be the query or any regular expression to remove extra quotes.?

Try REPLACE function:
replace(q'[(''acc','xyz'')]', q'['']',q'[']')
Demo: http://www.sqlfiddle.com/#!4/abb5d3/1
SELECT replace(q'[(''acc','xyz'')]', q'['']',q'[']')
FROM dual;
| REPLACE(Q'[(''ACC','XYZ'')]',Q'['']',Q'[']') |
|----------------------------------------------|
| ('acc','xyz') |

If that string always looks like you described, then replace two consecutive single quotes (CHR(39)) with a single one, such as
SQL> with test (col) as
2 (select q'[(''acc','xyz'')]' from dual)
3 select col,
4 replace(col, chr(39)||chr(39), chr(39)) result
5 from test;
COL RESULT
--------------- ---------------
(''acc','xyz'') ('acc','xyz')
SQL>
Why CHR(39)? Because this: replace(col, '''''', '''') is difficult to read, and this: replace(col, q'['']', q'[']') looks stupid, but - use any of these (or invent your own way).

Related

format string with comma

I am getting a string output as : A B C D
I want to get it converted to: A,B,C and D.
Is there and function in PostgreSQL to do it.
Try this:
select REPLACE('A B C D', ' ', ',')
And to replace A,B,C,D to A,B,C and D use the following:
select substring('A,B,C,D',1,
length('A,B,C,D')-position(',' in reverse('A,B,C,D')))
|| ' and ' ||
substring('A,B,C,D',length('A,B,C,D')-position(',' in reverse('A,B,C,D'))+2);
You can combine the above with replace to make it single query.
I would use a two-step approach. The first step replaces all spaces with a comma. And then the last comma is replaced with the AND using a regular expression:
regexp_replace(replace('A B C D',' ',','), '(.*)(,)(\w+)$', '\1 and \3')
Online example
No need to complicate things.
A single regex function that capture each element and lets you do whatever you want with it.
select regexp_replace(mycol, '(\S)+\s+(\S+)\s+(\S)\s+(\S+)','\1,\2,\3 and \4')
from mytab
-
+----------------+
| regexp_replace |
+----------------+
| A,B,C and D |
+----------------+
SQL Fiddle

Extract Value from a string PostgreSQL

Simple Question
I have the following type of results in a string field
'Number=123456'
'Number=1234567'
'Number=12345678'
How do I extract the value from the string with regard that the value can change between 5-8 figures
So far I did this but I doubt that fits my requirement
SELECT substring('Size' from 8 for ....
If I can tell it to start from the = sign till the end that would help!
The likely simplest solution is to trim 7 leading characters with right():
right(str, -7)
Demo:
SELECT str, right(str, -7)
FROM (
VALUES ('Number=123456')
, ('Number=1234567')
, ('Number=12345678')
) t(str);
str | right
-----------------+----------
Number=123456 | 123456
Number=1234567 | 1234567
Number=12345678 | 12345678
You could use REPLACE:
SELECT col, REPLACE(col, 'Number=', '')
FROM tab;
DBFiddle Demo
Based on this question:
Split comma separated column data into additional columns
You could probably do the following:
SELECT *, split_part(col, '=', 2)
FROM table;
You may use regexp_matches :
with t(str) as
(
select 'Number=123456' union all
select 'Number=1234567' union all
select 'Number=12345678' union all
select 'Number=12345678x9'
)
select t.str as "String",
regexp_matches(t.str, '=([A-Za-z0-9]+)', 'g') as "Number"
from t;
String Number
-------------- ---------
Number=123456 123456
Number=1234567 1234567
Number=12345678 12345678
Number=12345678x9 12345678x9
--> the last line shows only we look chars after equal sign even if non-digit
Rextester Demo

Convert array to rows in Postgres

If I have something like this in SQL statement ('A','B','C'), how do I convert it into a column with multiple rows like this
col
---
A
B
C
I cannot change the way that string is created (as it is injected into SQL query from external program). For example, I cannot make it as ['A','B','C'] (replace with square brackets). I could wrap anything around it though like [('A','B','C')] or whatever.
Any help?
UPDATE 1
I have PostgreSQL 8.4.20
You could create an ARRAY from VALUES and then unnest it:
SELECT
unnest(ARRAY[col_a, col_b, col_c])
FROM
(VALUES('A','B','C')) AS x(col_a, col_b, col_c)
Result:
| unnest |
|--------|
| A |
| B |
| C |
Edit: you could also tweak jspcal's answer by using dollar quotes ($$) like this so you can concatenate your string into the SQL statement:
SELECT * FROM regexp_split_to_table(
regexp_replace(
$$('A','B','C','D','foo')$$,
'^\(''|''\)+', '', 'g'),
''','''
);
The built-in regexp_split_to_table function will do this for you. Since you plan to inject it directly without escaping, use $$ (dollar quoting) from thibautg's answer.
select * from regexp_split_to_table(
regexp_replace($$('A','B','C')$$, '^\(''|''\)+', '', 'g'),
''','''
);

Oracle Update with Regular Expression

I have some values in a column like:
"This is test $ABC variables."
I want to update all the variable definition to below one:
"This is test $ABC$ variables."
How can I write this update statement? Somehow, I couldn't manage.
Do you just want replace()?
update t
set col = replace(col, '$ABC', '$ABC$')
where col like '%$ABC%';
You may use this REGEXP query. The parentheses in '\$(\w+)' capture the word following $ in \1
SELECT REGEXP_REPLACE (
'This is test $ABC variables.This is a second variable $variable2.I have 100$',
'\$(\w+)',
'$\1$')
FROM DUAL;
O/p:
This is test $ABC$ variables.This is a second variable $variable2$.I have 100$
Unfortunately, I'm not good at regular expressions so this poor attempt of mine could probably be replaced by something smarter. Looking forward to see it!
SQL> with test as
2 (select 'This is test $ABC variables' col from dual union
3 select 'Stackoverflow says $DEF is so cool' from dual
4 )
5 select
6 col,
7 regexp_replace(col, '\$\w+', regexp_substr(col, '\$\w+') || '$') result
8 from test;
COL RESULT
---------------------------------- ----------------------------------------
Stackoverflow says $DEF is so cool Stackoverflow says $DEF$ is so cool
This is test $ABC variables This is test $ABC$ variables
SQL>

how to skip particular character(s) in SQL LIKE query

I have a table(say users) in which there is a column say name.
you may think table structure a shown below:
-------------
name
--------------
Abdul Khalid
--------------
Abdul, Khalid
--------------
Abdul - Khalid
--------------
other names
My question is can I do some query to find all the 3 rows in which the name column value is "Abdul Khalid"(basically "Abdul Khalid" or "Abdul, Khalid" or "Abdul - Khalid" if I skip the "," and "-" character).
You can use like:
select t.*
from t
where name like 'Abdul%Khalid';
If you want the names anywhere in the string (but in that order), then put wildcards at the beginning:
select t.*
from t
where name like '%Abdul%Khalid%';
If you are passing in the value as a variable:
select t.*
from t
where name like replace('Abdul Khalid', ' ', '%');
For PostgreSQL is better to use '~'
name ~ '^Abdul[ ,-]Khalid$'
OR if you want also in middle of string:
name ~ 'Abdul[ ,-]Khalid'
Or you can use translate (with index on it) for any SQL:
translate(name, ' ,-') = 'AbdulKhalid'
you also can use REGEXP like this:
SELECT * from yourTable where name REGEXP 'Abdul( |, | - )Khalid';