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.
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.Or as @40-Love mentioned you can cast with leading zeroes:
For mariaDB you can:
Link: https://mariadb.com/kb/en/library/date_format/
If you want to stay having the field in datetime datatype, try using this:
It it also easy to change to group by hours, days, weeks, years...
I hope it is of use to someone,
Regards!
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.
To display the date as "month-date" format change the '.' to '-' The full syntax would be something like this.
SQL Server 2012 above, I prefer use format() function, more simplify.