Add string to end of IsNull expression - sql

I'd like to add a divisor symbol (%) to the end of my expression shown here:
select 'On-Site Case Rate' Exp1,
isnull(sum(onsite.a) * 100 / count(onsite.casecount), 0) '400',
isnull(sum(onsite.b) * 100 / count(onsite.casecount), 0) '401'
from onsite
How would I go about doing that? Do I need to use a concat and reformat my query or is it possible to insert a " + '%' "+ somewhere?
Here is a sample result, this is for an SSRS report
EDIT1: Here is the design view of my report as well

Considering that you're using SSRS, don't try to add a % sign to the end of your percentage, and convert it to a varchar, leave it as a decimal. Instead, change your display format.
Select the cell(s) that are returning your percentages and press F4. Then, in the now targeted Properties Pane locate the Format Property and change it to 0%. If you want it to display 1 (or more) decimal places then use 0.0%, 0.00%, ... you get the idea.
Note that you need to ensure that your values are returning a decimal value. You're multiplying your values by 100, which implies that you aren't. 15 isn't 15%, it's 1500%. 15% = 0.15.

In Sql Server (starting with version 2012) you can use the CONCAT function:
select 'On-Site Case Rate' Exp1,
CONCAT(isnull(sum(onsite.a) * 100 / count(onsite.casecount), 0), '%') '400',
CONCAT(isnull(sum(onsite.b) * 100 / count(onsite.casecount), 0), '%') '401'
from onsite

Related

WHERE condition to exclude amounts with decimals ending with '0's or '5's

Objective:
I have a column 'amount' with decimals. I am trying to exclude rows where the amount value ends either with '0's or '5's.
How can I achieve that...
Column type: decimal (7,2)
Ex: numbers to exclude
10.25
11.20
100.00
You probably want this:
WHERE (CAST(your_field * 100 AS INTEGER) % 5) <> 0
But it is hard to tell without more detail on your data type. Also there can be funky rounding issues with floating point values.
An interesting way to do this uses "modular" arithmetic
where col % 0.1 not in (0.00, 0.05)
The % operator works on non-integer bases as well as integer ones.
What I did here is changed the number into a string, trimmed off the trailing blanks, and then reversed the string to take the first character to see if it was no 1 or 5
SELECT * into #test FROM (SELECT CAST(10.25 as decimal(7,2)) as val UNION SELECT 8.21 UNION SELECT 6.00) DQ
select * from #test WHERE LEFT(REVERSE(RTRIM(CAST(val as nvarchar(50)))),1) NOT IN ('5', '0')
drop table #test

Remove trailing zeroes from numeric

