2013-03-01 59 views
1

我正在尝试对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中实现这种逻辑。有没有人对我有任何提示或方向?根据需要

感谢

+2

*** *** SQL只是*结构化查询语言* - 许多数据库系统中使用的语言,而不是AA的数据库产品。 ..很多东西都是特定于供应商的 - 所以我们真的需要知道您正在使用的数据库系统**(以及哪个版本)...... – 2013-03-01 16:46:31

+0

@marc_s我正在使用Microsoft SQL Server 2008 ..对不起 – proseidon 2013-03-01 16:48:03

回答

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

调整日期语法RDBMS。

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

使用Simple Case statment:

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