如何使用 SQL UPDATE 语句将 1 年添加到 DATETIME 列? [英] How can I use a SQL UPDATE statement to add 1 year to a DATETIME column?

查看:37
本文介绍了如何使用 SQL UPDATE 语句将 1 年添加到 DATETIME 列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在表中每一行的日期时间类型列中添加 1 年.对于数字类型,使用 UPDATE 语句进行添加很容易.例如:

I want to add 1 year to a datetime-type column in every single row in a table. Adding using an UPDATE statement is easy for numeric types. ex:

UPDATE TABLE SET NUMBERCOLUMN = NUMBERCOLUMN + 1

我想用 DATETIME 类型做同样的事情...

I'd like to do the same thing with a DATETIME-type...

UPDATE Procrastination SET DropDeadDueDate = DropDeadDueDate + ?

...但我不确定要使用什么值.是否有我可以使用的数值表示1 年"?或者 SQL Server 中是否有 DATEADD 函数或类似函数?

...but I'm not sure what value to use. Is there a numeric value I could use that means "1 year"? Or is there a DATEADD function or similar in SQL Server?

附加问题

我不想为一个字段执行此操作,而是为数据类型为datetime"的数据库中的每个字段执行此操作.有没有一种简单的方法来选择日期时间"类型的所有字段并执行添加 x 年数的更新?我是 sql 新手,所以请温柔...

I would like to do this for not one field, but for every field in the database of data type 'datetime'. Is there an easy way to select all fields of type 'datetime' and perform an update of adding x amount of years? I am new to sql so please be gentle...

推荐答案

T-SQL 中其实有 DATEADD 语句,你可以找到 这里

There is in fact a DATEADD statement in T-SQL, you can find it here

UPDATE Procrastination SET DropDeadDueDate = DATEADD(yyyy,1,DropDeadDueDate)

您可以使用 yearyyyyyy 作为 DATEADD 的第一个参数.

You could use year, yy, or yyyy for the first argument of DATEADD.

这篇关于如何使用 SQL UPDATE 语句将 1 年添加到 DATETIME 列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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