繁体   English   中英

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

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

我正在尝试在sql表上进行批量更新,但我不理解其逻辑等效项。 我想用伪代码做的是:

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)

我只是真的不确定在SQL中实现这种逻辑的起点。 有人对我有提示或指导吗?

谢谢

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

根据需要调整RDBMS的日期语法。

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

使用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