I've written an SQL function to turn an INT (month value) into a VARCHAR representing the Quarter of the year.
My code is as follows.
ALTER FUNCTION [sf_Quarters] (@DATE date)
WITH EXECUTE AS CALLER
DECLARE @QUARTER VARCHAR(4) = '';
DECLARE @DATEMONTH INT = DATEPART(MONTH, @DATE);
SELECT @QUARTER =
WHEN @DATEMONTH > 3 AND @DATEMONTH < 7 THEN 'Q1'
WHEN @DATEMONTH > 6 AND @DATEMONTH < 10 THEN 'Q2'
WHEN @DATEMONTH > 9 AND @DATEMONTH <= 12 THEN 'Q3'
WHEN @DATEMONTH > 0 AND @DATEMONTH < 4 THEN 'Q4'
DECLARE @DATE DATE = '20090405'
DECLARE @Qat VARCHAR
EXEC @Qat = sf_Quarters @DATE
The problem is in the second part where you are calling the function -
DECLARE @DATE DATE = '20090405' DECLARE @Qat VARCHAR -- change it to VARCHAR(4) --varchar [ ( n | max ) ] --When n is not specified in a data definition or variable declaration statement, the default length is 1 EXEC @Qat = sf_Quarters @DATE SELECT @Qat
Note: Follow the way Gordon suggested to invoke a function