日期:2014-05-17  浏览次数:20649 次

sql读取建立日期 在基础上加2年写入到期日期
表 Card 

列 card_jlrq(建立日期) card_enddate(到期日期)

现在数据格式
card_jlrq card_enddate
2012-7-27 13:39:32 2013-7-27

想要的格式 

2012-7-27 13:39:32 2014-7-27
读取建立日期 在基础上加2年写入到期日期

update Card set card_enddate = dateadd(day,3,card_jlrq) where card_no ='00000004'

这个脚本可以 但是 把时间都加上了 不想要时间只要日期

------解决方案--------------------
SQL code
update Card set card_enddate = dateadd(day,3,convert(varchar(10),card_jlrq,120)) where card_no ='00000004'

------解决方案--------------------
SQL code

create table Cards
(card_no varchar(15),
 card_jlrq datetime,
 card_enddate datetime)

insert into Cards
select '00000004','2012-7-27 13:39:32', '2013-7-27'


update Cards
set card_enddate=cast(dateadd(yy,2,card_jlrq) as date)
where card_no ='00000004'

select * from Cards

/*
card_no         card_jlrq               card_enddate
--------------- ----------------------- -----------------------
00000004        2012-07-27 13:39:32     2014-07-27 00:00:00

(1 row(s) affected)
*/