Get last occurrence for the string after '/' in redshift - sql

I am fairly new to regex expressions and always had a trouble to follow. It would be really helpful if I can get answer to the following problem.
I have a column with strings in redshift table and want to extract a certain part of the string(The string that is after the last '/'). For example, I have https://hello.com/my_first_website in my redshift table with the column name as customer_site, from this I want to extract my_first_website as output. Can someone tell me a regex expression that can help me to extract this.

You can use regexp_substr function such as
SELECT regexp_substr('https://hello.com/my_first_website','[^/]*$')

Related

Google BigQuery extract string from column with regexp_extract

I need to extract the app_id from the following string in the column below:
&app_id=4.25.9&
so anything that starts with &app_id= and ends with &
Any ideas on how to write this example? The number of characters and symbols may differ.
This should be sufficient:
REGEXP_EXTRACT(your_column, r'\&app_id=(.+?)\&')
The app id matching right now .+? is a little broad and will match any character, you may want to restrict it further.

Hive Regular expression - only portion of string needed

Hi i was trying to extract portion of data from one column in my hive table but the position of character is not in one place
select value4,regexp_extract(value4,'*****',0) from hive_table;
column value is shown below
grade:data:home made;Cat;dinnerbox_grade_Enroll
list:date:may;animal;dinnerbox_list_value
cgrade:made_data;dinnerbox_cgrade_notEnroll
I want data from dinnerbox to till end.
Can any one help on this?
It is a pretty simple regular expression
.*dinnerbox(.*?)$
Using a non-greedy wildcard, but forcing it to the end of the line makes sure that you always get the dinnerbox at the end.
You want capture group 1
To get rid of the _ you can use
.*dinnerbox_(.*?)$

How to select values around .(dot) using sql

I am running below query in Teradata :
sel requesttext from dbc.tables
where tablename='old_employee_table'
Result:
alter table DB_NAME.employee_table,no fallback ;
I want to get below result using SQL:
DB_NAME.employee_table
Requesttext can be:
create set table DB_NAME.employee_table;
DB Name and table can occur anywhere in the result. Since .(dot) is joining them that's why i want to split with .(dot).
Basically I need sql which can result me surrounding values of .(dot)
I want DBName and Tablename in result.
I'm not a Teradata person, but this should work for both strings given so far, as long as teradata's regexp_substr() supports positive look-behind and positive look-ahead assertions (I might have the Teradata syntax wrong, so a little tweaking may be needed):
SELECT REGEXP_SUBSTR(requesttext, '(?<= )(\w+\.\w+)(?=[,$]?)', 1, 1)
FROM dbc.tables
WHERE tablename='old_employee_table'
See the regex101 example. Hopefully it translates to Teradata easily.
The regex looks for and returns the words either side of and including the period, when preceded by a space, and followed by an optional comma or the end of the line.
You could do this with either regexp_substr() or strtok().
As Jamie Zawinski said:
Some people, when confronted with a problem, think "I know, I'll use
regular expressions." Now they have two problems.
So I would go with the strtok() method. Also I'm lazy and regular expressions are hard.
Function strtok() takes three arguments:
The string being split
The delimiter to split the string
The number of the token to grab.
To get at the <database>.<table> from that string that is returned in your query, we can split by a space, grab the third token, then split that by a comma and grab the first token.
That would look like:
SELECT strtok(strtok(requestText,' ',3),',',1)
FROM dbc.tables
WHERE tablename='old_employee_table'

I need a stored procedure that gives out put of matched patterns against a string in Oracle 10g

There is a column in a table which contains many patterns.
I need a query that takes one string as input and will give all the patterns from that table column that match that string as output.
Say in Pattern table column1 there are values like ab%,a%c%,%1%,k%.
Now,I will give abcd12 as input string to my query and it will give output as
ab%
a%c%
%1%
but it will not give k% as output, because it is not a pattern that matches for exact string abcd12.
I believe it can be done using stored procedure.
Can you guys please help me.
If it's not really regex but SQL pattern like in your examples then you can simply select it:
SELECT pattern FROM patterns WHERE 'abcd12' LIKE pattern

SQL Substring a range of data

I want to select out just the email address using SubString.
Here is my column data:
[{"IsPrimary":false,"Address":"test#gmail.com","Type":"Other"}]
Here is my Query:
SELECT SUBSTRING(EmailJson, CHARINDEX('ess":"', EmailJson)+6, CHARINDEX('","Type', EmailJson)) From Respondents
Problem is that it isn't working the way I thought substring would work. I expected it to give me a range of characters. For example I want substring to return a range of characters like 5-10. The way this substring works is that I establish the start and then how long I want it to be from the start position.
How can I alter my query to just return them email only from the column.
I agree with the above comments that this is not an elegant way of doing it but if you really need to use substring then have a look at the below.
I have changed this to work with oracle because that is what I have available and I am unsure what you are using but you should be able to get the idea from it.
SELECT substr(EmailJson, (instr(EmailJson,"Type":"Other"', 'ess":"')+6), (instr(EmailJson,"Type":"Other"', '","Type') - (instr(EmailJson,'ess":"')+6))) From Respondents;