55

I am trying to add days to the current date and it's working fine but when I add 360 days to the current date it gives me wrong value.

eg: Current Date is 11/04/2014

And I am adding 360 Days to it, it should give me 11/04/2015, but it is showing the same date 11/04/2014. the year is not changing.

Here is my code:

select dateadd(dd,360,getdate())
Dherik
  • 17,757
  • 11
  • 115
  • 164
Fazil Mir
  • 783
  • 2
  • 9
  • 23

10 Answers10

62

Just do-

Select (Getdate()+360) As MyDate

There is no need to use dateadd function for adding or subtracting days from a given date. For adding years, months, hours you need the dateadd function.

Shell
  • 6,818
  • 11
  • 39
  • 70
Shahid Mustafa
  • 661
  • 5
  • 3
  • 14
    Poor code as it is not explicit. dateadd(dd,360,getdate()) makes it more obvious (and you rely on the internal implementation of a datetime), though this may be part of the SQL standard I didn't think it was. – CestLaGalere Oct 06 '15 at 17:52
  • 4
    DateAdd should be used instead of +/- operators on dates. http://stackoverflow.com/a/29696475/354756 – daniloquio Dec 02 '16 at 21:27
38

select dateadd(dd,360,getdate()) will give you correct date as shown below:

2017-09-30 15:40:37.260

I just ran the query and checked:
Please check the attached image

Jeff
  • 12,555
  • 5
  • 33
  • 60
Mahantesh
  • 429
  • 6
  • 13
28
Dateadd(datepart,number,date)

You should use it like this:

select DATEADD(day,360,getdate())

Then you will find the same date but different year.

Dale K
  • 25,246
  • 15
  • 42
  • 71
TheChanger
  • 381
  • 3
  • 3
12

From the SQL Server 2017 official documentation:

SELECT DATEADD(day, 360, GETDATE());

If you would like to remove the time part of the GETDATE function, you can do:

SELECT DATEADD(day, 360, CAST(GETDATE() AS DATE));
Dherik
  • 17,757
  • 11
  • 115
  • 164
2

In SQL Server 2008 and above just do this:

SELECT DATEADD(day, 1, Getdate()) AS DateAdd;
Ling Vu
  • 4,740
  • 5
  • 24
  • 45
1

can try this

select (CONVERT(VARCHAR(10),GETDATE()+360,110)) as Date_Result
Asifuzzaman Redoy
  • 1,773
  • 1
  • 15
  • 30
1

Two or three ways (depends what you want), say we are at Current Date is (in tsql code) -

DECLARE @myCurrentDate datetime = '11Apr2014 10:02:25 AM'

(BTW - did you mean 11April2014 or 04Nov2014 in your original post? hard to tell, as datetime is culture biased. In Israel 11/04/2015 means 11April2014. I know in the USA 11/04/2014 it means 04Nov2014. tommatoes tomatos I guess)

  1. SELECT @myCurrentDate + 360 - by default datetime calculations followed by + (some integer), just add that in days. So you would get 2015-04-06 10:02:25.000 - not exactly what you wanted, but rather just a ball park figure for a close date next year.

  2. SELECT DateADD(DAY, 365, @myCurrentDate) or DateADD(dd, 365, @myCurrentDate) will give you '2015-04-11 10:02:25.000'. These two are syntatic sugar (exacly the same). This is what you wanted, I should think. But it's still wrong, because if the date was a "3 out of 4" year (say DECLARE @myCurrentDate datetime = '11Apr2011 10:02:25 AM') you would get '2012-04-10 10:02:25.000'. because 2012 had 366 days, remember? (29Feb2012 consumes an "extra" day. Almost every fourth year has 29Feb).

  3. So what I think you meant was

    SELECT DateADD(year, 1, @myCurrentDate)
    

    which gives 2015-04-11 10:02:25.000.

  4. or better yet

    SELECT DateADD(year, 1, DateADD(day, DateDiff(day, 0, @myCurrentDate), 0))
    

    which gives you 2015-04-11 00:00:00.000 (because datetime also has time, right?). Subtle, ah?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
LongChalk
  • 783
  • 8
  • 13
1

Add Days in Date in SQL

 DECLARE @NEWDOB DATE=null
 
 SET @NEWDOB= (SELECT DOB, DATEADD(dd,45,DOB)AS NEWDOB FROM tbl_Employees)
Dale K
  • 25,246
  • 15
  • 42
  • 71
Code
  • 679
  • 5
  • 9
1

This will give total number of days including today in the current month.

select day(getDate())
Dale K
  • 25,246
  • 15
  • 42
  • 71
Rinku Choudhary
  • 1,529
  • 1
  • 13
  • 22
-1

SELECT DateAdd(5,day(getdate()) this is for adding 5 days to current days. for eg:today date is 23/08/2018 it became 28/08/2018 by using the above query

Baptiste Mille-Mathias
  • 2,144
  • 4
  • 31
  • 37