How do I search for "'" character

  • Hi,

    I've got table Product with column Description. Description can contain character "'". I use sp to seach for products as follows:

    create procedure SearchProduct

    (

    @Desc varchar(50)

    )

    AS

    SELECT * FROM Product

    WHERE Description LIKE '%@Desc%'

    GO

    If @Desc contais "'" character (e.g. "John's") sp fails.

    How do I fix this?

    Thanks.

    dusan

  • Try:

    SELECT * FROM Product

    WHERE Description LIKE '%'+@Desc+'%'

    Example:

    Declare @d varchar(8000),

    @arg varchar(10)

    Select @d='ABC''DEF',

    @arg='C''D'

    Select @d

    Select Case When @d like '%'+@Arg+'%' then 'Yes' Else 'No ' end

  • Hi,

    The following code may solve your problem :

    CREATE PROCEDURE SearchProduct

    ( @Desc VARCHAR(50) )

    AS

    BEGIN

    SELECT @Desc = Replace(@Desc, '''', '''''')

    SELECT * FROM Product

    WHERE Description LIKE '%' + @Desc + '%'

    END

    GO

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

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