Extract subString from a string - Pattern Matching

Odpovědět Extract subString from a string - Pattern Matching

  • Saturday, April 28, 2012 12:08 PM
     
     

    Hi,

    I am searching a pattern with the following one and i would like to extract only the string which is in the format([0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9]).


    SELECT [Source-LineDescription]
    FROM [FAR_March2012_SourceLines]
    WHERE [Source-LineDescription] like '%[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9]%'

    Thanks.

     

All Replies

  • Saturday, April 28, 2012 12:16 PM
     
     Answered Has Code
    SELECT [Source-LineDescription],
    	SUBSTRING([Source-LineDescription],PATINDEX ('%[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9]%',[Source-LineDescription]),10)
     FROM [FAR_March2012_SourceLines]
     WHERE [Source-LineDescription] like '%[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9]%' 

    Something like this

    Chuck

    • Marked As Answer by Kashif Chotu Saturday, April 28, 2012 12:22 PM
    •  
  • Saturday, April 28, 2012 12:22 PM
     
     
    Thanks. Works for Me.
  • Saturday, April 28, 2012 12:26 PM
     
     Answered

    Try this:

    DECLARE @x nvarchar(MAX) = N'This is a string with numeric pattern in it: 123-00-123. More text follows!'
    SELECT substring(@x, patindex('%[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9]%', @x), 10)

    Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
    • Marked As Answer by Kashif Chotu Saturday, April 28, 2012 12:30 PM
    •