Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert text datatype "20101021" to date yyyymmdd 1

Status
Not open for further replies.

cdgeer

IS-IT--Management
Apr 8, 2008
133
US
Could someone please show how I would convert a text string such as "20101021" to a date format and datatype such as 10/21/2010 ? I've been toying with some VB code but not successful. Thanks for any help.
 


hi,
Code:
YourRealDate = DateSerial(Left[YourField],4),Mid[YourField],5,2),Right[YourField],4))
Dates are pure numbers that can be FORMATTED to DISPLAY in a number of different formats at report time, using the Format function.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Ahhh, You have to separate the yyyy, mm, dd . Thanks so much!!
 

Or you could do this:
Code:
Dim strADate As String
Dim datMyDate As Date

strADate = "20101021"

datMyDate = CDate(Mid(strADate, 5, 2) & "/" & Right(strADate, 2) & "/" & Left(strADate, 4))

MsgBox datMyDate

But I would go with Skip's code.....

Have fun.

---- Andy
 
I'm running into a snag. I'm getting a datatype mismatch. I looked up DateSerial() since I wasn't familiar with it. It said that DateSerial expects an integer. My "YourField" as Skip called it, is a text datatype. Would that be the problem? If so, how can I convert to integer?
The 20101021 is actually part of a table name like:
"20101021_All_Personnel" so I changed skip's statement slightly to:
YourRealDate = DateSerial(Left([YourField],4),Mid([YourField],5,2),Mid([YourField],7,2))
 


well you specifically asked, "how I would convert a text string such as "20101021" to a date format?"

Glad you figured it out on your own.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top