将日期和时间字段组合到日期时间,SQL Server 2008 [英] combining Date and Time fields to DateTime, SQL Server 2008

查看:57
本文介绍了将日期和时间字段组合到日期时间,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)

这篇关于将日期和时间字段组合到日期时间,SQL Server 2008的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