update 计算日期
///网络文章///
如果更新的时候需要对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
----------- ----------------------- -----------
1
2
3
4
(4 行受影响)
-----------------------
2008-07-25 11:52:53.827
(1 行受影响)
(4 行受影响)
id
----------- ----------------------- -----------
1
2
3
4
(4 行受影响)
/另一篇文章
标签: 杂谈
其实是为了实现一个很小的功能,就是为了同步两张表,
如果需要被同步的表中有值的话,则不做修改,如果没值(为空),则同步过来。
SQL语句如下:
1)
UPDATE `test` SET
`content_id` =
CASE WHEN `content_id` IS NULL THEN
ELSE `content_id` + 1
END
2)
UPDATE `test` SET
`content_id` =
CASE ISNULL(`content_id`) WHEN
ELSE `content_id` + 1
END
附上ISNULL函数用法:
ISNULL(expr)
如expr 为NULL,那么ISNULL() 的返回值为 1,否则返回值为 0。
mysql> SELECT ISNULL(1+1);
mysql> SELECT ISNULL(1/0);
使用= 的NULL 值对比通常是错误的。
update case
ALTER
as
begin
update tbForum set UltimoScore=
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