BigQuery/SQL: Split String based on the second dot from right - sql

update: there are situations that dot position that might not be the best solution.
I got a column of website.
website
www.abc.google.com
www.bcd.google.com
wwww.efd.google.co.za
I want to transform it into
website
google.com
google.com
google.co.za
Anyone knows how to split based on the '.' position from the right?
Thanks.

regexp_substr() does exactly what you want:
select regexp_substr('www.abc.google.com', '[^.]*[.][^.]*$')

Split String based on the second dot from right
you can use regexp_extract(website, r'(\w+\.\w+)$')
or
there are situations that dot position that might not be the best solution.
net.reg_domain(website)
if apply to sample data in your question - the last one gives below output

Related

Need to extract specific text from a column on excel using either Alteryx or Pandas

I have a column that contains a specific set of text that I need to be retained and the rest removed or moved to another column. Unfortunately, I am not able to use normal text-to-column due to the variation of the text arrangement.
For example, I need the word Issue and the id associated with it to be separated. I am struggling to figure out a way to do this with the variation of the arrangement of the text I need.
If someone can help me find a solution using Alteryx would be much appreciated, if not Pandas would also work.
Thanks all.
Use str.extract with Pattern to extract specific text from the data frame [Pandas]
df['After']=df['Before'].str.extract(pat='(ISSUE \d+|issue \d+)',expand=False)
For an Alteryx-only solution, the easiest way would be an Alteryx Formula using REGEX_Replace:
REGEX_Replace([Before],".*(issue \d+).*","?1",1)
If you don't like RegEx, basic string manipulations can do it also: basically it's a Substring...
Substring([Before], *starting index*, *length*)
The starting index is easy: it's just FindString([Before],"ISSUE")
The length isn't too hard either: it's the index (using FindString again) of the first comma in the substring that starts with "ISSUE": SubString([Before],FindString([Before],"ISSUE"))
Combining all that and spreading it out a bit:
Substring(
[Before],
FindString([Before],"ISSUE"),
FindString(
SubString(
[Before],
FindString([Before],"ISSUE")
),","
)
)

SQL remove spaces between specific character in a string?

I want to update a database table field using another field in the same table. Currently I have this table called sources.
Name Code
In the name column I have values like this example :
' Deals On Wheels '
'Homesru - Abu Dhabi - Madinat Zayed Gold Centre'
And I am having this update statement :
UPDATE Sources
SET Code = REPLACE((LTRIM(RTRIM(Name))),' ','-')
the result is :
Deals-On-Wheels-Al-Aweer
which is fine.
but for second one I have this :
Homesru---Abu-Dhabi---Madinat-Zayed-Gold-Centre
I want it to be like this :
Homesru-Abu-Dhabi-Madinat-Zayed-Gold-Centre
How can I Achieve this ? Any Help is appreciated.
As suggested by #DanielE. my answer will point to a more global solution, in case you ever need to replace duplicated/triplicated/quadriplicated/... occurrences of a character on a string.
I'll not create a full solution for this issue, is a recurring question and there are really good solutions around already. Check these links:
SQL Server Central: remove spaces between specific character in a string?. This forum post will point to the next link I'm posting here. But is good to know what they are asking and answering.
Replace multiple spaces with new one but you can slightly modify it to replace any character you want.
You can also rely on this answer Find and remove repeated strings from Aaron Bertrand.
try
REPLACE((LTRIM(RTRIM(REPLACE((LTRIM(RTRIM(Name))),' - ','-')))),' ','-')
this will first replace ' - ' with just '-'
You might want to look into using a UDF to do a regular expression search and replace. See https://launchpad.net/mysql-udf-regexp

Substring with delimiter in SQL

I'm trying to return a substring of the following, it's comma delimited [only one comma]
City-City-City, State-State-State
Sometimes it's only one city and state, sometimes it's more than one of either [or both]
Basically, I need to just return the state initials pass the comma.
What's the best way to do this? I'm looking into the substring function, but that doesn't seem that smart. I found a split function but it looks like overkill and I don't like to use code I don't understand.
Ex:
Cincinnati-Middletown, OH-KY-IN
Cleveland-Elyria-Mentor, OH
Abilene, TX
Output:
OH-KY-IN
OH
TX
Thanks for the answers;I just figured it out thanks to Sonam's starting point.
Here's what I got. Haven't looked into it but it seems to returning the right stuff.
select substring(CBSAName,charindex(',',CBSAName)+1, LEN(CBSAName)) FROM CBSAMasterList
select substring('Abilene, TX',charindex(',','Abilene, TX')+2,2)

what does "myimage.%2d.png" mean

cant believe i could not google this--i know this relates to image padding. Is it
myimage.%2d.png specifies a sequence like {myimage.01.png, myimage.02.png, myimage.03.png...
and
myimage.%3d.png specifies a sequence like {myimage.001.png, myimage.002.png, myimage.003.png...
thanks!
If your'e dealing with printf() style formatting ( and that's what it looks like to me ) you can use "myimage.%02d.png" to pad with zeros like your first sequence and "myimage.%03d.png" in the second sequence. You should really add some context to your question though...

How to reverse values in a string in T-SQL

Using T-SQL, I'm trying to find the easiest way to make:
"abc.def.ghi/jkl" become "abc/def/ghi.jkl"?
Basically switch the . and /
Thank you
One way
select replace(replace(replace('abc.def.ghi/jkl','/','-'),'.','/'),'-','.')
you need to use an intermediate step, I chose the - symbol, choose something which won't exist in your string
SELECT REVERSE(#myvar) AS Reversed,
RIGHT(#myVar, CHARINDEX(‘ ‘, REVERSE(#myvar))) as Lastname;
took the answer from this guys blog. The first google result. You will need to modify it for your needs
link text