Excel VBA SQL Query - Format Date Output

dave981

New Member
Joined
Jun 25, 2008
Messages
12
I'm using a SQL query in VBA to select and join data spread across two worksheets and then outputting the results to a third worksheet. One of the fields is an Excel Date.

On the source worksheet the value is: 9/25/2008 21:05
On the output worksheet the result is: 38996.402222

I would like the output to also be formatted: MM/DD/YYYY HH:MM

I read a reference to "DateAdd" somewhere... So i tried the following, but I do not receive any results.
Code:
stSQL = "SELECT [Worksheet1$].[Last Updated Date], DateAdd([Worksheet1$].[Last Updated Date], -2, 39083)  FROM [Worksheet1$]"
Thoughts/ guidance?

I would prefer to perform the formatting as part of the SQL query, rather than trying to capture the result during output. For now i just write out the entire table of results to the output sheet.

Thanks,
Dave
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi, formatting is not part of the SQL language per se, and its best to not try to use it for that.

Is this return value a date/time or the difference between two dates/times? If the former, format the output sheet for dates, if the latter format the output sheet for an integer or decimal value. You can "finish" your vba code with formatting statements that work on the excel sheet where you have the output data. Your "number" looks like a date. Excel is telling me its Oct 6, 2006 at 9:39 AM...

If you have further questions paste a little more of your code. Actually, I'd be interested in seeing that anyway.

--AB
 
Upvote 0
NateO - The CDate() function worked for me. It would be nice to be able to pull through the time too - but date meets 90% of my needs.

Thanks!
Dave
 
Upvote 0
Good call Nate. I guess I expected DateAdd to return a date already.
 
Upvote 0

Forum statistics

Threads
1,215,473
Messages
6,125,018
Members
449,203
Latest member
tungnmqn90

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