How to change number of digits after decimal point? - sql

I have this line in my query:
CASE WHEN count(n.Counter) <> 0 THEN (cast(count(n.Counter) as float)/count(t.Counter)) * 100 ELSE 0 END as percent
I'm getting results like 79.565974946.
I want to get a result like 80% (with no digits after the decimal point and with '%' sign).
How can i do that?

You need to ROUND and then, because you're trying to do formatting, convert to a string before adding the %:
CONVERT(varchar(20),
ROUND(
CASE WHEN count(n.Counter) <> 0
THEN (cast(count(n.Counter) as float)/count(t.Counter)) * 100
ELSE 0 END
,0)) + '%' as percent
Also, there may be a typo here - it looks like you're trying to avoid a divide by zero error, but you're testing the wrong operand (n.Counter rather than t.Counter) so you may still get a division by zero from this code.

If using MySQL, just use the ROUND function.
CASE WHEN count(n.Counter) <> 0 THEN ROUND(cast(count(n.Counter) as float)/count(t.Counter)) * 100 ELSE 0 END as percent

Try this:
CASE WHEN count(n.Counter) <> 0 THEN round((cast(count(n.Counter) as float)/count(t.Counter)) * 100,0) ELSE 0 END as percent

Where are you showing the data? Adding % should be done at the front end application. Just use this and format it in fornt end if you use
round(
case when count(n.counter) <> 0
then (cast(count(n.counter) as float)/count(t.counter)) * 100
else 0 end
,0))

Related

Attempting to determine decimals places using CHARINDEX, getting: Error converting data type varchar to float

I use this piece of code all the time on many objects when trying to determine the number of decimals after the '.'. The target system I am moving data to can only handle 2 d.p. So we I find data with >2 d.p. I multiply by 10, 100, 1000 and also multiply the 'price per' field value by the same factor.
SELECT
HQPROD, [HQPR1],
Decimals = CASE Charindex('.', [HQPR1])
WHEN 0 THEN 0
ELSE LEN(CAST(CAST(REVERSE(CONVERT(VARCHAR(50), [HQPR1], 128)) AS FLOAT) AS BIGINT))
END
FROM
[SM_REP].[dbo].[BPCSF64_HQT]) EDP ON P.HQPROD = EDP.HQPROD
The column datatype I am working with is DECIMAL(15,5).
Sample data:
HQPROD HQPR1
--------------------------
47001-RM64 175.89900
47001-T06OX 5.84500
47002-20T 80.44700
47002-T24 98.10300
47002-T32 144.07000
47003-01 1.54000
47003-02 1.54000
47003-03 0.51000
Error message:
Error converting data type varchar to float.
SELECT
HQPR1,
Decimals =
CASE WHEN HQPR1 > 0 THEN
CASE Charindex('.', [HQPR1])
WHEN 0 THEN 0
ELSE LEN(CAST(CAST(REVERSE(CONVERT(VARCHAR(50), [HQPR1], 128)) AS FLOAT) AS BIGINT))
END
ELSE
CASE Charindex('.', [HQPR1])
WHEN 0 THEN 0
ELSE LEN(CAST(CAST(REVERSE( Stuff((CONVERT(VARCHAR(50), [HQPR1], 128)),1,1,'' )) AS FLOAT) AS BIGINT))
END
END
This will work also for negative numbers, which as sticky bit said,could be causing your error.

Casting a String Column to 3 Digit scale in Hive Query Language

I have a select statement as follows
SELECT t5.Name AS ProductName
, CAST (t5.salerevenue AS DECIMAL(20,3)) AS Revenue
, CASE WHEN t5.PreviousRevenue <> 0 THEN CAST(t5.PresentRevenue/t5.PreviousRevenue*100 AS STRING)
ELSE 'NA'
END AS RevenueTrend
I want to cast the RevenueTrend as a decimal with maximum 3 point scale (20.123)
I have to cast it as String because WHEN t5.PreviousRevenue <> 0 is not met I have to show it as 'NA'
I tried doing it as follows
CASE WHEN t5.PreviousRevenue <> 0 THEN CAST((CAST(t5.PresentRevenue/t5.PreviousRevenue*100) AS DECIMAL(20,3)) AS STRING)
ELSE 'NA'
END AS RevenueTrend
but I am getting a syntax error.
The revenue part is projected as expected. I want the revenuetrend part also to be projected like revenue. Is there any way to achieve this?
Try converting first to a decimal, then to a string:
SELECT . . .
(CASE WHEN t5.PreviousRevenue <> 0
THEN CAST(CAST(t5.PresentRevenue/t5.PreviousRevenue*100 AS DECIMAL(20,3)) as string)
ELSE 'N/A'
END) AS RevenueTrend

