Solved

DATEADD FUNCTION

Posted on 1998-09-16
1
663 Views
Last Modified: 2010-03-19
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
Comment
Question by:Favourites
1 Comment
 
LVL 2

Accepted Solution

by:
formula earned 20 total points
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

772 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

10 Experts available now in Live!

Get 1:1 Help Now