How to manage when mybatis query returns null - sql-server-2008

I have a mybatis query that returns a Long value depending on a parameter , but if the parameter doesn´t exist on the db it returns null, but I´m getting this exception:
Mapper method 'mysqlservermethod attempted to return null from a method with a primitive return type (long).
How can I allow to return a null or what other thing can I do so, I can control in my java part for do something when the query returns null or let me know that there are no values that match the query, without doing a select count before?
MyBatys select header:
resultType="java.lang.Long" >

Related

prepared mysql query where value may be NULL

I'm using node's mysql library and trying to do a query like so:
connection.query(`SELECT * FROM table WHERE name = ? AND field = ?`, ['a', value]);
The problem I'm running into is that sometimes value = 1 but sometimes value = null.
From my testing, results only return when the query is written as WHERE value IS null and doesn't work with WHERE value = null.
Q: How can I use the prepared query if the value may be null?
Sorry to make you disappointed, but YOU CANNOT
You should use different comparative statement, which is
WHERE value IS NULL
WHERE value = <your value>
Regards to the Mysql Reference, null values are treated differently, furthermore null values are a missing values. So You can't use arithmetic comparison for NULL
Here is the reference https://dev.mysql.com/doc/refman/8.0/en/working-with-null.html
Nobody mentioned spaceship operator <=>, it works with null-to-null comparsions
Here is great spaceship operator description
Maybe you want try this:
SELECT * FROM table WHERE IFNULL(name, 'null') = 'null' AND IFNULL(field, 'null') = 'null'
But the next problem, you cannot fill your field with value 'null', or it will makes your query and data ambiguous.

ISNULL with multiple Integer values in SQL Server

My requirement is to pass input parameter with some Integers into DB to fetch the data. But in the front end, I'm checking if we didn't select any input data (Report type of application), NULL should pass else all the data or selected data should pass.
WHERE
a.DateTime BETWEEN '2018-04-12 00:00:00' AND '2018-04-12 23:59:59'
AND ISNULL('0000441183344450,0000447769267501,0000447789917187',CallNumber) = CallNUmber
AND ISNULL(CAST('1,2,3' AS INT), ID) = ID
In the above query ISNULL(CAST('1,2,3' AS INT), ID) = ID is causing an error:
Conversion failed when converting the varchar value '1,2,3' to data type int
I know its very generic error but my requirement is to pass the selected Integer values or all values if the user didn't select any values in the input.
In the database, ID has an int datatype and if used with quote values (and ISNULL('121','122',AgentSkillTargetID)=AgentSkillTargetID) I'm getting an error
The isnull function requires 2 argument(s)
In the same query
ISNULL('0000441183344450,0000447769267501,0000447789917187',CallNumber) = CallNUmber
is working, since the datatype of CallNumber is varchar.
The isnull function is a t-sql function that can only accept two parameters. For returning the first non-null value from a larger list of parameters, use the ansi-complient coalesce function instead. Some people might claim you should never use isnull, since it's not ansi complient. Personally, I don't think that it's a good enough reason. I think you should use the best tool available.
For more information, read the Comparing COALESCE and ISNULL section of the coalesce doc page.

How to set JasperReports 5.6 Equal Clause Function to IS NOT NULL

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!

Check the value type of a JSON value in Postgres

Let's say I have a json column fields, like so:
{phone: 5555555555, address: "55 awesome street", hair_color: "green"}
What I would like to do is update all entries where the json key phone is present, and the result is of type number to be a string.
What I have is:
SELECT *
FROM parent_object
WHERE (fields->'phone') IS NOT NULL;
Unfortunately this still returns values where phone:null. I'm guessing that a JSON null is not equivalent to a SQL NULL.
How do I
1) How do I rule out JSON nulls
AND (fields->'phone') <> null produces
LINE 4: ...phone') IS NOT NULL AND (fields->'phone') <> 'null';
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
2) Check the type of the value at that key, this pseudocode (type_of (fields->'phone') == Integer) but in working PGSQL.
3) Modify this to update the column
UPDATE parent_object
SET fields.phone = to_char(fields.phone)
WHERE query defined above
As other folks have said, there is no reason to convert the variable to an integer just to them cast it to a string. Also, phone numbers are not numbers. :-)
You need to be using the ->> operator instead of ->. That alongside IS NOT NULL gets your SELECT query working.
Note the difference between the two tuple values after running this query:
SELECT fields->'phone', fields->>'phone'
FROM parent_object;
Your working query:
SELECT *
FROM parent_object
WHERE (fields->>'phone') IS NOT NULL;
Postgres does not currently natively support atomically updating individual keys within a JSON column. You can write wrapper UDFs to provide this capability to you: How do I modify fields inside the new PostgreSQL JSON datatype?
For checking the type of the value at key, postgres has the following in the documentation.
json_typeof ( json ) → text
jsonb_typeof ( jsonb ) → text
Returns the type of the top-level JSON value as a text string. Possible types are object, array, string, number, boolean, and null. (The null result should not be confused with a SQL NULL; see the examples.)
json_typeof('-123.4') → number
json_typeof('null'::json) → null
json_typeof(NULL::json) IS NULL → t

COALESCE() to get Not null as well as NULL Values

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.