我想仅在没有时间更改的情况下更新列。 [英] I want to update a column only the date without the time change.

查看:95
本文介绍了我想仅在没有时间更改的情况下更新列。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我想更新一列没有时间更改的日期。

hi I want to update a column only the date without the time change.

推荐答案

试试这个:



Try this:

UPDATE
   Yourtable
SET
   datetimecol = DATEADD(day, DATEDIFF(day, datetimecol, getdate()), datetimecol)


让我们说新的date是使用getdate的当前日期()

我们可以这样做以获取日期部分:

Let say the new date is current date using getdate()
we can do this to get the date part:
convert(varchar, getdate(), 101)



我们也可以这样做以获得旧日期时间的一部分:


we can also do this to get the time part of the old date time:

convert(varchar, datetimefield, 114)



所以我们的想法是将新日期时间的日期部分与旧日期时间的时间部分连接起来:


So the idea is to concatenate the date part of a new date time with the time part of the old date time:

update table1 set datetimefield =
(convert(varchar, getdate(), 101) + ' ' + convert(varchar, datetimefield, 114))



至于101和104的含义,请参考以获取更多详细信息:

sql-convert [ ^ ]


这篇关于我想仅在没有时间更改的情况下更新列。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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