Mark Moonie Griffiths
Mark Moonie Griffiths

Reputation: 235

Search a table using a string

I have a stored procedure which searches a table for a variable set by the user it works fine but the search only finds the first letter of the string input.

I'm using ->

ALTER PROCEDURE [dbo].[test]
    @search_string varchar  
AS
BEGIN

    SET NOCOUNT ON;
    IF (@search_string IS NOT NULL) OR (LEN(@search_string) > 0)
        SELECT [archive_id]      
               [archive_id],
               [display_name]
        FROM [test1].[dbo].[ARCHIVE_id]
        WHERE [display_name] like @search_string+'%'; 

What I am trying to figure out is how I can search for a whole word in the display_name column.

Any help would be appreciated.

Mark

Upvotes: 0

Views: 74

Answers (2)

A_Sk
A_Sk

Reputation: 4630

See LIKE

%  Any string of zero or more characters.
_   Any single character.
[ ]  Any single character within the specified range ([a-f]) or set ([abcdef]).
[^]   Any single character not within the specified range ([^a-f]) or set ([^abcdef]).

Upvotes: 0

d_luffy_de
d_luffy_de

Reputation: 967

You have to declare the size of the variable

ALTER PROCEDURE [dbo].[test]
    @search_string varchar(50)

This should work

Upvotes: 4

Related Questions