Unix Technical Forum

can I create Date Variable?

This is a discussion on can I create Date Variable? within the Oracle Miscellaneous forums, part of the Oracle Database category; --> I have a union query that combines the results of several queries. Each of these queries has a condition ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database > Oracle Miscellaneous

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-05-2008, 12:00 PM
AP
 
Posts: n/a
Default can I create Date Variable?

I have a union query that combines the results of several queries.
Each of these queries has a condition to extract the row where the
date is between 1/1/08 and 1/31/08. Each month when I run this I have
to do a find and replace to change the date range. Is there a way to
declare a date variable and reference that in the select statement
instead of the literal date?

For example
instead of between 1/1/08 and 1/31/08

it would be something like...

startDate = 1/1/08
endDate = 1/31/08

statement = between startdate and enddate

Thanks in advance
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 09-05-2008, 12:00 PM
fitzjarrell@cox.net
 
Posts: n/a
Default Re: can I create Date Variable?

On Sep 4, 9:01*am, AP <adamwphoe...@gmail.com> wrote:
> I have a union query that combines the results of several queries.
> Each of these queries has a condition to extract the row where the
> date is between 1/1/08 and 1/31/08. Each month when I run this I have
> to do a find and replace to change the date range. Is there a way to
> declare a date variable and reference that in the select statement
> instead of the literal date?
>
> For example
> instead of between 1/1/08 and 1/31/08
>
> it would be something like...
>
> startDate = 1/1/08
> endDate = 1/31/08
>
> statement = between startdate and enddate
>
> Thanks in advance


Why are you relying upon default formats for your date strings? This
is the surest way to write code that can, and will, fail should that
default be changed.

Now, to answer your question, yes, you can declare variables, use
them, and reassign values to them:

SQL> variable startdt varchar2(8)
SQL> variable enddt varchar2(8)
SQL>
SQL> exec :startdt := '01/01/82';

PL/SQL procedure successfully completed.

SQL> exec :enddt := '01/31/82';

PL/SQL procedure successfully completed.

SQL>
SQL> select *
2 from emp
3 where hiredate between to_date(:startdt, 'MM/DD/RR') and
to_date(:enddt, 'MM/DD/RR')
4 /

EMPNO ENAME JOB MGR HIREDATE SAL
COMM DEPTNO BONUS
---------- ---------- --------- ---------- --------- ----------
---------- ---------- ----------
7934 MILLER CLERK 7782 23-JAN-82
1300 10

SQL>
SQL> exec :startdt := '02/01/81';

PL/SQL procedure successfully completed.

SQL> exec :enddt := '02/28/81';

PL/SQL procedure successfully completed.

SQL>
SQL> select *
2 from emp
3 where hiredate between to_date(:startdt, 'MM/DD/RR') and
to_date(:enddt, 'MM/DD/RR')
4 /

EMPNO ENAME JOB MGR HIREDATE SAL
COMM DEPTNO BONUS
---------- ---------- --------- ---------- --------- ----------
---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600
300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250
500 30

SQL>
SQL> exec :startdt := '01/01/83';

PL/SQL procedure successfully completed.

SQL> exec :enddt := '01/31/83';

PL/SQL procedure successfully completed.

SQL>
SQL> select *
2 from emp
3 where hiredate between to_date(:startdt, 'MM/DD/RR') and
to_date(:enddt, 'MM/DD/RR')
4 /

EMPNO ENAME JOB MGR HIREDATE SAL
COMM DEPTNO BONUS
---------- ---------- --------- ---------- --------- ----------
---------- ---------- ----------
7876 ADAMS CLERK 7788 12-JAN-83
1100 20

SQL>


David Fitzjarrell
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-05-2008, 12:00 PM
Mark D Powell
 
Posts: n/a
Default Re: can I create Date Variable?

On Sep 4, 11:05*am, "fitzjarr...@cox.net" <orat...@msn.com> wrote:
> On Sep 4, 9:01*am, AP <adamwphoe...@gmail.com> wrote:
>
>
>
>
>
> > I have a union query that combines the results of several queries.
> > Each of these queries has a condition to extract the row where the
> > date is between 1/1/08 and 1/31/08. Each month when I run this I have
> > to do a find and replace to change the date range. Is there a way to
> > declare a date variable and reference that in the select statement
> > instead of the literal date?

>
> > For example
> > instead of between 1/1/08 and 1/31/08

>
> > it would be something like...

>
> > startDate = 1/1/08
> > endDate = 1/31/08

>
> > statement = between startdate and enddate

>
> > Thanks in advance

