Passing Variable with "LIKE"

  • Hello,

    I want to utilize a stored procedure that will accept a string variable that I will pass from my app, and utilize the LIKE operator.

    I tried this:

    Declare @NAMEX varchar

    Set @NAMEX = 'S'

    Select @NAMEX = @NAMEX + '%'

    SELECT VENDORNO, NAMEX, STREET, CITY, STATE, ZIP,

    FROM VENDORDFROM

    WHERE NAMEX Like @NAMEX

    ORDER BY NAMEX

    and it returns, of course, zero rows. Replacing the variable code with 'S' returns data.

    Can you please help with the proper syntax?

    Thanks so much.

  • Which version of SQL you are using?  I just tried your query now in SQL 2000 and working fine.

  • 2000

  • Starnge..Its working fine in 2000 when i tried. can you try running the query again?

    The code i tried is

    Declare @NAMEX varchar(100)

    Set @NAMEX = 'S'

    Select @NAMEX = @NAMEX + '%'

    SELECT * FROM testTbl  WHERE NAMEX Like @NAMEX ORDER BY NAMEX

     

    Got your problem..make varchar(50) when you declare variable..

  • BINGO! That did it. Thanks. That was driving me crazy!

    Mike

  • You can also simplify this...

    Declare @NAMEX varchar(100)

    Set @NAMEX = 'S'

    SELECT * FROM testTbl WHERE NAMEX Like @NAMEX + '%' ORDER BY NAMEX

    cheers


    Cheers,

    Ben Sullins
    bensullins.com
    Beer is my primary key...

Viewing 6 posts - 1 through 5 (of 5 total)

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