How to round this result in SQL SELECT statement

I need a method to round the result returned from the following statement:
CASE
WHEN 0 <> (sod.XDFFLT - sod.DUEQTY)
THEN (sod.XDFFLT - sod.DUEQTY)
ELSE ''
END AS Balance
Which resides in this SELECT statement.
SELECT TOP (10000) som.ORDNUM
,som.NAME
,som.ADDR1
,som.ADDR2
,som.CITY
,som.STATE
,som.ZIPCD
,som.CNTRY
,som.CUSTPO
,COALESCE(cpd.CUSTPRT, sod.PRTNUM) AS CustomerSku
,cpd.CUSTPRT
,som.CUSTID
,sod.PRTNUM
,ps.PMDES1
,sod.CURQTY
,sod.DUEQTY
,sod.XDFFLT
,sod.SHPQTY
,CASE
WHEN 0 <> (sod.XDFFLT - sod.DUEQTY)
THEN (sod.XDFFLT - sod.DUEQTY)
ELSE ''
END AS Balance
,sod.LINNUM + sod.DELNUM AS LineDelNum
,CASE
WHEN 12 <= len(sod.UDFREF)
THEN substring(sod.UDFREF, 9, 4)
ELSE sod.UDFREF
END AS Skid
Replace the CASE with this:
,CASE
WHEN 0 <> (sod.XDFFLT - sod.DUEQTY)
THEN ROUND((sod.XDFFLT - sod.DUEQTY), <number_of_decimals>)
ELSE NULL
END AS Balance
Make sure to replace the <number_of_decimlas> with the appropriate number of decimals you want the number to be rounded to.
greater than 0 will return a number which is rounded on the right side of the comma (decimal point)
less than 0 will return a number which is rounded on the left side of the comma (decimal point)
Also, don't use a blank string in the ELSE clause of your case since this will cause a datatype missmatch and generate an error (a CASE must return a single datatype). Hence, it is better to replace this with NULL, since your first condition will always return a number.

prevent divide by zero when both values are obtained from a calculation

I'm trying to complete the following:
(value_a + value_b)/(value_c + value_d)
Usually I have avoided a divide by zero error with something like this:
CASE WHEN value_a + value_b = 0
THEN 0
ELSE (value_a + value_b)/(value_c + value_d)
END
For some reason in this isn't working, which makes me wonder if in the past one of my result sets have never included zero's.
Any pointers on this would be gratefully received.
You're checking the numerator, not the denominator:
CASE WHEN value_c + value_d = 0
THEN 0
ELSE (value_a + value_b)/(value_c + value_d)
END
Anothere way is to use NULLIF and COALESCE:
COALESCE((value_a + value_b)/NULLIF((value_c + value_d),0)
,0)

SQL CASE Statement Not Working Correctly

I have a view in SQL Server 2008 with several columns that are expressions of one column divided by another. I have to account for the divisor being 0, so I use a CASE statement. Here is an example of one:
CASE SUM(dbo.GameStats.BringBacksAttempted)
WHEN 0 THEN
0
ELSE
SUM(dbo.GameStats.BringBacks) / SUM(dbo.GameStats.BringBacksAttempted)
END
Even when SUM(BringBacksAttempted) and SUM(BringBacks) are not 0, the statement is always 0. What am I doing wrong?
What data type is BringBacksAttempted and BringBacks?
If both are int and the result comes to be a fraction, you will only see integer part of it.
e.g. 100 / 250 will return 0.
Whereas, CAST(100 as Decimal) / 250 will return 0.40000
Use a CAST or CONVERT on one of the fields.
Try:
CASE
WHEN SUM(dbo.GameStats.BringBacksAttempted) = 0 THEN
0
ELSE
SUM(dbo.GameStats.BringBacks) / SUM(dbo.GameStats.BringBacksAttempted)
END