>
> Why are you relying upon default formats for your date strings? *This
> is the surest way to write code that can, and will, fail should that
> default be changed.
>
> Now, to answer your question, yes, you can declare variables, use
> them, and reassign values to them:
>
> SQL> variable startdt varchar2(8)
> SQL> variable enddt varchar2(8)
> SQL>
> SQL> exec :startdt := '01/01/82';
>
> PL/SQL procedure successfully completed.
>
> SQL> exec :enddt := '01/31/82';
>
> PL/SQL procedure successfully completed.
>
> SQL>
> SQL> select *
> * 2 *from emp
> * 3 *where hiredate between to_date(:startdt, 'MM/DD/RR') and
> to_date(:enddt, 'MM/DD/RR')
> * 4 */
>
> * * *EMPNO ENAME * * *JOB * * * * * * *MGR HIREDATE * * * * SAL
> COMM * * DEPTNO * * *BONUS
> ---------- ---------- --------- ---------- --------- ----------
> ---------- ---------- ----------
> * * * 7934 MILLER * * CLERK * * * * * 7782 23-JAN-82
> 1300 * * * * * * * * * *10
>
> SQL>
> SQL> exec :startdt := '02/01/81';
>
> PL/SQL procedure successfully completed.
>
> SQL> exec :enddt := '02/28/81';
>
> PL/SQL procedure successfully completed.
>
> SQL>
> SQL> select *
> * 2 *from emp
> * 3 *where hiredate between to_date(:startdt, 'MM/DD/RR') and
> to_date(:enddt, 'MM/DD/RR')
> * 4 */
>
> * * *EMPNO ENAME * * *JOB * * * * * * *MGR HIREDATE * * * * SAL
> COMM * * DEPTNO * * *BONUS
> ---------- ---------- --------- ---------- --------- ----------
> ---------- ---------- ----------
> * * * 7499 ALLEN * * *SALESMAN * * * *7698 20-FEB-81 * * * 1600
> 300 * * * * 30
> * * * 7521 WARD * * * SALESMAN * * * *7698 22-FEB-81 * * * 1250
> 500 * * * * 30
>
> SQL>
> SQL> exec :startdt := '01/01/83';
>
> PL/SQL procedure successfully completed.
>
> SQL> exec :enddt := '01/31/83';
>
> PL/SQL procedure successfully completed.
>
> SQL>
> SQL> select *
> * 2 *from emp
> * 3 *where hiredate between to_date(:startdt, 'MM/DD/RR') and
> to_date(:enddt, 'MM/DD/RR')
> * 4 */
>
> * * *EMPNO ENAME * * *JOB * * * * * * *MGR HIREDATE * * * * SAL
> COMM * * DEPTNO * * *BONUS
> ---------- ---------- --------- ---------- --------- ----------
> ---------- ---------- ----------
> * * * 7876 ADAMS * * *CLERK * * * * * 7788 12-JAN-83
> 1100 * * * * * * * * * *20
>
> SQL>
>
> David Fitzjarrell- Hide quoted text -
>
> - Show quoted text -


If you are running the query from SQLPLus you might just be able to
use a SQLPlus substitution variable.

select .... from ... where date_col >= to_date('&the_date','YYYMMDD')

You will be prompted for the value of &the_date by SQLPlus. Use && if
you need to use the variable in the query several times

See the SQLPlus manual and look up the ACCEPT, PROMPT, and UNDEFINE
commands as well as look for substitiution (label) variables to
provide more control over the variable input and reuse.

HTH -- Mark D Powell --
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-12-2008, 05:24 PM
fitzjarrell@cox.net
 
Posts: n/a
Default Re: can I create Date Variable?

On Sep 4, 1:21*pm, Mark D Powell <Mark.Pow...@eds.com> wrote:
> On Sep 4, 11:05*am, "fitzjarr...@cox.net" <orat...@msn.com> wrote:
>
>
>
>
>
> > On Sep 4, 9:01*am, AP <adamwphoe...@gmail.com> wrote:

>
> > > I have a union query that combines the results of several queries.
> > > Each of these queries has a condition to extract the row where the
> > > date is between 1/1/08 and 1/31/08. Each month when I run this I have
> > > to do a find and replace to change the date range. Is there a way to
> > > declare a date variable and reference that in the select statement
> > > instead of the literal date?

>
> > > For example
> > > instead of between 1/1/08 and 1/31/08

>
> > > it would be something like...

>
> > > startDate = 1/1/08
> > > endDate = 1/31/08

>
> > > statement = between startdate and enddate

>
> > > Thanks in advance

>
> > Why are you relying upon default formats for your date strings? *This
> > is the surest way to write code that can, and will, fail should that
> > default be changed.

>
> > Now, to answer your question, yes, you can declare variables, use
> > them, and reassign values to them:

