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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text to Format as Date? 1

Status
Not open for further replies.

supportsvc

Technical User
Jan 24, 2018
249
US
There's a field with 6 characters in which the last 4 are the month and date (mmdd)
Looks like this for instance, today, the batch number is 000809

I need to convert that into a date so I've attempt the following:
Format(Right([Batch Number], 4), "mm/dd/" & "2018" in order to achieve 08/09/2018
However it is producing incorrect dates based on the Batch Number

So then I've tried in 2 steps
First Right([Batch Number],4) as InvoiceDate
Then Format([InvoiceDate], "mm/dd/" & "2018")

This produces the same incorrect dates.

Example: It should produce 01/07/2018

SalesOrderNo ImprintID InvoiceNo Batch Number InvoiceDate Expr1
A525894 93890372 A54490 00107 [highlight #FCE94F]0107[/highlight] 04/16/2018
 
You have provided 2 examples of Batch Number: [tt]000809[/tt] and [tt]0107[/tt]

So try this:

Code:
Dim strBNo As String
Dim datMyDate As Date

strBNo = "000809"
datMyDate = CDate(Left(Right(strBNo, 4), 2) & "/" & Right(strBNo, 2) & "/2018")

or

Code:
Dim strBNo As String
Dim datMyDate As Date

strBNo = "0107"
datMyDate = DateSerial("2018", Left(Right(strBNo, 4), 2), Right(strBNo, 2))

BTW - I am not crazy about hard-coding [tt]2018[/tt]. What will happen next year?

---- Andy

There is a great need for a sarcasm font.
 
Perfect! Thank you very much!

This works as hoping!

Code:
 CDate(Left(Right(Batch Number, 4), 2) & "/" & Right(BatchNumber, 2) & "/2018")
 
You are welcome :)
Both of these examples should work for you. I would tend to use the DateSerial myself, but that's a personal preference.


---- Andy

There is a great need for a sarcasm font.
 
I agree with Andy. The idea of hard coding the year is going to become problematic very quickly.

In the string originally provided, the last 4 digits are 2018. If that year will always correspond with the date of interest it would be a simple matter of using & Right(completestring,5) (including the /) in place of the & "/2018" Andy provided. If it does not correspond, perhaps you can use & "/" & Right(completestring,4) along with some calculation and either -1 or +1 as necessary to create the correct year.

Good luck.
 
Thank you. Fortunately, this is a one-time task.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top