Oracle sql make strings inside single qoutes - sql

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.

Related

4 quotes always give different result in SQL Select statement

So I've been googling for a couple of hours and I still don't understand a single thing about escaping quotes in sql.
Can somebody please explain me if '''' in sql means ' why does select '('||''''||')' give (') why not (''')?
A string in SQL must be enclosed in single quotes, e.g. 'a'
A single quote inside a SQL string is escaped by doubling it: '' - to create a string out of that, you need to enclose that in two single quotes: '''' - so the '' in the middle of that string are the same as the a in my first example.
The expression: '('||''''||')' consists of three string constants:
'(' --> (
'''' --> ' (as explained above)
')' --> )

TRIM doesn't remove inner whitespaces

I try to remove all whitespaces inside a string. For this, I use TRIM() function. Unfortunately it doesn't work as expected, inner whitespaces (between 35 and 'A') remain untouched:
select TRIM('Hopkins 35 A Street') as Street
Column type is nvarchar. The funny thing is that this function works fine (using example from above) when executed on W3Schools (TRIM function example): https://www.w3schools.com/sql/func_sqlserver_trim.asp.
I can use replace on this string and replace ' ' into '' without a problem. I work on SQL Server 18.7.1 (2020)
if you use TRIM like this you are only removing leading and trailing spaces from a string. To remove also spaces in between you should change to:
select TRIM(' ' FROM 'Hopkins 35 A Street') as Street
UPDATE: if you meant to remove all spaces you should use
SELECT REPLACE('Hopkins 35 A Street', ' ', '')
TRIM is only intended to make a double space become a single one
"Trimming" means the removal of whitespace from the start and/or the end of a string value. It never means (and has never meant to mean) the removal of whitespace within a string value (enclosed by non-whitespace characters).
You can indeed use the TRIM function with a FROM in its argument to specify other characters than whitespace to trim. In that case, the TRIM function will remove the specified characters from the start and the end of the string, but not within the string (enclosed by other characters).
In other words: the specified characters will be treated as if they were whitespace as well, but specifying them so will not affect the trimming behavior/algorithm itself.
Check out the sample on Microsoft Docs:
SELECT TRIM( '.,! ' FROM ' # test .') AS Result;
produces this result: # test
TRIM function will only remove only the leading and tailing spaces in the data. It cannot remove all the spaces in the data. I mean it cannot remove all the spaces if there are any spaces in the data like 'Hello World'. TRIM cannot remove the space between the word Hello and World and make it look like 'HelloWorld'. If you want to remove all the spaces, you can use the REPLACE function. In the REPLACE function you can replace the space with any character/number/symbol. If you don't need any you can simply remove the space with ''. like
SELECT REPLACE('Hopkins 35 A Street', ' ', '')

Postgresql regex_replace comma, single and double quotes in a single

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, '"'',', '')

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

how not to replace "]" when using regex_replace for removing special characters

I'm trying to remove few special characters from a comment column in my table. I used the below statement but it seems to remove the ']' even though it is in the ^[not] list.
UPDATE TEST
set comments=REGEXP_REPLACE(
comments,
'[^[a-z,A-Z,0-9,[:space:],''&'','':'',''/'',''.'',''?'',''!'','']'']]*',
' '
);
The table data contains the following:
[SYSTEM]:Do you have it in stock? 😊
My requirement is to have:
[SYSTEM]:Do you have it in stock?
You have two mistakes in you regex:
Do not put characters in quotes and don't split them with comma.
Remove inner square brackets.
And place closing square brackets first in the list, just after initial circumflex. Fixed regex:
UPDATE TEST set comments=REGEXP_REPLACE(comments,'[^]a-zA-Z0-9[:space:]&:/.?!]*',' ');
My try, I just removed the commas, put the "accepted" characters after the initial "not"(no brackets).
A special case are the brackets: https://dba.stackexchange.com/a/109294/6228
select REGEXP_REPLACE(
'[ION] are varză murată.',
'[^][a-zA-Z0-9[:space:]&:/,.?!]+',
' ')
from dual;
Result:
[ION] are varz murat .