Assuming that the column is integer, since integer division in SQL Server truncates the result, all you need to do is dividing out the lower four digits:
SELECT
datefield/10000 AS Year
, SUM(...) AS ...
FROM MyTable
WHERE ...
GROUP BY datefield/10000
If your column is a char or varchar, use LEFT(datefield,4)
instead.
Note: I assume that you do not own the table, so switching to a more appropriate type (i.e. date
) is not an option. Otherwise, I would strongly advise a switch, because date
is smaller, and it also gives you access to date-specific functionality, such as extracting the day, the month, and the year.