I have a query which is proving to be cumbersome for me. I want to remove the trailing zeroes from the result.
Remove trailing zeros from decimal in SQL Server
Remove trailing zeroes using sql
select concat(100 * round(cast(count(ft.*) filter (where "Realtor_Sale" = 'Yes')
as numeric(12,5)) /
cast(count(ft.*) as numeric(12,5)),3),'%') as "Realtor Sales"
Result I am getting is:
84.800% --------------> I want 84.8%.
I tried doing this as well:
select concat(100 * round(cast(cast(count(ft.*) filter (where "Realtor_Sale" = 'Yes')
as decimal(18,5)) as float) /
cast(cast(count(ft.*) as decimal(18,5)) as float),3), '%') as "Realtor Sales"
Error:
ERROR: function round(double precision, integer) does not exist
select round(cast(cast(count(ft.*) filter (where "Realtor_Sa...
How do I get the result to round of to 84.8%?
With PostgreSQL 13 it is matter of calling trim_scale function:
trim_scale ( numeric ) → numeric
Reduces the value's scale (number of fractional decimal digits) by removing trailing zeroes
trim_scale(8.4100) → 8.41
select trim_scale(100.0 * count(ft.*) filter (where "Realtor_Sale" = 'Yes')/
count(ft.*) ) ||'%'as "Realtor Sales"
db<>fiddle demo
No need for the many casts, just use to_char() on the result:
select to_char((100 * count(ft.*) filter (where "Realtor_Sale" = 'Yes'))::decimal
/ count(ft.*), 'FM99.0%') as "Realtor Sales"
The problem with the ROUND function is, it also adds 0 at the end for the integer values.
E.g. select round(238,2) --> 238.00
Here is the solution i tried, which also preserves the integer data.
select cast(trim(trailing '0' from round(238.0100::numeric,2)::text)::numeric as text)
This will round the values with removing the trailing spaces as well as keeps the whole number as it is.

DB2 SQL How do I turn a number into a string then concatenate a percent sign onto it?

I would like to display 71% in my RESULTS field. I have rounded the decimal percentage first (it was 71.04), now I want to turn it into a string so that I can concatenate the '%' onto it. This should be simple but everything I've searched for and tried has failed. Below are the 2 most promising versions of what I have tried and the result of each one:
CHAR
(
ROUND
(
(COUNT(ONTIMETOPDD) * 100.0) /
(
SELECT COUNT(ONTIMETOPDD)
FROM WG4
WHERE APPLICABLEDELIVCOLUMN <> 'NO DELIVERY DATE'
)
,0)
)
|| '%'
AS RESULTS
Result: No error message, just a blank field. I also tried this with the concatenation line commented out. That gave me the number 71 justified on the right side of the column which makes me believe it was not converted to a string.
CAST
(
ROUND
(
(COUNT(ONTIMETOPDD) * 100.0) /
(
SELECT COUNT(ONTIMETOPDD)
FROM WG4
WHERE APPLICABLEDELIVCOLUMN <> 'NO DELIVERY DATE'
)
,0)
AS CHAR)
|| '%'
AS RESULTS
Result: Error message SQL0433 - Significant data truncated during CAST to character. (#-433)
I would really appreciate it if someone can point me in the right direction.
CHAR is a fixed length character value. Which includes leading/trailing blanks.
Given that you what to concatenate a % sign, VARCHAR would be a better choice.
ROUND() does not change the scale of the number being rounded, it simply makes the appropriate digits zero.
So if you really wanted to use round, you'll need to another function to change the scale of the value to 0 prior to the conversion to VARCHAR. INT() is my personal favorite.
select .7184, varchar(int(round(.7184, 2) * 100)) concat '%'
from sysibm.sysdummy1
If you are ok with simple truncation, you can just use INT() without the round.
select .7184, varchar(int(.7184 * 100)) concat '%'
from sysibm.sysdummy1
I prefer to use TO_CHAR() in all cases:
TO_CHAR(
(SELECT COUNT(ONTIMETOPDD)
FROM WG4
WHERE APPLICABLEDELIVCOLUMN <> 'NO DELIVERY DATE'
),
'99'
) || '%'
The percentage ratio is a DECIMAL type prior to the conversion to a character column, so try casting it to a SMALLINT first in order to get rid of the decimal point. After that, casting to VARCHAR instead of CHAR will eliminate the trailing spaces for values between 0 and 99.
LTRIM
(
CAST
(
SMALLINT
(
ROUND
(
(COUNT(ONTIMETOPDD) * 100.0) /
(
SELECT COUNT(ONTIMETOPDD)
FROM WG4
WHERE APPLICABLEDELIVCOLUMN <> 'NO DELIVERY DATE'
)
,0)
)
AS VARCHAR(4)
)
)
|| '%'
AS RESULTS

T-SQL - how to round DOWN to nearest .05

The database I am using is SQL Server 2005. I am trying to round values DOWN to the nearest .05 (nickel).
So far I have:
SELECT ROUND(numberToBeRounded / 5, 2) * 5
which almost works - what I need is for the expression, when numberToBeRounded is 1.99, to evaluate to 1.95, not 2.
Specify a non-zero value for a third parameter to truncate instead of round:
SELECT ROUND(numberToBeRounded / 5, 2, 1) * 5
Note: Truncating rounds toward zero, rather than down, but that only makes a difference if you have negative values. To round down even for negative values you can use the floor function, but then you can't specify number of decimals so you need to multiply instead of dividing:
SELECT FLOOR(numberToBeRounded * 20) / 20
If your data type is numeric (ISO decimal) or `money, you can round towards zero quite easily, to any particular "unit", thus:
declare #value money = 123.3499
declare #unit money = 0.05
select value = value ,
rounded_towards_zero = value - ( value % #unit )
from #foo
And it works regardless of the sign of the value itself, though the unit to which you're rounding should be positive.
123.3499 -> 123.3000
-123.3499 -> -123.3000

SQL - How do i output string with numbers in sql?

I want to get a number 5000.1 and divide it by 1000 before adding an "F" infront of it.
How do i do this? I tried and failed this:
select "F" + round ( acq.store_size_net / 1000, 0) from acq
I suspect your missing the cast of the number to a text data type
Without knowing the exact dialect of sql you're using im gonna hazard a guess at ms-sql
select 'F' + cast(cast(round ( 5000.1 / 1000, 0)as int) as nvarchar(50))
produces output
F5
This will work in Oracle :
select 'F' || round (acq.store_size_net / 1000, 0) from acq