ReportViewer question - reportviewer

I have a column that divides a field by another field. And if it is 0 I it prints Error. How can I get it to print nothing or a 0 in this situation?

Can you just create an expression for the result column and display the result conditionally?
Eg, instead of:
=ColumnA!Value/ColumnB!Value
Use something like:
=Iff(ColumnB!Value <> 0, ColumnA!Value/ColumnB!Value, 0)

You should try this one:
=IIF(Fields!F1.Value <> 0, Fields!F2.Value /
IIF(Fields!F1.Value <> 0, Fields!F1.Value, 42), 0)
Also check this link for more information.

Related

Concise test for zero or NULL for multiple columns

I would like to make test for multiple columns if they have a positive number and return results in a binary string. Single test condition yield 0 if either =0 or is NULL. Say, we have a 3 conditions A, B, and C which return either 0 or 1. The result i.e., 101 means that A and C have positive numbers while B is either zero or null.
This gets what I want:
SELECT
format(
iif(coalesce([A], 0) > 0, 100, 0)
+ iif(coalesce([B], 0) > 0, 10, 0)
+ iif(coalesce([C], 0) > 0, 1, 0)
), '000'
)
Is there a more concise way to achieve the goal, perhaps avoiding COALESCE?
I'd just go for CONCAT if I am honest, it's far more performant than FORMAT, and remove the COALESCE as it isn't needed.
SELECT CONCAT(IIF(A>0,1,0),IIF(B>0,1,0),IIF(C>0,1,0))
FROM (VALUES(1,NULL,2),
(-1,12,18),
(-1,0,1),
(NULL,NULL,-4))V(A,B,C);

how to use is null with case statements

Hi can any one say me how to write query in sql server management studio for statement =>if(isnull("GROSS_SF")=1 or "GROSS_SF"=0,0,"GROSS_SALES"/"GROSS_SF")
by using case statement :
CASE
WHEN ("GROSS_SF"=1 or "GROSS_SF"=0) isnull then 0
else "GROSS_SALES"/"GROSS_SF"
end/* i am getting error if i write it like this */
thanks in advance
If you want to avoid a divide by zero, while at the same time also handling NULL, then you may try the following logic:
CASE WHEN COALESCE(GROSS_SF, 0) = 0
THEN 0
ELSE GROSS_SALES / GROSS_SF END AS output
To be clear, the above logic would return a zero value should either the GROSS_SF be zero or NULL.
I think what are you looking for is IFNULL, look here https://www.w3schools.com/sql/sql_isnull.asp
You are close. You can do:
(CASE WHEN "GROSS_SF" IS NULL OR "GROSS_SF" = 0 THEN 0
ELSE "GROSS_SALES" / "GROSS_SF"
END)
In SQL, divide by zero errors are often avoided using NULLIF():
"GROSS_SALES" / NULLIF("GROSS_SF", 0)
However, that returns NULL rather than 0. If you really want zero:
COALESCE("GROSS_SALES" / NULLIF("GROSS_SF", 0), 0)
This is a bit shorter to write and almost equivalent to your version (this returns 0 if "GROSS_SALES" is NULL, which your version does not).
You can use NULLIF() to prevent arithmetic divide by zero errors :
select . . . ,
GROSS_SALES / nullif(GROSS_SF, 0)
from table t;
However, this would return null instead of error, if you want to display 0 instead then you need to use isnull() (MS SQL Specific) or coalesce() (SQL Standard).
So, you can express it :
select . . . ,
isnull(GROSS_SALES / nullif(GROSS_SF, 0), 0)
from table t;

Mysql query deducting 2 counts result less 0

I have a a query which retrieves 2 times a count from 2 tables.
Now in the same query it has (countresult1-countresult2) AS restresult
Now restresult is sometimes less than 0 (eq -10) but I want it to return 0 if it's under 0.
Uhm did I explan that right? Minimum value should be 0 not below.
Cheers!!!
GREATEST((countresult1-countresult2), 0) AS restresult
if (countresult1<countresult2, 0, countresult1-countresult2) as restresult
neither countresult1 nor countresult2 will return a negative number, so above should be safe
without seeing your query, you could have something like...
MAX( if( countresult1-countresult2 < 0, 0, countresult1-countresult2 )) as YourResult
Take the maximum of 0 and the value you calculated, like this:
SELECT GREATEST(your-restresult-query,0)
FROM ... (etc)
Until Now I didn't know there was if-else commands in SQL, but I found some.
you will want to use:
WHEN (countresult1-countresult2) < 0 THEN 0 ELSE (countresult1-countresult2)
Here is the source where I found the SQL information: http://www.tizag.com/sqlTutorial/sqlcase.php

T-SQL Case statement utilizing substring and isnumeric

I have a T-SQL stored proc that supplies a good amount of data to a grid on a .NET page....so much so that I put choices at the top of the page for "0-9" and each letter in the alphabet so that when the user clicks the letter I want to filter my results based on results that begin with that first letter. Let's say we're using product names. So if the user clicks on "A" I only want my stored proc to return results where SUBSTRING(ProductName, 1, 1) = "A".
Where I'm getting hung up is on product names that begin with a number. In that case I want to fetch all ProductName values where ISNUMERIC(SUBSTRING(ProductName, 1, 1)) = 1. I'm using an input parameter called #FL. #FL will either be a zero (we have few products that begin with numerics, so I lump them all together this way).
Of course there's also the alternative of WHERE SUBSTRING(ProductName, 1, 1) IN ('0', '1', '2'.....) but even then, I've never been able to devise a CASE statement that will do an = on one evaluation and an IN statement for the other.
Here's what I have in my proc for the CASE part of my WHERE clause. It doesn't work, but it may be valuable if only from a pseudocode standpoint.
Thanks in advance for any ideas you may have.
AND CASE #FL
WHEN "0" THEN
CASE WHEN #FL = "0" THEN
isnumeric(substring(dbo.AssnCtrl.Name, 1, 1)) = 1
ELSE
SUBSTRING(dbo.AssnCtrl.Name, 1, 1) = #FL
END
END
*** I know that this use of the CASE statement is "non-standard", but I found it online and thought it had some promise. But attempts to use a single CASE statement yielded the same result (an error near '=').
Why not just use Like operator ?
Where dbo.AssnCtrl.Name Like #FL + '%'
When they select the Any Number option, pass in #FL as '[0-9]'
(I assume you have an index on this name column ?)
To steal a bit from Charles:
AND Substring(dbo.AssnCtrl.Name, 1, 1) Like
CASE WHEN #FL = '0' THEN '[0-9]'
ELSE #FL
END
Have you tried
AND (
isnumeric(substring(dbo.AssnCtrl.Name, 1, 1)) = 1
or
SUBSTRING(dbo.AssnCtrl.Name, 1, 1) = #FL )
this works for me:
select * from casefile where
isnumeric(substring(pmin,1,1)) = 1
or
substring(pmin,1,1) = 'a'

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