Solved

Formatting varchar to numeric

Posted on 2016-08-15
8
87 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
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 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
 
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
query in Oracle forms Builder 2 53
How can I rollback insert statements after commit in oracle? 7 134
execute immediate plsql block 5 46
constraint check 2 40
Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
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.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

777 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