views:

57

answers:

1

Hi, how to use LIKE in CASE statement, i have got this example script but does not work. I use MS SQL 2008

DECLARE
    @DataBaseName sysname,
    @Message char(255)

SET @DataBaseName = 'DBa';

SET @Message =
    CASE @DataBaseName 
        WHEN LIKE 'DBa'
        THEN 'Valid name'
    ELSE 'INVALID name'
    END
Print @Message;
+6  A: 

To use LIKE I think you need to use this form of the CASE statement (the "searched" form). The "simple" form allows only an equality check.

SET @Message =
    CASE  
        WHEN @DataBaseName LIKE 'DBa' /*As Yves points out in the comments
                                        should this be 
        WHEN @DataBaseName LIKE 'DBa%' COLLATE SQL_Latin1_General_CP1_CS_AS  */
        THEN 'Valid name'
    ELSE 'INVALID name'
    END
Martin Smith
thanks for your help
GIbboK