Using "LIKE" in Stored Proc

  • I'm having trouble with a query in a stored proc.

    If I run:

    CREATE PROCEDURE GenerateList

    ( @theName varchar(20) )

    AS

    SELECT field1, field2

    FROM myTable

    WHERE field1 = @theName

    it works fine. My problem is I want to use the LIKE statement instead of = with the "%" wildcard to the right of the variable such as:

    CREATE PROCEDURE GenerateList

    ( @theName varchar(20) )

    AS

    SELECT field1, field2

    FROM myTable

    WHERE field1 LIKE @theName%

    I've tried single quotes, double quotes, etc. with no luck. How should this be formatted?

    Thanks in advance.

  • You need to add the % character to the value of the variable, not the variable name:-

    CREATE PROCEDURE GenerateList

    ( @theName varchar(20) )

    AS

    SELECT field1, field2

    FROM myTable

    WHERE field1 LIKE @theName + '%'

  • Awesome. Works like a charm. Thanks for your help.

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

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