Snowflake SQL - Format Phone Number to 9 digits - sql

I have a column with phone numbers in varchar, currently looks something like this. Because there is no consistent format, I don't think substring works.
(956) 444-3399
964-293-4321
(929)293-1234
(919)2991234
How do I remove all brackets, spaces and dashes and have the query return just the digits, in Snowflake? The desired output:
9564443399
9642934321
9292931234
9192991234

You can use regexp_replace() function to achieve this:
REGEXP_REPLACE(yourcolumn, '[^0-9]','')
That will strip out any non-numeric character.

You could use regexp_replace to remove all of the special characters
something like this
select regexp_replace('(956) 444-3399', '[\(\) -]', '')

An alternative using translate . Documentation
select translate('(956) 444-3399', '() -', '')

Related

Redshift SQL REGEXP_REPLACE function

I have a value which is duplicated from source (can't do anything about that). I have read some examples here https://docs.aws.amazon.com/redshift/latest/dg/REGEXP_REPLACE.html
Example value:
ABC$ABC$
So just trimming anything after the first '€'. I tried this, but I cannot figure out the correct REGEX expression.
REGEXP_REPLACE(value, '€.*\\.$', '')
So just trimming anything after the first '€'.
Why use regex at all? Why not just..
SELECT LEFT(value, CHARINDEX('€', value)-1)
If not all your data has a euro sign, consider WHERE value like '%€%'
Your current regex pattern is including a dot as the final character. Remove it and your approach should work:
SELECT REGEXP_REPLACE(value, '€.*$', '') AS value_out
FROM yourTable;
Or you can take the initial sequence of non-€ characters:
REGEXP_SUBSTR(value, '^[^€]+')

Parse out word between two characters SQL Netezza

I am having trouble finding the correct syntax to parse out a word between two characters in Netezza.
PATIENT_NAME
SMITH,JOHN L
BROWN,JANE R
JONES,MARY LYNN
I need the first name which is always after the comma and before the first space. How would I do this in Netezza?
I think Netezza supports regexp_extract(). That would be:
select replace(regexp_extract(name, ',[^ ]+'), ',', '')
Or regexp_replace():
select regexp_replace(name, '^[^,]+,([^ ]+)( |$).*$', '\1')
Netezza supports regexp_extract and if there is reason to handle all kinds of whitespace between first and middle name(s) then this would work -
select regexp_extract(name,
'^[^[:space:],]+[[:space:],]+([^[:space:]]+)')
This would handle optional whitespaces, tabs etc on either side of , as well.

replace all occurrences of a sub string between 2 charcters using sql

Input string: ["1189-13627273","89-13706681","118-13708388"]
Expected Output: ["14013627273","14013706681","14013708388"]
What I am trying to achieve is to replace any numbers till the '-' for each item with hard coded text like '140'
SELECT replace(value_to_replace, '-', '140')
FROM (
VALUES ('1189-13627273-77'), ('89-13706681'), ('118-13708388')
) t(value_to_replace);
check this
I found the right way to achieve that using the below regular expression.
SELECT REGEXP_REPLACE (string_to_change, '\\"[0-9]+\\-', '140')
You don't need a regexp for this, it's as easy as concatenation of 140 and the substring from - (or the second part when you split by -)
select '140'||substring('89-13706681' from position('-' in '89-13706681')+1 for 1000)
select '140'||split_part('89-13706681','-',2)
also, it's important to consider if you might have instances that don't contain - and what would be the output in this case
Use regexp_replace(text,text,text) function to do so giving the pattern to match and replacement string.
First argument is the value to be replaced, second is the POSIX regular expression and third is a replacement text.
Example
SELECT regexp_replace('1189-13627273', '.*-', '140');
Output: 14013627273
Sample data set query
SELECT regexp_replace(value_to_replace, '.*-', '140')
FROM (
VALUES ('1189-13627273'), ('89-13706681'), ('118-13708388')
) t(value_to_replace);
Caution! Pattern .*- will replace every character until it finds last occurence of - with text 140.

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)

Trim numbers from end of SQL variable

I have a SQL variable called #FileName which I need to trim numbers off the end. Heres an example of how my strings look at the moment, and how i need them to be:
lorem-1 to lorem
lorem-ipsum-456 to lorem-ipsum (note: '-' in the middle remains)
foo-123-bar-1234 to foo-123-bar (note: number in the middle remains)
123-lorem to 123-lorem (note: no change as no number and '-' at the end)
Is this possible?
Thanks.
It is possible using PATINDEX and REVERSE. Try this:
SELECT LEFT(#FileName,LEN(#FileName)-patindex('%[^0-9]%',REVERSE(#FileName))+1)
Yes it's possible. Hava a look at the SUBSTRING & ASCII TSQL functions.