Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Formatting varchar to numeric

Posted on 2016-08-15
8
Medium Priority
?
129 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 74

Accepted Solution

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

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 332 total points
ID: 41757419
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 332 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 332 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 38

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

688 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