convert a short text yyyymmdd in dd-mm-yyyy (1 Viewer)

killerflappy

Registered User.
Local time
Today, 14:13
Joined
Aug 23, 2017
Messages
50
Hi guys,

I have a short text field with a date (yyyymmdd) and want to convert it to a short text field as dd-mm-yyyy. So 20171128 must be converted to 28-11-2017.

I can do this with the CDATE, but then it converts it to a date, and I want it to be a short text field.

I want to use an update query in VBA to do this. Can someone point me in the right direction?
 
Last edited:

plog

Banishment Pending
Local time
Today, 07:13
Joined
May 11, 2011
Messages
11,653
I want to use an update query in VBA to do this

Just caught the last part of that. Why?

It's a date correct? Then store it as a date, no reason to store this thing as the wrong data type. Store it correctly.
 

Cronk

Registered User.
Local time
Today, 22:13
Joined
Jul 4, 2013
Messages
2,772
@killerflappy

You can reformat using left, right and mid functions.

However, your example
20172811
is not consistent with
yyyymmdd
 

static

Registered User.
Local time
Today, 13:13
Joined
Nov 2, 2015
Messages
823
In a standard module

Code:
Public Function xdate(dt As String) As String
    If Len(dt) = 8 Then
        s = Left(dt, 4) & " " & Mid(dt, 5, 2) & " " & MonthName(CLng(Right(dt, 2)))
        If IsDate(s) Then xdate = Format(CDate(s), "dd-mm-yyyy")
    End If
End Function

e.g.

Code:
Debug.Print xdate("20172811")
Code:
update table1 set mydate=xdate(mydate)
 

MarkK

bit cruncher
Local time
Today, 05:13
Joined
Mar 17, 2004
Messages
8,186
Hey, one little trick you can do if you have a string like 20171128 is Format() it so it is a valid date, which saves you all the Left(), Mid(), Right() string extraction work, like...
Code:
Public Function xdate(ds As String) As String
[COLOR="Green"]    'format the cryptic string into a valid date format[/COLOR]
    ds = Format(ds, "0000-00-00")
[COLOR="green"]    'if the date is still valid, convert and reformat it[/COLOR]
    If IsDate(ds) Then xdate = Format(CDate(ds), "dd-mm-yyyy")
End Function
Mark
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:13
Joined
Feb 19, 2002
Messages
43,346
What is the point of storing a date as a string? Do you realize that if you store a date as a string, it acts like a string in comparisons and sorting rather than acting like a date.

01-01-2017 is LESS than 01-02-2016 -- is that the behavior you are looking for?
 

killerflappy

Registered User.
Local time
Today, 14:13
Joined
Aug 23, 2017
Messages
50
Just caught the last part of that. Why?

It's a date correct? Then store it as a date, no reason to store this thing as the wrong data type. Store it correctly.

To clearify. It's used as an import table. I receive the date format froma customer. Make changes to the import table and then proces it to a combined table. When I change it to a date field, the next import failed.
 

sonic8

AWF VIP
Local time
Today, 14:13
Joined
Oct 27, 2015
Messages
998
I receive the date format froma customer. Make changes to the import table and then proces it to a combined table. When I change it to a date field, the next import failed.
There is a little trick to make it work anyway.

  • Create an Import Specification with the date order YMD and a random date separator. Check four digit years and leading zeroes in dates.
  • Go to the Navigation Options (in the context menu of the Navigation Pane) and enable "Show System Objects".
  • Open the MSysIMEXSpecs table, find your import spec, and delete whatever there is in the column DateDelim.
  • Now run the import with that spec and Access will recognize the yyyymmdd dates from your import table as dates.
A word of warning: If you open the import spec details from the import wizard it will automatically add a date separator again. So, you need to repeat the above steps whenever you change anything in the spec via the UI.

By chance, I just very recently published a video tutorial on text file import and and an extensive text on date and time in Access/VBA. The do not address this very specific problem (yet), but they might still be worth your time to take look.
 
Last edited:

killerflappy

Registered User.
Local time
Today, 14:13
Joined
Aug 23, 2017
Messages
50
Thanks for putting me in the right direction you guys!

The SQL below does work and still seems to be short tekst.

UPDATE tblImport SET tblImport.date = Right([date],2) & '-' & Mid([date],5,2) & '-' & Left([date],4);

When I was testing the column one time changed into a date field.
How can I be sure that the column for date stays as short text?
 
Last edited:

sonic8

AWF VIP
Local time
Today, 14:13
Joined
Oct 27, 2015
Messages
998
When I was testing the column one time changed into a date field.
How can I be sure that the column for date stays as short text?
In Access databases table columns do not just randomly change their data type. Unless you explicitly change the table design, either manually or by code, it will stay the way it is.
 

isladogs

MVP / VIP
Local time
Today, 13:13
Joined
Jan 14, 2017
Messages
18,246
Unless you specify the datatypes in your import spec, Access will look at the first few records in the import & use them to determine the datatype.

So with new data, it may see one or more fields as a different datatype ...
...if you allow it to do so.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:13
Joined
Feb 19, 2002
Messages
43,346
Open the MSysIMEXSpecs table, find your import spec, and delete whatever there is in the column DateDelim.
Nice trick but unnecessary. The ImportSpec wizard allows you to do this directly. Just press the Advanced button. That gives you access to the arguments for defining a date. you can specify ymd, dmy, or mdy as the order, specify 2 or 4 digit years, and specify a separator or no separator.

To call up an existing Import/Export spec, start the import/export manually. make the changes to the spec and save it. Then close the wizard. There is no need t complete the process and actually import or export data.

PS - it is best to not use tricks when there is a legitimate method available.
 

sonic8

AWF VIP
Local time
Today, 14:13
Joined
Oct 27, 2015
Messages
998
The ImportSpec wizard allows you to [...] specify a separator or no separator.
Pat, thank you for the correction. My memory failed me there. Of course you can specify no separator at all, even though the UI creates the impression of an undeletable blank due to the input mask on that control.

What does not work, is to actually enter a blank as date separator, for it is removed automatically in general by the Access UI. If you need the blank, you have to update the MSysIMEXSpecs table with SQL and then not touch the spec in the UI again. - I guess, I mixed that up with the no-separator situation.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:13
Joined
Feb 19, 2002
Messages
43,346
What does not work, is to actually enter a blank as date separator
That's because "blank" is something. What you want is nothing and that means you need to remove (delete) the default value.
 

sonic8

AWF VIP
Local time
Today, 14:13
Joined
Oct 27, 2015
Messages
998
That's because "blank" is something. What you want is nothing and that means you need to remove (delete) the default value.
Yes, of course. That was not related to the particular problem in this thread but a general remark about limitations of the ImEx-Spec-UI.
 

Users who are viewing this thread

Top Bottom