CASE WHEN

logku 2009-04-13 12:36:24
一个字段 A ,我想要当值为‘’时则替换成当年时间,请问用SQL语句怎么实现?
请各位高手帮帮忙啊.
A
2004
2005
2006
2007
2008
‘’
2004
2005
2006
2007
2008
‘’

...全文
70 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ks_reny 2009-04-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 logku 的回复:]
不能更新表格啊,会出错的。除了UPDATE 还能用什么
[/Quote]
那就select吧

select
(case when col='' then year(getdate()) else col end )as col
From tb
claro 2009-04-13
  • 打赏
  • 举报
回复
/*

2004
2005
2006
2007
*/
select case when a='' then DATEPART (yy,getdate()) else a end
from (select '2004' a union select '2005' union select '2006' union select'2007' union select'' )b
/*
2009
2004
2005
2006
2007
*/
htl258_Tony 2009-04-13
  • 打赏
  • 举报
回复
update tb set A=year(getdate())
where A='‘’'
claro 2009-04-13
  • 打赏
  • 举报
回复
create table #t  (a varchar(20))
insert into #t select '2004' a union select '2005' union select '2006' union select'2007' union select''
/*

2004
2005
2006
2007
*/
select case when a='' then DATEPART (yy,getdate()) else a end from #t
/*
2009
2004
2005
2006
2007
*/
playwarcraft 2009-04-13
  • 打赏
  • 举报
回复
select case when col='' then year(getdate()) else col end as col
from T
you_tube 2009-04-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 logku 的回复:]
不能更新表格啊,会出错的。除了UPDATE 还能用什么
[/Quote]
UPDATE就行
csdyyr 2009-04-13
  • 打赏
  • 举报
回复
update tb set A=case when A='' then year(getdate()) else A end
you_tube 2009-04-13
  • 打赏
  • 举报
回复
update T
set col=case when col='' then CAST(RTRIM(YEAR(GETDATE())) +'0101' AS DATETIME ) else col end
logku 2009-04-13
  • 打赏
  • 举报
回复
不能更新表格啊,会出错的。除了UPDATE 还能用什么
you_tube 2009-04-13
  • 打赏
  • 举报
回复
SELECT CASE WHEN A  ='' THEN  CAST(RTRIM(YEAR(GETDATE())) +'0101' AS DATETIME ) END 
子陌红尘 2009-04-13
  • 打赏
  • 举报
回复
update tablename set A=rtrim(datepart(yy,getdate())) where A=''
playwarcraft 2009-04-13
  • 打赏
  • 举报
回复
update T
set col=case when col='' then year(getdate()) else col end

22,182

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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