sqlserverupdate与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
isnull的用法
(4 ⾏受影响)
id        EndTime                classid
----------- ----------------------- -----------
1          2008-07-24 11:52:53.827 1
2          2008-07-26 11:52:53.827 2
3          2008-07-2
4 11:52:53.827 2
4          2008-07-26 11:52:53.827 1
(4 ⾏受影响)
-----------------------
2008-07-25 11:52:53.827
(1 ⾏受影响)
(4 ⾏受影响)
id        EndTime                classid
----------- ----------------------- -----------
1          2008-07-26 11:52:53.827 1
2          2008-07-29 11:52:53.827 2
3          2008-07-28 11:52:53.827 2
4          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

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。