>
> > SQL> variable startdt varchar2(8)
> > SQL> variable enddt varchar2(8)
> > SQL>
> > SQL> exec :startdt := '01/01/82';

>
> > PL/SQL procedure successfully completed.

>
> > SQL> exec :enddt := '01/31/82';

>
> > PL/SQL procedure successfully completed.

>
> > SQL>
> > SQL> select *
> > * 2 *from emp
> > * 3 *where hiredate between to_date(:startdt, 'MM/DD/RR') and
> > to_date(:enddt, 'MM/DD/RR')
> > * 4 */

>
> > * * *EMPNO ENAME * * *JOB * * * * * * *MGR HIREDATE * * * * SAL
> > COMM * * DEPTNO * * *BONUS
> > ---------- ---------- --------- ---------- --------- ----------
> > ---------- ---------- ----------
> > * * * 7934 MILLER * * CLERK * * * * * 7782 23-JAN-82
> > 1300 * * * * * * * * * *10

>
> > SQL>
> > SQL> exec :startdt := '02/01/81';

>
> > PL/SQL procedure successfully completed.

>
> > SQL> exec :enddt := '02/28/81';

>
> > PL/SQL procedure successfully completed.

>
> > SQL>
> > SQL> select *
> > * 2 *from emp
> > * 3 *where hiredate between to_date(:startdt, 'MM/DD/RR') and
> > to_date(:enddt, 'MM/DD/RR')
> > * 4 */

>
> > * * *EMPNO ENAME * * *JOB * * * * * * *MGR HIREDATE * * * * SAL
> > COMM * * DEPTNO * * *BONUS
> > ---------- ---------- --------- ---------- --------- ----------
> > ---------- ---------- ----------
> > * * * 7499 ALLEN * * *SALESMAN * * * *7698 20-FEB-81 * * * 1600
> > 300 * * * * 30
> > * * * 7521 WARD * * * SALESMAN * * * *7698 22-FEB-81 * * * 1250
> > 500 * * * * 30

>
> > SQL>
> > SQL> exec :startdt := '01/01/83';

>
> > PL/SQL procedure successfully completed.

>
> > SQL> exec :enddt := '01/31/83';

>
> > PL/SQL procedure successfully completed.

>
> > SQL>
> > SQL> select *
> > * 2 *from emp
> > * 3 *where hiredate between to_date(:startdt, 'MM/DD/RR') and
> > to_date(:enddt, 'MM/DD/RR')
> > * 4 */

>
> > * * *EMPNO ENAME * * *JOB * * * * * * *MGR HIREDATE * * * * SAL
> > COMM * * DEPTNO * * *BONUS
> > ---------- ---------- --------- ---------- --------- ----------
> > ---------- ---------- ----------
> > * * * 7876 ADAMS * * *CLERK * * * * * 7788 12-JAN-83
> > 1100 * * * * * * * * * *20

>
> > SQL>

>
> > David Fitzjarrell- Hide quoted text -

>
> > - Show quoted text -

>
> If you are running the query from SQLPLus you might just be able to
> use a SQLPlus substitution variable.
>
> select .... from ... where date_col >= to_date('&the_date','YYYMMDD')
>
> You will be prompted for the value of &the_date by SQLPlus. *Use && if
> you need to use the variable in the query several times
>
> See the SQLPlus manual and look up the ACCEPT, PROMPT, and UNDEFINE
> commands as well as look for substitiution (label) variables to
> provide more control over the variable input and reuse.
>
> HTH -- Mark D Powell --- Hide quoted text -
>
> - Show quoted text -


To follow up with this using such replacement variables is easy:

SQL> select *
2 from emp
3 where hiredate between to_date(&startdt, 'MM/DD/RR') and
to_date(&enddt, 'MM/DD/RR')
4 /
Enter value for startdt: '01/01/82'
Enter value for enddt: '01/31/82'
old 3: where hiredate between to_date(&startdt, 'MM/DD/RR') and
to_date(&enddt, 'MM/DD/RR')
new 3: where hiredate between to_date('01/01/82', 'MM/DD/RR') and
to_date('01/31/82', 'MM/DD/RR')

EMPNO ENAME JOB MGR HIREDATE SAL
COMM DEPTNO BONUS
---------- ---------- --------- ---------- --------- ----------
---------- ---------- ----------
7934 MILLER CLERK 7782 23-JAN-82
1300 10

SQL>
SQL> select *
2 from emp
3 where hiredate between to_date(&startdt, 'MM/DD/RR') and
to_date(&enddt, 'MM/DD/RR')
4 /
Enter value for startdt: '02/01/81'
Enter value for enddt: '02/28/81'
old 3: where hiredate between to_date(&startdt, 'MM/DD/RR') and
to_date(&enddt, 'MM/DD/RR')
new 3: where hiredate between to_date('02/01/81', 'MM/DD/RR') and
to_date('02/28/81', 'MM/DD/RR')

