Pergunta

Ok so the long and short of it...

have 2008 r2 SQL query that runs in excel and drives a pivot table/chart. Some genius that designed this DB (I mean that literally as they are probably 1000x's smarter than I) has a vital date column listed as 'date' in the properties. So what you say???

Well excel will not allow the pivot chart/table to group by month (that is the short of it). Nothing in excel works, and if I format the cells etc etc... it all reverts upon refreshing that data, etc.. etc... nothing to fix that and can't use macros as my Org doesn't allow it.

SO MY FIX...

Convert 'date' to varchar, then from varchar to datetime... Works great and looks like this:

(CONVERT(varchar(10), [TestDate], 20)) as 'Date1',
(CONVERT(datetime, [TestDate], 20)) as 'Date2',

NOW MY QUESTION...

can anyone help me to combine these two statements into one producing only one column named 'Date'???

It's late, and my brain gave out on me 2 cups of coffee ago... Much appreciation from this newbie..

EDIT: as it is I obviously end up with 2 columns with the 'date1' and 'date2' labels.

Foi útil?

Solução

You need to nest them. You take the contents of Date1 and place them inside Date2 in place of the column name. This will how it works in SQL server

(CONVERT(datetime, (CONVERT(varchar(10), [TestDate], 20)), 20)) as 'Date2'

Outras dicas

If you want to remove the time part the you could avoid these conversions with following solutions:

DECLARE @TestDate DATETIME;
SET     @TestDate = GETDATE();
SELECT  DATEADD(DAY, DATEDIFF(DAY, 0, @TestDate), 0) AS Sol1
SELECT  DATEADD(DAY, DATEDIFF(DAY, {d '1900-01-01'}, @TestDate), {d '1900-01-01'}) AS Sol2

Note: Datetime 0 = 1900-01-01

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top