U of A University of Arkansas Division of Agriculture

Pictures of chickens, flowers, wheat, a boy looking through a magnifying glass, irrigation pipe, soybean pods, and fruits and vegetables.

Cooperative Extension Service

Cooperative Extension Service

Agricultural Experiment Station

Steve Rea's
Oracle Tips, Tricks, and Scripts

Link to Steve Rea's Oracle Certification graphic


Search | Publications | Jobs | Personnel Directory | Links

Defines Using Select

Instead of creating a one-line SQL script containing a define statement to set variables at runtime, you can use the new_value option of the column command to set the variable's value generated by a select statement. The snippet below is taken from my Y2K Date Input tip. It checks the length of a date string to see if it has a 2-digit year or a 4-digit year, converting 2-digit years using the RR format and returning the date string with the resulting 4-digit year. To do this, it creates a define statement in a file, and runs the resulting sql file.

spool fieldina.sql
select 'define fieldval = ' || to_char(decode(sign(9-length('&fieldval')),-1,
     to_date('&fieldval','DD-MON-YYYY'),to_date('&fieldval','DD-MON-RR')),
'DD-MON-YYYY') from dual;
spool off
@fieldina.sql

This can also be done without creating the sql file and running it. The select statement below does the conversion, producing a column named fieldvalset. The column command then takes the value in that fieldvalset column and stores that into the fieldval variable as its new value, eliminating the intermediate file.  (The fieldvalset column could also just be called fieldval.) (Thanks to Bruce Knox here for this tip.)

column fieldvalset new_value fieldval
select to_char(decode(sign(9-length('&fieldval')),-1,
     to_date('&fieldval','DD-MON-YYYY'),to_date('&fieldval','DD-MON-RR')),
     'DD-MON-YYYY') fieldvalset from dual;

This Page was Last Updated on 06/04/07

You Are Visitor Number

Hit Counter

Link to CES Home PageBack To CES Home Page

Link to Steve Rea's Oracle PageBack To Steve Rea's Oracle Page

Back to top of this pageBack To Top


© 2006
University of Arkansas
Division of Agriculture
All rights reserved.
Last Date Modified 02/09/2008
Webmaster

University of Arkansas • Division of Agriculture
Cooperative Extension Service
2301 South University Avenue
Little Rock, Arkansas 72204 • USA
Phone (501) 671-2000 • Fax (501) 671-2209
 

MissionDisclaimerEEO
PrivacyFOI