Solved

Access - Convert text field to date field

Posted on 2011-03-02
13
676 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
[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
  • 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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 50

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 50

Expert Comment

by:Gustav Brock
ID: 35019657
You are welcome!

/gustav
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

735 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