sql function case statement

This formula is only using the lvl1 value. It is not using lvl0 when leveln = 0
Is there a problem using a case statement this way in a user defined function?
 
select @G = round(sum(cratt * case when isnull(leveln,0) = 0 then lvl0 else lvl1 end)/sum(cratt),4)
dwiseman3Asked:
Who is Participating?
 
Scott PletcherSenior DBACommented:
>> Is there a problem using a case statement [this way] in a user defined function? <<

No; CASE statements are fully supported in functions.

You must have some other issue.
0
 
duttcomCommented:
I think the problem might be in your use of ISNULL.

ISNULL is a binary value so isnull(leveln,0) = 0 means isnull(leveln,0) = false (leveln is NOT null)

Try-

isnull(leveln,0) = 1
0
 
Scott PletcherSenior DBACommented:
No; the "= 0" doesn't mean "false".  ISNULL(leveln, 0) means that is leveln is NULL, substitute the value 0 in its place.  The "= 0" is a value comparison, just like any other value comparison in SQL, such as "CASE WHEN A = 3".
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
duttcomCommented:
Yes, it does mean false when it is used like that.

As you say, ISNULL(leveln, 0) means that is leveln is NULL when leveln = 0, however by using ISNULL(field,x) = 1 or 0 you are stating whether or not the the field's ISNULL 'property' is either true or false. ISNULL(field, 0) cannot = 3 as in a value comparison.

You could also just use-

isnull(leveln,0) then lvl0 else lvl1

and not use =0 or =1 at all
0
 
appariCommented:
it helps if you can post sample data and the result you want.
duttcom: are you sure what you are writing about isnull fuction in sqlserver?
0
 
duttcomCommented:
ISNULL is a boolean property being tested. Either the value is null or not, therefore ISNULL can only ever resolve to 0 or 1, true or false. ISNULL is a function of a field's property and can therefore be used as either (and both in this case).

The OP was using ISNULL as both a function (isnull(leveln,0)) AND then testing that property (by using the = 0), which was then negating the value of the function.

The way the original query is written, if the value of leveln = 0, then the following occurs -

isnull(leveln,0) by itself evaluates to 1 or true, so the value of the field IS null

But then by adding =0, the ISNULL value of the field is being tested again. The test is then being tested.

isnull(leveln,0) = 0 evaluates to 1 = 0 and the end result is 0
0
 
appariCommented:
duttcom: I suppose you are getting confused with isnull function in other languages. see BOL topic on isnull function, http://msdn.microsoft.com/en-us/library/ms184325(v=sql.90).aspx
there is no problem with the usage of isnull function as posted by OP.
0
 
duttcomCommented:
I use the ISNULL function that way all the time in SQL for different purposes. The OP is using the function (correctly, yes) but is then ALSO testing the VALUE of the function by adding =0.

When ISNULL(anything) = 1 or true, the field IS NULL
When ISNULL(anything) = 0 or false, the field is NOT NULL and contains data

Therefore the OP's query results in 1=0 and the query then thinks the field has data.

This article explains the boolean nature of the ISNULL property.

http://msdn.microsoft.com/en-au/library/microsoft.sqlserver.management.smo.property.isnull(v=sql.90).aspx
0
 
appariCommented:
the one you were talking is a property in property class in SMO namespace and it is different from transact sql isnull function.
0
 
duttcomCommented:
It works the same way regardless. ISNULL as it is being used here returns a boolean value and the OP needs to change the query to test if the ISNULL value of the field evaluates to 1 (when the field is null) and not 0 (when the field has a value).

when isnull(leveln,0) = 0

Effectively means - when the ISNULL property of leveln is false (has a value other than 0)

when isnull(leveln,0) = 1

Effectively means - when the ISNULL property of leveln is true (has a value of 0)

Therefore the test needs to be when isnull(leveln,0) = 1 to test for when the ISNULL-ness of the field is TRUE, not false the way it currently is.
0
 
appariCommented:
Don't know how to convince you.
they are two different things. the one used by OP in his question is the transact sql function and the usage syntax is correct.

ScottPletcher: request you to post your comments.
0
 
duttcomCommented:
"It is not using lvl0 when leveln = 0 ...
 
select @G = round(sum(cratt * case when isnull(leveln,0) = 0 then lvl0"

Okay, so when leveln = 0, step me through how "when isnull(leveln,0) = 0" is evaluated and should therefore use lvl0
0
 
appariCommented:
duttcom:
>>case when isnull(leveln,0) = 0 then lvl0 else lvl1 end
1. isnull(leveln,0) evaluates to 0 when leveln value is null and all other cases to leveln value.
2. value from step 1 is compared with 0. if the value in step 1 is 0 then returns lvl0 otherwise returns lvl1 value.


dwiseman3: may be null values in lvl0 and lvl1 is causing the problem,
try like this

select @G = round(sum(cratt * case when isnull(leveln,0) = 0 then isnull(lvl0,0) else isnull(lvl1, 0) end)/sum(cratt),4)
0
 
duttcomCommented:
I agree it would return 0 if it were in the select clause itself e.g.

select isnull(leveln,0)
from titles

However, I believe that since it is part of 'case when' clause it will return a 1 when the value is null because the ISNULL property is being tested, not the specified null value.

I look forward to seeing how this is gets solved. Time to go home.
0
 
Ephraim WangoyaCommented:
You can simply solve it by testing for null or 0 which will be much clearer but not much different

select @G = round(sum(cratt * 
				case 
					when (leveln IS null) OR (leveln = 0) then 
						lvl0 
					else 
						lvl1 
				end)/sum(cratt),4)

Open in new window


Now, anything multiplied by null is null, if you dont want you result set to contain null, you can add ISNULL to lvl1 and lvl0

select @G = round(sum(cratt * 
				case 
					when (leveln IS null) OR (leveln = 0) then 
						ISNULL(lvl0, 0)
					else 
						ISNULL(lvl1 , 0)
				end)/sum(cratt),4)

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.