Home » SQL & PL/SQL » SQL & PL/SQL » How to alter the varchar column to number datatype (oracle 11i)
How to alter the varchar column to number datatype [message #638464] Fri, 12 June 2015 04:50 Go to next message
Jothish
Messages: 22
Registered: February 2009
Location: Chennai
Junior Member
Hi Folks,
I need some clarifications.
I have a column which is varchar2 data type and has alpha numeric values. Later point of time i want to change the column to NUMBER datatype. Anyone please let me know what are the ways it can be achieved?

Possible i guess:
Solution 1:
===========
a. We can remove the characters alone from the column and leave the number values as it is. Using TRANSLATE function.
b. Then alter the datatype to NUMBER using ALTERcommand

Solution 2: (consider that emp_id has alphanumeric characters)
===========
a. create another table as with same structure from the original table like below
create table emp_2 as (select translate(emp_id,'1234567890abcdefghijklmnopqrstuvwxyz','1234567890'), emp_name from emp
now emp_id in emp_2 will have only the number values
b. now drop the table emp (original table)
c. Then alter the datatype to NUMBER using ALTER command
d. Then rename the table name to the original table emp using ALTER command

Solution 3:
===========
a. Create view (emp) by pointing the actual table (emp_).
b. Create view emp(emp_id, emp_name) as select emp_id,emp_name from emp_. Now consider that view emp has alpha numeric values.
c. Now Alter the table emp_ by adding new column "emp_id_1" as NUMBER data type.
d. Push all the records to the new column emp_id_1 from emp_id (only numeric values using TRANSLATE function)
e. Now re create the view
create or replace view emp(emp_id, emp_name) as select emp_id_1, emp_name from emp_
f. Finally emp view will have emp_id as the numeric values alone with NUMBER data type.


These are the possible ways to achieve this. Can anyone know if any other possible ways to achieve this?


Answers appreciated,

Thanks,
Jothish. G









Re: How to alter the varchar column to number datatype [message #638469 is a reply to message #638464] Fri, 12 June 2015 05:56 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/
Re: How to alter the varchar column to number datatype [message #638470 is a reply to message #638464] Fri, 12 June 2015 05:56 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Your posting history shows that you lack the courtesy to respond to those who heklp you with a basic 'Thanks for your help'. Good luck with your question.
Re: How to alter the varchar column to number datatype [message #638472 is a reply to message #638464] Fri, 12 June 2015 06:07 Go to previous messageGo to next message
John Watson
Messages: 9003
Registered: January 2010
Location: Global Village
Senior Member
Your solutions 1 and 2 will not work because you can't change datatype if column is populated.
Your solution 3 is theoretically possible, but will involve massive downtime and will mess up your constraints.

There is a far superior solution that will change the table definition online (zero downtime) with just two procedure calls.

[Updated on: Fri, 12 June 2015 06:12]

Report message to a moderator

Re: How to alter the varchar column to number datatype [message #638474 is a reply to message #638464] Fri, 12 June 2015 06:14 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
As john said, solution 1 and 2 is not possible as Oracle would throw
ORA-01439: column to be modified must be empty to change datatype


If you are not bothered about the downtime, then you could add a new column as NUMBER data type, update it with required data, drop old column, rename new column with old column name.
Re: How to alter the varchar column to number datatype [message #638475 is a reply to message #638464] Fri, 12 June 2015 06:43 Go to previous messageGo to next message
EdStevens
Messages: 1377
Registered: September 2013
Senior Member
So, if you have a varchar value of 'a123b789', what number do you want that to be? 123789? What would be the meaning/business value of such a number?
Re: How to alter the varchar column to number datatype [message #638478 is a reply to message #638475] Fri, 12 June 2015 06:48 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
EdStevens wrote on Fri, 12 June 2015 04:43
So, if you have a varchar value of 'a123b789', what number do you want that to be? 123789? What would be the meaning/business value of such a number?



No Business reason; just silly homework assignment.
Re: How to alter the varchar column to number datatype [message #638502 is a reply to message #638478] Fri, 12 June 2015 09:51 Go to previous messageGo to next message
Jothish
Messages: 22
Registered: February 2009
Location: Chennai
Junior Member
BlackSwan ,
If you don't find any answer/solution Just Shut off.
Re: How to alter the varchar column to number datatype [message #638503 is a reply to message #638470] Fri, 12 June 2015 09:54 Go to previous messageGo to next message
Jothish
Messages: 22
Registered: February 2009
Location: Chennai
Junior Member
pablole/EdStevens
If you don't find any answer Just shut off.
Re: How to alter the varchar column to number datatype [message #638504 is a reply to message #638503] Fri, 12 June 2015 09:55 Go to previous messageGo to next message
gazzag
Messages: 1119
Registered: November 2010
Location: Bedwas, UK
Senior Member
Sadly I think most of us will now "shut off" after that Shocked Good luck.
Re: How to alter the varchar column to number datatype [message #638505 is a reply to message #638503] Fri, 12 June 2015 09:57 Go to previous messageGo to next message
Jothish
Messages: 22
Registered: February 2009
Location: Chennai
Junior Member
Lalit Kumar B,
Thanks. yes Solution :3 can be used to create the procedure or function. Any other option please let me know
Re: How to alter the varchar column to number datatype [message #638506 is a reply to message #638472] Fri, 12 June 2015 10:01 Go to previous messageGo to next message
Jothish
Messages: 22
Registered: February 2009
Location: Chennai
Junior Member
Lalit Kumar B,
Without downtime u can do this activity using editioning the view using solution 3.
Re: How to alter the varchar column to number datatype [message #638508 is a reply to message #638504] Fri, 12 June 2015 10:15 Go to previous messageGo to next message
John Watson
Messages: 9003
Registered: January 2010
Location: Global Village
Senior Member
It is a pity that online redefinition cannot be applied to people as well as to tables. That should be enough of a hint - I'm shutting off.
Re: How to alter the varchar column to number datatype [message #638510 is a reply to message #638506] Fri, 12 June 2015 10:52 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Jothish wrote on Fri, 12 June 2015 17:01
Lalit Kumar B,
Without downtime u can do this activity using editioning the view using solution 3.


Unfortunately U is dead.

[Updated on: Fri, 12 June 2015 10:52]

Report message to a moderator

Re: How to alter the varchar column to number datatype [message #638511 is a reply to message #638508] Fri, 12 June 2015 10:59 Go to previous messageGo to next message
Jothish
Messages: 22
Registered: February 2009
Location: Chennai
Junior Member
John Watson ,
Thanks please shut off. that will be good for the blog.. Thank god.
Re: How to alter the varchar column to number datatype [message #638512 is a reply to message #638511] Fri, 12 June 2015 11:17 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

As you have a very scornful behaviour, never feedback to nor thank people who helped you or answered their questions (posted to help you) I think it would be good for the forum you leave it and never come back.

[Updated on: Fri, 12 June 2015 11:18]

Report message to a moderator

Re: How to alter the varchar column to number datatype [message #638513 is a reply to message #638512] Fri, 12 June 2015 11:35 Go to previous messageGo to next message
Jothish
Messages: 22
Registered: February 2009
Location: Chennai
Junior Member
Yes i already thanked people who really helped.. I have replied to those who doesn't have professional ethics for your information.
Re: How to alter the varchar column to number datatype [message #638514 is a reply to message #638513] Fri, 12 June 2015 11:46 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

I don't see you thank or feedback to John in this topic.
I don't see you answer or feedback or thank in this topic.
I don't see you feedback or thank in this topic.
I don't see you feedback or thank in this topic.
I don't see you feedback or thank in this topic.
Now I have covered all your topics where did you thank?
What is your ethic?

[Updated on: Fri, 12 June 2015 11:46]

Report message to a moderator

Re: How to alter the varchar column to number datatype [message #638515 is a reply to message #638514] Fri, 12 June 2015 12:02 Go to previous messageGo to next message
Jothish
Messages: 22
Registered: February 2009
Location: Chennai
Junior Member
Can u pls have a look. You are not able to see these conversations?
[message #638505 is a reply to message #638503]
[message #638506 is a reply to message #638472]
If
Re: How to alter the varchar column to number datatype [message #638516 is a reply to message #638515] Fri, 12 June 2015 12:16 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Yes, but I bet you thank Lalit just because the other ones put the point on your faults.
And I notice you first didn't answer to Ed's question.
And this does not change the fact of your scornful behaviour in your previous topics.
What is this ethic you are referring to? The one which says we have to help you and once you can then ignore us?

So FIRST review ALL your previous topics.
Then apologize for your bad behaviour.

Also do not use IM/SMS speak, this is unprofessional and a mark of disrespect towards those you are talking to.

Re: How to alter the varchar column to number datatype [message #638517 is a reply to message #638516] Fri, 12 June 2015 12:56 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Michel Cadot wrote on Fri, 12 June 2015 22:46

but I bet you thank Lalit just because the other ones put the point on your faults.


Lol Smile Oh! Come on Michel, I don't need the "thanks". And of course, I put my point about OP's fault:

Lalit Kumar B wrote on Fri, 12 June 2015 16:44
solution 1 and 2 is not possible as Oracle would throw
ORA-01439: column to be modified must be empty to change datatype


By the way, OP should keep distance from the keyboard. Period!

[Updated on: Fri, 12 June 2015 13:04]

Report message to a moderator

Re: How to alter the varchar column to number datatype [message #638518 is a reply to message #638517] Fri, 12 June 2015 13:14 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

It was not about you Lalit (I never meant you want thanks), it was just about OP (who never thank anyone until we pointed him on this) and I did not talk about technical faults but behaviour ones.

Re: How to alter the varchar column to number datatype [message #638530 is a reply to message #638503] Sat, 13 June 2015 08:07 Go to previous messageGo to next message
EdStevens
Messages: 1377
Registered: September 2013
Senior Member
Jothish wrote on Fri, 12 June 2015 09:54
pablole/EdStevens
If you don't find any answer Just shut off.


Most of us are more interested in helping people find optimal solutions to real-world problems. When someone wants to know how to implement some mis-guided technique, we try to uncover the real problem first. Quite often the best solution is NOT the mis-guided technique that is first presented. If someone is driving past a sign that says 'bridge out ahead', I am NOT going to fulfill their request to make their car go faster.

Given your attitude in this and your other threads, I will turn YOU off.

<PLONK>
Re: How to alter the varchar column to number datatype [message #638531 is a reply to message #638530] Sat, 13 June 2015 14:38 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
+1
Re: How to alter the varchar column to number datatype [message #638554 is a reply to message #638464] Mon, 15 June 2015 02:45 Go to previous messageGo to next message
Jothish
Messages: 22
Registered: February 2009
Location: Chennai
Junior Member
Thanks for who helped to solve the issue.



Re: How to alter the varchar column to number datatype [message #638556 is a reply to message #638503] Mon, 15 June 2015 03:02 Go to previous message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Jothish wrote on Fri, 12 June 2015 15:54
pablole/EdStevens
If you don't find any answer Just shut off.

Ahhh, always good to get the confirmation/justification for my opinion of someone. Cheers.
Previous Topic: Interview Question
Next Topic: Read a string char by char and compare
Goto Forum:
  


Current Time: Fri Aug 21 07:22:33 CDT 2026