SQL Substring

I need to parse out of this string Year-MonthName-Day

The result should look like this
2015-october-18
&
2015-october-04

My issue is that I am using 2 different patindex because of the single digit and two digit days. I would like 1 Patindex that will account for both scenarios.  

Once that is done add the "-Day". If the day is single digit 04 else 18

Sample code
Declare @LinkSource varchar(8000), @LinkSource2 varchar(8000)

Set @LinkSource = 'Testing -Mod +190 -8.5   +9.6    Tester-legoland-date-october-18-2015'
Set @LinkSource2 = 'Testing -Mod +120 -6.5   +7.5    Tester-legoland-date-october-4-2015'


Select 
right(@LinkSource,4)+'-'+substring(@LinkSource,charindex('date-',@LinkSource,0)+5,Len(@LinkSource)-patindex('%-[0-9][0-9]-%',@LinkSource))--charindex('-',@LinkSource,charindex('odds-',@LinkSource,0)) )
,right(@LinkSource2,4)+'-'+substring(@LinkSource2,charindex('date-',@LinkSource2,0)+5,Len(@LinkSource2)-patindex('%-[0-9]-%',@LinkSource2)+1)--charindex('-',@LinkSource,charindex('odds-',@LinkSource,0)) )
,Len(@LinkSource)-patindex('%-[0-9]%',@LinkSource)

Open in new window


If there is a better way to trim month and day out I am all ears.
LVL 8
Leo TorresSQL DeveloperAsked:
Who is Participating?

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

x
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.

Vikas GargAssociate Principal EngineerCommented:
Hello,

You can try this

Declare @LinkSource varchar(8000)

Set @LinkSource = 'Testing -235 +190 -3.5   +3.5    Tester-legoland-date-october-18-2015'
--Set @LinkSource = 'Testing -235 +190 -3.5   +3.5    Tester-legoland-date-october-4-2015'

SET @LinkSource = SUBSTRING(@LinkSource,CHARINDEX('DATE',@LinkSource)+5,LEN(@LinkSource))                         

select
RIGHT(@LinkSource,4)+'-'+LEFT(@LinkSource,CHARINDEX('-',@LinkSource)-1)
+'-'+ CASE WHEN SUBSTRING(@LinkSource,CHARINDEX('-',@LinkSource)+1,2) LIKE '%-%' THEN
'0'+ REPLACE(SUBSTRING(@LinkSource,CHARINDEX('-',@LinkSource)+1,2),'-','') ELSE 
REPLACE(SUBSTRING(@LinkSource,CHARINDEX('-',@LinkSource)+1,2),'-','')  END Dates

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
Vitor MontalvãoMSSQL Senior EngineerCommented:
I added 2 auxiliar variables since I don't know if you want to change the values of the original values but you can avoid those 2 auxiliar variables and work only with the original ones if isn't important to you to keep the original values:
Declare @LinkSource varchar(8000), @LinkSource2 varchar(8000)

Set @LinkSource = 'Testing -Mod +190 -8.5   +9.6    Tester-legoland-date-october-18-2015'
Set @LinkSource2 = 'Testing -Mod +120 -6.5   +7.5    Tester-legoland-date-october-4-2015'

Declare @LinkSourceAux varchar(8000), @LinkSource2Aux varchar(8000)

SET @LinkSourceAux = REPLACE(RIGHT(@LinkSource, LEN(@LinkSource)-CHARINDEX('date-', @LinkSource,1)-4),'-','.')
SET @LinkSource2Aux = REPLACE(RIGHT(@LinkSource2, LEN(@LinkSource2)-CHARINDEX('date-', @LinkSource2,1)-4),'-','.')

SELECT PARSENAME(@LinkSourceAux,1) + '-' + PARSENAME(@LinkSourceAux,3) + '-' + RIGHT('0'+PARSENAME(@LinkSourceAux,2),2), 
	PARSENAME(@LinkSource2Aux,1) + '-' + PARSENAME(@LinkSource2Aux,3) + '-' + RIGHT('0'+PARSENAME(@LinkSource2Aux,2),2)

Open in new window

Leo TorresSQL DeveloperAuthor Commented:
Thank you this was a better solution for me
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
Microsoft SQL Server 2008

From novice to tech pro — start learning today.