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

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

Related

How do I set an upper bound to an INT value within a select statement in SQL?

I'm looking to make part of my query more performant by cutting down on the number of case statements I use. I have a select statement as below currently:
SELECT
ID,
CASE WHEN sum(Value1) > 0 THEN 1 ELSE 0 END as [Value1],
CASE WHEN sum(Value2) > 0 THEN 1 ELSE 0 END as [Value2],
CASE WHEN sum(Value3) > 0 THEN 1 ELSE 0 END as [Value3],
CASE WHEN sum(Value4) > 0 THEN 1 ELSE 0 END as [Value4],
....
FROM table
Essentially I want the query to simply produce a boolean, either a 0 or a 1, but the case statements have tripled the runtime for my query which is less ideal. Is there a way I can force a boolean output or set a maximum value for my select and trim away the cases?
As John Cappelletti has provided, the expected output of limiting an INT value into a Boolean 0/1 output is achieved via the SIGN function. This reduces the runtime for the query significantly compared to the case statement.

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)

Null validation in Hive , looking for best approach

I have condition in datastage which verifies 30 columns as not null and assigning value as '0' if anyone of the field is null like below,
isnull(columnA) or isNull(coloumnB) or isnull(columnC) or isNull(coloumnD) then 0 else 1.
we have option to use case statement in hive to set values, however we have to give use CASE statement per column like below,
select
case when (columnA is NULL) then 0
case when (columnB is NULL) then 0
case when (columnC is NULL) then 0
.
.
.
else 1
end as iValidColumn
From tablea
What i am looking?
Trying to look for option where we can validate all the 30 columns in condition for null validation.

sql records validation in a table

I like to check the length of a specified fields is x length and available value is a numeric value
i can do this by using two separate case statements like below
(case when len(columnA) = 10 then 0 else 1 end)+
(case when IsNumeric(columnA) = 1 then 0 else 1 end)+
(case when len(columnB) = 8 then 0 else 1 end)+
(case when IsNumeric(columnB) = 1 then 0 else 1 end)
is there any better approach as i need to this for more than 40 columns and their datatype is varchar and each of the column will have specific length.
using some short cut to reduce above two case statements into one line
If you only care that a column is invalid and are less interested in which criteria it failed on then you could just put both criteria into the one case statement as
(case when len(columnA) = 10 and IsNumeric(columnA) = 1 then 0 else 1 end)
In regards to the issue Sean raised, ISNUMERIC only confirms the value can be converted to a numeric datatype so commas and periods are valid too. you could do a check for any single character that isn't in the range of numbers
case when len(columnA) = 10 and ColumnA not like '%[^0-9]%' then 0 else 1 end
It is a little ugly because we have to say is not [not in range], so you might want to change the logic a bit.

CASE WHEN NULL makes wrong result in SQLite?

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