Remove trailing zeroes from numeric - sql

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.

Related

SQL SERVER 2019 - LEN(FLOOR(CAST([value] AS FLOAT))) defaulting to 12

I am running the following code to get the length of a value before the decimal place:
SELECT LEN(FLOOR(CAST([VALUE] AS FLOAT))) FROM TABLE1 WHERE VALUE2 <> 'B'
The [VALUE] column in TABLE1 is of type nvarchar(30) hence the cast. The column also contains some non-numeric values but these are filtered out by the WHERE clause as they all have a 'B' value for VALUE2.
The code works as expected and returns '6' for values with 6 digits such as '123456.123'. It also works correctly for values with less than 6 digits. However, the code simply returns '12' for any value with greater than 6 digits such as '12345678'.
I've done some googling and can't seem to find a reason for this? Any explanations / alterations / alternatives would be much appreciated!
LENGTH() function expects string expression, so the float value is implicitly converted to string using scientific notation. The following statement demonstrates this issue and the unexpected result:
SELECT
LEN(FLOOR(CAST([VALUE] AS FLOAT))),
FLOOR(CAST([VALUE] AS FLOAT)),
CONVERT(varchar(50), FLOOR(CAST([VALUE] AS FLOAT)))
FROM (VALUES
(N'12345678')
) TABLE1 ([VALUE])
Result:
12 12345678 1.23457e+007
A possible solution, without using an integer (and/or float) conversion, is the following statement:
SELECT CHARINDEX(N'.', CONCAT([VALUE], N'.')) - 1
FROM (VALUES
(NULL),
(N'12345678'),
(N'123456.123'),
(N'99999.923')
) TABLE1 ([VALUE])
I am running the following code to get the length of a value before the decimal place:
This value is called the log base 10 plus 1 -- at least for numbers greater than 1. So how about using:
floor(log10(value)) + 1
You can tweak this for values less than 1 (including negative values) if that is needed.

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

How to preserve leading zeros when converting to a decimal in oracle

I am running the below query in oracle.
WITH
ta AS (
SELECT account_coid
,txn_id
,cbdev.cbzdt(effective_date) AS effective_date
,cbdev.cbchr(utl_raw.substr(txn_data, 113, 20)) AS CESG_amt
FROM bs_transaction
WHERE sub_type = 127469880)
SELECT
cast(ta.CESG_amt as DECIMAL (20,2)) AS cesg_amt
from ta
inner join ....
Here, i m getting the result (cesg_amt) as -156.57. But i need the result as -0000000000156.57.
I need the leading zeros with - retained (leading 0's and also the two digits after the decimal).
I have tried as to_char(ta.CESG_amt, '0000000000000.00') AS cesg_amt in the query but of no use.
Can you please help me what needs to be done in the DECIMAL field to get the result as below.
You may use such a formatting :
select to_char(-156.57,'fm0000000000000D00','NLS_NUMERIC_CHARACTERS = ''.,''')
as Result
from dual;
RESULT
-----------------
-0000000000156.57
select to_char(-156.57,'000000000.00')
from dual;

SQL select a decimal column and convert it to an int with leading zeroes

I am trying to select a decimal column and have it show without the decimal and with leading zeroes.
The column is defined as vendor_cost(decimal(9,2),null
select vendor_cost from inventory_vendors
The result is vendor_cost = 1.10
I want the result to show as vendor_cost = 000000110
Well, you could multiply by 10 and add the leading zeros:
select left(replicate('0', 10) + cast(cast(vendor_cost * 100 as int) as varchar(255)), 10)
Or, alternatively, use str() and replace the leading spaces with zeros:
select replace(str(vendor_cost * 100, 10, 0), ' ', '0')
Another option is Format() 2012+
Select format(vendor_cost *100,'000000000')
Returns
000000110
I should note that Format() has some great features, but it NOT known for its performance

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