简体   繁体   中英

SQL Server 2012, Concat a Date with a Time column to insert into a DateTime column in another table

In SQL Server 2012 ... I have two column in a table, TransactionDate(date,null) and TransactionTime(time, null)

I need to concatenate them together to insert into another table's column which is a Datetime datatype.

Is this possible?

Thanks in advance.

Why not

SELECT TransactionDate+ CAST(TransactionTime AS datetime)
FROM table

Try this one -

DECLARE @temp TABLE
(
      TransactionDate DATE
    , TransactionTime TIME
)

INSERT INTO @temp (TransactionDate, TransactionTime)
VALUES 
    ('2013-04-27', '08:37:01.217'),
    ('2013-04-27', '12:39:14.613')

--INSERT INTO ... (DatetimeColumn)
SELECT CAST(TransactionDate AS VARCHAR(10)) + ' ' + CAST(TransactionTime AS VARCHAR(12))
FROM @temp
dateadd(millisecond, datediff(millisecond, '00:00', TransactionTime), cast(TransactionDate as datetime))

计算自午夜以来的时间值(以毫秒为单位),并将其添加到日期值中。

Perhaps the best place to start is the SQL Server online guide to date and time datatypes / functions: http://msdn.microsoft.com/en-us/library/ms186724.aspx

You should be able to use the DATEPART function combined with DATETIMEFROMPARTS function to achieve this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM