我想将年份修改为 2013 年,我不想修改其余部分:月、日等
我想对表格中的每一行执行此操作
我知道我可以使用以下方法添加“x”年:
Set date = date+ x YEAR in the update statement
但我不知道如何实现 2013 年的恒定值,无论原始年份值如何
您还没有明确说明您希望闰年发生什么,这可能很重要(他们会朝哪个方向移动?)。但是,基本形式非常简单:
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
选择 to_date('2014/'|| to_char(sysdate, 'mm/dd HH24:MI:SS'), 'yyyy/mm/dd HH24:MI:SS') from dual;