Can someone please help me on adding/inserting a variable to an sql scipt? Basically I want to assign today's date. As shown below..
set head off;
set linesize 300;
set pagesize 200;
spool /opt/oracle/temp/output.txt
select value,count(*) as totalcount from pmowner.pinpebasev where availableat between '${date} 12.00.00 AM' and '${date} 11.59.59 PM' group by value;
spool off
quit
I want to use this variable...
date= `date '+%d-%b-%y' | tr '[a-z]' '[A-Z]'`
It should show like this...
set head off;
set linesize 300;
set pagesize 200;
spool /opt/oracle/temp/output.txt
select value,count(*) as totalcount from pmowner.pinpebasev where availableat between '09-JUN-11 12.00.00 AM' and '09-JUN-11 11.59.59 PM' group by value;
spool off
quit
I believe the point is moot since the other posters pointed out the sysdate command, but if you are using ksh, instead of doing this to make a variable uppercase:
date= `date '+%d-%b-%y' | tr '[a-z]' '[A-Z]'`
do this:
typeset -u date=`date '+%d-%b-%y'`
which does the same thing but saves some resources since a pipe and an external program
do not have to be used. typeset -u changes the variable's
value to uppercase.
Consider that if the column availableat is indexed (with a single column index or is in the leading part of a multi-column index) and there's no FBI on it, using the same function (trunc), your version of the query will perform a full table scan of the mowner.pinpebasev table (or underlying table, if mowner.pinpebasev is a view).
I think this will work for me. But instead of today's date, I want the date to be yesterday's date. In this case, instead of June 10... it should be June 09, 2011.
SQL> select value,count(*) as totalcount from pmowner.pinpebasev where availableat between trunc(sysdate) - 1, trunc(sysdate) - 1/24/60/60 group by value;
select value,count(*) as totalcount from pmowner.pinpebasev where availableat between trunc(sysdate) - 1, trunc(sysdate) - 1/24/60/60 group by value
*
ERROR at line 1:
ORA-00905: missing keyword
SQL>
select
value,
count(*) as totalcount
from
pmowner.pinpebasev
where
availableat
between
trunc(sysdate) - 1
and
trunc(sysdate) - 1/24/60/60
group by
value;