如何在日期时间字段中添加时间字段 [英] How to add datetime field with a time field

查看:145
本文介绍了如何在日期时间字段中添加时间字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在SQL Server 2012中使用T-SQL将 time 值添加到现有的 datetime 值中。



我在想 DATEADD 函数可能是一个解决方案,但不是...



也许我以某种方式有时间转换为 datetime



所以我有


StartDate 2013-02-18 18:34: 40.330(datetime)



时间间隔00:11:00.0000000(时间)



EndDate吗? tsql的? (日期时间)


有任何线索吗?

解决方案

尝试类似的方法。注意:我在这里不是毫秒级

  declare @dt datetime = getdate()
clarify @t time =' 01:35:45'

选择dateadd(second,
datepart(hour,@ t)* 3600 +
datepart(minute,@ t)* 60 +
datepart(second,@ t),
@dt)


I have to add time value to existing datetime value using T-SQL in SQL Server 2012.

I was thinking that DATEADD function it might be a solution, but it is not...

Perhaps I have somehow time convert to datetime?

So I have

StartDate 2013-02-18 18:34:40.330 (datetime)

Interval 00:11:00.0000000 (time)

EndDate ? tsql ? (datetime)

Any clue?

解决方案

Try something like this. Note: I am not taking milliseconds here

declare @dt datetime = getdate()
declare @t time = '01:35:45'

select dateadd(second, 
                  datepart(hour,@t) * 3600 + 
                  datepart(minute,@t) * 60 + 
                  datepart(second,@t),
                  @dt)

这篇关于如何在日期时间字段中添加时间字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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