October 10, 2013 at 9:14 am
Hi,
How do I add a time to a datetime? The dateadd function does not allow a time (hh:mm) as a part as far as I can see.
e.g.
DECLARE @DateTime datetime = '2013-10-16 00:00:00.000'
DECLARE @TheTime time = '11:00'
DECLARE @new datetime
I would like the resultant datetime to be '2013-10-16 11:00:00.000'
Thanks in advance
October 10, 2013 at 9:18 am
Look up DATEADD in Books Online. You can also CONVERT the date to a string and concatenate a string representation of the time and then CAST all that back to a datetime. Probably suboptimal to do that. 🙂
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
October 10, 2013 at 9:21 am
You can cheat a little bit here since you just want to add a time datatype to a datetime.
select @DateTime + @TheTime
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 10, 2013 at 9:27 am
Sean Lange (10/10/2013)
You can cheat a little bit here since you just want to add a time datatype to a datetime.
select @DateTime + @TheTime
Hi,
that seems to work but I needed to change the type of @TheTime to a datetime.
e.g.
DECLARE @DateTime datetime = '2013-10-16 00:00:00.000'
DECLARE @TheTime datetime = '11:00'
SELECT @DateTime + @TheTime
October 10, 2013 at 9:30 am
Fairly lengthy discussion of various scenarios with adding dates and times:
Stackoverflow: How to combine date from one field with time from another field in MS SQL Server
October 10, 2013 at 9:35 am
thedavegray (10/10/2013)
Sean Lange (10/10/2013)
You can cheat a little bit here since you just want to add a time datatype to a datetime.
select @DateTime + @TheTime
Hi,
that seems to work but I needed to change the type of @TheTime to a datetime.
e.g.
DECLARE @DateTime datetime = '2013-10-16 00:00:00.000'
DECLARE @TheTime datetime = '11:00'
SELECT @DateTime + @TheTime
OK.
select @DateTime + cast(@TheTime as time)
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 10, 2013 at 9:44 am
Thanks for all responses.
October 10, 2013 at 7:09 pm
Sean - Love your cheat!
Another way:
SELECT DATEADD(minute, DATEDIFF(minute, '00:00', @TheTime), @DateTime)
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply