Postgresql regex_replace comma, single and double quotes in a single - sql

I have a string which consists of double quotes, single quotes and commas. I would like to replace all the occurrences of them using regex_replace.
Tried
REGEXP_REPLACE(translate (links, '"',''), '['''''',]' , '')
It replaces the first occurrence of comma not the second one.
'https://google.com/khjdbgksdngksd#/","https://google.com/khjdbgksdngksd#/","'

Why are you mixing TRANSLATE and REGEXP_REPLACE? Just pick one and use it, as either one can do all that you want.
If you want REGEXP_REPLACE to replace all instances, you have to give it a fourth argument (the flag argument) of 'g' for 'global', otherwise it stops after the first match and substitution.
Also, to preserve sanity I would use dollar-quoting when the thing being quoted has single quote marks (which yours has in considerable excess).
Using TRANSLATE is probably a better tool for the job, but your title was specifically about REGEXP_REPLACE, so:
REGEXP_REPLACE(links, $$[',"]$$, '', 'g');

Why not just use replace()?
select replace(replace(replace(links, '"', ''), '''', ''), ',', '')
Or more simply, use translate():
select translate(links, '"'',', '')

Related

SQL Substring \g

I would just like to know where do I put the \g in this query?
SELECT project,
SUBSTRING(address FROM 'A-Za-z') AS letters,
SUBSTRING(address FROM '\d') AS numbers
FROM repositories
I tried this but this brings back nothing (it doesn't throw an error though)
SELECT project,
SUBSTRING(CONCAT(address, '#') FROM 'A-Za-z' FOR '#') AS letters,
SUBSTRING(CONCAT(address, '#') FROM '\d' FOR '#') AS numbers
FROM repositories
Here is an example: I would like the string 1DDsg6bXmh3W63FTVN4BLwuQ4HwiUk5hX to return DDsgbXmhWFTVNBLwuQHwiUkhX. So basically return all the letters...and then my second one is to return all the numbers.
The g (“global”) modifier in regular expressions indicates that all matches rather than only the first one should be used.
That doesn't make much sense in the substring function, which returns only a single value, namely the first match. So there is no way to use g with substring.
In those functions where it makes sense in PostgreSQL (regexp_replace and regexp_matches), the g can be specified in the optional last flags parameter.
If you want to find all substrings that match a pattern, use regexp_matches.
For your example, which really has nothing to do with substring at all, I'd use
SELECT translate('1DDsg6bXmh3W63FTVN4BLwuQ4HwiUk5hX', '0123456789', '');
translate
---------------------------
DDsgbXmhWFTVNBLwuQHwiUkhX
(1 row)
So this is not pure SQL but Postgresql, but this also does the job:
SELECT project,
regexp_replace(address, '[^A-Za-z]', '', 'g') AS letters,
regexp_replace(address, '[^0-9]', '', 'g') AS numbers
FROM repositories;

Oracle sql make strings inside single qoutes

What I wanted is to place every string inside single qoutes even if it is delimited by a dot something like this:
Input: Hi.Hello.World
Output: 'Hi'.'Hello'.'World'
Note: Inputs can be 2 or more words delimited by a dot
You could try this:
SELECT '''' || REPLACE(string, '.', '''.''') || ''''
FROM yourTable
Demo
The idea here is we replace every dot . with dot in single quotes '.'. This covers all internal dots/quotes. Then, to handle the outside single quotes, we can concatenate them on both sides.

Using RTRIM or REGEXP_REPLACE to replace a comma with a comma space and single quote

I'm attempting to learn Oracle regexp_replace well enough to take a value stored in a table as a comma-separated string and change the comma character with a single quote followed by a comma followed by a space, followed by a single quote.
For instance, the field (CourseListT) contains course codes that look like this:
PEOE100,H003,H102,L001,L100,L110,M005,M020,M130
I want it to look like this:
'PEOE100', 'H003', 'H102', 'L001', 'L100', 'L110', 'M005', 'M020', 'M130'
I started with baby steps and found article #25997057 here that showed me how to insert spaces. So I have this working:
SELECT
regexp_replace(gr.CourseListT,'([a-zA-Z0-9_]+)(,?)',' \1\2')
FROM gradreq gr
WHERE gr.gradreqsetid = 326
AND gr.SubjectArea = 'Electives'
But nothing I do will allow me to insert those silly single quote marks.
Would it be better to learn RTRIM replace? Could somebody please help me learn how to accomplish this?
Thank you
Schelly
You can simply do it with replace. Use double single-quotes to escape a single-quote.
select '''' || replace(CourseListT, ',', ''', ''') || ''''
from gradreq

SQL Replace multiple different characters in string

I need to replace multiple characters in a string. The result can't contain any '&' or any commas.
I currently have:
REPLACE(T2.[ShipToCode],'&','and')
Which converts & to and, but how do you put multiple values in one line?
You just need to daisy-chain them:
REPLACE(REPLACE(T2.[ShipToCode], '&', 'and'), ',', '')
One comment mentions "dozens of replace calls"... if removing dozens of single characters, you could also use Translate and a single Replace.
REPLACE(TRANSLATE(T2.[ShipToCode], '[];'',$#', '#######'), '#', '')
We used a function to do something similar that looped through the string, though this was mostly to remove characters that were not in the "#ValidCharacters" string. That was useful for removing anything that we didn't want - usually non-alphanumeric characters, though I think we also had space, quote, single quote and a handful of others in that string. It was really used to remove the non-printing characters that tended to sneak in at times so may not be perfect for your case, but may give you some ideas.
CREATE FUNCTION [dbo].[ufn_RemoveInvalidCharacters]
(#str VARCHAR(8000), #ValidCharacters VARCHAR(8000))
RETURNS VARCHAR(8000)
BEGIN
WHILE PATINDEX('%[^' + #ValidCharacters + ']%',#str) > 0
SET #str=REPLACE(#str, SUBSTRING(#str ,PATINDEX('%[^' + #ValidCharacters +
']%',#str), 1) ,'')
RETURN #str
END
If you need fine control, it helps to indent-format the REPLACE() nesting for readability.
SELECT Title,
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(RTRIM(Title),
' & ',''),
'++', ''),
'/', '-'),
'(',''),
')',''),
'.',''),
',',''),
' ', '-')
AS Title_SEO
FROM TitleTable
If you use SQL Server 2017 or 2019 you can use the TRANSLATE function.
TRANSLATE(ShipToCode, '|+,-', '____')
In this example de pipe, plus, comma en minus are all replaced by an underscore.
You can change every character with its own one.
So in the next example the plus and minus are replaced by a hash.
TRANSLATE(ShipToCode, '|+,-', '_#_#')
Just make sure the number of characters is the same in both groups.
Hope this might helps to anyone
If you want to replace multiple words or characters from a string with a blank string (i.e. wanted to remove characters), use regexp_replace() instead of multiple replace() clauses.
SELECT REGEXP_REPLACE("Hello world!123SQL$##$", "[^\w+ ]", "")
The above query will return Hello world123SQL
The same process will be applied if you want to remove multiple words from the string.
If you want to remove Hello and World from the string Hello World SQL, then you can use this query.
SELECT REGEXP_REPLACE("Hello World SQL", "(Hello|World)", "")
This will return SQL
With this process, the query will not look redundant and you didn't have to take care of multiple replace() clauses.
Conclusion
If you wanted to replace the words with blank string, go with REGEXP_REPLACE().
If you want to replace the words with other words, for example replacing & with and then use replace(). If there are multiple words to be replaced, use multiple nested replace().

Cut string after first occurrence of a character

I have strings like 'keepme:cutme' or 'string-without-separator' which should become respectively 'keepme' and 'string-without-separator'. Can this be done in PostgreSQL? I tried:
select substring('first:last' from '.+:')
But this leaves the : in and won't work if there is no : in the string.
Use split_part():
SELECT split_part('first:last', ':', 1) AS first_part
Returns the whole string if the delimiter is not there. And it's simple to get the 2nd or 3rd part etc.
Substantially faster than functions using regular expression matching. And since we have a fixed delimiter we don't need the magic of regular expressions.
Related:
Split comma separated column data into additional columns
regexp_replace() may be overload for what you need, but it also gives the additional benefit of regex. For instance, if strings use multiple delimiters.
Example use:
select regexp_replace( 'first:last', E':.*', '');
SQL Select to pick everything after the last occurrence of a character
select right('first:last', charindex(':', reverse('first:last')) - 1)