Date_Time as text

G

Guest

I am pulling data into access and the date time field is stored as a text
field in this format, 200705031200. How do I do a date diff between 2 dates
stored as text? Can I convert them? Or.... ?

Thanks,
Sean
 
F

fredg

I am pulling data into access and the date time field is stored as a text
field in this format, 200705031200. How do I do a date diff between 2 dates
stored as text? Can I convert them? Or.... ?

Thanks,
Sean

I assume the above 20070503 represents May 3, 2007 with the 1200 being
a time value.

DateSerial(Left([Field1],4),Mid([Field1],5,2)Mid([Field1],7,2))

will return a date datatype value of 05/03/2007.

You can use it in DateDiff().

Difference:DateDiff("d",DateSerial(Left([Field1],4), Mid([Field1],5,2)
, Mid([Field1],7,2)), DateSerial(Left([Field2],4), Mid([Field2],5,2),
Mid([Field2],7,2)))
 
J

John Spencer

You should be able to convert the string to a datetime data type using the
following.
CDate(Format([TextField],"@@@@ @@ @@ @@:mad:@"))

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
G

Guest

How do I include the time? I am comparing transactions that can be done on
the same day but only hours apart as where others can be 36 hrs apart.

fredg said:
I am pulling data into access and the date time field is stored as a text
field in this format, 200705031200. How do I do a date diff between 2 dates
stored as text? Can I convert them? Or.... ?

Thanks,
Sean

I assume the above 20070503 represents May 3, 2007 with the 1200 being
a time value.

DateSerial(Left([Field1],4),Mid([Field1],5,2)Mid([Field1],7,2))

will return a date datatype value of 05/03/2007.

You can use it in DateDiff().

Difference:DateDiff("d",DateSerial(Left([Field1],4), Mid([Field1],5,2)
, Mid([Field1],7,2)), DateSerial(Left([Field2],4), Mid([Field2],5,2),
Mid([Field2],7,2)))
 
G

Guest

I get an invalid syntax error, any suggestions?

John Spencer said:
You should be able to convert the string to a datetime data type using the
following.
CDate(Format([TextField],"@@@@ @@ @@ @@:mad:@"))

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..

Sean said:
I am pulling data into access and the date time field is stored as a text
field in this format, 200705031200. How do I do a date diff between 2
dates
stored as text? Can I convert them? Or.... ?

Thanks,
Sean
 
J

John Spencer

Is your text field ever Null? Is your text field ever anything other than
12 number characters? Is your textfield ever anything other than 12
characters long? Those would all generate an error when CDate attempted to
handle them and then the query might generate a too complex error.

You could try the following. IsDate returns true or false if the string can
be interpreted as a date. Then if IsDate believes the string can be
converted, CDate should not generate an error.

IIF(IsDate(Format([TextField],"@@@@ @@ @@
@@:mad:@"))),CDate(Format([TextField],"@@@@ @@ @@ @@:mad:@")),Null)

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..

Sean said:
I get an invalid syntax error, any suggestions?

John Spencer said:
You should be able to convert the string to a datetime data type using
the
following.
CDate(Format([TextField],"@@@@ @@ @@ @@:mad:@"))

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..

Sean said:
I am pulling data into access and the date time field is stored as a
text
field in this format, 200705031200. How do I do a date diff between 2
dates
stored as text? Can I convert them? Or.... ?

Thanks,
Sean
 
G

Guest

Forgot to include,

The field is never null and it is always 12 characters.

John Spencer said:
Is your text field ever Null? Is your text field ever anything other than
12 number characters? Is your textfield ever anything other than 12
characters long? Those would all generate an error when CDate attempted to
handle them and then the query might generate a too complex error.

You could try the following. IsDate returns true or false if the string can
be interpreted as a date. Then if IsDate believes the string can be
converted, CDate should not generate an error.

IIF(IsDate(Format([TextField],"@@@@ @@ @@
@@:mad:@"))),CDate(Format([TextField],"@@@@ @@ @@ @@:mad:@")),Null)

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..

Sean said:
I get an invalid syntax error, any suggestions?

John Spencer said:
You should be able to convert the string to a datetime data type using
the
following.
CDate(Format([TextField],"@@@@ @@ @@ @@:mad:@"))

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..

I am pulling data into access and the date time field is stored as a
text
field in this format, 200705031200. How do I do a date diff between 2
dates
stored as text? Can I convert them? Or.... ?

Thanks,
Sean
 

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

Top