SQL Server: Function to get previous session year
When you need to get previous session year of a supplied session year then you can use the below function.
Step-1 :
Execute below script.
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Suvendu> -- Create date: <13 Mar 2012> -- Description: <returns prev sess yr of the supplied sess yr> -- ============================================= CREATE FUNCTION [dbo].[fuGetPrevSessionYr] ( @SessYr NVARCHAR(7) ) RETURNS NVARCHAR(7) AS BEGIN DECLARE @PrevSessYr NVARCHAR(7) SELECT @PrevSessYr=CAST((CAST(LEFT(@SessYr,4) AS INT)-1) AS VARCHAR(4))+'-'+RIGHT(LEFT(@SessYr,4),2) RETURN @PrevSessYr END
Step-2:
Syntax for calling the function-
SELECT dbo.fuGetPrevSessionYr(<SessionYr>) [FROM TABLE1]
Where <SessionYr> is the session year, whose previous session you want to show.
Posted on March 12, 2012, in Function, SQL Server and tagged SQL Server. Bookmark the permalink. Leave a Comment.

Leave a Comment
Comments (0)