sql如何判断条件进行不同更新?

要什么呢称 2008-03-17 12:48:46
一个表中 有 个时间 字段

给这个时间字段增加天数 并根据 时间字段内的时间 如果比现在大则加上天数
如果比现在的时间小则更新为当前时间+天数
...全文
102 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
JL99000 2008-03-17
  • 打赏
  • 举报
回复
use tempdb
if object_id('dbo.#t') is not null
drop table dbo.#t
go
create table #t(id int identity,date_dt datetime)
insert into #t
select '2007-1-1' union all
select '2009-1-1'
select * from #t

update #t
set date_dt=case when date_dt>getdate() then dateadd(dd,abs(datediff(dd,getdate(),date_dt)),date_dt)
when date_dt<getdate() then dateadd(dd,3,getdate())
else date_dt
end
where date_dt='2007-1-1'

select * from #t
jxwangjm 2008-03-17
  • 打赏
  • 举报
回复
Mark一下
要什么呢称 2008-03-17
  • 打赏
  • 举报
回复
非常感谢 原来还有 case 明天加分
bootupnow 2008-03-17
  • 打赏
  • 举报
回复

create table #tb(
i int identity(1,1),
dtime datetime
)


insert into #tb
select '2007-01-01' union all
select '2009-01-01'


-- 加3天
update #tb
set dtime = case when dtime > getdate() then dateadd(day,3,dtime)
when dtime < getdate() then dateadd(day,3,getdate())
else dtime
end
where <条件>

select * from #tb

drop table #tb

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