?
Solved

Calculate the contractual year period

Posted on 2006-07-18
3
Medium Priority
?
334 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 1500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

649 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