CASE WHEN NULL makes wrong result in SQLite? - sql

I have a table with a column of image type, the table has some rows but all the rows haven't had any image yet, they are all null. To test the CASE WHEN NULL, I've tried this and it gave a strange result:
SELECT CASE myImageColumn WHEN NULL THEN 0 ELSE 1 END FROM myTable
All the returned rows were in a column of 1's (I thought 0's). What is wrong here?
Your help would be highly appreciated!
Thank you!

You can't compare with NULL like that, you should try:
SELECT CASE WHEN myImageColumn IS NULL THEN 0 ELSE 1 END
FROM myTable

Use a different form of CASE instead:
SELECT CASE WHEN myImageColumn IS NULL THEN 0 ELSE 1 END FROM myTable
Two useful links:
http://www.sqlite.org/nulls.html
http://www.sqlite.org/lang_expr.html

There's a bypass:
CASE ifnull(myValue, 'someUniqueStringOrValue')
WHEN 'someUniqueStringOrValue' THEN 0 -- this means null
WHEN 'someNormalValue' THEN 1
END

Related

Two conditions failure in teradata case - NOT NULL AND NOT 0

Trying to return a column, giving 1, when column1 is NOT NULL and different than 0. So far managed to do this:
MAX(CASE WHEN column1 IS NOT NULL
THEN CASE WHEN column1 <> 0 THEN 1 ELSE 0 END
ELSE 0 END)
Getting this error:
SELECT Failed. 2620: The format or data contains a bad character.
It works quite ok with NOT NULL as a single condition, though.
I'm not sure why you need to nest anything, but it looks like you're missing the non-equality sign.
You might try,
CASE
WHEN column1 IS NOT NULL AND column1 <> 0
THEN 1
ELSE 0
END
Alternatively, this will produce the same result as an OR operator, where, but CASE executes the WHEN clauses in order.
CASE
WHEN column1 IS NOT NULL
THEN 1
WHEN column1 <> 0
THEN 1
ELSE 0
END
But in your description, it sounds like you wanted BOTH conditions to be true, so it doesn't make sense to nest or use multiple WHEN statements, because you can just connect them together with AND

Conditional Where Statement

I am trying to setup a conditional statement that will determine which WHERE clause to use.
I need to check 2 separate conditions, one on each side of the AND and if results are greater than 1 then use a particular statement if not then use nothing
Like this logic but in pdw sql
WHERE
if cte1.counte > 1 then 'cte1.built is not null' else ''
AND
if cte2.countd > 1 then 'cte2.demo is not null' else ''
possible combinations:
WHERE CTE1.BUILD IS NOT NULL
WHERE CTE1.BUILD IS NOT NULL AND CTE2.DEMO IS NOT NULL
WHERE CTE2.DEMO IS NOT NULL
BLANK
Is this possible to do?
Thanks in advance
Something like this:
WHERE (cte1.counte > 1 and cte1.built is not null or cte1.counte <= 1) and
(cte2.countd > 1 and cte2.demo is not null or cte2.countd <= 1)

Separate sql value from one field

I have this:
**value**
S:581930640 | P:581930640
And I would like to get the value as in Oracle:
**valuaA ValueB**
581930640 581930640
select
case
when field like 's:%'
then substr(field,3,13)
else null
end as A,
case
when field like 's:%'
then substr(field,17)
else null
end as B
from table;
both the case condition are same, but the substring is different, this should do what you are trying to do.

Dynamic Label in Select

I was wondering if we can change the label of the select statement like we do for data in sql select using CASE
SELECT CASE column1 = 1 THEN 1 ELSE 0 END AS [Available]
But can we have a dynamic header something like
SELECT column1 AS <-- Available when 1 or Not Available when 0
This can be handled on the front end but its wise if we have it on backend. Any help or useful link is appreciated
You can do it with dynamic sql and if...else instruction but it not make sense for me. In relational database value in the cell tells you if something is available or not. If header tells you the same as cell it's duplicate information. If you want to description of the value you can use case syntax instead of 0/1 value
SELECT CASE when column1 = 1 THEN 'Available'
ELSE 'Not available'
END AS [Available]
Well, that would not make sense, as what would you expect the column name to be if you hade 2 rows, one with 1 (being available) and the other with 0(being Not Available)?
You would have to stick to something like
SELECT
CASE
WHEN column1 = 1
THEN 'Available'
ELSE 'Not available'
END as Availability
FROM YourTable

How can I assign one field's value to another field based on a third field?

I have three fields and if column three equals a certain value, I want to get the value of column 2. Can I do this in a SELECT, and would I use an IF or CASE statement?
A CASE statement would be the easiest way to do this. The following SQL compares two columns. You can alter this to use WHEN Col2 = 'xxxx' THEN xxxx'.
SELECT Col1, Col2, CASE
WHEN Col2 = Col1 THEN 'True'
ELSE 'False'
END AS 'Columns Match'
FROM Table
Hope this leads you in the right direction
Select
Case
when columnthree = 'VALUE'
then column2
else 'do something'
end as [column name]
from
table name
Also you might want to do some conversion if the datatypes for both columns are different
You could use a CASE clause within a SELECT statement - IF won't work this way in SQLServer.
select Alpha, Beta, Gamma,
case
when Alpha > 2 * Beta then Gamma
when Beta = Gamma then Alpha + 3
when Gamma = 2 then 13
else 42
end as 'Omega'
from Alphabet
First match wins.
CASE returns a value, so it may be helpful in various circumstances:
delete ... where case when Id > Limit then 1 else 0 end = 1
update ... set ShoeSize = case when Wide = 1 then ShoeSize + 1 else ShoeSize end, ...