Very Baisc Select Statement Question

  • I am fairly new to SQL Server and have not had many problems as of yet. However I am having an issue with a stored procedure. I want to pass in three variable from a text box (got that covered) and then I want to use a LIKE statement on the columns. Here is what i have:

    CREATE PROCEDURE sp_SelectAllPhysVar

    @name varchar(50),

    @facility varchar(50),

    @city varchar(50)

    AS

    SELECT * FROM tblPhysInfo WHERE (fldPhysName LIKE @name OR fldPhysFacility LIKE @facility OR fldPhysCity LIKE @city)

    GO

    Except it only returns the value if i type in the whole word.

    Example

    Value in table is Clinton

    if i send c

    it returns nothing if i send Clinton it returns the row.

    I know I have to put % signs but I cannot get it to work when I do.

    Appreciate the help.

  • Add 3 lines before the Select to prepend and append the Like wildcard operators around the user-entered values.:

    Select @name = '%' + @name + '%'

    Select @facility = '%' + @facility + '%'

    Select @city = '%' + @city + '%'

    Alternatively read the BOL on the CHARINDEX() function for finding substrings within strings.

  • Thank You, Worked like a charm.

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

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