Dates not recognized as dates in pivot table pulling directly from SQL Server

16,416

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:
16,416

Related videos on Youtube

Michael K
Author by

Michael K

Updated on September 17, 2022

Comments

  • Michael K
    Michael K over 1 year

    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.

    • Michael K
      Michael K over 13 years
      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
      Michael K over 13 years
      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.
    • Daniel R Hicks
      Daniel R Hicks over 9 years
      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).
  • Michael K
    Michael K over 13 years
    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.