Date Format MMDDYYYY to YYYYMMDD in access table

Hi , Can some one advise with the a query to modify the date in a column from MMDDYYYY TO YYYYMMDD in MS access table, sometimes I receive date with MDDYYYY for the months 01,02 etc. I have tried with below logic.


Right([DATE1],4) & "" & Left([DATE1],2) & "" & Mid([DATE1],4,2) - This is working for 01232017. But is there any other logic to update date for both types.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee W, MVPTechnology and Business Process AdvisorCommented:
Take the date parts - Month(), Year(), Day()

Function ReverseDate(UseDate)		'Formats date into "YYYY-MM-DD"
	If IsDate(UseDate) = False Then
		ReverseDate = "ReverseDate: INVALID DATE SPECIFIED"
		ReverseDate = Year(UseDate) & "-" 
		If Month(UseDate) < 10 Then ReverseDate = ReverseDate & "0" & Month(UseDate) & "-" Else ReverseDate = ReverseDate & Month(UseDate) & "-"
		If Day(UseDate) < 10 Then ReverseDate = ReverseDate & "0" & Day(UseDate) Else ReverseDate = ReverseDate & Day(UseDate)
	End If
End Function

Open in new window

If you don't want the - between Year/month and month/day, take out the & "-"
NorieAnalyst Assistant Commented:
What data type is the field/column?
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
>>But is there any other logic to update date for both types.

you got to first check the string length and the determine which logic to be used. apparently you would need 2 different logics to handle 2 different format of string.

try use a combination of IIF and Len functions
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Gustav BrockCIOCommented:
A one-liner will do as an expression:

YMD: Right([DATE1], 4) & Left(Right("0" & [DATE1], 8), 2) & Mid(Right("0" & [DATE1], 8), 3, 2)

Open in new window

rnkothaAuthor Commented:
I want the DATE1 column should be updated at the time of import only. Date1 data type is TEXT and trying to import it from excel file.
Gustav BrockCIOCommented:
Then just use the expression above.
rnkothaAuthor Commented:
Hi , Someone provide the detail query similar to below to change the field. I tried with below query to modify the Date field but there is no luck.  

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "Table", Targetlocation & "\File.xlsx", True

strsql  = "UPDATE Table SET Table.Date = Right([Table.Date],4), IIF(len[Table.Date]<7,&0& Left([Table.Date,1), Left([Table.Date,2]), & Mid([Table.Date],3,2)"

CurrentDb.Execute strsql
Gustav BrockCIOCommented:
Use an expression that works:

strsql  = _
    "UPDATE Table " & _
    "SET Table.Date = Right([Date], 4) & Left(Right("0" & [Date], 8), 2) & Mid(Right("0" & [Date], 8), 3, 2)"

Open in new window

Lee W, MVPTechnology and Business Process AdvisorCommented:
Of course, if you created the function I recommended, it would just be
strsql  = "UPDATE Table SET Table.Date = '" & ReverseDate(Date()) & "'

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rnkothaAuthor Commented:
Thanks all for your help, It is working fine.
Gustav BrockCIOCommented:
Great. Then please mark as answered.
rnkothaAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.