Solved

CAST(date_column + time_column as datetime)

Posted on 2016-07-27
8
33 Views
Last Modified: 2016-07-27
Hi guys

Question for you:  What's the fastest way to add a date column and a time column and cast it as a single datetime column?

Example:
Declare @dt date = '20160727', @tm time = '03:30:00'
SELECT CAST( ???? as datetime) 

Open in new window


Thanks in advance.
Jim
0
Comment
Question by:Jim Horn
8 Comments
 
LVL 12

Assisted Solution

by:Dustin Saunders
Dustin Saunders earned 250 total points
ID: 41731778
I use:
SELECT CONVERT(DATETIME, CONVERT(CHAR(8), @dt, 112) 
  + ' ' + CONVERT(CHAR(8), @tm, 108))

Open in new window

0
 
LVL 6

Expert Comment

by:Manuel Marienne-Duchêne
ID: 41731781
CAST(@dt + @tm AS DATETIME) with space in first position in time
0
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41731811
But don't you need extra steps to add in the space in time (say if it were from a column)?  [maybe we should add physics to the categories if we're talking about spacetime :P ]

Anyways, I guess if I used it a lot I'd just stick it in a UDF and call that when needed (seems easiest for me though I don't know if there's a more performance efficient way?)

CREATE FUNCTION concatDT (@dt date,@tm time)
RETURNS datetime
AS
BEGIN
RETURN(	SELECT CONVERT(DATETIME, CONVERT(CHAR(8), @dt, 112) 
	+ ' ' + CONVERT(CHAR(8), @tm, 108)))
END

Open in new window

SELECT dbo.concatDT(@dt,@tm)

Open in new window

0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 250 total points
ID: 41731997
SELECT CAST(@dt AS datetime) + @tm
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41732007
I tried:
Declare @dt date = '20160727', @tm time = '03:30:00'
SELECT CAST(@dt AS datetime) + @tm

Open in new window

and got:
Msg 402, Level 16, State 1, Line 2
The data types datetime and time are incompatible in the add operator.


but
Declare @dt date = '20160727', @tm time = '03:30:00'
SELECT CAST(@dt AS datetime) + CAST(@tm AS datetime)

Open in new window


does work and is less code than mine.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 41732066
I tried:
Declare @dt date = '20160727', @tm time = '03:30:00'
SELECT CAST(@dt AS datetime) + @tm
and got [an error]

Hmm, worked fine for me, at least through SQL 2012.  Maybe that's a later SQL version restriction?  I know they've made some changes in that area.
0
 
LVL 65

Author Closing Comment

by:Jim Horn
ID: 41732074
Thanks guys.  Chose Scott's answer as it was a less expensive option, which works on my 2008R2 box.
0
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41732077
Yeah, I agree that's better way.  It's weird that it doesn't run on my SQL test servers (I tried on SQL 2012 SP3 and 2014); so must have appeared somewhere around there, or maybe I angered some vengeful SQL God.

Might be a good note or item to test though, in case you upgrade production SQL versions in the future it might break the query.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now