morteza kavakebi morteza kavakebi - 1 year ago 96
SQL Question

SQL Server : concatenate YEAR and MONTH

I have a

group by YEAR(mydate),MONTH(mydate)

I want to select them as a unique field like

SELECT SUM(Price),YEAR(mydate) + '/' + MONTH(mydate)
FROM MyTable
GROUP BY YEAR(mydate), MONTH(mydate)

or something like this.


Is it possible?

Answer Source

try this:

SELECT cast(YEAR(mydate) as CHAR(4))+'/'+cast(MONTH(mydate) as varchar(2))
FROM MyTable
Group by YEAR(mydate),MONTH(mydate)