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?

Dante
  • 537
  • 2
  • 4
  • 18
  • 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)` – astentx Mar 25 '22 at 20:42
  • Answer for a similar question is [here](https://stackoverflow.com/a/56778621/4808122) showing the *trick* how to generate a dynamic SQL, so that you have the same number of *bind variables* for all options. – Marmite Bomber Mar 25 '22 at 22:07

2 Answers2

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;
Gnqz
  • 3,292
  • 3
  • 25
  • 35
  • 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'` – Dante Mar 25 '22 at 21:11
  • 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. – Gnqz Mar 25 '22 at 21:57
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)
kLorD
  • 41
  • 2
  • Thanks for noticing, I made a correction. – Dante Mar 25 '22 at 20:09
  • Does this take into account the change in <= and >=? – Dante Mar 25 '22 at 20:16
  • 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. – kLorD Mar 25 '22 at 20:25