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.

DATE1
1232017
01232017

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.
rnkothaAsked:
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"
	Else
		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 & "-"
0
NorieVBA ExpertCommented:
What data type is the field/column?
0
Ryan ChongCommented:
>>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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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

2
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.
0
Gustav BrockCIOCommented:
Then just use the expression above.
0
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
0
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

0
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()) & "'
0

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.
0
Gustav BrockCIOCommented:
Great. Then please mark as answered.
0
rnkothaAuthor Commented:
Thanks
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.