Solved

Access - Convert text field to date field

Posted on 2011-03-02
13
675 Views
Last Modified: 2012-05-11
I have been handed an Access database with 1.4 million records.  I didn't even think that was possible.  The date field is text.  Here is an example of a date, 20081001  What that date means is 01/10/2008.  Is there any VBA that will go through the table and turn it into a date field without destroying the current data?

 Any help is appreciated
0
Comment
Question by:JohnMac328
  • 6
  • 3
  • 2
  • +1
13 Comments
 
LVL 28

Expert Comment

by:omgang
ID: 35018943
Create a new date/time field first.  Then you can experiment with data conversion update queries without disturbing the original data.

CDate is a conversion function to convert a string to a date value.  Try it and see.  May need to get a bit creative but it's doable.
OM Gang
0
 
LVL 14

Expert Comment

by:pteranodon72
ID: 35018993
Back your data up.
Create a Date field in the table: NewDate.
Create an Update Query -- in SQL view:

UPDATE yourtablename SET NewDate = Iif(Len(TextDate & "")<>8, Null, DateValue(Left(TextDate,4), Mid(TextDate, 5,2), Right(TextDate,2))

HTH,

pT72
0
 

Author Comment

by:JohnMac328
ID: 35018999
Good suggestion. Due to the number of records, I need to see if someone can come up with the code to loop through and convert ******** to **/**/****
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:JohnMac328
ID: 35019004
I will try that pteranodon72 - did not see your post when I was writing
0
 
LVL 14

Expert Comment

by:pteranodon72
ID: 35019037
Sorry, I may have misread the month, day order, whatever your convention is.

Back your data up.
Create a Date field in the table: NewDate.
Create an Update Query -- in SQL view:

(If it is stored as yyyymmdd)
UPDATE yourtablename SET NewDate = Iif(Len(TextDate & "")<>8, Null, DateValue(Left(TextDate,4), Mid(TextDate, 5,2), Right(TextDate,2))


(If it is stored as yyyyddmm)
UPDATE yourtablename SET NewDate = Iif(Len(TextDate & "")<>8, Null, DateValue(Left(TextDate,4), Right(TextDate,2), Mid(TextDate, 5,2))


(DateValue takes arguments in (year, month, day) order.)
HTH,

pT72
0
 

Author Comment

by:JohnMac328
ID: 35019113
I am getting this error and can't see where the missing paren goes
Example.jpg
0
 

Author Comment

by:JohnMac328
ID: 35019121
Here is my statement

UPDATE DateCorrect2 SET NewDate = Iif(Len(D_DATE & "")<>8, Null, DateValue(Left(D_DATE,4), Mid(D_DATE, 5,2), Right(D_DATE,2))
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 400 total points
ID: 35019383
It is simpler to use Format:

UPDATE
  tblYourTable
SET
  NewDate = IIf(IsDate(Format([TextDate],"!@@@@/@@/@@")),CDate(Format([TextDate],"!@@@@/@@/@@")),Null)

/gustav
0
 

Author Comment

by:JohnMac328
ID: 35019448
cactus - It pops-up asking for the value of "NewDate"    Also here is an example of the original format - no slashes

20081001

0
 
LVL 28

Assisted Solution

by:omgang
omgang earned 100 total points
ID: 35019489
In cactus_data's example, NewDate is the name of the new field you create in your table.
OM Gang
0
 
LVL 28

Expert Comment

by:omgang
ID: 35019500
...and TextDate is the name of the current field in your table that has the dates stored as text.
OM Gang
0
 

Author Closing Comment

by:JohnMac328
ID: 35019550
That worked - thanks all
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 35019657
You are welcome!

/gustav
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

839 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