Solved

Incorrect syntax near the keyword 'LIKE'

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert null in sql server 12 34
Mysql Left Join Case 10 56
Sql server get data from a usp to use in a usp 5 16
SQL - Use results of SELECT DISTINCT in a JOIN 4 21
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

803 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