1. Registered User
Join Date
Dec 2006
Posts
35

this give me 6 digits to the right of the decimal point - can you help me round it to 2???

CONVERT (decimal(18 , 2), 1.0 * NULLIF (vVotesR, 0) / NULLIF (vVotesR + vVotesD, 0) * 100)

Thanks!

2. Resident Curmudgeon
Join Date
Feb 2004
Location
In front of the computer
Posts
15,579
A lot depends on the context in which you are using this code snippet. By itself, it returns a DECIMAL(18, 2) which only has two decimal places. Depending on the code around it, this might seem to return more decimal places.

Using NULLIF() this way has always caused me grief. I don't think that is what you want.

-PatP

3. Registered User
Join Date
Feb 2008
Location
Japan
Posts
3,483
CONVERT (decimal(18 , 2), 1.0 * NULLIF (vVotesR, 0) / NULLIF (vVotesR + vVotesD, 0) * 100)
1) Why did you multipled by 100 after division?
2) Is NULLIF required in "NULLIF (vVotesR, 0)"?

The following expression looks simple and understandable for me.

4. www.gvee.co.uk
Join Date
Jan 2007
Location
UK
Posts
11,445
Pat rightly points out that if you are converting to decimal(18, 2) then you can only have 2 decimal places. The "2" in the datatype here denote3s the precision (i.e. the number of decimal places).

As for NullIf() << avoid it.