Solved

Formatting varchar to numeric

Posted on 2016-08-15
8
76 Views
Last Modified: 2016-08-30
Hi EEE

 I'd like to  change the varchar columns to number in my returned SQL in Oracle. How to? Tried

Select blah format s3
from myTable
0
Comment
Question by:LuckyLucks
8 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 251 total points
ID: 41756973
select to_number(blah) from mytable
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 41757415
if you want to find which ones can't be converted ...

select * from table
where not regexp_like(column, '\d')
0
 
LVL 22

Assisted Solution

by:Helena Marková
Helena Marková earned 83 total points
ID: 41757419
0
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 41761537
Trying to do this: "change the varchar [values] to number in my returned SQL in Oracle" will usually result in this error:
ORA-01722: invalid number

Because, usually data that is in a VARCHAR2 column in Oracle is not all valid numbers, or is not all formatted consistently enough to automatically convert it to a NUMBER datatype.  You will usually need something like the suggestion from Geert Gruwez to identify the records with "bad" values first.  (I'm not an expert with "regular expressions" like what he suggested, but they can be handy.  I usually have to use a PL\SQL function with a "to_number" conversion and an exception section.)  Then, if you manually update any records with non-numeric data to valid, numeric values first, you should be able to run your query with TO_NUMBER.
1
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 32

Assisted Solution

by:awking00
awking00 earned 83 total points
ID: 41767757
If you actually want to change the column data type from varchar2 to number (assuming you have valid data to convert), the process usually goes something like this -
alter table add (newcol number);
update table set newcol = to_number(blah);
update table set blah = null;
update table set blah = newcol;
alter table drop newcol;
0
 
LVL 35

Assisted Solution

by:Mark Geerlings
Mark Geerlings earned 83 total points
ID: 41768671
I think those first two update statements can safely be combined into a single update (but test it!).
Also, before the last update, add a command like this:
alter table [your table name] modify blah number(12,0);

But, be aware that this "alter table..." command will commit your first one (or two) updates.  And the (12,0) is just a guess, since I don't know the range of the actual numeric values that you expect this column to hold.  True, you can simply declare an Oracle column to be a "number" column, with no explicit restriction on the maximum value it can hold, or on how many decimal places can be entered.  Oracle will then allow up to 22 significant digits, either to the right or the left of the decimal point, or a combination totaling 22 digits, with some to the right and some to the left of the decimal point.  I prefer to have the database help enforce a reasonable limit.  That's why I suggested (12,0), but you may know that (6,0) is enough, or if these are actually currency values, you may need something like this instead: (11,2).
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 41768681
awking ... that doesn't change the column type
0
 

Author Comment

by:LuckyLucks
ID: 41777025
thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Identify records which do NOT qualify for a view 9 36
Migrate database to ASM disks. 1 40
File generation using utl_file 4 46
Error executing command from server 6 42
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now