0

I have the below query:

SELECT *
FROM FOO
WHERE LOCATION = :LOCATION
AND MY_DATE >= TIMESTAMP :BEGIN AND MY_DATE <= TIMESTAMP :END -- option 1 if :BEGIN & :END is not NULL
AND MY_DATE >= TIMESTAMP :BEGIN AND MY_DATE <= sysdate        -- option 2 if :BEGIN is not NULL & :END is NULL
AND MY_DATE <= TIMESTAMP :END                                 -- option 3 if :BEGIN is NULL & :END is not NULL
AND MY_DATE <= sysdate                                        -- option 4 if both :BEGIN & :END is NULL
ORDER BY MY_DATE;

so here :LOCATION is supplied by the user on code level using OCI8. For example:

require 'oci8'
cursor = conn.parse(query)
cursor.bind_param(':LOCATION', 'Chicago', String)

I only want one of the options from 1 to 4 to be part of the final query. For example if option 3 is true (:BEGIN is NULL & :END is not NULL) then the final query will be:

SELECT *
FROM FOO
WHERE LOCATION = :LOCATION
AND MY_DATE <= TIMESTAMP :END                                 -- option 3 if :BEGIN is not NULL & :END is NULL
ORDER BY MY_DATE;

Where user would supply an :END date

require 'oci8'
cursor = conn.parse(query)
cursor.bind_param(':LOCATION', 'Chicago', String)
cursor.bind_param(':START', NULL)
cursor.bind_param(':END', '2001-01-22 12:01:00', String)

and would result in:

SELECT *
FROM FOO
WHERE LOCATION = 'chicago'
AND MY_DATE <= TIMESTAMP '2001-01-22 12:01:00'
ORDER BY MY_DATE;

How do I write a query to allow this logic?

2
  • You may use my_date >= coalesce(:begin, date '0001-01-01') and my_date <= coalesce(:end, date '9999-12-31' + interval '23:59:59' hour to second) Commented Mar 25, 2022 at 20:42
  • Answer for a similar question is here showing the trick how to generate a dynamic SQL, so that you have the same number of bind variables for all options. Commented Mar 25, 2022 at 22:07

2 Answers 2

1

An option is to use CASE :

SELECT *
  FROM FOO
 WHERE LOCATION = :LOCATION
   AND MY_DATE >= CASE WHEN :BEGIN IS NOT NULL THEN :BEGIN
                       ELSE MY_DATE
                  END
   AND MY_DATE <= CASE WHEN :END IS NOT NULL THEN :END
                       ELSE SYSDATE
                  END
 ORDER BY MY_DATE;
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure if the first clause would work if :BEGIN = NULL as it would result in AND MY_DATE >=MY_DATE. Instead it would be better if we hard-coded it as AND MY_DATE >= CASE WHEN :BEGIN IS NOT NULL THEN :BEGIN ELSE '1990-01-01 00:00:00'
You got greater or equal, if you compare MY_DATE with MY_DATE it will return true (basically remove the condition), which is what you need. Moreover, putting the hardcoded value limits the use of your query, which is generally bad.
0

wondering if you exchanged the comments for options 2 and 3? I see you are using :BEGIN and :END when they are NULL

AND MY_DATE >= TIMESTAMP :BEGIN AND MY_DATE <= sysdate        -- option 2 if :BEGIN is NULL & :END is not NULL
AND MY_DATE <= TIMESTAMP :END                                 -- option 3 if :BEGIN is not NULL & :END is NULL

I think you can try:

SELECT *
FROM FOO
WHERE LOCATION = :LOCATION
and my_date BETWEEN nvl(:BEGIN,'1900-01-01 00:00:00') and nvl(:END,sysdate)

3 Comments

Thanks for noticing, I made a correction.
Does this take into account the change in <= and >=?
BETWEEN is inclusive which means it will work exactly as >= & <=. Also notice that on my query I put a default lower bound value of '1900-01-01 00:00:00' if :BEGIN is null, which in most of the cases I think should work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.