Solved

Calculate the contractual year period

Posted on 2006-07-18
3
293 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

919 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