Solved

Formatting varchar to numeric

Posted on 2016-08-15
8
67 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 36

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 34

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 31

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 34

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 36

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.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Oracle 12c 10 100
Add 0 to end of Number 21 72
performance tunning sql insert - challenging one 2 32
null value 15 67
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…
Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
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…

758 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

24 Experts available now in Live!

Get 1:1 Help Now