Can anyone let me know how to write a CASE STATEMENT like in SQL but in a Jaspersoft 6 report? More precisely inside the expression editor.
For example:
CASE ProductLine
WHEN 'R' THEN 'Road'
WHEN 'M' THEN 'Mountain'
WHEN 'T' THEN 'Touring'
WHEN 'S' THEN 'Other sale items'
ELSE 'Not for sale'
END
You need to use a ternary operator like below:
$F{variable_1}.equals("R") == true ? 'Road' : "Else not for Sale"
Well you can't use case directly but having if else condition within another if else condition will give the solution.
Here's an example below where the switch-case functionality is achieved through multiple if else conditions
( $F{FIELDV}.equals("ab") ? "aaaa" :
( $F{FIELDV}.equals("bc") ? "bbbb" :
( $F{FIELDV}.equals("de") ? "cccc" : ""
)))
Related
Hello I am working in crystal and am wanting to add a formula field called Group 1, and want to use SQL query like this:
CASE '{?Group 1}'
WHEN 'Location' THEN (CASE WHEN COALESCE(TABLE_NAME_1.COLUMN_NAME_1, TABLE_NAME_1.COLUMN_NAME_2) IS NULL THEN '*Unspecified Location'
WHEN TABLE_NAME_2.COLUMN_NAME_1 IS NULL THEN CONCAT('*Unknown Location', CONCAT(' [',CONCAT(COALESCE(TABLE_NAME_1.COLUMN_NAME_1, TABLE_NAME_1.COLUMN_NAME_2),']')))
WHEN TABLE_NAME_2.COLUMN_NAME_2 IS NULL THEN CONCAT('*Unnamed Location', CONCAT(' [',CONCAT(TABLE_NAME_2.COLUMN_NAME_1,']')))
ELSE TABLE_NAME_2.COLUMN_NAME_2 END)
ELSE NULL END GROUP_1
Not entirely sure how this translates into crystal. Any help would be great.
Thank you
Ok I reworked the formula and came up with:
IF {?Group1} = "Location" THEN
IF ISNULL({TABLE_NAME_1.COLUMN_NAME_1}) THEN "*Unspecified Location" ELSE
IF ISNULL({TABLE_NAME_1.COLUMN_NAME_2}) THEN "*Unspecified Location" ELSE
IF ISNULL({TABLE_NAME_2.COLUMN_NAME_1}) THEN "*Unknown Location" ELSE
IF ISNULL({TABLE_NAME_2.COLUMN_NAME_2}) THEN "*Unnamed Location"
ELSE {TABLE_NAME_2.COLUMN_NAME_2}
I think this might work. Would you agree or is there a better way?
your conversion is almost correct need some changes check below.
IF {?Group1} = "Location" THEN
(
IF (ISNULL({TABLE_NAME_1.COLUMN_NAME_1}) or ISNULL({TABLE_NAME_1.COLUMN_NAME_2}))
THEN "*Unspecified Location"
ELSE IF ISNULL({TABLE_NAME_2.COLUMN_NAME_1})
THEN "*Unknown Location " &IIF((ISNULL({TABLE_NAME_1.COLUMN_NAME_1}) or ISNULL({TABLE_NAME_1.COLUMN_NAME_2})),{TABLE_NAME_1.COLUMN_NAME_1},{TABLE_NAME_1.COLUMN_NAME_2})
ELSE IF ISNULL({TABLE_NAME_2.COLUMN_NAME_2})
THEN "*Unnamed Location " & {TABLE_NAME_2.COLUMN_NAME_1}
)
ELSE {TABLE_NAME_2.COLUMN_NAME_2}
My assumption was that it would return a true if that value was numeric (within the isnumeric range) but FALSE if the ISNULL returns 'blah'. Seems like my assumption was off...
I'm using the it in the following way
case when ISNULL(ISNUMERIC(c.npinumber), 'blah') = 1
then c.NPiNUmber
else 'not valid: ' + c.NpiNumber
end as npi
Building on Dhruvesh's answer,
case
when ISNUMERIC(c.npinumber) = 1 then c.NPiNUmber
else 'not valid: ' + c.NpiNumber
end as npi
Will produce NULL anytime NpiNumber is NULL. The reason is that NULL + any string will still return NULL. The solution is to simply use the COALESCE function
case
when ISNUMERIC(c.npinumber) = 1 then c.NPiNUmber
else 'not valid: ' + COALESCE(c.NpiNumber, 'NULL VALUE')
end as npi
select ISNUMERIC(ISNULL(NULL, 'blah')),
ISNUMERIC(ISNULL(1234, 'blah')),
ISNUMERIC(ISNULL('ab', 'blah'))
Returns 0, 1, 0 - so your logic is correct.
When SQL's not behaving I like to simplify my query. Try running the query without your case statement first. If the results look right, then add additional logic.
What collation is your database? It's always a good idea to keep your column names properly cased (I'm looking at that all-lowercase column name over there...).
You don't require ISNULL. ISNUMERIC will return 1 if it's numberic or 0 if it's NULL or non-numeric.
case
when ISNUMERIC(c.NpiNumber) = 1 then c.NPiNUmber
else 'not valid: ' + c.NpiNumber
end as npi
Also as Euric Mentioned you may want to look at your all-lowercase column name.
Need some small help with some SQL. I am using a VARCHAR value to determine in a case which logic to use in WHERE clause but I am having some issues writing this case statement.
where
(CASE WHEN #p_flag = 'ATA'
THEN (#p_start_ata IS NULL AND #p_end_ata IS NULL) OR (vso.poa_ata between #p_start_ata and #p_end_ata)
ELSE (#p_start_atd IS NULL AND #p_end_atd IS NULL) OR (vso.pol_atd between #p_start_atd and #p_end_atd)
)
Line 93 Incorrect syntax near the keyword 'IS'.
Its probably a minor error but its frustrating me. Maybe there is a better way of writing this? Thanks!
Like said before you can not use a case statement like you are doing it. So this might be a suggestion:
WHERE
(
#p_flag = 'ATA'
AND
(
(#p_start_ata IS NULL AND #p_end_ata)
OR (vso.poa_ata between #p_start_ata and #p_end_ata)
)
)
OR
(
NOT #p_flag = 'ATA'
AND
(
(#p_start_atd IS NULL AND #p_end_atd IS NULL)
OR (vso.pol_atd between #p_start_atd and #p_end_atd)
)
)
You can't use the CASE like that!
It's use to get values and not restrictions. Check this to view how to properly use CASE.
In your case you've to change your logic probably using IF's and making the wanted SELECT's.
I want to do a comparison such as:
if <field> == 0 then "-"
Can somebody tell me the syntax using JasperReports?
iReport (JasperReports) uses a Ternary operator. For example, consider the following logic:
IF boolean condition THEN
execute true code
ELSE
execute false code
END IF
Using a ternary operator, this becomes:
boolean condition ? execute true code : execute false code
When using a variable with the following expression:
$F{column_value}.intValue() == 42 ? "Life, Universe, Everything" : "Naught"
Then the variable's value would be "Life, Universe, Everything" if, and only if, the integer value of $F{column_value} is equal to 42.
Where things get a little obtuse is when you have to have nested conditions. For these, put the nested conditions in parenthesis and on a separate line:
condition1 ?
(condition2 ? true_code2 : false_code2) :
false_code1
So when you need to do many of them:
condition1 ?
(condition2 ?
(condition3 ? true_code3 : false_code3) :
false_code2) :
(condition4 ? true_code4 : false_code4)
example of expression in ireport:
(
$F{foo} == 0 ?
"Planned" :
$F{foo} == 1 ?
"Reserved" :
$F{foo} == 2 ?
"Canceled" :
$F{foo} == 3 ?
"Absent" :
$F{foo} == 4 ?
"Complete" :
"Unknown"
)
Use if-else condition:
if customer name is null write '-' (absent), else write customer name.
Be careful of your field data type!
<textFieldExpression class="java.lang.String">
<![CDATA[
$F{CustomerName} == null ? '-' : $F{CustomerName}
]]>
</textFieldExpression>
Can I refactor the below SQL CASE statements into single for each case ?
SELECT
CASE RDV.DOMAIN_CODE WHEN 'L' THEN CN.FAMILY_NAME ELSE NULL END AS [LEGAL_FAMILY_NAME],
CASE RDV.DOMAIN_CODE WHEN 'L' THEN CN.GIVEN_NAME ELSE NULL END AS [LEGAL_GIVEN_NAME],
CASE RDV.DOMAIN_CODE WHEN 'L' THEN CN.MIDDLE_NAMES ELSE NULL END AS [LEGAL_MIDDLE_NAMES],
CASE RDV.DOMAIN_CODE WHEN 'L' THEN CN.NAME_TITLE ELSE NULL END AS [LEGAL_NAME_TITLE],
CASE RDV.DOMAIN_CODE WHEN 'P' THEN CN.FAMILY_NAME ELSE NULL END AS [PREFERRED_FAMILY_NAME],
CASE RDV.DOMAIN_CODE WHEN 'P' THEN CN.GIVEN_NAME ELSE NULL END AS [PREFERRED_GIVEN_NAME],
CASE RDV.DOMAIN_CODE WHEN 'P' THEN CN.MIDDLE_NAMES ELSE NULL END AS [PREFERRED_MIDDLE_NAMES],
CASE RDV.DOMAIN_CODE WHEN 'P' THEN CN.NAME_TITLE ELSE NULL END AS [PREFERRED_NAME_TITLE]
FROM dbo.CLIENT_NAME CN
JOIN dbo.REFERENCE_DOMAIN_VALUE RDV
ON CN.NAME_TYPE_CODE = RDV.DOMAIN_CODE AND RDV.REFERENCE_DOMAIN_ID = '7966'
No, you will require 8 separate statements as case and other such variants can only be used in a select to modify the results of a single column, not a series of columns.
If RDV.DOMAIN_COD can only by 'P' or 'L' use NULLIf. It's cleaner.
NULLIF ( expression , expression )
NULLIF is equivalent to a searched CASE expression in which the two expressions are equal and the resulting expression is NULL.
SELECT
NullIf('P', RDV.DOMAIN_CODE) AS [LEGAL_FAMILY_NAME],
...
NullIf('L', RDV.DOMAIN_CODE) AS [PREFERRED_FAMILY_NAME],
...
Since a CASE expression returns a single value, you cannot take eight CASE expressions returning 8 values and make a single CASE expression that returns all eight.
A less efficient alternative with no cases:
SELECT LEGAL_FAMILY_NAME, LEGAL_GIVEN_NAME, LEGAL_MIDDLE_NAMES, LEGAL_NAME_TITLE,
PREFERRED_FAMILY_NAME, PREFERRED_GIVEN_NAME, PREFERRED_MIDDLE_NAMES, PREFERRED_NAME_TITLE
FROM dbo.REFERENCE_DOMAIN_VALUE RDV
LEFT OUTER JOIN
( SELECT
NAME_TYPE_CODE,
FAMILY_NAME AS [LEGAL_FAMILY_NAME],
GIVEN_NAME AS [LEGAL_GIVEN_NAME],
MIDDLE_NAMES AS [LEGAL_MIDDLE_NAMES],
NAME_TITLE AS [LEGAL_NAME_TITLE]
FROM dbo.CLIENT_NAME
WHERE NAME_TYPE_CODE = 'L') LN ON RDV.DOMAIN_CODE = LN.NAME_TYPE_CODE
LEFT OUTER JOIN
( SELECT
NAME_TYPE_CODE,
FAMILY_NAME AS [PREFERRED_FAMILY_NAME],
GIVEN_NAME AS [PREFERRED_GIVEN_NAME],
MIDDLE_NAMES AS [PREFERRED_MIDDLE_NAMES],
NAME_TITLE AS [PREFERRED_NAME_TITLE]
FROM dbo.CLIENT_NAME
WHERE NAME_TYPE_CODE = 'P') PN ON RDV.DOMAIN_CODE = PN.NAME_TYPE_CODE
WHERE RDV.REFERENCE_DOMAIN_ID = '7966'
You could also use a temp table or table variable with all 8 columns and then do two inserts. You could also use a UNION ALL. My guess is that the 8 case statements are the most efficient way. This is especially true if you have some key where you will want some type of ClientID, Legal Names, Preferred Names so you will wrap a MAX around the cases or something and group by a ClientID......
You could generate the SQL script using syscols/INFORMATION_SCHEMA.columns with two case whens for each column 'CASE WHEN DOMAIN_CODE = ''P'' THEN ' + COLUMN_NAME + ' ELSE NULL END AS PREFERRED_' + COLUMN_NAME and then another for L. You could make a LOOP so that the same code executes once for P and once for L and get it down to one loop. Then you could EXEC the string directly, or PRINT it and put it into your SQL script. Anyway for just 8 columns I would cut/paste the case statements...
But anyway in general T-SQL is limited on being able to do for eaches over columns. Either you generate the script using a code generator (done in t-sql or another programming language) or you rethink your problem in another way. But many times you get better performance from duplicate cut/paste code. And many times it isn't worth the hassle of writing an external code generator (ie just for 8 case statements).