Solved

DATEADD FUNCTION

Posted on 1998-09-16
1
674 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
ID: 1090127
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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

810 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