Solved

Calculate the contractual year period

Posted on 2006-07-18
3
321 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

687 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