How to format sql result as amount - ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to format sql result as amount - ksh
# 1  
Old 04-12-2010
How to format sql result as amount - ksh

I am currently returning an sql result with a number value that I want to format as an amount. The sql runs smoothly on its own, but when run inside my ksh script an error is encountered:

ERROR at line 3:
ORA-01481: invalid number format model

My sql is --
Code:
select distinct
    to_char(amount,'$9,999,999,999.00')
from    account;

The amount field in the account table is already defined as a number. Is there a specific way for me to return this value with the $9,999,999,999.00 format?

Thanks in advance. Smilie
# 2  
Old 04-12-2010
Hi,

The problem is becuase of the $9,999,999,999. When you run this query in sql, it will work fine. When you run this from shell, shell interprets $9,999,999,999(since there is a $ in it) as a variable name and tries to replace the value in it.

Remove the $ in the to_char and run it, and you can prepend the $ later in the output.

Thanks
Guru.
# 3  
Old 04-12-2010
How do you call the SQL? Do you echo to sqlplus, or do you use a here-doc?
# 4  
Old 04-12-2010
Quote:
Originally Posted by guruprasadpr
Hi,

The problem is becuase of the $9,999,999,999. When you run this query in sql, it will work fine. When you run this from shell, shell interprets $9,999,999,999(since there is a $ in it) as a variable name and tries to replace the value in it.

Remove the $ in the to_char and run it, and you can prepend the $ later in the output.

Thanks
Guru.
Thanks for your reply. Does this mean that ksh doesn't interpret the $ symbol?
# 5  
Old 04-12-2010
Hi
ksh does interpret the $ symbol. That is why by the time sql query gets executed, the $9 gets replaced with nothing and hence the error.

In order to overcome, use your query like this:

select distinct
to_char(amount,'9,999,999,999.00')
from account;

This will return the amount without the $ symbol. You can prepend the $ symbol to the amount later in the shell, once the sql query is executed.

Thanks
Guru.
# 6  
Old 04-12-2010
Or, if you pass the SQL proper, you can get the dollar sign without post-processing. Eg:
Code:
sqlplus user/pass@sid << 'EOF'
select distinct
    to_char(amount,'$9,999,999,999.00')
from    account;
EOF

will not expand $9, but instead will pass the whole text as is.
# 7  
Old 04-12-2010
Quote:
Originally Posted by pludi
Or, if you pass the SQL proper, you can get the dollar sign without post-processing. Eg:
Code:
sqlplus user/pass@sid << 'EOF'
select distinct
    to_char(amount,'$9,999,999,999.00')
from    account;
EOF

will not expand $9, but instead will pass the whole text as is.
I did your suggestion but I still encountered the error. I was able to work around it by taking out the $ sign. Thanks! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Using AWK to extract SQL result

I just wish to extract the result onli... but I dont which edit to perform to start from (**) :wall: Current display result SQL*Plus: Release 10.2.0.1.0 - Production on Mon Sep 24 14:14:49 2012 Copyright (c) 1982, 2005, Oracle. All rights reserved. Connected to: Oracle Database 10g... (4 Replies)
Discussion started by: ment0smintz
4 Replies

2. Shell Programming and Scripting

Shifting result of echo by specific amount

I am using echo "HELLO" I want to specify a number shiftWt so that I move hello forward by shiftWt charcaters. Is there a way to do this? (2 Replies)
Discussion started by: kristinu
2 Replies

3. UNIX for Advanced & Expert Users

Output the SQL Query result to a File

Hello Guys, This message is somewhat relates with last thread. But I need to re-write thing. I start over a little. I am stuck now and need your help. Here is my script- #! /bin/ksh export ORACLE_HOME=/opt/oracle/app/oracle/product/9.2 /opt/oracle/app/oracle/product/9.2/bin/sqlplus -s... (5 Replies)
Discussion started by: thepurple
5 Replies

4. Shell Programming and Scripting

How to format a character to look like an amount -solved

Hi All, I want to format the retrieved varchar2 value in the database to $9,999,999,990.00 but remained as a character value. Is it possible? Example: 378273.23 to $378,273.23 below is the code i want to implement: sqlplus -s ${USERNAME}/${PASSWORD}@${ORACLE_SID} << END1 >> $LOGFILE... (0 Replies)
Discussion started by: ryukishin_17
0 Replies

5. Shell Programming and Scripting

How to Format the result driven from a SQL Query

Hi All, I want to format the result driven from the query into neat format. For example pls find the below code, #! /bin/sh result=' sqlplus -s uname/passwrd@DBname select no,name,address,ph_no, passport_no,salary,designation from emp_table where salary>1000; exit EOF' ... (8 Replies)
Discussion started by: little_wonder
8 Replies

6. UNIX for Dummies Questions & Answers

Grabbing result of sql command

Hi guys, Is there a way a script can run an SQL statement and dump the results into a variable which can then be used later in the script? Thanks. (3 Replies)
Discussion started by: hern14
3 Replies

7. Shell Programming and Scripting

formatting the sql select result

Hi, I have about 12 columns and 15 rows to be retrived from sybase isql command through unix. But when i output the sql into a file and see it, the formatting is going for a toss. can someone please suggest how can i get the result correctly in the output file ? Thanks, Sateesh (2 Replies)
Discussion started by: kotasateesh
2 Replies

8. Shell Programming and Scripting

reply amount to another format

hello, i have a txt file called a.txt with million of records...I would like to change amount field (last two field)..if it displayed ".00" changed to "0.00" and displayed ".51" changed to "0.51" example of a.txt RECORD ID,CLIENT,CUSTOMER NAME,2883,12:05,8, 16.39, 191.34 RECORD... (10 Replies)
Discussion started by: happyv
10 Replies

9. Shell Programming and Scripting

any possible to run sql result and output to file

Hi, I search all post...and no soluation about..if i would like to run a sql statement and output the result to txt file. for example, i usually run "sql" to logon the database and run select statement. Then I need to copy the output into the result.txt. Can I run the script to do this... (7 Replies)
Discussion started by: happyv
7 Replies

10. Shell Programming and Scripting

how to retrieve sql result to unix....

Hi, i would like to retrieve seql result and write it into unix text file like "result.txt" In unix, normally, I type "sql" and get into sql,then type "select....." to run and get the result....then copy and paste into result.txt any possible way to write a script to run it automatically?... (3 Replies)
Discussion started by: happyv
3 Replies
Login or Register to Ask a Question