Question

Change datatype from varchar2 to date

Asked by: sikyala

I am trying to change a field from varchar2 to date datatype. However, I keep getting an error that the column must be empty. The table has over 10,000 records.

sample of some data:

01/Mar/1996
01/Feb/1996
01/Jan/1996

Here is the workaround I have been trying.

alter table ntiac_table add

(publication_date_copy date) ;

update ntiac_table
set publication_date_copy=publication_date;

alter table ntiac_table set unused column

publication_date;

alter table ntiac_table drop unused columns;

alter table ntiac_table rename column

publication_date_copy TO publication_date;

I thought this would work because it worked when I changed a datatype of another field from varchar2 to number. However, when I run the above code I get an error after the following code is run:

update ntiac_table
set publication_date_copy=publication_date;


ORA-01841: (full) year must be between -4713 and +999, and not be 0

Could someone please provide a solution

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2005-08-31 at 10:01:38ID21546588
Tags

change

,

date

,

data

Topic

Oracle Database

Participating Experts
5
Points
500
Comments
15

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Using CHAR vs VARCHAR2 datatype
    i have been using CHAR datatype for columns that contain a small domain, for example: {'NEW','OPENED','ACCEPTED','INPROG','COMPETED','APPROVED'} some database designers will opt for VARCHAR2 in this circumstance. i think CHAR is better because it is not really a waste of s...
  2. varchar2
    what is the history of varchar2.I mean why it is varchar2 rather varchar.
  3. Convert LONG datatype to varchar2
    Does any one know easy way of converting LONG datatype to varchar2? I am using following function, but it will not work for a view definition. create or replace function getlong( p_tname in varchar2, p_cname in varchar2, ...
  4. varchar2 to long
    Hi Oracle Gurus, I have the following problem. I have a table ( 500000 rows) in which one of the columns being varchar2(4000). Users wanted to accomodate more than 4000. I thought of changing that column to long. Can you please suggest me what is the best & fastest meth...
  5. VARCHAR2 TO LONG or RAW or....
    Hello, I'm working with Oracle 8.1.7 The problem: SELECT descripcion1||descripcion2||descripcion3 from mytable; Error: ORA-01489: result of string concatenation is too long Of course, the max value for a varchar2 is 4000, here i'm trying to concat 3 varchar2(2000) I have ...
  6. converting long datatype to varchar2
    How do I convert a field of LONG datatype to one of a VARCHAR2 datatype? I have a table in an oracle database that tracks table constraints in an application that I have written. I wish to copy the contents of the SEARCH_CONDITION field of the user_constraints view to a sim...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: SDuttaPosted on 2005-08-31 at 10:21:22ID: 14795785

You should use :

update ntiac_table
set publication_date_copy=to_date(publication_date,'DD/Mon/YYYY');

 

by: sikyalaPosted on 2005-08-31 at 11:19:50ID: 14796283

I tried it and I got the same error. I tried querying the field to find out if there were any fields with non valid value. But no rows were returned. It is possible that some of the values aren't valid. I can't seem to find it. I tried

select pdf_name, publication_date from ntiac_table where publication_date not like '__/___/____';

 

by: Cyber-X-CrossPosted on 2005-08-31 at 11:22:01ID: 14796310

create a temp table and insert into it all of the dates in a query from the table.
after that set all of the varchar column to be null and then u can change it.

 

by: sikyalaPosted on 2005-08-31 at 11:33:48ID: 14796420

I tried that and I got an error because I am trying to change the datatype of over 10,000 rows. The error read something like it couldn't update because I am trying to update more than 1 row.

 

by: SDuttaPosted on 2005-08-31 at 12:24:44ID: 14796868

OK try this :

update ntiac_table a set a.publication_date_copy=
(select to_date(b.publication_date,'DD/Mon/YYYY') from ntiac_table b where a.rowid=b.rowid);

 

by: slightwvPosted on 2005-08-31 at 12:30:21ID: 14796922

See if this will get you where you need to be.  It uses Cybers method of a temp table:
-----------------------------------------------------

drop table tab1;
create table tab1 (
col1 char(1),
col2 varchar2(20)
);


insert into tab1 values('a','01/Mar/1996');
insert into tab1 values('b','01/Feb/1996');
insert into tab1 values('c','fred');
insert into tab1 values('d','01/Jan/1996');
insert into tab1 values('e','barney');
commit;


declare
      junk date;
begin
      for i in (select * from tab1) loop
            begin
                  junk := to_date(i.col2,'DD/MON/YYYY');      
            exception when others then
                  dbms_output.put_line('Bad data: ' || i.col1);
            end;
      end loop;
end;
/

--clean up bad data
delete from tab1 where col1 in ('c','e');
commit;

drop table junk;
create table junk as (select col1, to_date(col2,'DD/MON/YYYY') new_col2 from tab1);

update tab1 set col2 = null;

alter table tab1 modify col2 date;

update tab1 set col2 = (select new_col2 from junk where junk.col1=tab1.col1);
commit;

select * from tab1;

 

by: markgeerPosted on 2005-08-31 at 12:40:00ID: 14797023

There are basically two obstacles you have to get past:
1. Oracle will not allow you to change the data type of a column that has any data in it.
2. Whenever varchar2 columns are used to store "date" information, they almost always get some records with invalid date values, or some values that have a different date format than the rest.

So, you have to make a place to put the existing column values for a while (this can either be another column in this table, or a "temporary" table that contains the primary key value, plus the existing varchar2/date value).  Then you have to find and correct the invalid date values.  The you can alter the current column, and load the valid data back into it with a to_date operator.

 

by: sikyalaPosted on 2005-08-31 at 14:18:43ID: 14797906

SDutta,

I tried your solution:

update ntiac_table a set a.publication_date_copy=
(select to_date(b.publication_date,'DD/Mon/YYYY') from ntiac_table b where a.rowid=b.rowid);

and I got the following error:

ORA-01410: invalid ROWID

 

by: markgeerPosted on 2005-09-01 at 05:55:18ID: 14801427

I don't think that's the first problem you need to solve.  You first have to find and correct the records with invalid date values.  Only after you do that, can you try to load them into a date column.

You need to use a PL\SQL function along with a query that checks every record in the table to find the invalid dates first.  Here is a function you can use for that:

create or replace function bad_date (char_val in varchar2, format_mask in varchar2) return number as
  dummy   date;
begin
  if not (char_val is null) then
    dummy := to_date(char_val,format_mask);
  end if;
  return 0;
exception
  when others then
    return 1;
end;
/

Then use that in a query like this:
select rowid, b.publication_date
from ntiac_table b
where (bad_date,b.publication_date,'DD/Mon/YYYY') = 1;

That will identify the records you need to fix manually first.  After that, you can do your update like this:
update ntiac_table
set publication_date_copy = to_date(publication_date'DD/Mon/YYYY');

 

by: sikyalaPosted on 2005-09-01 at 07:20:12ID: 14802279

markgeer,

When I ran the following code:

select rowid, b.publication_date
from ntiac_table b
where (bad_date,b.publication_date,'DD/Mon/YYYY') = 1;

I got the following error:

ORA-00920 Invalid relational operator

 

by: jimaricatPosted on 2005-09-05 at 22:40:38ID: 14826873

yea thats true markgeer ,

first u need to find out the invalid date formatted records

sikyala , just modify the sql of markgeer to

select rowid, b.publication_date
from ntiac_table b
where bad_date(b.publication_date,'DD/Mon/YYYY') = 1;

or you can update the column like following

if you want to put null date instead of invalid dates

update ntiac_table
set publication_date_copy = decode(bad_date(b.publication_date,'DD/Mon/YYYY'),0,to_date(publication_date'DD/Mon/YYYY'),1,null);

if you want to put current date instead of invalid dates

update ntiac_table
set publication_date_copy = decode(bad_date(b.publication_date,'DD/Mon/YYYY'),0,to_date(publication_date'DD/Mon/YYYY'),1,sysdate);

for all the above said, bad_date function should be there compiled.

 

by: markgeerPosted on 2005-09-07 at 09:50:48ID: 14838335

Did you create the function "bad_date" before you tried to use it in a query?

 

by: sikyalaPosted on 2005-09-07 at 10:52:12ID: 14838743

Sorry that I am just now responding.  Monday was a holiday and the past 2 days I have been in meetings all day and away from my office.

Honestly, when I didn't receive anymore input after I submitted a comment on 9/1/05 I figured out how to find the bad dates and correct them before I got the response dated 9/5/05.

What I did is I created a table in a new database and assigned that field the Date datatype. Then I loaded the data into that table using sqlldr. What it did was reject or not load all the records with bad dates. I looked in the bad data text file and was able to identify several records where the date was 01 Jan 0000. So I corrected the date in the database where I have been working on the data.

Then I ran SDutta code: update ntiac_table
set publication_date_copy=to_date(publication_date,'DD/Mon/YYYY');

and it worked fine.

 

by: slightwvPosted on 2005-09-07 at 11:07:24ID: 14838859

Your soultion was one of the many ways to solve this problem.  SDutta provided the final update after you had clean data.  Both markgeer and myself provided solutions to locate the bad data.  Cyber-X-Cross also gave a probable solution.  All prior to 9-1-05.

Please close out as you see fit.  Might I suggest a 4-way split?

 

by: sikyalaPosted on 2005-09-07 at 13:47:44ID: 14840028

Oh I forgot to answer your question markgeer from 9/7/05 Yes I did create the function before I ran the query.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...