We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

sql case statement error

Medium Priority
185 Views
Last Modified: 2012-05-06
I have a case statement in my sql

case when [SSS] In ('34','33') And [ST]='y' then [TT]*[TS] else (case when([SSP] Is Not Null And [ST]='y' then [PT]*[TSS] else 0) end AS STT,

I am getting an error reading:

Incorrect syntax near the keyword 'then'.

I have tried to fix it but i am not sure what the problem is
Comment
Watch Question

CERTIFIED EXPERT
Awarded 2008
Awarded 2008

Commented:
case when [SSS] In ('34','33') And [ST]='y' then [TT]*[TS] else (case when([SSP] Is Not Null And [ST]='y' then [PT]*[TSS] else 0 end) end AS STT,
try

case when [SSS] In ('34','33') And [ST]='y' then [TT]*[TS]
else case when([SSP] Is Not Null And [ST]='y' then [PT]*[TSS] else 0 end end AS STT

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
you are missing the second END:

case when [SSS] In ('34','33') And [ST]='y' then [TT]*[TS] else (case when([SSP] Is Not Null And [ST]='y' then [PT]*[TSS] else 0 end) end AS STT,

however, you can "simplify":

case when [SSS] In ('34','33') And [ST]='y' then [TT]*[TS] when([SSP] Is Not Null And [ST]='y' then [PT]*[TSS] else 0 end AS STT,
You are missing an END ... also a ) ... which  I think is what's raising that message.

case when [SSS] In ('34','33') And [ST]='y' then [TT]*[TS] else (case when([SSP] Is Not Null And [ST]='y') then [PT]*[TSS] else 0 end) end AS STT,

Open in new window

Commented:
You don't a nested case statement -

Try -
CASE	when [SSS] In ('34','33') And [ST]='y' then [TT]*[TS]
	when [SSP] Is Not Null And [ST]='y' then [PT]*[TSS]
	else 0
END AS STT

Open in new window

Author

Commented:
i tried

case when [SSS] In ('34','33') And [ST]='y' then [TT]*[TS] when([SSP] Is Not Null And [ST]='y' then [PT]*[TSS] else 0 end AS STT,

but still got an error
CERTIFIED EXPERT
Awarded 2008
Awarded 2008
Commented:
you didn't copy what I gave you....
case when [SSS] In ('34','33') And [ST]='y' then [TT]*[TS] else (case when([SSP] Is Not Null And [ST]='y' then [PT]*[TSS] else 0 end) end AS STT,

Author

Commented:
The following cleared the error

CASE      when [SSS] In ('34','33') And [ST]='y' then [TT]*[TS]
      when [SSP] Is Not Null And [ST]='y' then [PT]*[TSS]
      else 0
END AS STT
You don't need that ( between WHEN and [SSP] is NOT NULL

but if you do have the (, match it with a )

Author

Commented:
thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.