日期:2014-05-17  浏览次数:20713 次

小问题:请使用 CONVERT 函数来运行此查询
update Emp set money='100' where money=''
update Emp set money='100' where money<'1'
update Emp set Dj='1' where Dj=''

-----如果money为空的话充100元
-----如果小于1的话充100元
-----如果Dj为空的话更新为1



以上怎么解决,求高手回答,谢谢

------解决方案--------------------
SQL code

declare @EMP table (ID int,Dj varchar(2),money varchar(8))
insert into @EMP
select 1,'','' union all
select 2,'2','10' union all
select 3,'3','100' union all
select 4,'4','0' union all
select 5,'5','80'
--原数据
select * from @EMP
/*
ID          Dj   money
----------- ---- --------
1                
2           2    10
3           3    100
4           4    0
5           5    80
*/
UPDATE @EMP SET DJ=CASE WHEN DJ='' THEN '1' ELSE DJ END,
MONEY=(CASE WHEN MONEY+0<1 and MONEY<>'' THEN 
CONVERT(VARCHAR(10),MONEY+80) 
WHEN MONEY='' THEN CONVERT(VARCHAR(10),MONEY+100) ELSE MONEY END)

--更新后
SELECT * FROM  @EMP
/*
ID          Dj   money
----------- ---- --------
1           1    100
2           2    10
3           3    100
4           4    80
5           5    80
*/

------解决方案--------------------
叶子正解!
------解决方案--------------------
to maco_wang:
money为0的时候为什么是冲了80?
------解决方案--------------------
一楼正解!!!
------解决方案--------------------
SQL code
declare @EMP table (ID int,Dj varchar(2),money varchar(8))
insert into @EMP
select 1,'','' union all
select 2,'2','10' union all
select 3,'3','100' union all
select 4,'4','0' union all
select 5,'5','80'

UPDATE @EMP
SET [money] = CASE WHEN LEN(ISNULL([money],'')) = 0 OR [money] < 1
                THEN 100 ELSE [money]
                END,
    Dj = CASE WHEN LEN(ISNULL(Dj,'')) = 0 THEN '1'
            ELSE Dj
            END;
            
SELECT * FROM     @EMP;

ID          Dj   money
----------- ---- --------
1           1    100
2           2    10
3           3    100
4           4    100
5           5    80

(5 row(s) affected)

------解决方案--------------------
探讨

to maco_wang:
money为0的时候为什么是冲了80?