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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check a range of values to see if they are all zero 1

Status
Not open for further replies.

Craftor

Programmer
Feb 1, 2001
420
NZ
Hi everyone,

I'm currently doing a calculation in a stored procedure and I need to check if all of my calculated values are zero.

I've allocated the values to variables as they are used in various places during the calculations - aside from a massive IF statement, is there an elegant way to check this?

Thanks as always


Craftor
:cool:
 
Not sure what you asking about, but try this:
Code:
IF (ABS(@var1)+ABS(@var2)+...+ABS(@varN)) = 0
   --- All variables are zero

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
Microsoft MVP VFP
 
Code:
declare @a int, @b int, @c int, @d int
select @a = 0, @b=0, @c=0, @d=0
select 
	case when @a=@b and @b=@c and @c=@d and @d=0
		then 'true'
	else 'False'
	end

"I'm living so far beyond my income that we may almost be said to be living apart
 
Hi,

Thanks for your responses. bborissov's suggestion seems to be the best as it takes my calculated values into account.

Thanks as always


Craftor
:cool:
 
hmckillop, while that works, it seems an intentional obfuscation. Just use zeroes. Since they're literals it might even take less CPU time than comparing variables:

Code:
case when @a=0 and @b=0 and @c=0 and @d=0
Also, for what it's worth, Microsoft recommends that you not use select except for data retrieval from a table, and in the rare case that you need two variables in a single statement as in SELECT @Err = @@Error, @Rowcount = @@Rowcount.

[COLOR=black #e0e0e0]For SQL and technical ideas, visit my blog, Squared Thoughts.

The best part about anything that has cheese is the cheese.[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top