I have two fields that I'm comparing with MySQL's function COALESCE(). For example, COALESCE(Field1, Field2). The problem is, Field1 is sometimes blank but not null; since it's not null COALESCE() selects Field1, even though its blank. In that case, I need it to select Field2.
I know I can write a if-then-else (CASE) statement in the query to check for this, but is there a nice simple function like COALESCE() for blank-but-not-null fields?
SELECT IFNULL(NULLIF(Field1,''),Field2)
NULLIF returns a NULL if Field1 is blank, while IFNULL returns Field1 if it's not blank or NULL and Field2 otherwise.
I know I'm late to the party here, but there is a way to do this while still using COALESCE(). This would then work if your value was NULL or ''.
Select COALESCE(NULLIF(Field1,''), Field2)
You can use a CASE expression:
CASE WHEN Field1 <> '' THEN Field1 ELSE Field2 END
Use CASE, for comparing both NULL and Blank.
SELECT CASE
WHEN Field1 IS NULL
OR LEN(LTRIM(RTRIM(Field1))) < 1
THEN Field2
ELSE Field1
END;
For comparing blank, do not use '', use LEN, LTRIM, RTRIM. Sometimes, blank may be with more than one spaces.
Related
I am trying to pull all rows where two fields don't match. I understand that nulls are essentially "unknowns" and so have taken steps to define nulls as 0's. As below.
Select ifnull(field1, 0), ifnull(field2, 0)
from table
where field1 != field2
Can anyone tell me why this doesn't work? I still get no results even though i feel i am defining nulls as "0" with ifnull(field1, 0) correctly.
It doesn't work because that is how NULL values work in relational databases. Almost all comparisons return NULL, which is treated as false.
Also, what you do in the SELECT has no impact on the WHERE. The fields are still coming from the original table.
More importantly is what you can do. The simplest method is to use a NULL-safe comparison:
where not field1 <=> field2
Because 0 is inserted after and not before you select
Should be instead:
Select * from
((Select *,ifnull(field1, 0) new_field1, ifnull(field2, 0) new_field2
from table) As query)
WHERE new_field1 != new_field2;
You should move the IFNULL() down to the WHERE clause like this:
Select ifnull(field1,0) as 'Field1', ifnull(field2, 0) as 'Field2'
from table
where ifnull(field1,0) != ifnull(field2,0)
Is it possible to use Select with Case to select from one of several fields depending on which of them is null or not?
I basically want to return a value for all records with logic that says
Return $Value from FIeldA if not null
else from FieldB if not NULL
else from FieldC if not Null
else '0'
I've used Case/When/Then to compare values from a specific field but not as a way of comparing values between fields and not sure if this is possible.
Short answer, use COALESCE:
SELECT COALESCE(FieldA, FieldB, FieldC, 0) AS FieldName
FROM tableName;
It will give you the first non nullable value from the three fields FieldA, FieldB, FieldC. If all are null, then it will return 0. Which what you are trying to do.
Long answer, use CASE expression.
if you want to check multiple filed and if null then return same result 0 then use COALESCE function . this is simple code
SELECT COALESCE(filed1, filed2, filed3, 0) as output from table;
for more information
https://www.w3schools.com/sql/func_sqlserver_coalesce.asp
extra option if you want to select filed using condition then use case. this is demo code
SELECT CASE 1 WHEN 1 THEN 'this is case one'
WHEN 2 THEN 'this is case two'
ELSE 'this is not in the case'
END as 'how to execute case statement';
for more information
https://dev.mysql.com/doc/refman/5.7/en/case.html
http://www.mysqltutorial.org/mysql-case-function/
I have a single column in a table which looks like this:
(field3 is NULL. I use QTODBC7.0 to perform SQL queries, and this application makes NULL fields appear blank in the query results, rather than displaying the text 'NULL')
I have a simple SQL query as follows:
select * from table where field3 <> 'randomstring'
I expect the query to return ALL fields where field3 does not equal the value of 'randomstring'.
The column, 'field3' does not equal the value of 'randomstring' therefore, it should be returned in the query.
A strange thing happens:
The above query returns nothing!
field3 equals NULL therefore, it does not equal 'randomstring' therefore, the row should be returned in my query, but it is not.
I did some testing, and discovered, this is caused when field3 is NULL but not If field3 equals a blank string (not NULL).
How can I make the select statement work when there could be NULL values in field3?
This behavior is caused by a three-value logic that is part of SQL standard. It means that every boolean condition can be evaluated to true, false or unknown and the row is returned only when the condition is true. It is defined that if there is a NULL value on one side of the equation (or both) then the result is unknown. In your case, the result of the condition is unknown when field3 is NULL. Therefore you need to use a solution proposed by jarlh:
SELECT * FROM table WHERE field3 <> 'randomstring' or field3 IS NULL
In such query the condition is evaluated to true if the value of field3 is NULL since unknown or true = true
I want to use different conditions depending on a certain field value.
If for example the value of field1='abc' then field2 should be '123' else field3 should be '456'. My query is much more complex, but the question is whether I could use different conditinons depending on a field value.
Something like that:
SELECT * FROM table
WHERE
CASE table.field1='abc'
THEN table.field2='123'
ELSE table.field3='456'
I have a solution for my problem, but it goes with 3 subqueries which takes a lot of time to respond.
If for example the value of field1='abc' then field2 should be '123'
else field3 should be '456'
You don't really need a CASE statement here. Rather use different combination of condition like.
WHERE (field1 = 'abc' and field2 = '123')
OR (field1 != 'abc' and field3 = '456')
money
20.00
17.87
5.00
NULL
3.00
I want the null entries turned into zeroes. The money column is for money spent. Ive tried the following and it didnt like changing NUll to a dollar value.
select case money when 'NULL' then 0 end
from mytable
select coalesce(money, 0) from mytable
Coalesce will take the first non-null value in the list.
Try:
SELECT IsNull(money, 0)
FROM mytable
EDIT
If you want to replace the values in the table:
UPDATE mytable
SET money = 0
WHERE money Is Null
While I agree with the other two answers, I wasnted to explain why what you did was not right.
select case money when 'NULL' then 0 end
from mytable
Here you are treating NULL as a string value. But NULL is a condition not a value. It means there is no known value. If you inserted the actual word 'null' into the table in that field, then your code would have worked.
When you want to test for a NULL condition you use:
WHERE field1 IS NULL
or you change the value using coalesce or ISNULL. If you need to test for NULL as part of a case statement, then you would do it like this:
CASE WHEN field1 IS NULL THEN 0 ELSE T1 END
For simple cases like yours, COALESCE or ISNULL is best. But at times you may need to do a very complicated CASE and then it is handy to know how to do it.