SQL 2005 Case Statement syntax

I'm trying to run the following script but am getting an error
                      Msg 156, Level 15, State 1, Line 7
                       Incorrect syntax near the keyword 'like'.

The intent is if @AcctNumb1='Admin' then select all customers from the RM00101 table otherwise select either the customer number in @AcctNumb1 or @AcctNumb2

declare @AcctNumb1 varchar (31)
declare @AcctNumb2 varchar (31)
set @AcctNumb1='Admin'
set @AcctNumb2='12345'

select * from rm00101 a
      where (case when @AcctNumb1='ADMIN' then a.custnmbr like '%'
            else a.custnmbr = @AcctNumb1+'%'
                  or a.custnmbr = @AcctNumb2+'%' end)

Is "Like" not valid in this context?

Thanks
jdr0606Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
You should convert this to a boolean logic equivalent but I don't understand

a.custnmbr = @AcctNumb1+'%'
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You can't code a condition into the THEN of a CASE. CASE needs to return an expression (and conditions aren't expressions). You can write it like this:
declare @AcctNumb1 varchar (31)
declare @AcctNumb2 varchar (31)
set @AcctNumb1='Admin'
set @AcctNumb2='12345'

select * from rm00101 a
      where @AcctNumb1='ADMIN' and a.custnmbr like '%' 
        or @AcctNumb1 != 'ADMIN' and (a.custnmbr = @AcctNumb1+'%'
                  or a.custnmbr = @AcctNumb2+'%')

Open in new window

Though I doubt the percent is correct for @AcctNumb1 != 'ADMIN' here, you want an exact match, which won't have the percent sign at the end for sure. Or you want to use LIKE here, too.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jdr0606Author Commented:
I sure tried to make it more complicated than necessary.

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.