Convert text date to date/time

N

Nico

The date and time is stored as text, for example, as "Thursday, May 22, 2008
4:54 PM", but I need to query from it so I need it in a recognizable
date/time format.

Any advice on how to convert it?

Thanks!
 
J

Jerry Whittle

CDate is the function that you want. It will convert text string that can be
evaluated as a valid date/time into an actual date/time.

The problem is that CDate doesn't play well with non-valid dates. Therefore
I usually check with IsDate first and handle bogus dates. Something like
below will convert valid dates and put in a bogus 1/1/1950 for non-valid date
strings or nulls.

SELECT IIf(IsDate([date])=True,CDate([date]),#1/1/1950#) AS NotDate
FROM tblDatesText;

However your dates have a problem:
Debug.Print IsDate("Thursday, May 22, 2008 4:54 PM") = False

Whereas
Debug.Print IsDate("May 22, 2008 4:54 PM") = True

So you might need to strip out the day of the week. Assuming that all of the
records have one, something like below would work:

Mid("Thursday, May 22, 2008 4:54 PM",Instr("Thursday, May 22, 2008 4:54 PM",
" ")+1)

Therefore it might take something like:

SELECT IIf(IsDate(Mid([date],Instr([date], " ")+1))=True,
CDate(Mid([date],Instr([date], " ")+1)),
#1/1/1950#) AS NotDate
FROM tblDatesText;
 
M

Michel Walsh

If you can remove the name of the day, use CDate:


? CDate( "May 22, 2008 4:54 PM")
2008.05.22 16:54:00


(note that my format is ISO 24h, but any US based OS should be able to
recognize your American date, 12h, AM/PM format)


To eliminate the day name, either do it at the source, probably the faster
way, either spot the first coma:

InStr( yourString, ",")


and use

CDate (MID( yourString, 1+ InStr(yourString, "," ) ))


Hoping it may help,
Vanderghast, Access MVP
 
N

Nico

This worked perfectly, thanks so much for your help!

Jerry Whittle said:
CDate is the function that you want. It will convert text string that can be
evaluated as a valid date/time into an actual date/time.

The problem is that CDate doesn't play well with non-valid dates. Therefore
I usually check with IsDate first and handle bogus dates. Something like
below will convert valid dates and put in a bogus 1/1/1950 for non-valid date
strings or nulls.

SELECT IIf(IsDate([date])=True,CDate([date]),#1/1/1950#) AS NotDate
FROM tblDatesText;

However your dates have a problem:
Debug.Print IsDate("Thursday, May 22, 2008 4:54 PM") = False

Whereas
Debug.Print IsDate("May 22, 2008 4:54 PM") = True

So you might need to strip out the day of the week. Assuming that all of the
records have one, something like below would work:

Mid("Thursday, May 22, 2008 4:54 PM",Instr("Thursday, May 22, 2008 4:54 PM",
" ")+1)

Therefore it might take something like:

SELECT IIf(IsDate(Mid([date],Instr([date], " ")+1))=True,
CDate(Mid([date],Instr([date], " ")+1)),
#1/1/1950#) AS NotDate
FROM tblDatesText;
--
Jerry Whittle, Microsoft Access MVP
Light. Strong. Cheap. Pick two. Keith Bontrager - Bicycle Builder.

Nico said:
The date and time is stored as text, for example, as "Thursday, May 22, 2008
4:54 PM", but I need to query from it so I need it in a recognizable
date/time format.

Any advice on how to convert it?

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Windows 7 I need to convert date to text 3
Text to Date 4
working with time date in a query 4
Convert GPS Date and Time 2
Convert Date 1
Convert String to Date 1
Is there a Date Time Conversion from text? 2
How Convert Date to Text 7

Top