Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


add 10 years to getdate


add 10 years to getdate

Author
Message
river1
river1
SSC Eights!
SSC Eights! (950 reputation)SSC Eights! (950 reputation)SSC Eights! (950 reputation)SSC Eights! (950 reputation)SSC Eights! (950 reputation)SSC Eights! (950 reputation)SSC Eights! (950 reputation)SSC Eights! (950 reputation)

Group: General Forum Members
Points: 950 Visits: 1350
Hi,

I want to add 10 years to the current date.

select getdate()

I made:

select getdate() +10 but it does not work.

Thank you
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24157 Visits: 37929
river1 (6/24/2014)
Hi,

I want to add 10 years to the current date.

select getdate()

I made:

select getdate() +10 but it does not work.

Thank you



Try:
dateadd(year,10,getdate())

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Mr. Kapsicum
Mr. Kapsicum
Right there with Babe
Right there with Babe (739 reputation)Right there with Babe (739 reputation)Right there with Babe (739 reputation)Right there with Babe (739 reputation)Right there with Babe (739 reputation)Right there with Babe (739 reputation)Right there with Babe (739 reputation)Right there with Babe (739 reputation)

Group: General Forum Members
Points: 739 Visits: 968
SELECT DATEADD(YEAR,10,GETDATE()), This will add 10 Years to the current date.
For ref: http://msdn.microsoft.com/en-IN/library/ms186819.aspx


Moreover, the Query you provided I.e SELECT GETDATE()+10 will add 10 Days to current date.
Can anyone please explain this.?
Thanks in advance.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16525 Visits: 16992
Mr. Kapsicum (6/24/2014)

Moreover, the Query you provided I.e SELECT GETDATE()+10 will add 10 Days to current date.
Can anyone please explain this.?
Thanks in advance.


You discovered one of those (un)documented features. When doing math against a datetime value it will use days. The correct way is to use DATEADD.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
river1
river1
SSC Eights!
SSC Eights! (950 reputation)SSC Eights! (950 reputation)SSC Eights! (950 reputation)SSC Eights! (950 reputation)SSC Eights! (950 reputation)SSC Eights! (950 reputation)SSC Eights! (950 reputation)SSC Eights! (950 reputation)

Group: General Forum Members
Points: 950 Visits: 1350
thanks
Ed Wagner
Ed Wagner
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10208 Visits: 9539
It adds to the days because under the hood, a datetime is really two integers. The first integer is the number of days since 1/1/1753. The second integer is the number of 0.003 timeslices since midnight. When you add an integer to a date, it adds the number to the first integer, so you increment by N days. To play with this a little more, try this:

SELECT GETDATE() + GETDATE();



For a great list of date routines, see Lynn's post at http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/.

Sean is right - the right way to do it is to use DATEADD.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24157 Visits: 37929
Sean Lange (6/24/2014)
Mr. Kapsicum (6/24/2014)

Moreover, the Query you provided I.e SELECT GETDATE()+10 will add 10 Days to current date.
Can anyone please explain this.?
Thanks in advance.


You discovered one of those (un)documented features. When doing math against a datetime value it will use days. The correct way is to use DATEADD.


It comes down to the underlying storage of the datetime value. You can add days by adding integer values to the function and it acts just as if you used DATEADD(DAY,10,GETDATE()). This doesn't work with new DATE or DATETIME2 data types.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search