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

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

Related

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

CASE statement to IIF statement

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)

How can I remove the commas from a multi-select parameter in Cognos 10 Report Studio?

I started with a text prompt where a user can enter a string representing days in a single week. For example, M = Monday, and MWF can be typed in to choose Monday, Wednesday, and Friday.
I've just changed the prompt to a multi-select value prompt, and modified my filters to work correctly. Now my prompt looks like this:
When the user selects the items Monday, Wednesday, Friday, the ParamValue list is M,W,F.
How can I display MWF as a data item?
Specifically, I want to combine the new day(s) with a time selected so I can get an output result of MWF 0800 - 1100 but the time values are already taken care of.
Can I somehow index the values inside the multi-select value?
Edit:
I should also mention that variations on
?p_NewDays? || ' ' || ?p_NewStartTime? || '-' || ?p_NewEndTime?
don't seem to work, it will only list the first item in the selection. And TO_CHAR only works for dates.
You can accomplish this with a vendor provided replace() function replacing each comma with an empty string. However, the implementation of such function varies from vendor to vendor. For a vendor-neutral solution you can use this expression to do the same thing:
CASE
WHEN 'M' in ?param? THEN 'M'
ELSE ''
END
||
CASE
WHEN 'T' in ?param? THEN 'T'
ELSE ''
END
||
CASE
WHEN 'W' in ?param? THEN 'W'
ELSE ''
END
||
CASE
WHEN 'Th' in ?param? THEN 'Th'
ELSE ''
END
||
CASE
WHEN 'F' in ?param? THEN 'F'
ELSE ''
END
||
CASE
WHEN 'S' in ?param? THEN 'S'
ELSE ''
END
||
CASE
WHEN 'Su' in ?param? THEN 'Su'
ELSE ''
END
I used the generic ?param? for the parameter name. You would have to change it to match your specific parameter.
You didn't indicate how you differentiate between Tuesday and Thursday as well as Saturday and Sunday. I simply used the first initial for the first day of the week to start with that letter and used a common two-letter abbreviation for the second instance.

Using Case Function to make NULL values appear as blank

I keep getting error that my Case Function is incorrect. My goal is to use the Case Function to make every NULL or '0' values appear as blank. Incompatible data types in case statement is the prompt that I get when running it in report studio. Example of the table would be this. WCPDOD is an Integer, WCPDOD 9int,null)
WCPDOD POLICY CLAIM NUMBER
19741020 2001A 123456N000040
20040101 2003A 456789001531
20021220 00594 123456
20040507 2003A 2222043621
20040517 2003I 90043625
CASE
WHEN [WCPDOD] = '0' THEN ' '
WHEN [WCPDOD] IS NULL THEN ' '
ELSE [WCPDOD]
END
Since WCPDOD is an integer, you shouldn't compare it to string literals such as '0', but to numeric literals, such as 0. More importantly, different branches of your case statement return different types - the first two return strings (the ' ' literal) and the else branch returns an integer. You should cast it to a string:
CASE
WHEN [WCPDOD] = 0 THEN ' '
WHEN [WCPDOD] IS NULL THEN ' '
ELSE CAST ([WCPDOD] AS VARCHAR(40))
END

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)

Resources