TSQL Substring

How to extact the sub string between last two "."

For example
  Str1 = "\\Myfolder\File.MyFile1.csv"
             Result should be MyFile1

  Str1 = "\\Myfolder\Folder1\subfolder2\filename.MyFile1.csv"
             Result should be MyFile1

  Str1 = "c:\Myfolder\Folder1\sub.fol.der2\fi.l.e.name.MyFile1.csv"
             Result should be MyFile1
Bharat GuruAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
SELECT str1,
    LEFT(ending_string, CHARINDEX('.', ending_string) - 1) AS result
    SELECT '\\Myfolder\File.MyFile1.csv' AS str1 UNION ALL
    SELECT '\\Myfolder\Folder1\subfolder2\filename.MyFile1.csv' UNION ALL
    SELECT 'c:\Myfolder\Folder1\sub.fol.der2\fi.l.e.name.MyFile1.csv'
) AS test_data
    SELECT CHARINDEX('.', REVERSE(str1)) AS last_period
) AS assign_alias_names1
    SELECT RIGHT(str1, CHARINDEX('.', REVERSE(str1), last_period + 1) - 1) AS ending_string
) AS assign_alias_names2
awking00Information Technology SpecialistCommented:
I don't have a way to test but, in pseudo code, reverse the string and take the substring of that starting with the index of the (now first) '.' + 1, then take the substring of that up until the index of '.' -1, then reverse the result.
Vitor MontalvãoMSSQL Senior EngineerCommented:

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Bharat GuruAuthor Commented:
Its not always ".csv" extension but it will have file extension of 4 character including the "."
Vitor MontalvãoMSSQL Senior EngineerCommented:
Then you just need to replace the '.csv' with the desired extension.
awking00Information Technology SpecialistCommented:
Still have no means of testing but, trying to add syntax (assumes 3-character file extensions -
select reverse(substring(reverse(substring(str1,1,len(str1) - 4)),1,charindex('.',reverse(substring(str1,1,len(str1) - 4) - 1)))
Scott PletcherSenior DBACommented:
Did you look at the first code I posted?  It has no dependency on any specific extension.
awking00Information Technology SpecialistCommented:
The -4 that I used to manage a 3-character extension could be substituted with additional charindex statements which you code addressed. I was just trying to reach a solution with just one select statement.
Scott PletcherSenior DBACommented:
It is one SELECT statement; the other SELECTs just provide test data, which you would replace with your own table name.

The CROSS APPLYs are part of the SELECT, and just reduce the complexity of the statement so it can be maintained later.  But, if you don't want those, then, yes, you could use Vitor's massively imbedded functions method.
Bharat GuruAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.