Filter empty or null value in JSP file using SQL

Asked

Viewed 88 times

0

Good afternoon, I have an SQL query as code below where I have 3 filters, these filters use the clause Where, and e and but if one of the filters is blank the value returned is also blank.

How can I do this treatment, either in the database or directly in the JSP file?

SELECT DISTINCT DECODE (initcap(PP.OVERALL_HEALTH), '', '/itg/images/statusreport/NA.gif', '/itg/images/statusreport/'   || DECODE(UPPER(PP.OVERALL_HEALTH), 'GREEN', 'Verde', initcap(PP.OVERALL_HEALTH))   || '.gif' ) SAUDE_PROGRAMA_ICONE, PP.CONTAINER_NAME NOME_PROGRAMA, HD.VISIBLE_PARAMETER1 TIPO_CLIENTE, HD.VISIBLE_PARAMETER32 DIRETOR, P.NAME
FROM PGM_PROGRAMS PP
INNER JOIN KCRT_REQ_HEADER_DETAILS HD ON HD.REQUEST_ID = PP.PGM_REQUEST_ID
INNER JOIN KCRT_FG_PFM_PROGRAM FP ON FP.REQUEST_ID = PP.PGM_REQUEST_ID
INNER JOIN FM_FINANCIAL_SUMMARY FS ON PP.FINANCIAL_SUMMARY_ID = FS.FINANCIAL_SUMMARY_ID
INNER JOIN PFM_PORTFOLIO_CONTENTS PC ON FS.FINANCIAL_SUMMARY_ID = PC.FINANCIAL_SUMMARY_ID
INNER JOIN PFM_PORTFOLIOS P ON PC.PORTFOLIO_ID = P.PORTFOLIO_ID
WHERE HD.PARAMETER1 = 'CORPORATIVO'
AND HD.PARAMETER32 IN '100427' 
AND P.NAME IN (select p.name as portfolio from pfm_portfolios p
start with p.portfolio_id in '34006' 
connect by prior p.portfolio_id = p.parent_portfolio_id)

1 answer

0


You can use a OR together with the AND:

SELECT *
  FROM tabela t
 WHERE (:condicao1 = '' OR t.condicao1 = :condicao1)
   AND (:condicao2 = '' OR t.condicao2 = :condicao2)

Just don’t forget to isolate the conditions in parentheses to have the behavior as explained

  • Thanks for the answer, but if I tested it correctly it does not meet my need because it needs to bring exactly what is in the filter and so it brings as if it were an OR, then ends up not filtering, although it solves the value return empty problem.

  • @Guilhermemontagnani if you put the parentheses in the right place will certainly filter. This behavior you described only occurs if the parentheses are in the wrong place or if you do not use AND between the parameters

  • The expression was as follows: WHERE (HD.PARAMETER1 = 'CORPORATE' AND HD.PARAMETER1 = HD.PARAMETER1) AND (HD.PARAMETER32 = '100427' OR HD.PARAMETER32 is = HD.PARAMETER32)

  • @Guilhermemontagnani you applied wrong what I answered. It would look something like this: WHERE (:PARAMETER1 = '' OR HD.PARAMETER1 = :PARAMETER1) AND (:PARAMETER32 = '' OR :PARAMETER32 = HD.PARAMETER32)

  • I understand, now I can not do the test, but I will save the query and as soon as possible answer if solved my problem. Thanks in advance for the help!

  • Good afternoon brother, I thank you for your help but it really didn’t work, the query returns empty.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.