Teradata Case Expression and Text Comparison <> Not Working - case

I am trying to run a Case expression that will return a value when a field does not equal a specific text value.
Specifically, I am trying to return 'YES' when the field does not equal 'Yes'. The current value is null. I would expect the result to return 'YES' since the value does not equal 'Yes'. The query is returning 'NO'.
The excerpt of my query is listed below. Any help would be greatly appreciated.
SELECT
CASE
WHEN "Renewal"<>'Yes'
THEN 'YES'
ELSE 'NO'
END
FROM "DATABASE"."TABLE"
WHERE "Asset_Id" = '006817'

Related

MS Access IIF Field Exists

I am creating a report from a query where a field ABC is displayed as CAT if yes and as MOUSE if no. But unfortunately, when there are instances where the table inside the query does not contain field ABC, the report generates a error pop-up. Is there any way to by-pass this and run the report with other fields excluding the missing field?
I heard that IIF Exist function could help, but I am really blank here. I wrote the access query like below:
Iif (fieldExists(iif([ABC]=5, 'CAT', 'MOUSE'),iif([ABC]=5, 'CAT', 'MOUSE'), '')) AS TOMnJERRY
This function is maybe the shortest one to test if a field exists in Access:
Function FieldExists(ByVal Table As String, ByVal Field As String) As Boolean
On Error Resume Next
FieldExists = (DCount(Field, Table) >= 0)
End Function
How it works:
If the field exists, the expression (DCount(Field, Table) >= 0) is obviously always true, because DCount never returns a negative value. If the field doesn't exist, an error will occur and the program will jump to the next line without setting the return variable FieldExist, so this one will keep at default and this is false.
So the solution for your problem should look like this:
Iif (FieldExists('YourTable','ABC'), iif([ABC]=5, 'CAT', 'MOUSE'), '')

SQLite - update command yields null values

I'm having trouble performing some update on a SQLite database. I'm using the SQLite 3 shell for Windows.
I'm running the following command:
update resovled_chrom_counts set genus =
case resolved_name_full
when resolved_name_full is not null and resolved_name_full != ''
then substr(resolved_name_full,0,instr(resolved_name_full,' ')-1)
else
substr(original_name,0,instr(original_name,' ')-1)
end;
It seems to work on most rows, but some simply end up with a null value in their genus field. I tried checking some of them manually, by using the 'id' field of this table. For example, I found out that the row with id='kew-1' is null in it's genus field, and ran the following query:
select substr(resolved_name_full,0,instr(resolved_name_full,' ')-1)
from resovled_chrom_counts
where id='kew-1';
and to my surprise, I got a result (not null)!
Looks like the query works under the 'select' statement, but not under the 'update' statement.
Can anyone give an explanation and/or a solution?
Any help would be appreciated. Thanks!
The problem is not with the substr(resolved_name_full... but with the CASE.
A CASE expression can have two different forms:
CASE x WHEN y THEN ...: This compares the value of x against the value of y.
CASE WHEN a THEN ...: This checks whether the value of a is true or false.
The problem in the UPDATE statement is that there is a value (resolved_name_full) directly after the CASE, so the value of resolved_name_full is compared with the value of the expression resolved_name_full is not null and resolved_name_full != '', and this comparison always fails because resolved_name_full never happens to be 0 or 1.
Just use the second form of the CASE expression:
update resovled_chrom_counts set genus =
case
when resolved_name_full is not null and resolved_name_full != ''
then substr(resolved_name_full,0,instr(resolved_name_full,' ')-1)
else
substr(original_name,0,instr(original_name,' ')-1)
end;
SQLFiddle

pl sql decode function for use to check two values

In the select statement i need to return a value based on the data on other two colomns. For example,
select clarify, clarify_rece_date, clarify_process_date
from test_db;
So the clarify should return yes if both clarify_rece_date and clarify_process_date is not null and if they null clarify should return No.
I could have use decode here if i want to check only onle column as below,
select decode(clarify_rece_date, null,'Yes','No') clarify, clarify_rece_date, clarify_process_date
from test_db;
But how can i check for both columns in this scenario?
You could have a rather complex DECODE. However, it would make far more sense to use a CASE statement
SELECT (CASE WHEN clarify_rece_date IS NOT NULL AND
clarify_process_date IS NOT NULL
THEN 'Yes'
ELSE 'No'
END) clarify,
clarify_rece_date,
clarify_process_date
FROM test_db

SQLite SELECT statement where column equals zero

I'm preety new to SQLite.
I have a preety basic question.. Why can't I select rows where specific column equals zero?
The is_unwanted column is type TINYINT (which I see in SQLite basically means INTEGER)
So, I have only one record in the database (for testing).
When I try
SELECT is_unwanted FROM 'urls'
I get a result of "0" (zero), which is fine because that column contains the actual number 0.
I tried =>
SELECT * FROM 'urls' WHERE is_unwanted = 0
And got NO result, but
SELECT * FROM 'urls' WHERE is_unwanted <> 0
gives me result.
What am I doing wrong??
Try running
select '{' || is_unwanted || '}' from urls
to see if the value in the database is really a string containing spaces.
SQLite is a dynamically typed database; when you specify TINYINT is is a hint (SQLite uses the term "affinity") for the column. You can use
select is_unwanted, typeof(is_unwanted) from urls
to see the values with their types.
You could try:
SELECT * FROM urls WHERE coalesce(is_unwanted,'') = ''

SQL SERVER CASE STATEMENT CLARIFICATION

I have to execute a statement like ( i need and keyword along with when ).
select
'Is Allowed'= case A.Column
when
A.Column='XXXX' and Isnull(A.AnotherColumn,'')<>'' then 'Yes'
else
'No' end from TableName
I am getting syntax error,how to rewrite it without affecting the condition.
Try:
select case when A.Column='XXXX' and Isnull(A.AnotherColumn,'')<>'' then 'Yes'
else 'No' end as 'Is Allowed'
from TableName
SELECT
CASE A.Column
WHEN 'Is Allowed THEN 'First'
WHEN 2 THEN 'Second'
WHEN 3 THEN 'Third'
ELSE 'Other'
END
Is the general way of making a CASE (which is your question). However, your query/logic looks a bit convoluted. More detailed answer / query is possible, but would perhaps use more statements/ nested CASE.
Have a closer look at CASE (Transact-SQL)
SELECT CASE
WHEN some boolean expression
THEN value
ELSE default value
END
or
SELECT CASE value to check
WHEN vlue to check agains
THEN value
ELSE default value
END

Resources