Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

sql case statement error

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
0
Mr_Shaw
Asked:
Mr_Shaw
  • 3
  • 2
  • 2
  • +3
5 Solutions
 
chapmandewCommented:
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,
0
 
momi_sabagCommented:
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
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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,
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Daniel WilsonCommented:
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

0
 
reb73Commented:
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

0
 
Mr_ShawAuthor 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
0
 
chapmandewCommented:
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,
0
 
Mr_ShawAuthor 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
0
 
Daniel WilsonCommented:
You don't need that ( between WHEN and [SSP] is NOT NULL

but if you do have the (, match it with a )
0
 
Mr_ShawAuthor Commented:
thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now