This is my query.
select CONVERT(varchar, cast(date as datetime), 3)
from shoptransfer
group by year (date)
I want to group by the year part of the date (varchar) column, however I get the following error:
Column 'shoptransfer.Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
How do I group by the year part of the date column?
You should add column
shoptransfer.Date
into group by clause.I don't know about T-SQL, but in SQL in general, what is in the group by clause must exactly match each non-aggregate function column in the select clause. Try
also,
where SUBSTRING(productcode, 5, 3) like '%'
is not filtering out much - maybe remove it.If you want to select both the date and the year you should not use a GROUP BY clause as it combines all of the rows with similar years into one row. If you want all of the dates with similar years together you can use an ORDER BY:
OR
How about:
Or:
This is based on OP's command: "I want to group by year part of date (varchar) column"
year 2004 2005 2006