?
Solved

t-sql add days to getdate function

Posted on 2014-12-11
2
Medium Priority
?
336 Views
Last Modified: 2014-12-11
When I run this:

-- end of todays date
SELECT DATEADD(DAY, DATEDIFF(DAY, '19000101', GETDATE()), '23:59:59')

I get this:

2014-12-11 23:59:59.000

How do I add 5 days to this.
I want 5 days added to GETDATE()  in the same format .

So I want to get this:

2014-12-16 23:59:59.000
0
Comment
Question by:maqskywalker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 1000 total points
ID: 40495384
Since the code already uses DATEADD, just add five to the middle parameter (increment)

SELECT DATEADD(DAY, DATEDIFF(DAY, '19000101', GETDATE()) + 5, '23:59:59')
0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 1000 total points
ID: 40495392
DO NOT USE '23:59:59' because that is NOT the end of the day.

It is one second short of the end of the day.

The end of today's day is ( 00:00:00 of tomorrow )
e.g.
if today is 16th December 2014, then the end of that day is '2014-12-17 00:00:00'

to set time to 00:00:00 from getdate() use
dateadd(day, datediff(day,0, getdate() ), 0)
or
cast(getdate() as date)

then to add 5 days use dateadd(day,5, .... ) where ... is one of the options above

also note to avoid use of between for date/time ranges.
see: "Beware of Between"
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

770 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