Zend: Problems defining select option with NULL value for submission to INT MySQL field - zend-framework

I have a database field defined as INT with NULL Allowed. The data submitted to this field is produced by a select form element whos values are defined using addMultiOption and addMultiOptions in combination e.g.
$selectData = $dataModel->getPairs();
$updateForm->getElement('type_id')->addMultiOption(NULL, '- Not Selected -');
$updateForm->getElement('type_id')->addMultiOptions($selectData);
The problem is that the NULL I'm trying to assign to the "- Not Selected -" option is being converted to an empty string at some point and MySQL is throwing an error because an empty string is an incorrect integer value.
How can I modify this such that the NULL arrives at the MySQL insert as a NULL?
Regards,
Nick
EDIT: See Daniel Gadawski's answer below. Below is the final result.
$selectData = $dataModel->getPairs();
$updateForm->getElement('type_id')->addFilter(new Zend_Filter_Null());
$updateForm->getElement('type_id')->addMultiOption('', '- Not Selected -');
$updateForm->getElement('type_id')->addMultiOptions($selectData);
This allows the value NULL to be written to a MySQL Integer field with NULL Allowed.

Add Zend_Filter_Null to this field:
$updateForm->getElement('type_id')->addFilter(new Zend_Filter_Null());
Then just retrieve data from this form using $updateForm->getValues() and the empty strings will be replaced by null values.

Related

How to use replace query in yii2 updateAll()?

I am using Postgresql. My Yii2 code for the update is
ModelName::updateAll(['my_column' => "REPLACE(my_column1,'removed_','')"]);
Actual query is
update my_table set my_column = REPLACE(my_column1,'removed_','');
When I run my yii2 code it shows the error
SQLSTATE[22001]: String data, right truncated: 7 ERROR: value too long for type character varying(50)
The SQL being executed was: UPDATE "my_table" SET "my_column1"='REPLACE(my_column1,''removed_'','''')'
If you use ['column' => 'value'] syntax for attributes the framework expects that values of array are simple values and treats them accordingly. That's why your expression gets converted to string value instead of using as expression.
If you want to avoid that you need to wrap your values in yii\db\Expression like this:
ModelName::updateAll([
'my_column' => new \yii\db\Expression("REPLACE(my_column1,'removed_','')")
]);

Updating JSONB object using another table

I am trying to update a JSONB field in one table with data from another table. For example,
update ms
set data = data || '{"COMMERCIAL": 3.4, "PCT" : medi_percent}'
from mix
where mix.id = mss.data_id
and data_id = 6000
and set_id = 20
This is giving me the following error -
Invalid input syntax for type json
DETAIL: Token "medi_percent" is invalid.
When I change medi_percent to a number, I don't get this error.
{"COMMERCIAL": 3.4, "PCT" : medi_percent} is not a valid JSON text. Notice there is no string interpolation happening here. You might be looking for
json_build_object('COMMERCIAL', 3.4, 'PCT', medi_percent)
instead where medi_percent is now an expression (that will presumably refer to your mix column).

How to set a JSONB value to null

I'm trying to update a bunch of jsonb values to null. Here's an example of what i'm trying to do and i'm getting the error below.
How can I set first-name to null in this case?
UPDATE users
SET
fields = fields || '{"first-name": NULL}'
WHERE user_id = 1;
ERROR: invalid input syntax for type json
LINE 3: fields = fields || '{"first-name": NULL}'
^
DETAIL: Token "NULL" is invalid.
CONTEXT: JSON data, line 1: {"first-name": NULL...
Use jsonb_set:
UPDATE users
SET
fields = jsonb_set(fields, '{first-name}', 'null')
WHERE user_id = 1;
If you want the null value inside the JSONB, then it must be a JSON null value, not an SQL NULL value. JSON null must be spelled in lower case.

Insert null value to date field in access using vba

Am trying to insert date from a table to another table using vba in access, But one of the date values from the table is blank. So while inserting the date to new table it shows Invalid use of null exception Run-time error 94.
If writing DAO code to clear a Date field, I found that I had to use "Empty". Null and "" won't work. So for field dtmDelivery (type Date), I had to use the following. strDelivery is just a string with the date in it.
Set rst = dbs.OpenRecordset("tblSomething", dbOpenDynaset)
If (strDelivery = "") Then
rst!dtmDelivery = Empty
Else
rst!dtmDelivery = strDelivery
End If
rst.Update
you can use the NZ() function to define a value that should be used instead of NULL. Internally a date is a float value where the number represents the days between 01.01.1900 and the date that should be stored. The part behind the decimal point represents hours/minutes (.25 for example is 6 AM). So you can use the NZ() funtion to replace NULL bei 0 (what would be interpreted as 01.01.1900).
Another solution would be to configure the target table in a way that it allows NULL values. You can do this easily in the design view of the table.
I think i figure it out , declare the variable to string. Then check whether value from the table is empty, then assign null to it.Other wise assign the value from the table.
for example
Dim newdate As String
Check whether the fetched values is null or not
If IsNull(rst![value]) Then
newdate = "null"
Else
newdate = rst![value]
End If
I encountered this problem and solved it this way.
SQL = "update mytable set mydatefield = '" & txtdate & "'"
On the txtdate on your form, put a date format to ensure date is either a real date or just blank.

How to filter rows with null values in any of its columns in SSRS

I want to filter out the output without rows containing null values or blank columns. I am using SQL Server 2012 there is no option named 'Blank' as in SS2005 where I can filter the rows. I also tried following expression but it gives me error or not showing correct output
=IsNothing(Fields!ABC.Value)!= True
=Fields!ABC.Value = ''
Please suggest the solution.
Pull up the tablix or group properties
Switch to "Filters"
Add a new filter
Set the expression to:
=IsNothing(Fields!YourFieldHere.Value)
Set the type to "Boolean" (see screenshot below) otherwise you'll get a "cannot compare data of types boolean and string" error.
Set the value to false
This works for filtering both rows and groups.
We should use the isNothing method in the Expression, change the Text to Boolean
and then Value will be "True"
for example:
Expression
=IsNothing(Fields!TestA.Value)<>True
(Expression type should be Boolean)
Operator
=
Value
=True
Edit the SQL query, so that it will not return NULL values in the column to group on, but let it return a dummy value; for example: ISNULL(columnA, 'dummy')
In the column group definition add a filter: ColumnA <> 'dummy'.