Page 1 of 1

TVF_SessionCodeYearlyMapping

Posted: Thu Mar 02, 2017 11:49 pm
by robinwilson16
Given a year/session and the number of years (can be negative or positive) returns the year/session the specified number of years behind or ahead.
Useful for comparing two years of data

Code: Select all

CREATE FUNCTION [dbo].[TVF_SessionCodeYearlyMapping] (
	@Session VARCHAR(7), 
	@Years int
)
RETURNS TABLE
AS
RETURN

	SELECT
		Value = 
			CAST ( CAST ( LEFT ( @Session, 4 ) AS int ) + @Years AS VARCHAR(4) ) 
			+ '-' 
			+ CAST ( CAST ( RIGHT ( @Session, 2 ) AS int ) + @Years AS VARCHAR(4) )