Pass a variable string in To_Date Oracle function in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass a variable string in To_Date Oracle function in shell script
# 1  
Old 03-20-2014
Pass a variable string in To_Date Oracle function in shell script

Hello,

I am trying to execute an SQL query from shell script.

A part of script is something like this:
Code:
fromDate=`echo $(date +"%F%T") | sed "s/-//g" | sed "s/://g"`

$ORACLE_HOME/sqlplus -s /nolog <<EOD1
connect $COSDBUID/$COSDBPWD@$COSDBSID
spool $SCMSORDERREPORT_HOME/SCMS_Order_Report.csv;
Select column1 From Table1 Where creationDate > ('$fromDate', 'YYYYMMDDHH24MISS')

spool off;
exit
EOD1

Here, I have initialized a variable fromDate in the beginning of the script, and I am trying to pass it to the SQL query. Is this the right way or I need to change the way how it is passed to query? I am not getting anything in the spooled file and I suspect there is something wrong with the way the parameter has been passed.
Please help!

Last edited by sanketpatel.86; 03-20-2014 at 01:56 PM.. Reason: Adding more details
# 2  
Old 03-20-2014
No need to use those echo and sed, you can simply do:
Code:
fromDate=$(date +%Y%m%d%H%M%S)

or simply do it in the SQL itself using sysdate instead.
# 3  
Old 03-20-2014
Thanks for the reply!

Sorry for the less information. I am actually doing some more arithmetic with the fromDate and not simply passing the current timestamp.

I am just trying to get an idea whether I can pass the variable like the way I have done in the query.

Code:
creationDate > ('$fromDate', 'YYYYMMDDHH24MISS')

Will this be the correct way to pass my initialized variable?
# 4  
Old 03-20-2014
Whenever in doubt, replace mydbms with cat.

It then becomes the simple question -- does this query look like what I expected?

Code:
cat <<EOD1
connect $COSDBUID/$COSDBPWD@$COSDBSID
spool $SCMSORDERREPORT_HOME/SCMS_Order_Report.csv;
Select column1 From Table1 Where creationDate > ('$(date +%Y%m%d%H%M%S)', 'YYYYMMDDHH24MISS')

spool off;
exit
EOD1

# 5  
Old 03-20-2014
Thanks a lot! This helped me to verify what was wrong.
This User Gave Thanks to sanketpatel.86 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass and read an array in ksh shell script function.?

I'm able to read & print an array in varaible called "filelist" I need to pass this array variable to a function called verify() and then read and loop through the passed array inside the function. Unfortunately it does not print the entire array from inside the funstion's loop. #/bin/ksh... (5 Replies)
Discussion started by: mohtashims
5 Replies

2. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

3. Homework & Coursework Questions

How to Dynamically Pass Parameter to plsql Function & Capture its Output Value in a Shell Variable?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 2. Relevant commands, code, scripts, algorithms: #! /bin/ksh v="ORG_ID" ... (2 Replies)
Discussion started by: sujitdas2104
2 Replies

4. Shell Programming and Scripting

How to pass Oracle sql script as argument to UNIX shell script?

Hi all, $ echo $SHELL /bin/bash Requirement - How to pass oracle sql script as argument to unix shell script? $ ./output.sh users.sql Below are the shell scripts and the oracle sql file in the same folder. Shell Script $ cat output.sh #!/bin/bash .... (7 Replies)
Discussion started by: a1_win
7 Replies

5. Shell Programming and Scripting

How to pass an array to a function in shell script.?

hi, I have a array say SAP_ARRAY="s1.txt" SAP_ARRAY="s2.txt" how can i pass this full array to a function. here is the sample code i am using.. CHECK_NO_FILES() { FARRAY=$1 echo "FARRAY = $FARRAY" echo "FARRAY = $FARRAY" ............... (5 Replies)
Discussion started by: Little
5 Replies

6. Programming

Oracle Database: INSERT INTO with TO_DATE function

Hello, I am writting a procedure in shell script as, set serveroutput on declare date_gen DATE := $DATEGEN; begin INSERT INTO TBL1 ( EMPNAME,EMPID,EMPBDATE) VALUES($EMPNAME,$EMPID,TO_DATE(date_gen,'YYYY-MM-DD')); end; / Where DATEGEN is unix string variable which I need to use into... (10 Replies)
Discussion started by: Poonamol
10 Replies

7. Shell Programming and Scripting

How to pass parameter to User defined function in shell script?

Hello, Can anyone guide me tin passing parameters into user defined function of shell script (KSH). Here is my code, InsertRecord() { DB_TBL=$(sqlplus $USERID/$PASSWORD@$DATABASE << EOF set head off set feed off set serveroutput on INSERT INTO TBL1 ( OLD_VAL, NEW_VAL, ... (7 Replies)
Discussion started by: Poonamol
7 Replies

8. Shell Programming and Scripting

What is the maximum number of parameter we can pass to a shell script function?

what is the maximum number of parameter we can pass to a shell script function (8 Replies)
Discussion started by: alokjyotibal
8 Replies

9. UNIX for Dummies Questions & Answers

How to pass a oracle variable back to the shell script

Hi, I am calling an oracle function that returns a number (either 0 or 2), how do I pass that pass to the wrapping shell script as I would like to do other things based on the value returned by the oracle function. Your help will be appreciated. -------------------------- sqlplus / <<... (3 Replies)
Discussion started by: Jtrinh
3 Replies

10. Shell Programming and Scripting

How to pass arguments to a function in a shell script?

Hi, I have two shell variables $t1 and $t2 which I need to pass to a function in a shell script. The function will do some computation with those two variables and echo the resultant. But I do not know how to pass teh arguments. The function written is f1() {...... ........ } What should... (3 Replies)
Discussion started by: preetikate
3 Replies
Login or Register to Ask a Question