0

I basically want to reuse an input to save the user from inputting same thing again..

Here's how the query looks like:

select * from table
where bb.bbn_from_dttm >= TO_DATE('&Bill_Date_ddmmyyyy','dd-mm-yyyy')
and bb.bbn_to_dttm <= LAST_DAY('&Bill_Date_ddmmyyyy');

I don't want the user inputting this twice and getting the value from the first input would give me the date range I need.

This is SQL Oracle.

Any clues?

6
  • 1
    use '&&Bill_Date_ddmmyyyy' at both the places in your code. -- select * from table where bb.bbn_from_dttm >= TO_DATE('&&Bill_Date_ddmmyyyy','dd-mm-yyyy') and bb.bbn_to_dttm <= LAST_DAY('&&Bill_Date_ddmmyyyy'); Commented Jul 4, 2019 at 15:16
  • @Tejash----ORA-00980 Wouldn't this fail the second time you run the query as it would not ask for input? Also if so Could you change the original query to how you suggest it - would be helpful. Commented Jul 4, 2019 at 15:19
  • 1
    In a single session, it will not ask for the input again. If you run it in another session then only it will ask you for input again. Commented Jul 4, 2019 at 15:22
  • @Tejash----ORA-00980 Got it :) Thanks! Commented Jul 4, 2019 at 15:23
  • 1
    Making WHERE condition as bb.bbn_to_dttm between date'&&Bill_Date_yyyy_mm_dd' and date'&&Bill_Date_yyyy_mm_dd' without less, greater and equal signs, and entering the value as 2019-07-04(conforming to ISO standard) is enough whenever prompted. Commented Jul 4, 2019 at 16:00

1 Answer 1

1

Note the use of an inline view to convert the input string into a date (just once for the entire query) - then the rest of the query uses the date created in this inline view, not the user's input. Change & to && if you need the query to use the same input whenever run in the same session (although in most cases that's not what users would want).

select bb.* 
from   my_table bb
       join
       ( select TO_DATE('&Bill_Date_ddmmyyyy','dd-mm-yyyy') as bill_date
         from   dual
       ) d
  on   bb.bbn_from_dttm >= d.bill_date and bb.bbn_to_dttm <= LAST_DAY(d.bill_date);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.