Solved

Formatting varchar to numeric

Posted on 2016-08-15
8
101 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 251 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 83 total points
ID: 41757419
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
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 explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

617 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