Solved

Incorrect syntax near the keyword 'LIKE'

Posted on 2009-05-13
4
1,608 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SqlServer Table Triggers 3 28
Want an individual results display div 8 39
sql query 5 38
Need help with another query 10 34
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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 shrink a transaction log file down to a reasonable size.

685 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