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.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s