how to remove ' from string - sql

Im trying to use the TRIM command in SQL to Remove special characters from a string. thing is i cant seem to figure out how to remove the ' character like how when people use it in their surname.
e.g O'Reilly
in order to remove a character i have to quote it, but how can put in quotes or identify the character ' when it is used for quoting.

You want to use replace() and not trim(). Then, the escaping of single quotes requires doubling it, plus the outer single quotes. So:
replace(name, '''', '')
---------------^^ escaped single quote
--------------^--^ string delimiter for the single quote character

Use Replace function to replace that character (').
replace(name,"'","");
Link 1
Link 2

Related

replace backslash in redshift

I am working with a string column in redshift database where the instance of \" occurs multiple times in the same value.
I want to replace every occurrence of \" with "
For example, if a string = \"name\"
I want the output to be string = "name"
From what I have found, redshift does not allow the existence of a single backslash, and automatically converts it to a double backslash, but that is not happening in this case.
I have tried to use the REPLACE() with REPLACE( string, '\"', '"' ) but it did not have any effect. Can the string being a JSON string have any bearing on the function of REPLACE()?
I have been trying to use regexp_replace but maybe I am not using the right regular expression, hence I am not able to solve the problem.
REPLACE( string, '\\"', '"' ) seems works in this situation. I am guessing its because redshift doesn't allow a single backslash, but converts them to double backslash.
So, even though the string looked like \"name\" it was probably stored as \\"name\\" and hence putting a single backslash in the replace was not working.
EDIT: please read Bill's explanation in the comment below this reply

How to remove semicolon in a string in HIVE SQL

I am trying to remove ";" semi-colon from a string.
What command in HIVE SQL should I use. I know regexp_replace may work..but what to put ?
It appears that ; - the special character does not work but other special characters like , or : works.
For example ,
Data looks like
;;;;;0123445
I want the data to look like this
0123445
Any help on this will be appreciated. I have been struggling with this.
REGEXP_REPLACE indeed looks like a good pick. For example, this removes all semicolons from the field :
REGEXP_REPLACE(my_column, ';', '')
From the documentation :
Returns the string resulting from replacing all substrings in INITIAL_STRING that match the java regular expression syntax defined in PATTERN with instances of REPLACEMENT.
Please note that the semicolon has no special meaning in the regexp language.
If you want to match on semicolons on the beginning of the string only (as shown in your question), use regexp special character ^, which indicates the beginning of the string
REGEXP_REPLACE(my_column, '^;', '')
To remove all semicolons, you can simply use replace():
replace(my_column, ';', '')
To remove leading semicolons, you can use:
replace(my_column, '^;+', '')
In Hive you need to escape the semi-colon.
regexp_replace(column_name,'\;','')

Remove Special Characters from an Oracle String

From within an Oracle 11g database, using SQL, I need to remove the following sequence of special characters from a string, i.e.
~!##$%^&*()_+=\{}[]:”;’<,>./?
If any of these characters exist within a string, except for these two characters, which I DO NOT want removed, i.e.: "|" and "-" then I would like them completely removed.
For example:
From: 'ABC(D E+FGH?/IJK LMN~OP' To: 'ABCD EFGHIJK LMNOP' after removal of special characters.
I have tried this small test which works for this sample, i.e:
select regexp_replace('abc+de)fg','\+|\)') from dual
but is there a better means of using my sequence of special characters above without doing this string pattern of '\+|\)' for every special character using Oracle SQL?
You can replace anything other than letters and space with empty string
[^a-zA-Z ]
here is online demo
As per below comments
I still need to keep the following two special characters within my string, i.e. "|" and "-".
Just exclude more
[^a-zA-Z|-]
Note: hyphen - should be in the starting or ending or escaped like \- because it has special meaning in the Character class to define a range.
For more info read about Character Classes or Character Sets
Consider using this regex replacement instead:
REGEXP_REPLACE('abc+de)fg', '[~!##$%^&*()_+=\\{}[\]:”;’<,>.\/?]', '')
The replacement will match any character from your list.
Here is a regex demo!
The regex to match your sequence of special characters is:
[]~!##$%^&*()_+=\{}[:”;’<,>./?]+
I feel you still missed to escape all regex-special characters.
To achieve that, go iteratively:
build a test-tring and start to build up your regex-string character by character to see if it removes what you expect to be removed.
If the latest character does not work you have to escape it.
That should do the trick.
SELECT TRANSLATE('~!##$%sdv^&*()_+=\dsv{}[]:”;’<,>dsvsdd./?', '~!##$%^&*()_+=\{}[]:”;’<,>./?',' ')
FROM dual;
result:
TRANSLATE
-------------
sdvdsvdsvsdd
SQL> select translate('abc+de#fg-hq!m', 'a+-#!', etc.) from dual;
TRANSLATE(
----------
abcdefghqm

How to escape value in parameter passed to Oracle SQL script

I have an sql script which is executed using sql plus. It reads input parameters and the beginning looks like this:
SET DEFINE ON
DEFINE PARAM1 = '&1'
DEFINE PARAM2 = '&2'
DECLARE
...
Now I would like to use this script with the parameters, but I need to use some special characters, particularly '
##./update.sql 'value of first param' 'Doesn't work'
^
--------------------------------------------| Here's the problem
commit;
When I do the usual way of concatenation strings like this:
'Doesn'||chr(39)||'t work'
only Doesn appear in the PARAM2. Is there some way to escape the character in a way that the sqlplus will read it as a single string?
You need to use escape characters to achieve this.
{} Use braces to escape a string of characters or symbols. Everything within a set of braces in considered part of the escape sequence. When you use braces to escape a single character, the escaped character becomes a separate token in the query.
\ Use the backslash character to escape a single character or symbol. Only the character immediately following the backslash is escaped.
Some examples on single character escape
SELECT 'Frank''s site' AS text FROM DUAL;
TEXT
--------------------
Franks's site
Read more here
For escaping & in SQL*Plus
SET ESCAPE '\'
SELECT '\&abc' FROM dual;
OR
SET SCAN OFF
SELECT '&ABC' x FROM dual;
Escaping wild card
SELECT name FROM emp
WHERE id LIKE '%/_%' ESCAPE '/';
SELECT name FROM emp
WHERE id LIKE '%\%%' ESCAPE '\';
From a shell you should call the script like that:
sqlplus user#db/passwd #update 'value of first param' "Doesn't work"
(the word #update refer to your script which is named update.sql)
Then you have to use literal quoting in your script:
DEFINE PARAM2 = q'[&2]';
Documentation for literals can be found here.

SQL Server LIKE containing bracket characters

I am using SQL Server 2008. I have a table with the following column:
sampleData (nvarchar(max))
The value for this column in some of these rows are lists formatted as follows:
["value1","value2","value3"]
I'm trying to write a simple query that will return all rows with lists formatted like this, by just detecting the opening bracket.
SELECT * from sampleTable where sampleData like '[%'
The above query doesn't work, because '[' is a special character. How can I escape the bracket so my query does what I want?
... like '[[]%'
You use [ ] to surround a special character (or range).
See the section "Using Wildcard Characters As Literals" in SQL Server LIKE
Note: You don't need to escape the closing bracket...
Aside from gbn's answer, the other method is to use the ESCAPE option:
SELECT * from sampleTable where sampleData like '\[%' ESCAPE '\'
See the documentation for details.
Just a further note here...
If you want to include the bracket (or other specials) within a set of characters, you only have the option of using ESCAPE (since you are already using the brackets to indicate the set).
Also you must specify the ESCAPE clause, since there is no default escape character (it isn't backslash by default as I first thought, coming from a C background).
E.g., if I want to pull out rows where a column contains anything outside of a set of 'acceptable' characters, for the sake of argument let's say alphanumerics... we might start with this:
SELECT * FROM MyTest WHERE MyCol LIKE '%[^a-zA-Z0-9]%'
So we are returning anything that has any character not in the list (due to the leading caret ^ character).
If we then want to add special characters in this set of acceptable characters, we cannot nest the brackets, so we must use an escape character, like this...
SELECT * FROM MyTest WHERE MyCol LIKE '%[^a-zA-Z0-9\[\]]%' ESCAPE '\'
Preceding the brackets (individually) with a backslash and indicating that we are using backslash for the escape character allows us to escape them within the functioning brackets indicating the set of characters.