The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Databases
> MySQL Help
|
Syntax Error Missing Operator
Discuss Syntax Error Missing Operator in the MySQL Help forum on Dev Shed. Syntax Error Missing Operator MySQL Help forum discussing administration, SQL syntax, and other MySQL-related topics. MySQL is an open-source relational database management system (RDBMS).
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 4th, 2013, 12:05 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 4
Time spent in forums: 1 h 14 m 37 sec
Reputation Power: 0
|
|
|
Syntax Error Missing Operator
I have the following code meant to give me the counts of certain data codes and I am using a simple CASE WHEN to do the counts but I keep getting a syntax error Near the word THEN and I am embarassed I cannot figure it out. THe code is
SELECT ID_NR, DEG_CD, CASE WHEN( DEG_CD IN ('20','25') THEN 1 ELSE 0 END) AS Associates
FROM dbo.ACCT
GROUP BY ID_NR, DEG_CD
|

February 4th, 2013, 02:16 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Look at your parentheses.
|

February 4th, 2013, 03:13 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 4
Time spent in forums: 1 h 14 m 37 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by requinix Look at your parentheses. |
What about them
|

February 4th, 2013, 03:31 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Here is the syntax for a CASE expression.
Code:
CASE value WHEN [compare_value] THEN result [WHEN [compare_value] THEN result ...] [ELSE result] END
Note that it doesn't include any parentheses. That means yours have no additional meaning beyond the normal "group these things together".
Code:
CASE WHEN( DEG_CD IN ('20','25') THEN 1 ELSE 0 END)
After the WHEN is the condition. You used parentheses so MySQL sees it as
Code:
CASE WHEN(condition)
That doesn't obey the syntax. As for the condition itself,
Code:
DEG_CD IN ('20','25') THEN 1 ELSE 0 END
that doesn't correspond to anything MySQL supports. Why is there a "THEN"? What are the "ELSE" and "END" doing? It doesn't know.
Your parentheses are messing it up.
|

February 4th, 2013, 06:33 PM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
Quote: | Originally Posted by requinix As for the condition itself...
... that doesn't correspond to anything MySQL supports. | great explanation of the parentheses messing it up, but you messed up on this point
that CASE is perfectly valid
THEN 1 ELSE 0 END obviously sets up the CASE expression as a binary tinyint to indicate which rows have either a 20 or 25 in DEG_CD
all quite normal
if i were to guess, i would say DEG_CD is some kind of degree code and people who have earned a degree code of 20 or 25 are called associates
|

February 4th, 2013, 07:33 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Quote: | Originally Posted by r937 great explanation of the parentheses messing it up, but you messed up on this point
that CASE is perfectly valid |
How? The CASE is missing a THEN...END and for the stuff inside there's a THEN in the middle of nowhere. Neither is valid.
|

February 5th, 2013, 02:12 AM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
Quote: | Originally Posted by requinix How? The CASE is missing a THEN...END and for the stuff inside there's a THEN in the middle of nowhere. Neither is valid. | obviously, look at it without the parentheses!!
|

February 5th, 2013, 02:29 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Quote: | Originally Posted by r937 obviously, look at it without the parentheses!! |
But you can't just ignore them! They're the problem!
Okay yeah, what OP was trying to do is totally right, I see what you're getting at. It's just... it's not right until those parentheses go away. Which is easy to do, I know. Just saying.
Yeah.
|

February 5th, 2013, 02:36 AM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
Quote: | Originally Posted by requinix Yeah. | yeah, me too 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|