SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Incorrect syntax near the keyword 'case'.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Anabik
Starting Member

India
9 Posts

Posted - 10/04/2012 :  02:46:33  Show Profile  Reply with Quote
Can someone please let me know whats wrong in the query given below

declare @strPmtModeId varchar(10),
@LeftInt Smallint,
@RightInt Smallint

set @strPmtModeId = '1,18'
set @LeftInt = Convert(Smallint,Left(@strPmtModeId,1))
set @RightInt = Convert(Smallint,Right(@strPmtModeId,2))

select * from tbl_Pmt_Mode
where (isnull(Pmt_Mode_Id,99) in
case
when Convert(SmallInt,Len(@strPmtModeId)) > 2
then
(@LeftInt,@RightInt))
or
Convert(SmallInt,@strPmtModeId) is null)
else
(Convert(SmallInt,@strPmtModeId))) or Convert(SmallInt,@strPmtModeId) is null)
end


Thanks.

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 10/04/2012 :  07:06:03  Show Profile  Reply with Quote
I didn't quite follow the logic you are trying to implement, but SQL Server does not like the way you are using the case expression. Perhaps something like this?
WHERE
	( CONVERT(SMALLINT, LEN(@strPmtModeId)) > 2 AND ISNULL(Pmt_Mode_Id, 99) IN (@LeftInt, @RightInt) )
	OR 
	@strPmtModeId IS NULL 
	--- OR ???
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 10/04/2012 :  10:49:54  Show Profile  Reply with Quote
i think all of your conditions can be simplified into below

where ',' + @strPmtModeId + ',' like '%,' + cast(isnull(Pmt_Mode_Id,99) as varchar(3)) + ',%' 
or @strPmtModeId  is null


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

AndrewMurphy
Flowing Fount of Yak Knowledge

Ireland
2916 Posts

Posted - 10/05/2012 :  08:55:47  Show Profile  Reply with Quote
a) the IN clause needs to be followed by ()
b) the CASE statement is not terminated properly. The ELSE portion is outside of a pair of brackets.
c) the OR clause also seems to be in the wrong place....especially in relation to the CASE statement.

start with a simple condition and then work upwards adding more complexity as you go along. it looks like you wrote it all in one go and misplaced some brackets.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000