• Hi,

    did you try the PATINDEX function?

    The following code will check the start position of each value from your table within the given search string.

    It will return the max. number with start position = 1.

    DECLARE @tab TABLE (id INT, val INT)

    INSERT INTO @tab

    SELECT 1, 95 UNION ALL

    SELECT 2, 959 UNION ALL

    SELECT 3, 44 UNION ALL

    SELECT 4, 959898

    SELECT TOP 1 id, val

    FROM @tab

    WHERE PATINDEX('%'+CAST(val AS VARCHAR(20))+'%','9598989898954412') = 1

    ORDER BY val DESC

    -- Alternative code (the above is a little oversized for this purpose...) -> driven by Atif's solution below...

    SELECT TOP 1 id, val

    FROM @tab

    WHERE '9598989898954412' like CAST(val AS VARCHAR(20))+'%'

    ORDER BY val DESC

    -- result set:

    -- ID val

    -- 4 959898

    Edit: added second option.



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]