Solved

t-sql add days to getdate function

Posted on 2014-12-11
2
288 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 65

Accepted Solution

by:
Jim Horn earned 250 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 48

Assisted Solution

by:PortletPaul
PortletPaul earned 250 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
get_systemdrive info from tsql? 1 26
Amazon RDS migrate to SQL Server 3 49
query optimization 6 30
Compare a column in results by values left of decimal 2 33
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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