Solved

Access - Convert text field to date field

Posted on 2011-03-02
13
668 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

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

Expert Comment

by:pteranodon72
Comment Utility
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
Comment Utility
I am getting this error and can't see where the missing paren goes
Example.jpg
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:JohnMac328
Comment Utility
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
Comment Utility
It is simpler to use Format:

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

/gustav
0
 

Author Comment

by:JohnMac328
Comment Utility
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
Comment Utility
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
Comment Utility
...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
Comment Utility
That worked - thanks all
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
You are welcome!

/gustav
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Many companies are making the switch from Microsoft to Google Apps (https://www.google.com/work/apps/business/). Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now