简体   繁体   English

根据日期时间将列更新为整数

[英]Updating a column to an integer based on a datetime

I'm trying to do a mass update on an sql table, but I don't understand the logical equivalent. 我正在尝试在sql表上进行批量更新,但我不理解其逻辑等效项。 What I'm wanting to do, in pseudo code, is: 我想用伪代码做的是:

UPDATE mytable
SET mycolumn = (pull down the datetime from mycolumn2. if the year of that datetime is not equal to 2013, then mycolumn = 24. if the year is 2013, then mycolumn = 24 - number of months in that datetime)

I'm just really not sure where to start on implementing this kind of logic in SQL. 我只是真的不确定在SQL中实现这种逻辑的起点。 Does anyone have any tips or direction for me? 有人对我有提示或指导吗?

Thanks 谢谢

UPDATE myTable
SET mycolumn = CASE WHEN DATEPART(year, mycolumn2) = 2013 
                        THEN 24 - DATEPART(month, mycolumn2) 
                    ELSE 24 
               END

Adjust date syntax for RDBMS as needed. 根据需要调整RDBMS的日期语法。

UPDATE mytable
SET mycolumn = CASE WHEN YEAR(mycolumn2) <> 2013 
                    THEN 24
                    WHEN YEAR(mycolumn2) = 2013 
                    THEN 24 - mycolumn
                END

Using a Simple Case statment: 使用Simple Case陈述:

update mytable
set mycolumn = 24 - case year(mycolumn2) 
                        when 2013 then  month(mycolumn2)
                        else 0 
                    end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM