Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hi GuysI am trying to get the value of the string found by a regular expression.For example: If this statement select dbo.regexFind('fdjakfda jfdalf 123456', '\d{6}', 1)evaluates to 1, bring back the value of the string that caused the regular expression to evaluate to true.In other words, bring back the value: 123456ThanksNick
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2009-04-19 : 10:40:13
No need for regular expression here. Just PATINDEX and you're fine
DECLARE @Sample TABLE ( Data VARCHAR(200) )INSERT @SampleSELECT 'fdjakfda jfdalf 123456' UNION ALLSELECT 'fdjakfda 987654 jfdalf'DECLARE @Digits INTSET @Digits = 6SELECT Data, SUBSTRING(Data, Pos, @Digits) AS PesoFROM ( SELECT Data, PATINDEX('%' + REPLICATE('[0-9]', @Digits) + '%', Data) AS Pos FROM @Sample ) AS d