Link to home
Start Free TrialLog in
Avatar of endurance
enduranceFlag for United States of America

asked on

Inverse of a SQL Function

I'm trying to find a way to create a formula to produce the inverse of this function. When writing the answer can you also write down what happens at each step? I tried to break down the formula to see what happens when we do (@StartQtr/10*10) since it looks like a redundant statement but I couldnt see the answer.

GO

/****** Object:  UserDefinedFunction [dbo].[FutureQtr_fn]    Script Date: 08/12/2016 11:52:56 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- This function takes a qtr (yyyyq), and adds the number of qtrs, and returns the future qtr
--e.g. FutureQtr (20154,7) returns 20173
CREATE FUNCTION [dbo].[FutureQtr_fn] (@StartQtr int,@AddlQtrs int)

Returns int
AS BEGIN
RETURN
      (@StartQtr/10*10)+ -- Current Year (yyyy)
            ((@StartQtr%(@StartQtr/10*10) +@AddlQtrs - 1)/4)*10 -- add'l years (n0)
            +(@StartQtr%(@StartQtr/10*10) +@AddlQtrs-1)%4  + 1 -- new qtr (n)
END

GO

Open in new window


Thanks
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

What do you mean by "inverse"?  Subtract qtrs?
Avatar of endurance

ASKER

Yes
SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that doesn't really work the way I need. I'll be creating a new function call it dbo.PastQtr_fn and when I write select dbo.PastQtr_fn(20154,7) I should get 20141.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so much.