sql server update 与case 用法


update 计算日期  set 当前年=case when(计算日期>substring(计算日期,1,4)+'0301') then
         substring(计算日期,1,4) else  计算日期-1 end

///网络文章///

如果更新的时候需要对2个条件都要判断的可以用一个update +case来完成
例子如下:
declare @a table(id int,EndTime datetime,classid int)
insert @a
select 1,dateadd(day,-1,getdate()),1
union all
select 2,dateadd(day,1,getdate()),2
union all
select 3,dateadd(day,-1,getdate()),2
union all
select 4,dateadd(day,1,getdate()),1
select * from @a
select getdate()
update @a
set EndTime=dateadd(hh,case when Classid=1 then 24 else 72 end,casewhen EndTime>=getdate() then Endtime elsegetdate()end)
select * from @a

(4 行受影响)
id         EndTime                classid
----------- ----------------------- -----------
         2008-07-24 11:52:53.827 1
         2008-07-26 11:52:53.827 2
         2008-07-24 11:52:53.827 2
         2008-07-26 11:52:53.827 1

(4 行受影响)


-----------------------
2008-07-25 11:52:53.827

(1 行受影响)

(4 行受影响)

id         EndTime                classid
----------- ----------------------- -----------
         2008-07-26 11:52:53.827 1
         2008-07-29 11:52:53.827 2
         2008-07-28 11:52:53.827 2
         2008-07-27 11:52:53.827 1

(4 行受影响)
/另一篇文章

 UPDATE CASE WHEN (2010-05-1713:54:27)转载▼
标签: 杂谈 分类: Mysql
其实是为了实现一个很小的功能,就是为了同步两张表,
如果需要被同步的表中有值的话,则不做修改,如果没值(为空),则同步过来。
SQL语句如下:
1)
UPDATE `test` SET
`content_id` =
CASE WHEN `content_id` IS NULL THEN `content_id`
ELSE `content_id` + 1
END
 
2)
UPDATE `test` SET
`content_id` =
CASE ISNULL(`content_id`) WHEN  1THEN  `content_id`
ELSE `content_id` + 1
END
 
 
附上ISNULL函数用法:
ISNULL(expr)
如expr 为NULL,那么ISNULL() 的返回值为 1,否则返回值为 0。
mysql> SELECT ISNULL(1+1);
       -> 0
mysql> SELECT ISNULL(1/0);
       -> 1
使用= 的NULL 值对比通常是错误的。
 /另一篇文章

2011-03-04 10:28

update case

ALTER     proc usp_clearActiveScoreCalcCommentScore
as
begin
update tbForum set UltimoScore=
   (case
    whenAllScore-RemainScore<0 then 0
    elseAllScore-RemainScore
   end),AllScore=
   (case
    whenActivityDegree>=0 andActivityDegree<=300 then 250
    whenActivityDegree>=301 andActivityDegree<=800 then 500
    whenActivityDegree>=801 andActivityDegree<=2000 then 1000
    whenActivityDegree>=2001 andActivityDegree<=4000 then 2000
    whenActivityDegree>=4001 andActivityDegree<=8000 then 4500
    whenActivityDegree>8000 then9000  
  end),UltimoDegree=ActivityDegree,ActivityDegree = 0
update tbForum set RemainScore=AllScore
end

-------------------------------

其它用法:

--★★★★★★★★★★SQL里面的case不能象switch那样来用,很遗憾!!★★★★★★★★★★★★★

--简单case
--[dbo.TBL_EVENT]的Status字段检测 生成一个eventStatus列
select EventName,
case [Status]
when 0 then 'ing'
when 2 then 'over'
else '-'
end as eventStatus
from TBL_EVENT

--布尔case
--在布尔case中,如果遇到了第一个为真的when条件,就不会再继续对其他的when条件进行检测
select
case
when 1<0 then '1怎么会小于0撒'
when datepart(yy,getdate())='2006' then '今天是我的生日'
when 1>0 then '1>0当然是正确的啊'
end as result

--布尔case表达式可以处理包含and or 的复杂条件
declare @b int,@q int

set @b=2007
set @q=25

select
case
when @b=2007 and @q between 10 and 30 then 1
else null
end as test

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值