我想修改到2013年的年份,我不想修改其余的:月,日等等
我想对表中的每一行执行此操作
我知道我可以使用以下方式添加'x'年:
Set date = date+ x YEAR in the update statement
但我不知道如何实现2013年的恒定价值,无论原始年份价值
答案 0 :(得分:3)
你还没有明确说过闰年会发生什么,这可能很重要(他们会朝哪个方向移动?)。但是,基本形式非常简单:
UPDATE <table> SET <column> = <column> + (2013 - YEAR(<column)) YEARS
示例(在我的本地iSeries上运行和验证所有语句):
CREATE TABLE QTEMP/Example (initial TIMESTAMP NOT NULL WITH DEFAULT,
updated TIMESTAMP NOT NULL WITH DEFAULT)
INSERT INTO Example VALUES(CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
(TIMESTAMP(DATE('2012-02-29'), CURRENT_TIME),
TIMESTAMP(DATE('2012-02-29'), CURRENT_TIME)),
(TIMESTAMP(DATE('2012-02-28'), CURRENT_TIME),
TIMESTAMP(DATE('2012-02-28'), CURRENT_TIME)),
(TIMESTAMP(DATE('2012-03-31'), CURRENT_TIME),
TIMESTAMP(DATE('2012-02-31'), CURRENT_TIME)),
(TIMESTAMP(DATE('2013-05-08'), CURRENT_TIME),
TIMESTAMP(DATE('2013-05-08'), CURRENT_TIME)),
(TIMESTAMP(DATE('2014-09-20'), CURRENT_TIME),
TIMESTAMP(DATE('2014-09-20'), CURRENT_TIME)),
产生的初始数据:
initial updated
2012-11-14-15.05.58.246187 2012-11-14-15.05.58.246187
2012-02-29-15.05.58.000000 2012-02-29-15.05.58.000000
2012-02-28-15.05.58.000000 2012-02-28-15.05.58.000000
2012-03-01-15.05.58.000000 2012-03-01-15.05.58.000000
2013-05-08-15.05.58.000000 2013-05-08-15.05.58.000000
2014-09-20-15.05.58.000000 2014-09-20-15.05.58.000000
实际陈述
UPDATE Example SET updated = updated + (2013 - YEAR(updated)) YEARS
WHERE YEAR(updated) <> 2013
导致:
initial updated
2012-11-14-15.05.58.246187 2013-11-14-15.05.58.246187
2012-02-29-15.05.58.000000 2013-02-28-15.05.58.000000
2012-02-28-15.05.58.000000 2013-02-28-15.05.58.000000
2012-03-01-15.05.58.000000 2013-03-01-15.05.58.000000
2013-05-08-15.05.58.000000 2013-05-08-15.05.58.000000
2014-09-20-15.05.58.000000 2013-09-20-15.05.58.000000
答案 1 :(得分:-1)
从双重选择to_date('2014 /'|| to_char(sysdate,'mm / dd HH24:MI:SS'),'yyyy / mm / dd HH24:MI:SS');