T-SQL:如何只更新datetime字段的日期部分? [英] T-SQL: How to update just date part of datetime field?

查看:502
本文介绍了T-SQL:如何只更新datetime字段的日期部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SQL Server 2008中,我需要更新日期时间字段的日期部分。



在我的存储过程中,我收到日期时间格式的新日期。从这个参数我必须提取日期(不感兴趣的时间)和更新现有的值日期部分。

我该怎么做?

解决方案

一种方法是将日期之间的差异添加到旧日期之间

  UPDATE TABLE 
SET< datetime> = dateadd(dd,datediff(dd,< datetime>,@ newDate),< datetime>)
WHERE ...


In SQL Server 2008 I need to update just the date part of a datetime field.

In my stored procedure I receive the new date in datetime format. From this parameter I have to extract the date (not interested in time) and update the existing values date part.
How can I do this?

解决方案

One way would be to add the difference in days between the dates to the old date

UPDATE TABLE
SET <datetime> = dateadd(dd,datediff(dd,<datetime>,@newDate),<datetime>)
WHERE ...

这篇关于T-SQL:如何只更新datetime字段的日期部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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