Solved

Calculate the contractual year period

Posted on 2006-07-18
3
284 Views
Last Modified: 2012-05-05
Hi

I would be grateful if someone could create (or point me in the direction of) a user defined function that takes 4 parameters  (1) fromDD  (2) fromMM  (3) toDD  (4) toMM and dependant on todays date will return two dates (1) fromDate (2) toDate

Example 1:

fromDD = 01
fromMM = 04
toDD = 31
toMM = 03
todays date 18th July 2006

Return values would be

fromDate = 1-Apr-2006
toDate = 31-Mar-2007


Example 2:

fromDD = 01
fromMM = 10
toDD = 30
toMM = 09
todays date 18th July 2006

Return values would be

fromDate = 1-Oct-2005
toDate = 30-Sep-2006


Thanks.

0
Comment
Question by:cubixSoftware
  • 2
3 Comments
 
LVL 25

Expert Comment

by:Mr_Peerapol
ID: 17131384
DECLARE @fromDD CHAR(2)
DECLARE @fromMM CHAR(2)
DECLARE @toDD CHAR(2)
DECLARE @toMM CHAR(2)

DECLARE @curYear INT
SET @curYear = YEAR(GETDATE())

DECLARE @fromDate DATETIME
DECLARE @toDate DATETIME

SET @fromDate = CONVERT(DATETIME, CONVERT(VARCHAR, @curYear)+@fromMM+@fromDD, 112)
SET @toDate = CONVERT(DATETIME, CONVERT(VARCHAR, @curYear)+@toMM+@toDD, 112)

IF (@fromDate > @toDate)
  BEGIN
    SET @toDate = DATEADD(y, 1, @toDate)
  END

-- Here you may return the date as VARCHAR by using CONVERT(VARCHAR, @toDate, XXX). Consult bookonline for XXX to get the format you want

/*
You may need to create 2 user-defined functions - one for getFromDate, the other for getToDate.
GETDATE() cannot be used inside a user-defined fucntion, so you need to pass it to your functions as well.
*/
0
 
LVL 25

Accepted Solution

by:
jrb1 earned 500 total points
ID: 17131405
CREATE PROCEDURE getFromToDates
( @fromDD varchar, @fromMM varchar, @toDD varchar, @toMM varchar
, @fromDate datetime OUT, @toDate datetime OUT)
AS
BEGIN
   @fromDate = @fromMM + "/" + @fromDD + "/" + Year(GetDate())
   if @fromDate > GetDate()
      @fromDate = DateAdd(YEAR, -1, @fromDate)

  @toDate = @toMM + "/" + @toDD + "/" + Year(GetDate())
  if @toDate < GetDate()
     @toDate = DateAdd(YEAR, 1, @toDaet)
END
0
 
LVL 25

Expert Comment

by:jrb1
ID: 17131411
sorry....last toDaet needs to be toDate
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now