Stores Procedure with Conditions based on a parameter

  • I'm trying to have a condition like

    if @Type = 'TWO' then

    ColumnA LIKE N'%' + ' 20-' + N'%'

    else

    ColumnA LIKE N'%' + ' 30-' + N'%' OR ColumnA LIKE N'%' + ' 40-' + N'%'

    end if

    I'm not sure how to manage the syntax of such a thing in a stored procedure.

    This has been my approach so far.

    CREATE PROCEDURE dbo.Test

    @Type nvarchar(50) = 'TWO'

    AS

    SELECT ......

    FROM ....

    WHERE

    ColumnA LIKE

    CASE

    WHEN @Type = 'TWO' THEN

    N'%' + ' 20-' + N'%' ................ this part works

    ELSE

    ** I want this to be equiv to

    ColumnA LIKE N'%' + ' 30-' + N'%' OR ColumnA LIKE N'%' + ' 40-' + N'%'

    END

    I hope someone can assist.

    Thanks, Geoff

  • WHERE ColumnA LIKE

    CASE

    WHEN @Type = 'TWO' THEN N'%' + N' 20-' + N'%'

    ELSE N'%' + N' [3-4]0-' + N'%'

    END

  • Thanks so much!

  • How do I say something equivalent to

    .....

    WHERE

    ColumnA LIKE

    CASE

    WHEN @Type = 'TWO' THEN N'%' + ' 20-' + N'%'

    WHEN @Type = 'THREE_FOUR' THEN N'%' + N' [3-4]0-' + N'%'

    WHEN @Type = 'OTHER' THEN ....NOT (N'%' + ' 20-' + N'%') OR (N'%' + N' [3-4]0-' + N'%')

    (ie: all others except those defined by the previous two parameters)

    END

    Any assistance most appreciated.

Viewing 4 posts - 1 through 4 (of 4 total)

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