Dividing 2 ReportItems - sql

I try to divide 2 ReportItems FreeNumberField_14 and FreeNumberField_13 with the following expression:
=(ReportItems!FreeNumberField_14.Value / ReportItems!FreeNumberField_13.Value)
When running the report I receive the following error:
The Value expression for the textrun
‘Textbox82.Paragraphs[0].TextRuns[0]’ contains an error: Input string
was not in a correct format. (rsRuntimeErrorInExpression)
What is the correct way to dividing 2 ReportItems?

=Cdec(ReportItems!freefield1.Value)/Cdec(ReportItems!freefiled2.Value)

try to Convert it to Int,Decimal Or Number before you divide like this
=(CDec(ReportItems!FreeNumberField_14.Value) / CInt(ReportItems!FreeNumberField_13.Value))

Related

Msg 3623, An invalid floating point operation occurred

When I tried to update some tables value I got this error.
I have table with column some float values
Values
---------------------
11.7830808596997
-9.41220524769667
1.70771403155729
-26.1598813945345
1.40722323420305
0.315202798180501
-3.27870943910565
4.49832736699458
-21.8406197004573
12.6569938818624
10.3327211608816
-14.9366297400332
-1.96665717283736
5.90430556370099
1.59122918690946
1.01784176743728
-41.3800628432377
Now I want to change to power of 0.3333 with all that value.
So I write a statement
update table
set value = power(value, 0.3333)
But I get an error
Msg 3623
An invalid floating point operation occurred
Can you suggest how I can fix that? I want to do something like
(1 + value) ^ 1/3 - 1
in T-SQL
I think this is the logic you want:
select sign(val) * exp(log(abs(val)) / 3)
from (select -8 as val) t
The issue is that you cannot raise negative numbers to fractional powers because the result is generally an imaginary number, not a floating point number. This simple converts the value to a positive number and remembers the sign.
You could also use:
select sign(val) * power(abs(val), 1.0 / 3)
Here is a db<>fiddle.

Get a response with the remainder of the request

I want to separate the two numbers and get the answer with the remainder.
select 13/5;
I get an answer 2.
I add types.
select 13::numeric/5::numeric;
I get an answer 2.6000000000000000.
But I need only one number after the decimal point. How do I get 2.6?
Like so:
SELECT (13.0 / 5.0)::numeric(10, 1)
The .0 tells PG that it is a numeric literal. Division of two numerics result in numeric.
You can use ROUND to restrict to 1 decimal place. Note that you only need to cast the first value as numeric:
SELECT ROUND(13::numeric/5, 1)
Output
2.6
Try:
select round(13::numeric/5::numeric, 1);
https://www.w3resource.com/PostgreSQL/round-function.php
If you want it displayed in that fashion, you should convert the result to a string with the proper format:
SELECT to_char(13::numeric / 5::numeric, '9999.9999FM');
to_char
---------
2.6
(1 row)

Division in SQL Server

I am trying to use division in SQL Server. I have tried the code snippet below.
SELECT m.[Profit] / f.[Target]
And I have also tried
SELECT SUM(m.[Profit] / f.[Target])
but I get the error
Divide by zero error encountered.
Warning: Null value is eliminated by an aggregate or other SET operation.
What is the reason for this and how can I fix it? Suggested fixes online say that this code should work but it doesn't.
Thanks
Use NULLIF to avoid divided by zero exception
SELECT SUM(m.[Profit] / NULLIF(f.[Target],0))
When denominator is zero then it will be replaced with NULL
If I were trying to determine the % of a goal (inferred by profit /target columns), I would try something like:
SELECT
CASE WHEN ISNULL(f.[Target],0) = 0 THEN NULL -- or whatever you need to return when the target value is undefined, or it is 0
ELSE m.[Profit] / f.[Target] END AS [result]
The error you get specifies exactly what is the problem. Your code is trying to divide by zero, which is mathematically impossible.
Please NULLIF to avoid this error.

Trying to Find Maximum Value

I'm trying to find a maximum number of a string. First I try to turn it into an Integer field first, but keep getting error message for example:
Conversion failed when converting the nvarchar value '3,029' to data type int.
I tried to replace the possible single quotation marks into a blank char like below:
SELECT TOP 100 (CAST(REPLACE(a.PortNumber,'''','') AS INT)) FROM dbo.Account a
WHERE nwp_AccountType = 121710000
ORDER BY (CAST(REPLACE(a.PortNumber,'''','') AS INT)) DESC
But still getting the same error message again.
Any idea?
The error is in your REPLACE statement
(CAST(REPLACE(a.PortNumber,',','') AS INT))
The problem was the comma, I added another replace for the comma to an empty string and it works.

SQL: Unable to CAST a query

SELECT CAST ((SUM(r.SalesVolume)/1000) AS decimal(3,3)) FROM RawData r
The above is a part of a query that I am trying to run but returns an error:
Lookup Error - SQL Server Database Error: Arithmetic overflow error converting int to data type numeric.
Not sure what this means.
The result column looks like(Without dividing by 1000 and casting):
Total_Sales_Volume
64146
69814
68259
56318
66585
51158
44365
49855
49553
88998
102739
55713
Tried casting as float but doesnt help.
The Problem is decimal(3,3) --> this means a number with 3 digit, 3 of them behind the decimal point. If you want a number like this 1234567.123 you would have do declare it as decimal(10,3)
Try this:
SELECT CAST ((SUM(r.SalesVolume)/1000.0) AS decimal(6,3)) FROM RawData r
decimal(3,3) means that you allow numbers with 3 digits in total, and 3 of these are behind the comma ... I think you meant decimal(6,3)
EDIT: In addition, you need to to divide by 1000.0, not by 1000.
If you divide by 1000, it is an integer division.
If you divide by 1000.0, then it becomes a decimal division, with commas.
Try following:
SELECT CAST ((SUM(r.SalesVolume)/1000) AS numeric(6,3)) FROM RawData r