Formatting Date Variable (Pls help)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Formatting Date Variable (Pls help)
# 1  
Old 02-19-2008
Formatting Date Variable (Pls help)

I have a situation where I am writing a shell script that will accept a date value it will then pass this date value to an Oracle stored procedure for processing.

I want to format the date into (01-SEP-08) before passing to the proc. I also want to make sure the value passed in is a date value if its not then i need to display an error message i.e. Invalid Date.

I am new to this language and in language's like Java etc this would be a 1 liner just call a function pass in the date along with the format you want.
Is this achievable with a Shell Script?

Here is my code...

#!/bin/ksh

DATE=$1

if [ -z "$DATE" ] #I first check for null and format sysdate which is no problem
then
DATE=`date '+%d-%b-%y`
echo $DATE
else
#This is where I want to format the arg variable how can I do that?
echo $DATE
fi

Please help...
# 2  
Old 02-19-2008
Try:
Code:
#!/bin/ksh

DATE=$1

if [[ -z "$DATE" ]] #I first check for null and format sysdate which is no problem
then
	DATE=`date +%d-%b-%y"`
	echo $DATE
else
	#This is where I want to format the arg variable how can I do that?
	DATE=$(echo "$1" | tr -s '[:lower:]' '[:upper:]')	
	echo $DATE
	# test validity of date given
	echo "
      set pages 0
      set feedback off
      select 'OK' 
      from 
        dual
      where 
        to_date($(printf "'%s'" $DATE),'DD-MON-YYYY') > to_date('01-JAN-1000','DD-MON-YYYY');
      " | sqlplus -s jmcnama/555tgb | read okay
      if [[ "$okay" = "OK" ]] ; then
           echo "good date"
      else
           echo "bad date"
      fi          
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in formatting the date taken from a variable

Hi, I am having the below data in input file. The file contains multiple such lines. The file is comma delimited. AAA,M,CCCCCC,EE,DD,FF,GG,1187.00000,01-MAY-05 BBB,M,CCCCCC,EE,DD,FF,GG,87.00000,10-MAY-05 I need to create below output file out of it- <tag1>AAA</tag1>... (3 Replies)
Discussion started by: Arjun_CV
3 Replies

2. UNIX for Dummies Questions & Answers

Need Date Formatting help

Hi, How can i store the date + time from the output of the ls command in loop in a variable date1? -rw-rw---- 1 user1 admin 500002 Jan 2 21:24 P002607.cssI then want to convert Jan 2 21:24 to this date format 2014-01-02 21:24:00 and save it in date2 variable. Then i would like to add... (1 Reply)
Discussion started by: mohtashims
1 Replies

3. Programming

sort data by date.. pls help

hi all, could anyone help me? I need to query output by compare dates from 2 table and i'm using a UNION query..and wanted to sort the output by date.. My query like this: SELECT TO_CHAR(DATE) DATE1, INVOICE FROM ACCOUNT1 WHERE DATE < (to_date('122003','MMYYYY')) UNION ... (2 Replies)
Discussion started by: kate katherine
2 Replies

4. Shell Programming and Scripting

Formatting date

Hi all Y=`date +'%Y'` M=`date +'%m'` D=`date +'%d'` if && ;then yesterday=$Y$M`expr $D + 30` echo $yesterday else if && ; then yesterday=$Y$M`expr $D + 29` echo $yesterday else if ; then yesterday=$Y$M`expr $D + 27` echo $yesterday else yesterday=$Y$M`expr $D - 1` echo... (8 Replies)
Discussion started by: ultimatix
8 Replies

5. Shell Programming and Scripting

Formatting a date

Hi, the date value retrieved by a parameter from the table is of the format dd/mm/yyyy. please let me know how to convert this to YYYYMMDD using sed thanks (4 Replies)
Discussion started by: swasid
4 Replies

6. OS X (Apple)

Date Formatting, etc.

Hi - I'm using GeekTool to customize my desktop in OS X 10.5.8 I'm a complete novice as far as UNIX commands, just know enough to be dangerous. I have a command entered as a Shell to display my events from iCal: This makes my events show something like this: While this is... (1 Reply)
Discussion started by: patricksprague
1 Replies

7. Shell Programming and Scripting

date formatting

Hi i need to have the date in the format like dd-mon-yyyy my script goes like this #!/usr/bin/bash for f in /space/can /home/lbs/current/externalcdrbackup/L_CDR_Configuration/1/200903122* ; do awk '{sum++;}END{for(i in sum) {print d,h,m,i, sum}}' "d=$(date +'%m-%d-%Y')" "h=$(date +'%H')"... (8 Replies)
Discussion started by: aemunathan
8 Replies

8. UNIX for Dummies Questions & Answers

Date formatting

Running bash how do I input the date in the command line like 3/20/90 and get an output formmated like March, 20 1990. (8 Replies)
Discussion started by: knc9233
8 Replies

9. Shell Programming and Scripting

date formatting

Date format MM/DD/YYYY required is YYYYMMDD, I tried using sed but could not get it any help please. (4 Replies)
Discussion started by: mgirinath
4 Replies

10. Shell Programming and Scripting

Formatting date

i need date in the following format December 14, 2005. With date +"%b %d, %Y" command i am getting the following output :- Dec 14, 2005. can anyone pls tell me how to get the full month name (2 Replies)
Discussion started by: radhika03
2 Replies
Login or Register to Ask a Question