Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not equal to syntax 1

Status
Not open for further replies.

ashstampede

Programmer
Joined
Aug 30, 2004
Messages
104
Location
GB
I have a store procedure with an if statment, i cant get the not equal comparison in the store procedure.
i have an if statement and try both not equal to operators but it doesn't work/go into the if statement
Code:
if @e <> @emp
    set @rt = 0

if @e != @emp
    set @rt = 0

what am i doing wrong
 
My first guess is that some of variable(s) are set to NULL.
 
What datatype is:

@e
@rt
@emp

As vongrunt says, are any of @emp or @e NULLs? If so, you could use ISNULL to change the NULL to a different value.

SELECT ISNULL(@emp,0)

That will return 0's where there are NULLs in @emp.

-SQLBill
 
They are int variables, i will try what you suggested sqlbill
 
Just keep in mind folks that in some situations you can handle nulls without IsNull by flipflopping the logic.

Here are two examples which demonstrate this:

Code:
SELECT
		A,
		B,
		CASE WHEN A<> B THEN 1 ELSE 0 END,
		CASE WHEN A = B THEN 0 ELSE 1 END
	FROM (
		SELECT A=1,B=2 UNION
		SELECT 3,3 UNION
		SELECT Null,4 UNION
		SELECT Null, Null UNION
		SELECT 5, Null
	) X

IF Null <> 1 Print 'Not Equal' ELSE Print 'Equal'
IF Null = 1 Print 'Equal' ELSE Print 'Not Equal'
--Note that the following doesn't work the same as the previous line, because the *entire* condition evaluates to false when there's a null, no matter what, even if you put a NOT in front.
IF NOT (Null = 1) Print 'Not Equal'


-------------------------------------
It is better to have honor than a good reputation.
(Reputation is what other people think about you. Honor is what you know about yourself.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top