Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 0 in place of NULL

Author  Topic 

skybvi
Posting Yak Master

193 Posts

Posted - 2012-02-24 : 10:12:54
Hi,
I am running this code :-

select temp16.*,(Pasea_Cost_Price/NULLIF(UNIT_CASE,0)) as Pasea_CP_Modified

from temp16

Now , I am seeing NULL values in Pasea_CP_Modified,
I want to see a 0 in place of a NULL value.
How to do tht??

Regards,
Sushant
DBA
Virgin Islands(U.K)

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-02-24 : 10:13:53
[code]ISNULL((Pasea_Cost_Price/NULLIF(UNIT_CASE,0)), 0) AS [Pasea_CP_Modified][/code]

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

skybvi
Posting Yak Master

193 Posts

Posted - 2012-02-24 : 10:20:46
Cool, that was easy and fast !

Regards,
Sushant
DBA
Virgin Islands(U.K)
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-24 : 10:55:00
EDIT: ...and incomplete and NON ANSI

SELECT *
,CASE
WHEN UNIT_CASE IS NULL THEN 0
WHEN UNIT_CASE = 0 THEN 0
ELSE Pasea_Cost_Price/UNIT_CASE*1.00
END AS Pasea_CP_Modified
from temp16

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

skybvi
Posting Yak Master

193 Posts

Posted - 2012-02-24 : 11:57:31
quote:
Originally posted by X002548

UNIT_CASE*1.00

Brett




Why this UNIT_CASE*1.00 ??

Regards,
Sushant
DBA
Virgin Islands(U.K)
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-24 : 13:28:33
Did you run the 2?

What is the Data type of UNIT_CASE?




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-02-24 : 13:59:58
he's saying that if UNIT_CASE is and integer type (smallint / bigint / int) then you'll get integer division which may not be what you want.

If UNIT_CASE is a DECIMAL/NUMERIC (same thing) or a FLOAT TYPE (or even a money type) then you'll get 'standard' division.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-02-24 : 14:42:29
Just to clarify, if both the numerator and denominator are of an integer type, sql will provide the result as an integer type (thus no decimal places). If, either the numerator or denominator are of a decimal type, then the result will be based on the sql rules for converstion and such, but you (should) get a decimal result.

In this case, assuming that Pasea_Cost_Price is not an integer type, you should not need to multiply by 1.00. I'd also go so far to suggest not properly casting your data is bad form anyway, but that's a whole other topic.
Go to Top of Page
   

- Advertisement -