我可以在SQL Server 2005中仅更改日期时间的一天吗? [英] Can I change only day in datetime in SQL Server 2005?

查看:104
本文介绍了我可以在SQL Server 2005中仅更改日期时间的一天吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我被困在必须更新60000条记录日期的情况下.我只想更新日期,但这意味着如果日期是1980年4月23日和1989年10月10日,那么我只是想更新日期,即23& 10 直到日期,如09.04.1980& 1989年9月9日保持剩下的月份和年份不变.这可能吗?

请为我提供解决方案.我尝试使用datepart来执行此操作,但是将整个日期更新为1900年,这是错误的.另外dateadd在这里不起作用,因为日期不同.

Dear All,

I''m stuck in a situation where I have to update 60000 records date. I just want to update the date but that means if the date is 23.04.1980 and 10.09.1989 then I just want to update the date i.e. 23 & 10 only to date like 09.04.1980 & 09.09.1989 keeping the rest month and year untouched. Is this possible?

Please provide me solution of this. I have tried datepart to do this but that update the whole date to year 1900 which is wrong. Also dateadd not work here because the dates are different.

推荐答案

这是经过测试的解决方案.

THIS IS THE TESTED SOLUTION.

update personnaldetails
set dbdate = dateadd(day,(29-datepart(dd,dbdate)),dbdate)


I不知道这是否可以轻松地使用SQL来实现.我将放置SELECT查询以获取列表,然后对于每条记录,将其提取为DateTime类型.然后使用更新的日期进行更新.

修改DateTime的示例:

I do not know if this can be easly use SQL to achive this. I would put SELECT query to get a list and then for each record I would fetch into DateTime type. Then update with updated date.

Example to modify DateTime:

DateTime dt = DateTime.Now.Date.AddDays(2);
dt = dt.AddMonth(1);
dt = dt.AddYear(1);


您知道,如果您使用google,则编码会更快:

http://msdn.microsoft.com/en-us/library/ms186724.aspx [ ^ ]
You know, your coding would go A LOT faster if you used google:

http://msdn.microsoft.com/en-us/library/ms186724.aspx[^]


这篇关于我可以在SQL Server 2005中仅更改日期时间的一天吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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