SQLTeam.com | Weblogs | Forums

Extracting a date from a string

Hello.

So I have this messy string of data and I am just trying to get the date out of it. The issue is the date isn't always in the same spot, but it's always in the string.

25 15.88 57.60 21 04/23/2021 23 906573 22 RAISING CANE'S 435 PHOENIX AZ 20 24231681113796189598993

How would I got about just extracting the date? Thank you.

If the pattern -- mm/dd/yyyy -- is consistent, then do it by pattern:

SELECT 
    string,
    CASE WHEN string LIKE '%[01][0-9]/[0123][0-9]/2[0-9][0-9][0-9]%' 
         THEN SUBSTRING(string, PATINDEX('%[01][0-9]/[0123][0-9]/2[0-9][0-9][0-9]%', string), 10)
         ELSE '' END
FROM ( VALUES('25 15.88 57.60 21 04/23/2021 23 906573 22 RAISING CANE''S 435 PHOENIX AZ 20 24231681113796189598993') ) AS x(string)
1 Like

Boom!

Case When ah.TokenString Like '%[01][0-9]/[0123][0-9]/2[0-9][0-9]%' Then SUBSTRING(ah.TokenString, PATINDEX('%[01][0-9]/[0123][0-9]/2[0-9][0-9]%',ah.TokenString),10) Else '' End

Thanks for your help!

@ScottPletcher is a champ of tricky expression.