Forum Discussion
date convertion (s)
need help converting a date formatted 20230117093739 YYYYMMDDHHMMSS
to 01/17/2023 (in one cell) 9:37:39 AM (in the next cell). i normally use this mid string
Date: Mid([INB_DATE_TIME],5,2) & "/" & Mid([INB_DATE_TIME],7,2) & "/" & Mid([INB_DATE_TIME],1,4)
Time: Mid([INB_DATE_TIME],9,2)&":"&Mid([INB_DATE_TIME],11,2)&":"&Mid([INB_DATE_TIME],13,2)
and the use DATE() to only show the current date. but the query comes up blank (no data) i know i have current days date.
12 Replies
- arnel_gpSteel Contributor
you can also create a custom function and call it from your query:
Public Function fnToDateTime(ByVal var As Variant, ByVal whichPart As String) As Variant ' arnelgp ' ' Note: ' ' whichPart can be "D" (for date) or "T" for time ' Static pvar As Variant Static retDate As Date, retTime As Date Dim d As String, t As String If IsNull(var) Then Exit Function End If If var <> pvar Then d = Left$(var, 8) t = Mid$(var, 9) retDate = DateSerial(Left$(d, 4), Mid$(d, 5, 2), Right$(d, 2)) retTime = #12:00:00 AM# If Len(t) <> 0 Then retTime = CDate(Left$(t, 2) & ":" & Mid$(t, 3, 2) & ":" & Right$(t, 2)) End If pvar = var End If Select Case whichPart Case "D" fnToDateTime = retDate Case "T" fnToDateTime = retTime End Select End Function
on your Query:
select [strDateField], fnToDateTime([strDateField], "D") As Dte, fnToDateTime([strDateField], "T") As Tim
from yourTable;
- kregggowerCopper Contributorarnel_gp,
Thank you for you reply. but i've just starting to look into VB language and it may as well be Hieroglyphics. it is over my head (for now). but thank you very much for your input.- arnel_gpSteel Contributor
Instead of bunch of Mid, you can simply use this.
to Return date:
IIF(IsNull([INB_DATE_TIME]),[INB_DATE_TIME],CDate(Format(Left([INB_DATE_TIME],8),"@@@@-@@-@@")))
To return the Time:
IIF(IsNull([INB_DATE_TIME]),[INB_DATE_TIME],CDate(Format(Right([INB_DATE_TIME],6),"@@:@@:@@")))
- kregggowerCopper Contributor
Hi,
Usually such a "type mismatch" happens when there are empty values in some records. If that's the case in your data then you can try to exclude empty values:
NewDate: IIf(Not IsNull([INB_DATE_TIME]),cdate(Mid([INB_DATE_TIME],5,2) & "/" & Mid([INB_DATE_TIME],7,2) & "/" & Mid([INB_DATE_TIME],1,4)))
Servus
Karl
****************
Access Bug Trackers
Access News
Access DevCon- kregggowerCopper ContributorKarl,
Thank you for the reply The "IIf(Not IsNull([INB_DATE_TIME])" really helped clean up the data in the table Thank You! and is shows the date in the query if i run it with out filters "DATE()" but if i use the "DATE()" or "DATE()+1" in the Criteria field no dates are returned the out put query is blank, like there is no matching dates?
Hi,
With the string functions you create a string. If you want to compare it with a date, you have to convert it to a date:
NewDate: cdate(Mid([INB_DATE_TIME],5,2) & "/" & Mid([INB_DATE_TIME],7,2) & "/" & Mid([INB_DATE_TIME],1,4))
Servus
Karl
****************
Access Bug Trackers
Access News
Access DevCon