LIKE with Numeric values

  • I am trying to access records which start with numbers 75

    Looks like LIke doesn't work.

    How can I do so for

    Case when Numbers like '75%'

  • sharonsql2013 (5/12/2015)


    I am trying to access records which start with numbers 75

    Looks like LIke doesn't work.

    How can I do so for

    Case when Numbers like '75%'

    You could try something like:

    CASE WHEN CAST(Numbers AS VARCHAR) LIKE '75%'

    Or

    CASE WHEN LEFT(CAST(Numbers AS VARCHAR), 2) = '75'



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Thanks

  • I'm not sure how Alvin's query solved the problem as the LIKE operator will implicitly convert values into strings.

    DECLARE @test-2 TABLE(

    Numbers int)

    INSERT INTO @test-2 VALUES(12),(75924),(75),(53756),(2354)

    SELECT Numbers

    ,Case when Numbers like '75%' THEN 'True' ELSE 'False' END

    ,CASE WHEN CAST(Numbers AS VARCHAR) LIKE '75%' THEN 'True' ELSE 'False' END

    ,CASE WHEN LEFT(CAST(Numbers AS VARCHAR), 2) = '75' THEN 'True' ELSE 'False' END

    FROM @test-2

    You can change the Numbers data type to any type that can be implicitly converted and you'll get the same result for the different options. You'll have more problems with the input values than with the LIKE operator.

    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

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply