CASE statement to IIF statement - ms-access-2010

I have this CASE statement that I need to convert to IIF in Access:
Case When sfrstcr_pidm is not null Then 'A' WHen sfrstcr_pidm <> '' Then 'A' Else Null End as StudentStatus,
Would this be the correct way to convert to IIF?
IIF ([sfrstcr_pidm] is not null, ‘A’, IIF([sfrstcr_pidm] <> ‘’, ‘A’, Null))

The most obvious way to convert a Case statement to Access SQL is using the Switch function. This takes multiple arguments, and uses if ... then ... elseif ... logic. It doesn't have an else option, but elseif true works around that.
Using this function, your code would become the following:
Switch(sfrstcr_pidm is not null, 'A', sfrstcr_pidm <> '', 'A', True, Null)

Related

Not able to concatenate Values Based on Different columns in case when statement -Snowflake

Hope are you doing well!..I am trying to concatenate values of case when statement based on different columns in snowflake ..Please find the block of code below
select *,
case when checkouttime is null then ',Patient is not checked out' else '' END
+ case when primarypatientinsuranceid is null then ',No insurance information' else '' END
+ case when closedby is null then ',Encounter not signed off' else '' END
+ case when billingtabcheckeddate is null then ',Billing tab is not checked' else ''
+ case when alreadyrouted is null then ',Missing slip already routed' else 'Valid Missing slip'
END as resultant
from final
I am getting the error saying " Unexpected as"
I am trying to build the resultant column output as the following
Patient is not checked out/Billing tab is not checked
Missing slip already routed
Encounter not signed off/No insurance information /Billing tab is not checked
Valid Missing slip
Thanks,
Arun
A cleaner alternative that adds commas as necessary, using array_to_string(array_construct_compact()):
with data as (
select null checkouttime
, 2 primarypatientinsuranceid
, null closedby
, 4 billingtabcheckeddate
, 5 alreadyrouted
)
select array_to_string(array_construct_compact(
iff(checkouttime is null, 'Patient is not checked out', null)
, iff(primarypatientinsuranceid is null, 'No insurance information', null)
, iff(closedby is null, 'Encounter not signed off', null)
, iff(billingtabcheckeddate is null, 'Billing tab is not checked', null)
, iff(alreadyrouted is null, 'Missing slip already routed', 'Valid Missing slip')
), ', ')
as resultant
from data
In Snowflake, you use "||" to concat strings, not "+":
select
case when true then ',Patient is not checked out' else '' END
|| case when false then ',No insurance information' else '' END
|| case when true then ',Encounter not signed off' else '' END
|| case when true then ',Billing tab is not checked' else '' END
|| case when false then ',Missing slip already routed' else 'Valid Missing slip' END
as resultant;
https://docs.snowflake.com/en/sql-reference/functions/concat.html

Sqlite3 Case statement weirdness

I have the following case statement on a TEXT file and I'm trying to get it to say '' instead of none.
CASE CustomField1
When 'None'
then ''
Else CAST(CustomField1 as INT)
End ReceiptLoc
Can anyone see anything wrong with this. If the column is NULL then put nothing in the column or empty string is fine. I've tried replacing 'None' with NULL and NaN. What am I missing?
I'm more of a SQL Server person so This is a little different for me.
You could use searched CASE expression:
CASE WHEN CustomField1 IS NULL THEN ''
WHEN CustomField1 = 'None' THEN ''
Else CAST(CustomField1 AS INT)
End ReceiptLoc
db<>fiddle demo

Checking for null values in a table

I have a table with several columns some of them are:
FILTER_COLUMN VARCHAR2
L_FILTER_VALUEN NUMBER
L_FILTER_VALUEA VARCHAR
L_FILTER_LIST_ID NUMBER
I'm making a procedure to append a string (later to be used on a query)
but there is a catch:
If all of those 4 columns are empty, that's OK. However, if FILTER_COLUMN is not empty, I must guarantee that at least one of the other three is NOT empty.
Since I'm a bit of a nooblet in PL/SQL I've decided to do it "the wrong way":
IF FILTER_COLUMN IS NOT NULL
IF L_FILTER_VALUEN IS NULL
IF L_FILTER_VALUEA IS NULL
IF L_FILTER_LIST_ID IS NULL
RETURN FALSE;
ELSE
SQL_STMT := L_FILTER_LIST_ID
END IF;
ELSE
....
(and so on)
Is there a cleaner way to it and append all the not null columns?
From the top of my head, you have 2 basic options:
Use AND:
IF filter_column IS NOT NULL THEN
IF l_filter_valuen IS NULL AND l_filter_valuea IS NULL AND l_filter_list_id IS NULL THEN
RETURN FALSE;
ELSE
sql_stmt := l_filter_list_id;
END IF;
ELSE
....
(and so on)
Use the COALESCE function which will return the first not null variable. If it returns NULL it means all of the variables are NULL:
IF filter_column IS NOT NULL THEN
IF COALESCE(l_filter_valuen, l_filter_valuea, l_filter_list_id) IS NULL THEN
RETURN FALSE;
ELSE
sql_stmt := l_filter_list_id;
END IF;
ELSE
....
(and so on)

Cast function in Teradata to convert character to number

decode(BITAND(Col,1),1,'D','')|| decode(BITAND(Col,2),2,'d','')|| decode(BITAND(Col,4),4,'B','')|| decode(BITAND(Col,8),8,'b','')||
How i can Cast the above condition in teradata.
Thanks in advance!!
I don't believe there is a 1:1 DECODE() equivalent in Teradata. You will probably need to use a CASE statement instead:
CASE WHEN BITAND(col,1)=1 THEN 'D' END || CASE WHEN BITAND(col,2)=2 THEN 'd' END || CASE WHEN BITAND(Col,4)=4 THEN 'B' END || CASE WHEN BITAND(Col,8)=8 THEN 'b' END

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

Resources