I have 2 strings for exampe:
FO-123-4444-3353-9999-TEXT and
123-4444-3353-88888-something
How can i get substring without TEXT,something on the end and if there is FO also without FO. FO will be always there or won't , but TEXT can have any other word instead
Final result shoul be:
123-4444-3353-88888 and
123-4444-3353-9999
If you want to replace FO- in the beginning and -TEXT in the end, use regexp_replace. It can be done in single regexp using OR (|)
select regexp_replace('FO-123-4444-3353-9999-TEXT','^FO-|-TEXT$','') from SYSIBM.SYSDUMMY1;
Result:
123-4444-3353-9999
This answers the original version of the question.
Assuming that "TEXT" and "FO" do not appear anywhere else in the string (as in your examples), you can use REPLACE():
select replace(replace(col, '-TEXT', ''), 'FO-', '')
For the revised question, you can use regexp_replace():
select regexp_replace(col, '^FO-|-TEXT[0-9]*$', '')
This assumes that the "FO" and "TEXT" are at the beginning and end of your strings.
Related
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', '() -', '')
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, '^[^€]+')
I'm trying to extract everything after the first instance of a delimiter.
For example:
01443-30413 -> 30413
1221-935-5801 -> 935-5801
I have tried the following queries:
select regexp_replace(car_id, E'-.*', '') from schema.table_name;
select reverse(split_part(reverse(car_id), '-', 1)) from schema.table_name;
However both of them return:
01443-30413 -> 30413
1221-935-5801 -> 5801
So it's not working if delimiter appears multiple times.
I'm using Postgresql 11. I come from a MySQL background where you can do:
select SUBSTRING(car_id FROM (LOCATE('-',car_id)+1)) from table_name
Why not just do the PG equivalent of your MySQL approach and substring it?
SELECT SUBSTRING('abcdef-ghi' FROM POSITION('-' in 'abcdef-ghi') + 1)
If you don't like the "from" and "in" way of writing arguments, PG also has "normal" comma separated functions:
SELECT SUBSTR('abcdef-ghi', STRPOS('abcdef-ghi', '-') + 1)
I think that regexp_replace is appropriate, but using the correct pattern:
select regexp_replace('1221-935-5801', E'^[^-]+-', '');
935-5801
The regex pattern ^[^-]+- matches, from the start of the string, one or more non dash characters, ending with a dash. It then replaces with empty string, effectively removing this content.
Note that this approach also works if the input has no dashes at all, in which case it would just return the original input.
Use this regexp pattern :
select regexp_replace('1221-935-5801', E'^[^-]+-', '') from schema.table_name
Regexp explanation :
^ is the beginning of the string
[^-]+ means at least one character different than -
...until the - character is met
I tried it in a conventional way in general what we do (found
something similar to instr as strpos in postgrsql .) Can try the below
SELECT
SUBSTR(car_id,strpos(car_id,'-')+1,
length(car_id) ) from table ;
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.
In my column, there is a string that contains a word which starts with character '=' How can I extract those words? I found REGEXP_SUBSTR but I couldn't find out particular regular expression to do this? I appreciate any help. Thanks.
EDIT :
I have such a string :
"What a =lovely day!"
I want to get "=lovely"
You can use regexp_substr for this:
select regexp_substr(col, '=\S+')
from your_table;
=\S+:
= - match literal =
\S+ - match one or more non space characters
Try
REGEXP_SUBSTR('What a =lovely day!', '=\w+')
or
REGEXP_SUBSTR('What a =lovely day!', '=\S+')
depending on your needs.
Also if you want to match based on a list of special characters, use something like below.
In this example, you can match = and #. You can add more special character if you like.
Also if you want just = to be returned in case rest word is missing after that, then use \S*. Else use \S+
For strings which dont have this format, you will get null.
select regexp_substr(col1,'[=#]\S*') from
(select 'what a =lovely day' as col1 from dual union all
select 'some other #word as' from dual union all
select 'a normal string' from dual)