Home Forums SQL Server 2008 T-SQL (SS2K8) Determine which pattern comes first in a string<!-- 864 --><!-- 864 --> RE: Determine which pattern comes first in a string<!-- 864 --><!-- 864 --><!-- 864 --><!-- 864 -->

  • I was just going to mention that. Here's a revised version to include strings with no matches at all using OUTER APPLY.

    WITH cteString ( ArtifactID, PATH )

    AS ( SELECT ArtifactID ,

    PATH

    FROM [Document]

    WHERE PATH IS NOT NULL

    )

    SELECT ArtifactID ,

    PATH ,

    y.pattern ,

    ISNULL( y.Datatype, 'Check Subject and Record type etc.') AS Datatype

    FROM cteString

    OUTER APPLY ( SELECT TOP 1

    pattern, Datatype

    FROM ( VALUES

    ( 'PublicIM', '_pim', CHARINDEX('_pim', PATH)),

    ( 'HubIMs', '_him', CHARINDEX('_him', PATH)),

    ( 'B Email', '.b.', CHARINDEX('.b.', PATH)),

    ( 'B Chats', '.ib', CHARINDEX('.ib', PATH)),

    ( 'Bloomberg Chats', 'IB Conversation', CHARINDEX('IB Conversation',

    path)) ) x (Datatype, pattern, position )

    WHERE position > 0

    ORDER BY position

    ) y

    EDIT: Oracle? :crazy::sick:

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2