Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Another DATETIME to DATE conversion question - getting just the DATE to display in Excel Expand / Collapse
Posted Tuesday, December 07, 2010 5:33 PM


Group: General Forum Members
Last Login: Monday, September 09, 2013 8:59 AM
Points: 13, Visits: 25

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)
Post #1031600
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse