Search code examples
sqlvariablesbooleansql-like

Declaring Variables, LIKE and booleans


Can "LIKE" and a boolean behave the same way in a variable (IN SQL)? Or is there a way to do this?

I'm trying to declare a variable and have that variable be brought into my WHERE and everywhere else in my query.

DECLARE @SKU VARCHAR(10)
SET @SKU = LIKE 'RUSH%'

SELECT Name, SKU, Rate
FROM PriceTable
WHERE SKU = @SKU

Solution

  • You can't do what you're trying to do. The variable is only a VARCHAR. It can't include logic.

    This accomplishes the same thing, but the logic needs to be in the statements, not in the variable.

    DECLARE @SKU_FILTER VARCHAR(10)
    SET @SKU_FILTER = 'RUSH%'
    
    SELECT Name, SKU, Rate
    FROM PriceTable
    WHERE SKU LIKE @SKU_FILTER