REPLACE 2 string with replacement string in sql - sql

SSN=LTRIM(RTRIM(REPLACE(A.SSN,'-','')))
Can some one please help me? This code replaces "-" with '' of ssn column which works. But now I want to also replace dots . with '' string. Can someone help me how to replace both - and .?
Any help will be highly appreciated.
Thank you

Use one more replace
LTRIM(RTRIM(REPLACE(REPLACE(A.SSN,'-',''),'.','')))
If you are using Oracle, the easiest way to do this is using regexp_replace to only get the digits.
regexp_replace(ssn, '\D', '')

Related

KQL - Remove Characters from String

just starting out with KQL, I currently have a string which is set to: "server1-Incremantal")
I am looking to remove the front '"' and trailing '")' . So effectively just reads "server1-Incremantal" (without the quotes) any help to get this done will greatly appreciated!!
I am looking to for the string to read "server1-Incremantal" (without the quotes) any help to get this done will greatly appreciated!!
you can use the parse operator.
other functions you may find helpful are:
trim_end()
trim_start()
trim()
substring()
for example:
print input = '"server1-Incremantal")'
| parse input with '"' output '")'
input
output
"server1-Incremantal")
server1-Incremantal

TERADATA REGEXP_SUBSTR Get string between two values

I am fairly new to teradata, but I was trying to understand how to use REGEXP_SUBSTR
For example I have the following cell value = ABCD^1234567890^1
How can I extract 1234567890
What I attempted to do is the following:
REGEXP_SUBSTR(x, '(?<=^).*?(?=^)')
But this didnt seem to work.
Can anyone help?
It might (or might not) be possible to use REGEXP_SUBSTR() to handle this, but you would need to use a capture group. An alternative here would be to do a regex replacement instead:
SELECT x, REGEXP_REPLACE(x, '^.*?\^|\^.*$', '') AS output
FROM yourTable;
The regex pattern used here matches:
^.*?\^ everything from the start to the first ^
| OR
\^.*$ everything from the second ^ to the end
We then replace with empty string to remove the content being matched.

REGEXP REPLACE with backslashes in Spark-SQL

I have a string containing \s\ keyword. Now, I want to replace it with NULL.
select string,REGEXP_REPLACE(string,'\\\s\\','') from test
But unable to replace with the above statement in spark sql
input: \s\help
output: help
want to use regexp_replace
To replace one \ in the actual string you need to use \\\\ (4 backslashes) in the pattern of the regexep_replace. Please do look at https://stackoverflow.com/a/4025508/9042433 to understand why 4 backslashes are needed to replace just one backslash
So, the required statement would become like below
select name, regexp_replace(name, '\\\\s\\\\', '') from test
Below screenshot has examples for better understanding

SQL removing characters from a string

I have a column in a datawarehouse task which needs replacing these characters:
"ABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyz" with nothing.
For example I have this form of data "88k77.22" and it should be "8877.22"
Does anyone know any particular function which can do this, or any workaround.
Thanks in advance
Use a regular expression
REGEXP_REPLACE(column, '[A-Za-z]*', '')
Is '\' supposed to be included as well? Then use
REGEXP_REPLACE(column, '[A-Za-z\]*', '')
Oracle supports translate(), which does exactly what you want:
translate(col, ' ABCDEFGHIJKLMNOPQRSTUVWXYZ\abcdefghijklmnopqrstuvwxyz', ' ')

Split a string to only use the middle part in SQL

I have a string
ABC - ABCDEFGHIJK - 05/07/2016
I want to only use the ABCDEFGHIJK section and remove the first and third parts of the string.
I have tried using SUBSTRING with CHARINDEX, but was only able to remove the first part of the string.
Anyone help with this?
You can use SUBSTRING_INDEX() and TRIM() for spaces :
SELECT TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(string_col,'-',2),'-',-1)) AS Strig_Col
FROM YourTable;