convert yyyyddd to yyyymmdd from and linked table

LTDragon07

New Member
Joined
Mar 13, 2015
Messages
8
I'm querying a linked table and trying to reformat the date to yyyymmdd from yyyyddd
example data
DATA OUTPUT
2015001 2015/01/01
2015125 2015/04/05
what I have now in he query
OUTPUT: Left([DATA],4) & "/" & Right([DATA],3)
2015125 2015/125
which only gets me to so far I don't know how to convert the ddd to mmdd
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Code:
Option Explicit


Sub doit()


    Dim s As String
    s = "2015125"
    
    Dim d As Date
    
    d = DateAdd("y", CLng(Right(s, 3)) - 1, DateSerial(Left(s, 4), 1, 1))
    
    MsgBox d


End Sub
 
Upvote 0
You cant do that, its not a true date, so:
put the code in a module, and
in query call: ConvertStr2Date([DATA])


Code:
'--------
Function ConvertStr2Date(ByVal pvText)
'--------
Dim vDate
Dim iDoy As Integer, iYr As Integer


iYr = Left(pvText, 4)
iDoy = Right(pvText, 3)


vDate = CJulian2Date(iDoy, iYr)
ConvertStr2Date = Format(vDate, "yyyymmdd")
End Function


'--------
Function CJulian2Date(JulDay As Integer, Optional YYYY)
'--------
      If IsMissing(YYYY) Then YYYY = Year(Date)
      If Not IsNumeric(YYYY) Or YYYY \ 1 <> YYYY Or YYYY < 100 Or YYYY _
        > 9999 Then Exit Function
      If JulDay > 0 And JulDay < 366 Or JulDay = 366 And _
      YYYY Mod 4 = 0 And YYYY Mod 100 <> 0 Or YYYY Mod 400 = 0 Then _
             CJulian2Date = DateSerial(YYYY, 1, JulDay)
End Function
 
Upvote 0
You could also simplify to:
Code:
d = DateSerial(Left(s, 4), 1, 1) + Right(s, 3) - 1
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top