Using LIKE in CASE Operations

how to use LIKEin CASE, i have this example script, but it does not work. I am using 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;
+3
source share
1 answer

To use LIKE, I think you need to use this form of the operator CASE(search form). The "simple" form allows only equality checking .

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
+9
source

Source: https://habr.com/ru/post/1757959/


All Articles