Solved

Incorrect syntax near the keyword 'LIKE'

Posted on 2009-05-13
4
1,532 Views
Last Modified: 2012-05-06
Sorry, I'm not seeing where my syntax is incorrect on this simple query:

SELECT
      CASE strMerchItemName
            WHEN LIKE 'group%' THEN GroupItem
            WHEN LIKE 'trade%' THEN TradeItem
            ELSE EverythingElse
            END AS ItemType,
      SUM(curPrice) AS Total
FROM tblMerchItem
GROUP BY strMerchItemName

But I'm getting the following error:
"Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'LIKE'."

I don't see what's wrong with my CASE statement and the LIKE statements.  Can somebody please show me where I'm making my mistake(s)?
0
Comment
Question by:sevenpeaks
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 50 total points
ID: 24377481
SELECT
      CASE
            WHEN strMerchItemName LIKE 'group%' THEN GroupItem
            WHEN strMerchItemName LIKE 'trade%' THEN TradeItem
            ELSE EverythingElse
            END AS ItemType,
      SUM(curPrice) AS Total
FROM tblMerchItem
GROUP BY strMerchItemName
0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 50 total points
ID: 24377541
All evaluations in a case, that are not = evaluations, must be done like this:


SELECT
      CASE
            WHEN strMerchItemName LIKE 'group%' THEN GroupItem
            WHEN strMerchItemName LIKE 'trade%' THEN TradeItem
            ELSE EverythingElse
            END AS ItemType,
      SUM(curPrice) AS Total
FROM tblMerchItem
GROUP BY strMerchItemName


Same would apply for <, >, <=, >=, etc...


SELECT
      CASE
            WHEN strMerchItemName > 1 THEN GroupItem
            WHEN strMerchItemName < 1 THEN TradeItem
..
..
..
..
            ELSE EverythingElse
            END AS ItemType,
      SUM(curPrice) AS Total
FROM tblMerchItem
GROUP BY strMerchItemName
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24377542
sorry for the double post aneeshattingal
0
 

Author Closing Comment

by:sevenpeaks
ID: 31581113
Beautiful.  Thanks for your work.  That will help me out.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now