Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading