I have the following case statement, the AppointmentDate isn’t always populated and therefore require to pull from other date fields - looking to have one appointment date (populated from many fields)
When running this statement, doesn’t return full results.
,Case=Appointment
When AppointmentDate Is Null Then ChaseDate
When ChaseDate Is Null Then DueDate
Else AppointmentDate
End
Any thoughts on where I’ve went wrong?
Thanking you in advance.
You haven't stated if this is supposed to be an expression in SSRS or this is part of your dataset query.
Assuming SQL Server here...
If this is part of your dataset query then you can do something like
SELECT
FieldA, FieldB,
myAppointmentDate = COALESCE(AppointmentDate, ChaseDate, DueDate)
FROM myTable
COALESCE will return the first non-null value
If you are trying to do this in an SSRS expression then you need to use the SWITCH() statement.
=SWITCH(
Fields!AppointmentDate.Value <> Nothing, Fields!AppointmentDate.Value,
Fields!ChaseDate.Value <> Nothing, Fields!ChaseDate.Value,
Fields!DueDate.Value <> Nothing, Fields!DueDate.Value,
True, Fields!AppointmentDate.Value
)
This will return the first expression that is true, if non of the first three exression return true, the final True acts like an else (which in your case would return Nothing anyway)
Related
How can I test an IF statement in MySQL ?
I need to know, what happens when a query is into a IF statement and it returns nothing (0 row selected) ?
In other word, what a SELECT query returns when the no row is selected? Will it return NULL? If yes, so will this throw an error? IF ( NULL ) THEN ...? Or in this case just the statement of that IF won't execute?
In reality, I have a code like this:
IF ( SELECT banned FROM users WHERE id=new.user_id ) THEN
// do stuff
Note: banned column contains either 0 or 1.
All I want to know, is my code safe when that query doesn't return any row? I mean will it throw an error or nothing happens (just that IF statement doesn't execute) ?
Noted that I wanted to test it myselft, but I don't know how can I test a IF statement in MySQL. For example, I wanted to test it by this code:
BEGIN
IF ( SELECT banned FROM users WHERE id=new.user_id ) THEN
SELECT 'test';
ENDIF;
END
But code above doesn't even run. That's why I've asked this question.
An IF statement can be tested in the context of a MySQL stored program (a procedure, function or trigger.) MySQL doesn't support anonymous blocks.
To test an IF statement, you can create a procedure
DELIMITER $$
CREATE PROCEDURE foo
BEGIN
IF ( some_condition ) THEN
SELECT 'evaluation of some_condition returned TRUE' AS msg;
ELSE
SELECT 'evaluation of some_condition did NOT return TRUE' AS msg;
END IF;
END$$
DELIMITER ;
CALL foo;
I need to know, what happens when a query is into a IF statement and it returns nothing (0 row selected) ?
What you're actually asking about here is a scalar subquery.
Scalar subqueries return exactly one column and exactly one or zero rows. Returning NULL and matching zero rows are equivalent, and neither will throw an error. In both cases, the subquery's return "value" is NULL.
You will, however, throw an error if you manage to write a scalar subquery that returns more than one row (such as when you forget WHERE in the subquery. But zero rows is perfecty valid.
IF tests for truthiness, and anything that IS TRUE ... which technically means any expression that both IS NOT FALSE and IS NOT NULL -- satisfies the IF. If the return value is FALSE (0) or NULL then the ELSE branch will be followed, if present.
I highlighted keyword phrases like IS NOT FALSE, above, to emphasize the point that these are logical operators in SQL.
Try queries like SELECT 0 IS FALSE (returns 1, because it is) or SELECT NULL IS NOT TRUE (returns 1, because it isn't) or SELECT NULL IS NOT FALSE (also returns 1, because it isn't) SELECT NULL IS FALSE (returns 0, because it isn't) to see what I mean.
I need a condition that when used with an IN condition, it won't return any results. I initially thought this would work, but it still gives back results:
SELECT * FROM MyTable WHERE myColumn IN (SELECT FALSE FROM dual WHERE FALSE);
Is there a condition I could put in the IN condition to have it always return no results?
NOTE: This is being used in a PHP function where the column could be any type with any value.
Instead of false use null. When null occurs in an expression, that always leads to a overall null value (there are some exceptions, like with is null but not in this context of in):
... in (select null from dual where null)
Is there a way to update any return values in MySQL that come back as either NULL or blank to "Unknown" or any value? My NULLs and blanks exist because I'm joining quite a few tables together and sometimes records exist but are blank and sometimes records don't exist in the other tables at all.
I would prefer not to update the original table because I don't save my result each time I run the query -- I just copy and paste the return into Excel before I send out the report.
Basically, it's just getting annoying sending this out multiple times a day and after pasting into Excel hitting Ctrl+F and replacing anywhere that says "NULL" with "Unknown" and then doing it again to replace any blank cell with "Unknown."
After looking around, I found IFNULL which obviously works if it's NULL but not blank -- but (1) it would be great to not have to wrap every part of my SELECT statement with IFNULLs if possible and (2) use something to encompass the blanks as well.
Just hoping there's something that I could put at the end of the query or something that I can't find. But it might just not exist within the way I'm doing it. I don't think this question needs code or schema because of the general-ness of it, but I'm certainly happy to get more specific if it helps. Thanks!
You could wrap all of the columns that could be null in a case statement that checks if any of them are null or blank before returning them.
For example:
SELECT
CASE WHEN firsttable.column1 IS NULL or firsttable.column1 = '' THEN 'Unknown' ELSE firsttable.column1 END AS firsttable.column1
CASE WHEN firsttable.column2 IS NULL or firsttable.column2 = '' THEN 'Unknown' ELSE firsttable.column2 END AS firsttable.column2
CASE WHEN secondtable.column1 IS NULL or secondtable.column1 = '' THEN 'Unknown' ELSE secondtable.column1 END AS firsttable.column1
CASE WHEN secondtable.column2 IS NULL or secondtable.column1 = '' THEN 'Unknown' ELSE secondtable.column2 END AS firsttable.column2
FROM
firsttable,
secondtable
WHERE
firsttable.id = secondtable.firsttableid
You can use IF in combination with COALESCE:
SET #val := '';
select IF(COALESCE(#val,'')='','Unknown',#val);
Returns 'Unknown', the same will returns for NULL. Any other value will be returned:
SET #val := 'test';
select IF(COALESCE(#val,'')='','Unknown',#val);
Returns 'test'.
Trick is in COALESCE - it returns first non-NULL value, so COALESCE(NULL,'') will return '' and COALESCE('','') will return '' too. You can replace variable #val with any column from table or view etc.
I'm working with iReport Designer and JasperServer 5.6 on a MySQL database and I'm trying to make my report return all results when the parameter is null.
I've been looking at the documentation here: http://jasperreports.sourceforge.net/sample.reference/query/, which has been quite helpful except it doesn't have what I want. The closest I've gotten is here in the documentation:
The $X{EQUAL, column_name, parameter_name} clause function
The function expects three mandatory clause tokens:
The first token represents the function ID and always takes the fixed value EQUAL.
The second token is the SQL column (or column combination) to be used in the clause.
The third token is the name of the report parameter that contains the value to compare to.
If the parameter's value is not null, the function constructs a
= ? clause. If the parameter's value is null, the
function generates a IS NULL clause.
All the parameters I'm inputting are the id's of the records I'd like to see, so when I use this I get no results because an id or the Primary Key cannot be null.
Ex.
SELECT *
FROM User
WHERE $X{EQUAL, user.id, user_id}
Inputting 1 will return user id 1 and inputting nothing, or null, will return me nothing. What I want it to return instead is all users in the table.
Is there an easy fix to this problem, like having this function return IS NOT NULL when this happens? Is there something else in JasperServer or iReports that will help me or is there something I can do in SQL that will ignore a WHERE clause when I have this parameter set to null?
You can use the below logic in your query's WHERE clause to achieve what you are asking (this works in postgres, but it should work in MySQL as well):
where (($P{parameter1} is null) or (user.id = $P{parameter1}))
If the parameter is null the first part of the OR comparator will return true, and the query will ignore the rest of the expression, which should give you all users in the user table. If it is not null, it will skip over the first part and execute the query for the user.id passed to the parameter.
Hope this helps!
I have my Mysql Query as :
select RecordID,ID,Name,Description,RecordDateTimeUTC,Location from
eemployee where RecordID like ? and(isNull(RecordDateTimeUTC) OR RecordDateTimeUTC= CASE WHEN COALESCE(?,'') = '' THEN RecordDateTimeUTC ELSE ? END)........
Now my query There are lott of fields(? --> this is where i am putting my data for filter perpose) upon which user can query so i am using coalesce but the disadvantage of using coalesce is it doesn't give me null values when i want to get all data present in employee table , so to get null values i have used isNull function.
Now i am testing the above query for two use cases
1-->User can pass percentage in RecordID:-In this case he should get all data either null as well as not null values :
select RecordID,ID,Name,Description,RecordDateTimeUTC,Location from
eemployee where RecordID like '%'
and(isNull(RecordDateTimeUTC) OR RecordDateTimeUTC= CASE WHEN COALESCE('','') = '' THEN RecordDateTimeUTC ELSE '' END)
2-->User can filter data based on RecordDateTimeUTC:- If user passes this parameter then he should get all not null data satisfying the above filteration.:-
select RecordID,ID,Name,Description,RecordDateTimeUTC,Location from
eemployee where RecordID like '%' and(isNull(RecordDateTimeUTC) OR RecordDateTimeUTC= CASE WHEN COALESCE('2012-07-09 11:11:00','') = '' THEN RecordDateTimeUTC ELSE '2012-07-09 11:11:00' END)
For the first use case it is working fine but for the second use case it is giving me filtered data as well as null Data
So what should be my query so that both my use cases are supported by the above single query.
I am using MYSQl Query Browser to test my query
Thanks in advance
It looks like you're trying to make parameters optional while using a single static SQL statement. In that case I would imagine you mean something like:
SELECT RecordID,ID,Name,Description,RecordDateTimeUTC,Location
FROM eemployee
WHERE (#RecordId IS NULL OR RecordID LIKE #RecordId)
AND (#RecordDateTimeUTC IS NULL OR RecordDateTimeUTC = #RecordDateTimeUTC)
If the parameter value is NULL, it will be omitted completely from the filter, otherwise the passed value will be used.
Note you can't use ? here because of the need to re-use the parameter - unless you want to specify each parameter twice, that is. Specifying a name makes the query a lot more readable IMHO.