I have a table called User_info. In which some columns contains values and some columns contains null values. How can I select value fields alone?
From your comment:
In my case I want to select all the values from the table and need to
bind the values to the corresponding fields again in the form. while
binding, if a column contain null value then it raising an error. The
error is
Conversion from type 'DBNull' to type 'String' is not valid
Then you either have to show the code that causes the invalid cast or replace nulls with an empty string:
SELECT Col1 = COALESCE(Col1,'')
FROM User_info u
Instead of COALESCE you could just as well use ISNULL.
Related
UPDATE kpi.data
SET MetricValue = ''
WHERE (MetricValue IS NULL )
and PeriodDate = '2020-01-02'
and ReportID = 4
I got this error
Msg 8114, Level 16, State 5, Line 4 Error converting data type varchar
to numeric.
What you are trying to do does not make any sense, so it cannot work, and it will not work.
If the data type of a column is numeric then the column can only contain numbers.
If the data type of a column is numeric and nullable the column can contain either a number or null.
There is no other value that a numeric field can receive, either in MySQL or in any other relational database that I have ever heard of.
Perhaps what you want to do is to convert null to blank when selecting, (not when inserting/updating,) in which case you should look at some other Q&A like this one: MySql Query Replace NULL with Empty String in Select
I have a table which primary key is numeric and auto-incremented.
When I run a query such as:
SELECT * FROM my_table where id = '1a';
The query returns the row with the primary key set to "1".
I was not aware of this behavior, is it possible to prevent it?
I was expecting this WHERE clause to retrieve nothing since the id is "1" and not "1a". It is behaving like it was a LIKE clause.
MySQL implicitly converts a String literal to int while comparing with an int column.
You should really fix your application code (eg: PHP), and properly typecast to (int) before using them in a query. Ideally, your application should not have been inputting string values to compare against an integer field.
Now still, if you don't have control over input value, an approach can be to check if the value is numeric or not, and use it accordingly for comparison. Adapting a sargable approach from https://dba.stackexchange.com/q/89760/160363
SELECT * FROM my_table
WHERE id = CASE WHEN CONCAT('','1a'*1) = '1a' THEN '1a' ELSE NULL END;
mysql automatically converts strings to numbers, and just takes the leading characters that are digits. You could instead explicitly cast the ID to a string:
SELECT * FROM my_table where CAST(id AS CHAR) = '1a';
If I use the MIN function on a text field, I get an unexpected result.
I have a table with one text field with following values 'a', '', 'c'.
My expectation is that the empty value is returned (when you would sort the table the empty string comes before the 'a'), but instead I get the 'c' as a result !!!
One should easily be able to see this problem by performing the following queries in an access database:
create table testbug (Field1 varchar (255) NULL)
insert into testbug (Field1) values ('a')
insert into testbug (Field1) values ('')
insert into testbug (Field1) values ('c')
insert into testbug (Field1) values ('d')
select min(field1) from testbug
If the blank value contains something else (even a space for example) then it works correct!
Can anyone reproduce this and/or explain and/or indicate whether this is a known bug (I could not find anything about this).
I work with the following version:
Microsoft Office Professional 2010
Microsoft Access Version: 14.0.7166.5000 (32-bit)
MIN simply ignores NULL values.
If you still want to use MIN with NULL values you can try :
SELECT MIN(Nz(Field1, " ")) FROM testbug
the MIN function indeed seems to handle empty values (zero length string values) as NULL.
I added a Field2 to your testbug table and added two rows manually:
no value for Field1, value 'k' for Field2
value for Field1, value 'l' for Field2, remove value for Field1
I also entered a value 'm' in Field2 for the second row you inserted in your table.
Next, I ran this query:
SELECT Field1, Field2,
IIf(min(Field1) Is Null,'null','not null') AS result, Len(Field1)
FROM testbug
GROUP BY Field1, Field2
The IIF function returns null for all values being null or empty.
For the second row you inserted, the function Len returns 0, meaning this is a zero length value. However, the IIF function returns null.
Meaning, MIN seems to handle empty values as NULL values.
Apparently, this is standard behavior within MS Access.
Hope this helps.
Also hope help here was better than the help you got on Helpmij ;)
I'm creating a calculated field (Field3) in a query in MS Access. In this example, Field2 contains both numeric and character values. I want Field3 to contain only numeric values from Field2 and to convert all character values to Null values so that I can later perform calculations on Field3 as a numeric field. This is in an IIf function because I want Field3 to contain only values from Field2 if Field1 = "AA". This is what I tried typing in the Field row in the Query Design View:
Field3: IIf([Field1]="AA",[Field2]*1,NULL)
This works except where Field2 is a character value then Field3 reads "#Error" instead of being blank.
What is the proper syntax for assigning NULL values if the IIf condition is not met?
If you want the expression to return [Field2]*1 only when [Field1]="AA" and [Field2] contains a number, but otherwise return Null, use IsNumeric() in the condition ...
IIf([Field1]='AA' And IsNumeric([Field2]), [Field2]*1, Null)
If the purpose of [Field2]*1 is to cast the [Field2] text value as a number, consider the Val() function instead ...
IIf([Field1]='AA' And IsNumeric([Field2]), Val([Field2]), Null)
Null is the correct syntax.
But at least in the old German Access version which I have on this machine, I need to use semicolons instead of commas in the Query Design View.
So can you try this?
Field3: IIf([Field1]="AA";[Field2]*1;NULL)
In SQL View, the syntax is exactly like in your question:
SELECT IIf([Field1]="aa",[Field2]*1,Null) AS Field3
FROM TestTable;
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.