Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

SQL WHERE statement optional

Questions related to using nuBuilder Forte.
Post Reply
Uzlander
Posts: 52
Joined: Sat Jul 08, 2023 10:21 am
Has thanked: 8 times
Been thanked: 2 times

SQL WHERE statement optional

Unread post by Uzlander »

Hi people! Probably this is a beginner level question but i couldn't make the WHERE statement optional.
Context: The second (select type )input gets filtered based on the first input, but if the first left blank (empty|null|undefined) then no filter for the second input should work.
So

Code: Select all

  WHERE
    ((rblocks.kompleks = '#KOMPLEKS#'))
clause somehow doesn't make it really optional and filter's working anyway, whereas i need WHERE eliminated if '#KOMPLEKS#' equals null and all records should show up
kev1n
nuBuilder Team
Posts: 4428
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 74 times
Been thanked: 476 times
Contact:

Re: SQL WHERE statement optional

Unread post by kev1n »

Maybe like this?

Code: Select all

WHERE (
   IFNULL('#KOMPLEKS#','') = ''
   OR LEFT('#KOMPLEKS#',1) = '#'
   OR rblocks.kompleks = '#KOMPLEKS#'
)

Explanation line by line
  1. IFNULL('#KOMPLEKS#','') = ''
     – If #KOMPLEKS# is NULL or empty, the condition is true.
     – Meaning: Don’t filter, show all records.
  2. LEFT('#KOMPLEKS#',1) = '#'
     – If the first character of #KOMPLEKS# is #, the condition is true.
     – This usually indicates the hash cookie placeholder wasn’t replaced.
     – Meaning: Ignore filter, show all records.
  3. rblocks.kompleks = '#KOMPLEKS#'
     – If #KOMPLEKS# contains a valid value, only rows where rblocks.kompleks matches it will be returned.
Post Reply