EMPNO ENAME JOB MGR HIREDATE SAL
COMM DEPTNO BONUS
---------- ---------- --------- ---------- --------- ----------
---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600
300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250
500 30

SQL>

Note the single & maintains the value for the duration of the query.
Using && allows you to pass the value through the script to several
queries:

SQL> select *
2 from emp
3 where hiredate between to_date(&&startdt, 'MM/DD/RR') and
to_date(&&enddt, 'MM/DD/RR')
4 /
Enter value for startdt: '01/01/83'
Enter value for enddt: '01/31/83'
old 3: where hiredate between to_date(&&startdt, 'MM/DD/RR') and
to_date(&&enddt, 'MM/DD/RR')
new 3: where hiredate between to_date('01/01/83', 'MM/DD/RR') and
to_date('01/31/83', 'MM/DD/RR')

EMPNO ENAME JOB MGR HIREDATE SAL
COMM DEPTNO BONUS
---------- ---------- --------- ---------- --------- ----------
---------- ---------- ----------
7876 ADAMS CLERK 7788 12-JAN-83
1100 20

SQL>
SQL> select *
2 from emp
3 where hiredate between to_date(&&startdt, 'MM/DD/RR') and
to_date(&&enddt, 'MM/DD/RR')
4 /
old 3: where hiredate between to_date(&&startdt, 'MM/DD/RR') and
to_date(&&enddt, 'MM/DD/RR')
new 3: where hiredate between to_date('01/01/83', 'MM/DD/RR') and
to_date('01/31/83', 'MM/DD/RR')

EMPNO ENAME JOB MGR HIREDATE SAL
COMM DEPTNO BONUS
---------- ---------- --------- ---------- --------- ----------
---------- ---------- ----------
7876 ADAMS CLERK 7788 12-JAN-83
1100 20

SQL>

which can 'backfire' if you're not careful (like it did above). To
fix that it's necessary to undefine the variables:

SQL> select *
2 from emp
3 where hiredate between to_date(&&startdt, 'MM/DD/RR') and
to_date(&&enddt, 'MM/DD/RR')
4 /
old 3: where hiredate between to_date(&&startdt, 'MM/DD/RR') and
to_date(&&enddt, 'MM/DD/RR')
new 3: where hiredate between to_date('01/01/83', 'MM/DD/RR') and
to_date('01/31/83', 'MM/DD/RR')

EMPNO ENAME JOB MGR HIREDATE SAL
COMM DEPTNO BONUS
---------- ---------- --------- ---------- --------- ----------
---------- ---------- ----------
7876 ADAMS CLERK 7788 12-JAN-83
1100 20

SQL>
SQL> undefine startdt
SQL> undefine enddt
SQL>
SQL> select *
2 from emp
3 where hiredate between to_date(&&startdt, 'MM/DD/RR') and
to_date(&&enddt, 'MM/DD/RR')
4 /
Enter value for startdt: '01/01/82'
Enter value for enddt: '01/31/82'
old 3: where hiredate between to_date(&&startdt, 'MM/DD/RR') and
to_date(&&enddt, 'MM/DD/RR')
new 3: where hiredate between to_date('01/01/82', 'MM/DD/RR') and
to_date('01/31/82', 'MM/DD/RR')

EMPNO ENAME JOB MGR HIREDATE SAL
COMM DEPTNO BONUS
---------- ---------- --------- ---------- --------- ----------
---------- ---------- ----------
7934 MILLER CLERK 7782 23-JAN-82
1300 10

SQL>
SQL> undefine startdt
SQL> undefine enddt
SQL>

Once undefined the variables are ready for new assignments. You can
do this with any name you like (even numerics) but it's difficult for
others to know what, say, &&1 mighr be used to do:

Enter value for 1:

Not very descriptive, which is why one should use more definitive
names for such variables (as illustrated).


David Fitzjarrell
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads for: can I create Date Variable?

Thread Thread Starter Forum Replies Last Post
Use of TZ env variable in presentation of date values droesler Ingres 6 04-20-2008 10:57 PM
Change date format through an environmental variable? Mark Fenbers pgsql Sql 9 04-19-2008 01:17 PM
How Do You Assign A Date Variable From A Parameter? Brian pgsql Databases 1 04-10-2008 12:26 AM
need help to take the variables associated with the 1st occurrence of a date variable menglin.cao@gmail.com SQL Server 3 02-29-2008 07:39 PM
date query problem, date in variable roy MySQL 1 02-28-2008 08:05 AM


All times are GMT. The time now is 09:19 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com