sql server varchar to int conversion error

i'm using sql server 2008

I have this query

DECLARE @EmployeeLastName AS VARCHAR(50)

-- Test Case 1 - Multiple Employees
--SET @EmployeeLastName = 'Johnson,Jackson,Roberts'

-- Test Case 2 - All Employees
--SET @EmployeeLastName = -1

-- Test Case 3 - Single Employee
SET @EmployeeLastName = 'Jackson'


IF @EmployeeLastName LIKE '%,%'


	-- Test Case 1
	SELECT 'Multiple Employees Selected' AS EmployeesSelectedText 


ELSE IF @EmployeeLastName = -1


	-- Test Case 2
	SELECT 'All Employees' AS EmployeesSelectedText 




	SELECT 'Single Employee Selected' AS EmployeesSelectedText 


Open in new window

When I use Test Case 1 Parameter  like this it works fine:

When I use Test Case 2 Parameter  like this it works fine:

When I use Test Case 3 Parameter  like this I get an error


Any idea of why I'm getting an error when I use this ?

SET @EmployeeLastName = 'Jackson'
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
In the below line @EmployeeLastName is a varchar(50) and -1 is numeric, hence the error.   '-1' would work though.
ELSE IF @EmployeeLastName = -1

Open in new window

Also just for kicks and giggles double-click on the error message, watch the cursor jump to the offending line, and tell us what line that is.

The reason the error wasn't thrown until Test Case 3 is that code execution didn't get that far in 1, and  -1 = -1 worked via implicit conversion for Test Case 2.

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
maqskywalkerAuthor Commented:
Thanks setting it to this @EmployeeLastName = '-1'  worked.
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

From novice to tech pro — start learning today.