我正在尝试在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中实现这种逻辑。有没有人对我有任何提示或指示?
由于
答案 0 :(得分:1)
UPDATE myTable
SET mycolumn = CASE WHEN DATEPART(year, mycolumn2) = 2013
THEN 24 - DATEPART(month, mycolumn2)
ELSE 24
END
根据需要调整RDBMS的日期语法。
答案 1 :(得分:0)
UPDATE mytable
SET mycolumn = CASE WHEN YEAR(mycolumn2) <> 2013
THEN 24
WHEN YEAR(mycolumn2) = 2013
THEN 24 - mycolumn
END
答案 2 :(得分:0)
使用 Simple Case
声明:
update mytable
set mycolumn = 24 - case year(mycolumn2)
when 2013 then month(mycolumn2)
else 0
end