SQL Stored Procedure parameter in like statement

  • I have a simple Stored Procedure that accepts one parameter. I then want to use that parameter in a LIKE statement within a SELECT. What is the correct systax for the select statement?

  • Try this

    CREATE PROCEDURE usp_TestLike @VarName varchar(100)

    AS

    SET NOCOUNT ON

    SET @VarName = ISNULL(@VarName,'') + '%'

    SELECT *

    FROM sysobjects

    WHERE [name] LIKE @VarName

    GO

    This will work with the variable that start with this name if you want to look for variable in any portion then change the SET to

    SET @VarName = '%' + ISNULL(@VarName,'') + '%'

  • That worked perfectly! Thanks!!!

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

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