Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 693
  • Last Modified:

DATEADD FUNCTION

My previous applications runs in access2.0 wherein there is an built-in function "DateSerial"
which has an syntax like : DateSerial(year,month,day)
In my Sql Server I have an function lllr to this called "DateAdd".
Syntax : DateAdd(year,period,getdate())

In my case the month and day fields are 2 different fields which has smallint as datatype.
I have to add some integer values to month and day fields and concatenate with year field. The final result should return me the date in dd/mm/yyyy format.

For Eg. Year - 1998
           Month - 08 (Smallint Datatype)
           Day    - 16  (Smallint Datatype)
       
Add 7 to month field and 42 to Day field.
Taking today's date as "16/08/1998"
The final output should be    "27/03/1999"

I need the entire stuff of sql code ..

Can anyone help me out in this regard.

0
Favourites
Asked:
Favourites
1 Solution
 
formulaCommented:
Here's your answer, which I tested in a SQL window:

/*         For Eg. Year - 1998
           Month - 08 (Smallint Datatype)
           Day    - 16  (Smallint Datatype)  */

declare @initialdate char(12)
declare @day smallint
declare @month smallint
declare @year smallint
declare @finaldate datetime

select @day=16
select @month=8
select @year=1998

select @initialdate = convert(char(2),@month) + '/' + convert(char(2),@day) + '/' + convert(char(4),@year)

select @finaldate=dateadd(month,7,(dateadd(day,42,@initialdate)))

select finaldate=convert(char(12),@finaldate,101)

Note that the returned date is exactly 1 month greater that you wanted it to be.  That's because you added 7 months and 42 days which is equivalent to 8 months plus 12 days, hence the extra month.  If this is not what you want, add 6 months and 42 days.
I also assumed year was also smallint in a separate field.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now