107

I'm not sure what should I write in the following SQL query to show 'date' column like this: "month-year" - "9-2011".

SELECT MONTH(date) + '.' + YEAR(date) AS Mjesec, SUM(marketingExpense) AS SumaMarketing, SUM(revenue) AS SumaZarada 
FROM [Order]
WHERE (idCustomer = 1) AND (date BETWEEN '2001-11-3' AND '2011-11-3')
GROUP BY MONTH(date), YEAR(date)

So, what I want to do is to change the data from the first column to show month and year instead of showing month only.

4
  • This question is somewhat confusing--are you trying to SORT by date? Commented Mar 23, 2011 at 13:47
  • I dont understand this: "I would like to show the first column as I want to differently format the first column so it would be unique". Commented Mar 23, 2011 at 13:51
  • @JNK - Microsoft SQL @Chris - No, I'm not trying to sort by date, I just want to change the results from the first column to show month and year instead of showing just month. Commented Mar 23, 2011 at 13:53
  • 1
    Don't you need to group first by year, then by month? And can you show some sample output, please? Commented Mar 23, 2011 at 14:01

10 Answers 10

201
SELECT CAST(MONTH(date) AS VARCHAR(2)) + '-' + CAST(YEAR(date) AS VARCHAR(4)) AS Mjesec, SUM(marketingExpense) AS SumaMarketing, SUM(revenue) AS SumaZarada 
FROM [Order]
WHERE (idCustomer = 1) AND (date BETWEEN '2001-11-3' AND '2011-11-3')
GROUP BY CAST(MONTH(date) AS VARCHAR(2)) + '-' + CAST(YEAR(date) AS VARCHAR(4))

Or as @40-Love mentioned you can cast with leading zeroes:

GROUP BY 
  CAST(YEAR(date) AS VARCHAR(4)) + '-' + right('00' + CAST(MONTH(date) AS VARCHAR(2)), 2) 
1
  • 2
    it's useless for sorting by date with this method
    – madd
    Commented Dec 10, 2020 at 15:17
26

I'm guessing this is MS SQL, as it looks like MS SQL syntax.

You should put the same thing in the group by as you have in the select.

For example:

Select MONTH(date)+'-'+YEAR(date), ....
...
...
...
group by MONTH(date)+'-'+YEAR(date)
3
  • 5
    MONTH(date) returns an int. It will need some cast -ing. Commented Mar 23, 2011 at 13:53
  • 1
    true .. should be casted as varchar, though my point was the fact that you can put anything in the select as long as the same thing is in the group part. Theoretically the select statement is valid from the sql point of view. Commented Mar 23, 2011 at 13:58
  • 1
    no in MS SQL this will not return a string, but what I wrote was for the demonstration point of view of grouping. Commented Mar 23, 2011 at 14:00
24

SQL Server 2012 above, I prefer use format() function, more simplify.

SELECT format(date,'MM.yyyy') AS Mjesec, SUM(marketingExpense) AS SumaMarketing, SUM(revenue) AS SumaZarada 
FROM [Order]
WHERE (idCustomer = 1) AND (date BETWEEN '2001-11-3' AND '2011-11-3')
GROUP BY format(date,'MM.yyyy')
1
  • 2
    I had to use DATE_FORMAT(date, '%m.%Y')
    – CTS_AE
    Commented Aug 29, 2019 at 23:00
14

You can try multiplication to adjust the year and month so they will be one number. This, from my tests, runs much faster than format(date,'yyyy.MM'). I prefer having the year before month for sorting purpose. Code created from MS SQL Server Express Version 12.0.

SELECT (YEAR(Date) * 100) + MONTH(Date) AS yyyyMM
FROM [Order]
...
GROUP BY (YEAR(Date) * 100) + MONTH(Date)
ORDER BY yyyyMM
2
  • 2
    I was just reading this and about to make the same comment. It's very efficient Commented Aug 28, 2020 at 16:33
  • 1
    Tried all of the answers, this one is the fastest. Should have more upvotes.
    – Akli
    Commented Nov 16, 2022 at 3:20
10
Yet another alternative: 

Select FORMAT(date,'MM.yy')
...
...
group by FORMAT(date,'MM.yy')
1
7

If I understand correctly. In order to group your results as requested, your Group By clause needs to have the same expression as your select statement.

GROUP BY MONTH(date) + '.' + YEAR(date)

To display the date as "month-date" format change the '.' to '-' The full syntax would be something like this.

SELECT MONTH(date) + '-' + YEAR(date) AS Mjesec, SUM(marketingExpense) AS
SumaMarketing, SUM(revenue) AS SumaZarada 
FROM [Order]
WHERE (idCustomer = 1) AND (date BETWEEN '2001-11-3' AND '2011-11-3')
GROUP BY MONTH(date) + '.' + YEAR(date)
1
  • 4
    Guessing you never tried running this code because it can't possibly work?
    – NickG
    Commented Apr 6, 2018 at 15:49
6

For mariaDB you can:

SELECT DATE_FORMAT(date, '%m-%Y')
FROM [Order]
GROUP BY 
DATE_FORMAT(date, '%m-%Y')

Link: https://mariadb.com/kb/en/library/date_format/

2

In postgresql I can write a similar query with a date-format function (to_char) and grouping just by date:

SELECT to_char (datum, 'MM-YYYY') AS mjesec 
FROM test 
GROUP BY datum 
ORDER BY datum;

Such thing is surely possible with SQL-Server too, isn't it?

0

Years later, it still was a problem for me. Didn't find a solution that worked well for me for pre 2012 SQL Server in this forum, so I propose this workaround, which also works with the ORDER BY clause:

SELECT 
  CONVERT(DATE, CAST(DAY(0) AS NVARCHAR) + '/' + 
    CAST(MONTH(GETDATE()) AS NVARCHAR) + '/' + 
    CAST(YEAR(GETDATE()) AS NVARCHAR), 105)

Which yields:

2022-08-01

Obviously, replace the GETDATE() with the relevant field. Hope it's some use to someone.

-6

If you want to stay having the field in datetime datatype, try using this:

SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, o.[date]), 0) AS Mjesec, SUM(marketingExpense) AS SumaMarketing, SUM(revenue) AS SumaZarada 
FROM [Order] o
WHERE (idCustomer = 1) AND (o.[date] BETWEEN '2001-11-3' AND '2011-11-3')
GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, o.[date]), 0)

It it also easy to change to group by hours, days, weeks, years...
I hope it is of use to someone,

Regards!

1
  • Your statement opens 3 brackets and closes 1 and datediff requires 3 arguments. This cannot work.
    – gus27
    Commented May 23, 2016 at 8:01

Your Answer

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

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