3

This is perhaps a very simple question.

I have a very basic query in Excel that pulls in some values from a db on Sql Server 2008 R2.

The date fields in this db are stored as datetime. In this query, I am uninterested in the time part, and so only want to select the date. I have tried using Cast or Convert, however both return the date in sql (yyyy-mm-dd) to Excel. Excel recognizes this as text rather than a date, and so I am unable to do date filters in my table once the data is in Excel.

If I do datepart() in Excel, it shows it as a date and I can filter as expected, however I want the data coming directly from the query to return as a date value that Excel can filter on.

Below is my sample query:

SELECT order_header.oh_id, 
order_header.oh_order_number, 
order_header.oh_datetime, 
convert(date, order_header.oh_datetime, 103) as 'ConvertOrderDate',
cast(order_header.oh_datetime as date) as 'CastOrderDate'
FROM order_header
WHERE order_header.oh_datetime >= '2013-09-01'

When returning this to Excel, this is what is being returned:

Output Image http://img834.imageshack.us/img834/4417/7gbg.png

2
  • 1
    where you convert/cast to date try changing it to datetime Commented Sep 23, 2013 at 13:54
  • 1
    I hate Excel's date recognition: it seems to always guess wrong. If I want text or numeric, it will think it's a date. If I want a date, it won't see it. Commented Sep 23, 2013 at 15:19

1 Answer 1

1

I have actually found the answer without doing anything weird :)

SELECT order_header.oh_id, 
order_header.oh_order_number, 
order_header.oh_datetime, 
DATEADD(dd, DATEDIFF(dd, 0, order_header.oh_datetime), 0) as 'OrderDate'
FROM order_header
WHERE order_header.oh_datetime >= '2011-09-01'

This returns as a datetime but with 00:00 in the time, so I can simply format this out in Excel.

Sign up to request clarification or add additional context in comments.

1 Comment

Not all Cultures can 'eat' your dashed time. '20110901' a little better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.