Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

My pivot pulls from an external data source with a date column. Excel doesn't see this column as a date and the 'Format Cells' option panel doesn't change how the dates are displayed. The cell data is left-aligned, suggesting a string rather than a date. I have tried cast(myvar as date) and convert(varchar, myvar, 101) and convert(varchar, myvar, 1) in the base table, but none of these have been picked up by Excel as dates.

If the column is recognized as a date, I can group by week and month. I understand that if I can't fix this, the next step is to add columns with weeks and months for each date to the table, but I'd like to give formatting the column one more shot before doing that.

share|improve this question
    
I should also note that I tried using Text-To-Columns, but I get the error that I can't move parts of the Pivot Table inside the pivot report. –  Michael K Feb 3 '11 at 19:21
    
Changing the various cast methods does alter the string that excel shows in the pivot but does not change whether or not excel sees that cell as a date. –  Michael K Feb 3 '11 at 19:48
    
Understand that an Excel date is implemented as a floating-point number whose integer portion is the number of days since some epoch and the fraction is the fraction of 24 hours represented by the time. I'm thinking there's some sort of "interpret date" function so you could read the string date into a hidden cell and then apply the function to it to get an Excel date (but I've never tried that). –  Daniel R Hicks Nov 28 '14 at 13:32

3 Answers 3

Try casting your date to datetime.

SQL Server's date is internally represented as a 3 byte integer whereas datetime is 8 bytes and presumably floating point. Excel uses a double to represent dates/times so SQL Server's datetime format might map across better than date.

share|improve this answer
    
Thanks-- I tried this and also casting as an int - 2, but wasn't able to get excel to recognize the dates. I seem to have identified the problem: this pivot has dates on rows, and thus the content are seen as row labels (strings) rather than values (more interesting data types). In fact, it seems like any of the various castings will be picked up by excel as long as the dates are not on row or column labels. –  Michael K Feb 4 '11 at 14:11
1  
This worked for me in Excel 2010 querying SQL Server 2008. After making this change, the values came in as the numeric Excel serialized date and I was then able to format / filter as date. –  N Jones Sep 24 '13 at 15:31

Try using SQL code as the source for your pivot table, as opposed to the table or query located in SQL Server. Maybe your Cast() statements will be more effective that way.

  1. Click on the pivot table
  2. Click PivotTable tools>Options>Change Data Source(arrow)>Connection Properties
  3. Click Definition
  4. Change Commang Type to 'SQL Query'
  5. Paste or type your SQL statement into the command text area.

I'm assuming you're using Excel 2007. Excel 2003 would be a similar approach, just different way of getting to it.

share|improve this answer
    
This is a fantastic suggestion but we're actually trying to avoid Excel-side execution of the SQL because the procedure is massive. –  Michael K Feb 3 '11 at 19:18

Use the Convert function with the 120 style, this is the ISO date format, Excel should recognise this.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.