?
Solved

Incorrect syntax near the keyword 'LIKE'

Posted on 2009-05-13
4
Medium Priority
?
1,737 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 200 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 200 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

777 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