morteza kavakebi morteza kavakebi - 5 months ago 18
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.

mydate
is
smalldatetime
.

Is it possible?

Answer

try this:

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