I have a different variant of the common question relating to conversion of DateTime variables in Excel to "Date Only."
I want to create a view in Excel 2008 that will be used by Excel users via MSQuery. Several of the fields in the view are DateTime values. The users also need to select/filter the query results by these DateTime fields.
So, there are issues with all of the "standard" approaches to this issue:
1. If I just include the DateTime field in the view, when it gets to Excel, it is displayed as Datetime (with the Time shown). The users then have to format the column as mm/dd/yyyy date format. Now this isn't the worst thing, but they use this view frequently, so they have to format the 4 date columns every single time they create a new spreadsheet.
2. If I use CONVERT, e.g.
Convert(nvarchar(10),j.[Creation Date],101) AS [Creation Date],
Then the result is converted to string, and you can't do a select/filter, e.g. you can't filter on "Creation Date" > 11/15/2010 - you will get all dates that begin with "11" as well as "12" regardless of year, e.g. you'll get dates like "12/1/2009" in the result set.
3. If I use CAST to type DATE, e.g.
CAST(j.[Creation Date] AS Date) As [Creation Date],
Then the user CAN select/filter correctly (the correct result set is returned). However, in Excel, the field is returned as string in format YYYY-MM-DD, e.g. 2010-12-15 and you can't set a format in Excel to change it to mm/dd/yyyy.
So, to Summarize:
1. I have fields of type DateTime in my view
2. Users have to be able to select in MSQuery based on a date range e.g. > 12/7/2010
3. I want the date to display in Excel as 12/07/2010 without having to reformat the column every time I create a new spreadsheet/query.
Is this possible?
Thanks in advance
dateadd(dd,datediff(dd, 0, getdate()), 0)