In sql server, I see a strange behaviour:
When I do
select col1+ ' ' + cast(col2 as varchar(10)) as concat_col
it returns me a column with a value = NULL (not a blank column).
My suspicion is that it is because col2 has a value = NULL (not blank column).
So what is the reason for this behavior? But more importantly, what is the meaning of a column with value = NULL as opposed to a blank column? I do not imagine somebody went in the table and updated all the columns with value = NULL.
NULL means that the field does not have any value. You can use ISNULL function to convert a null value into something that you want.The following will hopefully give you the correct result (will not give null)
select ISNULL(col1,'')+ ' ' + cast(ISNULL(col2,'') as varchar(10)) as concat_col
Blank is a value while null is completely nothing.
Think of zero is a number while null is nothing.
Related
I have a sample table with one of the column called Expression with the TEXT data type. This is the data I am going to insert and the number of characters in that string is 504 characters.
Expression="CASE WHEN (txt_Second_23.TYPE_OF_AUTH='OP' or txt_Second_23.TYPE_OF_AUTH='OPS') then '1' else (CASE WHEN ((txt_Second_23.TYPE_OF_AUTH='IP' or txt_Second_23.TYPE_OF_AUTH='IPN') and ((txt_Second_23.Requested_Provider_NPI is not null or txt_Second_23.FACILITY_NPI is not null) OR (txt_Second_23.Requested_Provider_Last_Name is not null or txt_Second_23.Facility_Name is not null) OR (txt_Second_23.REQUESTED_PROVIDER_CITY is not null or txt_Second_23.FACILITY_CITY is not null) OR (txt_Second_23.abcdef) END"
Whenever I tried to add one more character to the above string, it's not allowing to insert. Can anyone please let me what is causing the issue or am I missing something here.
Note: I have changed the data type from TEXT to VARCHAR(1000), still no luck.
Update: This issue is happening while inserting it through programatically with prepared statement. I am able to insert when I do it manually by creating sample table with expression column.
This is the actual string I am trying to insert.
CASE WHEN (txt_Second_23.TYPE_OF_AUTH='OP' or txt_Second_23.TYPE_OF_AUTH='OPS') then '1' else (case when ((txt_Second_23.TYPE_OF_AUTH='IP' or txt_Second_23.TYPE_OF_AUTH='IPN') and ((txt_Second_23.Requested_Provider_NPI is not null or txt_Second_23.FACILITY_NPI is not null) OR (txt_Second_23.Requested_Provider_Last_Name is not null or txt_Second_23.Facility_Name is not null) OR (txt_Second_23.REQUESTED_PROVIDER_CITY is not null or txt_Second_23.FACILITY_CITY is not null) OR (txt_Second_23.REQUESTED_PROVIDER_STATE is not null or txt_Second_23.FACILITY_STATE is not null) OR (txt_Second_23.requested_provider_zip is not null or txt_Second_23.facility_zip is not null))) then '1' else null end) END
Thanks In Advance.
In my code some where expression column was appending to one more column called description and it's length is only 512 characters. That's causing the issue.
Taken care it by changing the data type from varchar(512) to text.
I have installed recently MySQL 5.7 . Something weird is happening with a date column.
If I execute a SELECT query using that field in a WHERE section, I have a resultset, but when I use the same WHERE condition to UPDATE a row I get an Invalid date format error message.
Here is the SELECT sentence:
SELECT *
FROM
FDpoCargTran
WHERE FDpoCargTran.Banco = '001'
AND (FDpoCargTran.Conciliacion = '' OR FDpoCargTran.Conciliacion IS NULL)
AND FDpoCargTran.Fecha = '2016-09-27'
This sentence returns 2 rows resultset, that's ok.
Now, Here's the UPDATE sentence:
UPDATE
FDpoCargTran
SET
Edo = 'C'
WHERE FDpoCargTran.Banco = '001'
AND (FDpoCargTran.Conciliacion = '' OR FDpoCargTran.Conciliacion IS NULL)
AND FDpoCargTran.Fecha = '2016-09-27'
AND Deposito = 1041
And I get this error message:
Data truncation: Incorrect date value: '' for column 'Conciliacion'
The Conciliacion columns is defined as:
`Conciliacion` date DEFAULT NULL,
If I remove the Conciliacion = '' condition, everything works fine.
Why empty string is not valid to evaluate the column in an UPDATE sentence and not in a SELECT?
Please, an idea!!!
Basically, for date datatype, you cannot store something like White
Spaces or ' ' strings. You need to make the column to accept NULL
values and insert an actual NULL into it. This is not a problem in MySQL 5.7, its how date is set in databases.
Update is a DML statement, when you actually want to write something into table, or check for a condition, MySQL is unable to understand what ' ' is for the date column type.
So, you cannot have ' ', instead you can have NULL set. Thats how MySQL can check the condition and make appropriate changes!!
I would suggest, change it to NULL.
Alter your datetime column to varchar.
Import whatever table/database.
Update FDpoCargTran set Conciliacion=NULL where Conciliacion='';
Alter column back to datetime.
I am getting following error when i execute following command. Can someone please help!
Cannot insert the value NULL into column '', table column does not allow nulls. INSERT fails.Failed to execute following SQL block
BEGIN
Select #v_setting_val=setting_val from EGPL_PROGRAM where setting_id=#v_setting_id
and group_id =
(select group_id
from egpl_pref_group
where group_type = 'departmental'
and department_id=#v_department_id)
INSERT INTO EGPL_PROGRAM
(GROUP_ID, SETTING_ID, SETTING_VAL, IS_PREFERENCE, MODIFIER_ID, MODIFIED_DATE)
VALUES
(#v_group_id, #v_setting_id, #v_setting_val,'n',1,getdate());
PRINT('Inserted the following value for group ' + convert(nvarchar, #v_group_id ))
PRINT('Setting_id : ' + convert(nvarchar, #v_setting_id) + ' setting_val : ' + #v_setting_val)
END
Check the table for columns that do not allow nulls
Either the value of one of those columns is null or you are not passing that column at all.
If you have a column that is not null in not in the list of columns then the insert will fail in this manner.
Check the table for columns that do not allow nulls AND also be sure to check your table for triggers if you have modified it. This issue vexed me for most of an afternoon before I realized it was the trigger reporting the error.
I am using wamp in win 7.
In my database, I have one table be_users, two fields: username and email, both of them are set NOT NULL.
But why I still can insert empty value and null value into field: email, see below image:
Actually, you are inserting text and they are not NULL. The text NULL is very different from NULL. You can never violate the rule if the field is set to NOT NULL.
Try executing this statement,
INSERT INTO tableName VALUES (null, null) -- will fail
it will surely fail because those are NULL. But this statement below will surely work because you are inserting string.
INSERT INTO tableName VALUES ('', 'null') -- will work
An empty value does not equal NULL, and it looks like you are inserting 'null' not NULL into the database, so you are ending up with a string 'null' not a NULL value.
Dont send NULL enclosed with quotes 'NULL' or "NULL" its getting treated as text value.
the issue is due to the data type used for thease two field. "text" you can change this to varchar(255) to use NULL. There are some issue with NULL and data type text.
Null is a keyword. If you will enter null directly then it will show you error.
Example-
insert into tablename(fieldname) values(null);
This above line will generate error(if you have mentioned not null).
I think you have enter something else. Please check again the table structure and enter new data.
I have a very large table with two INT columns that are null on Default. This is a problem because since they are INT fields, it would help in many cases if they were originally set to 0.
So my questions are, is there a way I can UPDATE and INCREMENT(+1) these fields while they are like this (null on Default)? BTW.. I didn't have luck so far, it seems increment only works when the default=0
..or is my only option to Change the Default to none from null
UPDATE TableName SET column = IFNULL(column, 0) + 1 WHERE ...
More info on IFNULL. It returns the first argument if it is not NULL, the second otherwise.
Try setting the field as NOT NULL to get away with the problem so that default value of 0 is used instead of null. The other option is to set column as zero whenever it is null.
UPDATE TableName SET FieldName = '0' WHERE FieldName IS NULL
Other alternative would be to issue IFNULL to return 0 in case the column is null and then incrementing the column.
UPDATE TableName SET FieldName = IFNULL(FieldName,0)
The SQL standard would be to use COALESCE(); this has been available in MySQL since version 3.23 (which was released into production in 2001):
UPDATE mytable
SET mycolumn = COALESCE(mycolumn, 0) + 1
WHERE my_other_columns = ...
I can't see any reason to choose IFNULL() over COALESCE() here.
Hope this helps.