将日期和时间字段组合到日期时间,SQL Server 2008

将日期和时间字段组合到日期时间,SQL Server 2008

问题描述:

好的 - 我问了几个人,必须有一个简单的方法来做到这一点......

Ok - I've asked a few people and there has got to be an easy way to do this....

declare @Date date
declare @Time time 
declare @datetime datetime

select @Date = convert(Date,GetDate()) 
select @Time = convert(Time,GetDate())

select @Date, @Time, @Date + @Time (+ operator fails!)

我真的必须:1)转换为字符串,然后转换为日期时间字段?2) 使用 DateAdd 和 DatePart 先添加小时,然后添加分钟,然后添加秒.....

Do I really have to: 1) convert to a string, then convert to datetime field? 2) use DateAdd and DatePart to add hours first then minutes, then seconds.....

@Date + cast(@Time as datetime)

在 SQL Server 2012 中,我假设在 SQL Server 2014 中您需要将日期和时间变量都转换为日期时间.

In SQL Server 2012 and I assume SQL Server 2014 you neeed to cast both the date and the time variable to datetime.

cast(@Date as datetime) + cast(@Time as datetime)