Inserting NULL value to MySQL, from Coldfusion - mysql

I have a database that may have a value in one of the fields. Circumstance may need to remove the initial value and replace it with a NULL.
How can NULL values be inserted in an empty field?
Appreciate it.

You'd write a query that's something like this:
<cfquery name="queryName" datasource="dsn">
UPDATE tableName SET fieldName = NULL
WHERE recordId = <cfqueryparam cfsqltype="cf_sql_integer" value="#recordId#" />
</cfquery>
Also, there's a difference between a blank string and a NULL value. The thing to remember about NULL is that it doesn't equate to anything but NULL. But it also doesn't equate to non-NULL values. So if you have three records in your table:
ID VALUE
1 15
2 NULL
3 30
When you run this query:
SELECT ID from tableName where value != 15
you will ONLY get record 3. The way to make sure record 2 is included in the resultset is to alter your query as such:
SELECT ID from tableName where coalesce(value,16) != 15
The purpose of coalesce is to check the first parameter for NULL-ness, and use the value of the second parameter instead if the first parameter is NULL. In the above example, I've put '16' as the second parameter, but you can use any value you want as long as it's not the same as what you're comparing for (in this case, 15).

Related

Why does <> 'null' works in MySQL?

Hope the question is not too generic. Couldn't find anything on the site or in SQL documentation:
While coding, i tested this, and to my surprise it worked:
SELECT * FROM cal_entry WHERE cal_entry.parent_id <> 'null'
It actually shows the rows without the ones with NULL values (these are real NULL values in database, not strings with 'null' inside).
According to the docs, I should have used NOT NULL, of course. By the way, it doesn't work with = 'null', like it is correctly stated in the docs.
Can someone explain that?
You are selecting all rows where <> 'null' is true.
Comparing(equals or not-equals) to null is null, so if a row where cal_entry.parent_id is null, your condition will be false/null.
So your query gets all rows that are not null, nor contain the string 'null'.
(Note, you could just as well have written <>'something_else')
Assuming parent_id in an int column the query will return all non-null, non-zero rows:
SELECT *
FROM (
SELECT NULL AS parent_id UNION ALL
SELECT 0 UNION ALL
SELECT 1 UNION ALL
SELECT 2
) AS cal_entry
WHERE cal_entry.parent_id <> 'null'
-- returns 1 and 2 but not 0!
When comparing a number to string MySQL will convert the string to number. Some examples:
'null' becomes 0
'asdf' becomes 0
'1asdf' becomes 123
'1' becomes 1
Your query will behave like:
WHERE cal_entry.parent_id <> 0
this operator give you result of not equal to. ex. $var != null.
we write in mysql as <>. this is kind of validation that the value shoud never be equal to null.
When working with null following two statements should always be taken note of -
An expression can be null, but it can never be equal to null.
Two nulls are never equal to each other.
So, in your query wherever there is a comparison null<>null it returns true by second statement.
Also, always account the possibility that some rows might contain null -
Select * from cal_entry where cal_entry.parent_id!=10
This query would leave out the rows with null entries. Instead use -
Select * from cal_entry where cal_entry.parent_id!=10 or cal_entry.parent_id is null

How do I find the newest matching record in MySQL when all of my search criteria can be null?

I have a table with the nullable fields that I use as search criteria, and a creation_date for sorting by.
I'm trying to write a query to find the newest record based on those search criteria, but if one of them is missing I'd like to still match it.
So I tried doing fieldname = ? or null for each field and used order by creation_date limit 1, but if I do this then if there were a row where all of the fields were null, it would return that one instead of an older record where they are not all null.
How can I make it prefer results with more matching fields, but not require that every field exists?
Please try something like the following:
SELECT
{field list}
FROM
{your table}
WHERE
IFNULL(field1, #par1) = #par1
AND IFNULL(field2, #par2) = #par2
.....
ORDER BY
creation_date DESC
limit 1
In the above case if i.e. field1 had a NULL value for a particular record, IFNULL function would return the second parameter given, so the real comparison in that case would be #par1 = #par1 which is always-true. Thus in case of NULL-valued field, the part doesn't affect the search results.
I hope it helps some way.

Why does MySQL ignore null values when looking for not equal?

I am noticing something weird in MySQL and I would like to see why it behaves this way and of there a way to change it?
Scenario
I have accounts InnoDB table with the following columns id, name, type where the type is a null-able.
Now, lets say I have 10 records where the type = "TEST" and 100 records with they type IS NULL and 20 records where the type = "STANDARD"
If I run this query
SELECT * FROM accounts WHERE type <> "TEST"
This query will only show me the the 20 records that have type = "STANDARD" only and it ignores the 100 records that have a null value.
To work around this I would have to do something like this
SELECT * FROM accounts WHERE IFNULL(type,"") <> "TEST"
OR
SELECT * FROM accounts WHERE (type <> "TEST" OR type IS NULL)
NULL value means blank "aka no value" and no value means <> "TEST"
It is probably an expected behavior but I am not sure why is it designed like so
SELECT * FROM accounts WHERE type <> "TEST"
The meaning of this statement would be,
"Select the rows from accounts where the value of column type is not equal to 'TEST' ".
Which means mysql returns the records having a value in type which is not equal to "TEST".
Now here, since NULL means there is no value, it does not return those records which does not have any value for type column.
NULL means "the value cannot be known/is not known": which is different to "no value" - a comparison cannot be carried out at all against an unavailable value.

SSRS Multi Select Parameter, both Null and not null

I'm new to SSRS, and have been tasked with a report that allows users to select NULL, non NULL or both.
For example
Name | ID
BOB 1
ALICE 2
DAVE NULL
ROGER NULL
Users need to be able to select Records with an ID value and/or records with no ID value.
So it would be a multi select parameter (ID, NO ID , ALL).
I can make this work with a sql query, but am not sure how to make it work with user parameters that can be either, or or both.
The id field is a decimal.
Any advice for how to do this?
Thanks.
One way would be to include conditions like the following in the WHERE clause:
and (ID is null and 'NO ID' in #Parameter or
ID is not null and 'ID' in #Parameter)
Alternatively, if the non-null ID values are always positive, you could use "NO ID" and "ID" as the parameter value labels, with the actual available values as 0 and 1 respectively, and use a condition like:
and sign(coalesce(ID, 0)) in #Parameter
(These conditions won't work in conventional SQL - where #Parameter will always be single-valued - but should work in SSRS.)
In the general tab of your Report parameter. There is an option for Allow Null Values. Check that box.
Also update your query to
From UserTable
Where (ID IS NULL OR ID IN (#IDparam))
Here are links if you need to know more abut Multi Select parameters
http://jsimonbi.wordpress.com/2011/01/15/using-multi-select-with-stored-procedures/
http://technet.microsoft.com/en-us/library/aa337396(v=sql.105).aspx

UNION of empty set with AVG function and any non-empty set

this is my first question ever, so please be patient.. :)
We are two developers and both have the same MySql DB with same tables and values.
One is MySql version 5.5 and works ok (apparently) as I am told by the other developer.
On my machine with MySql 5.1.44 (a basic MAMP install) I have the following weird problem.
A very huge query (not mine) fails with error "Column 'xd' cannot be null".
Removing pieces I slimmedi it down to this:
select xd, avg(media) from questionario_punteggi where somefield = 1 union select 1,2
Note, there is no record with somefield = 1 so the first select returns an empty set
We have a SELECT with AVG() function that returns an empty set UNION another SELECT that returns something (1,2 are just random values I put now as an example)
If I remove the AVG() the query works.
If I remove xd (and the 2 of 1,2 to the right) the query works.
If I remove the UNION the query works.
If I set some record with somefield = 1 the query works.
On the other machine 5.5 the query works.
Otherwise the error is:
1048 - Column 'xd' cannot be null
Fields are:
`xd` char(3) NOT NULL DEFAULT '001',
`media` decimal(7,4) NOT NULL DEFAULT '0.0000',
`somefield` tinyint(4) NOT NULL DEFAULT '0',
Gosh. Any help? Thanks.
UPDATE
It has been reported to me as a BUG in MySql <= 5.1 that was fixed before MySql 5.5. I don't have the details but I trust the source
I suggest reversing the order of the queries in the UNION.
This is because the first SELECT in a UNION determines the data type of the columns in the resultset; in your case, the first column of the UNION took the type of the questionario_punteggi.xd column: that is, CHAR(3) NOT NULL.
Since you are applying an aggregate function over the first part of the UNION, it results in a single row even though no records are matched by the filter criterion. As documented under GROUP BY (Aggregate) Functions:
AVG() returns NULL if there were no matching rows.
The value taken for the hidden xd column would normally be an indeterminately chosen record from those that match the filter (which is why you probably don't want to do that anyway); however, since in this case no records match, the server instead returns NULL (which obviously cannot go into a column with the NOT NULL attribute).
By reversing the order of the UNION, the column will not have the NOT NULL attribute. You may need to alias your columns appropriately:
SELECT 1 AS xd, 2 AS avg_media
UNION
SELECT xd, AVG(media) FROM questionario_punteggi WHERE somefield = 1
Using this to explain each of your observations in turn:
If I remove the AVG() the query works.
Since aggregation is no longer performed, the first SELECT in the UNION yields an empty recordset and therefore no NULL record in the first column.
If I remove xd (and the 2 of 1,2 to the right) the query works.
Since the hidden column is no longer selected, MySQL no longer returns NULL in its place.
If I remove the UNION the query works.
This is the bug that was likely fixed between your version of MySQL and your colleague's: the NOT NULL attribute shouldn't really apply to the UNION result.
If I set some record with somefield = 1 the query works.
The value selected for the hidden column is an indeterminate (but non-NULL value, due to the column's attributes) from the matching records.
On the other machine 5.5 the query works.
This bug (I'm still searching for it) must have been fixed between your respective versions of MySQL.
try using the SELECT IFNULL();
Select IFNULL(xd,0), avg(media) f
rom questionario_punteggi
where somefield = 1
union
select 1,2
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_ifnull