使用CASE - SQL SERVER更新记录

时间:2017-05-03 14:00:46

标签: sql-server sql-server-2008

我的原始表格有这样的值:

   ID           TELPHONE
  ----------- --------------
  1923        6276572   
  1949        2126670825
  2125
  1926        12126670825

我执行以下查询以获得预期结果,如下所示:

UPDATE #T  
SET     TELEPHONE = CASE  
                    WHEN CHARINDEX('1212', TELEPHONE, 1) = 0 THEN 
  '1212'+TELEPHONE

        WHEN CHARINDEX('212', TELEPHONE , 1) = 1 THEN '1'+TELEPHONE

        ELSE TELEPHONE

                END 
 WHERE (STATE LIKE '%UU%' OR STATE LIKE '%U&P%' OR STATE LIKE '%UTTER%') AND 
LEN(TELEPHONE) <= 10 

我的期望:

 ID           TELPHONE
 ----------- --------------
 1923        12126276572   
 1949        12126670825
 2125
 1926        12126670825

然而,我在下面(这是错误的)

 id      TELPHONE      
 ----------- --------------   
  1923        12126276572
 1949        12122126670825             
 2125        1212                         
 1926        12126670825
我在做错了什么。如何得到预期的结果?

2 个答案:

答案 0 :(得分:1)

我用这个更正了(我省略了你的where条件):

  UPDATE TELE  SET TELPHONE = CASE WHEN CHARINDEX('212', TELPHONE , 1) = 1 THEN '1'+TELPHONE
                              WHEN CHARINDEX('1212', TELPHONE, 1) = 0 AND TELPHONE<>'' THEN '1212'+TELPHONE                            
                        ELSE TELPHONE
                        END

输出:

+------+-------------+
|  ID  |  TELPHONE   |
+------+-------------+
| 1923 | 12126276572 |
| 1949 | 12126670825 |
| 2125 |             |
| 1926 | 12126670825 |
+------+-------------+

我认为如果你可以添加WHERE条件AND TELEPHONE&lt;&gt;''并简化案例(在我的情况下删除AND),性能会更好:

UPDATE TELE  SET TELPHONE = CASE WHEN CHARINDEX('212', TELPHONE , 1) = 1 THEN '1'+TELPHONE
                              WHEN CHARINDEX('1212', TELPHONE, 1) = 0   THEN '1212'+TELPHONE                               
                        ELSE TELPHONE
                        END                  
   WHERE --(STATE LIKE '%UU%' OR STATE LIKE '%U&P%' OR STATE LIKE '%UTTER%') 
       --AND 
        LEN(TELPHONE) <= 10 AND TELPHONE<>'';

答案 1 :(得分:0)

这是因为您的case在第一个表达式中匹配您不想要的行。重新排列case,以便首先检查212,因为这只是一个积极的匹配,如果不符合,则只检查1212上的否定匹配:

declare @t table (
                 ID       int
                ,TELEPHONE nvarchar(50)
                 );
insert into @t
values (1923,'6276572'),
       (1949,'2126670825'),
       (2125,''),
       (1926,'12126670825');

select Telephone
        ,case when charindex('212',TELEPHONE,1) = 1
                then '1'+TELEPHONE
              when charindex('1212',TELEPHONE,1) = 0
                   and TELEPHONE <> ''
                then '1212'+TELEPHONE
              else TELEPHONE
              end as TelephoneNew
       ,charindex('212',TELEPHONE,1) as [212Match]
       ,charindex('1212',TELEPHONE,1) as [1212Match]
from @t

输出:

+-------------+--------------+----------+-----------+
|  Telephone  | TelephoneNew | 212Match | 1212Match |
+-------------+--------------+----------+-----------+
|     6276572 |  12126276572 |        0 |         0 |
|  2126670825 |  12126670825 |        1 |         0 |
|             |              |        0 |         0 |
| 12126670825 |  12126670825 |        2 |         1 |
+-------------+--------------+----------+-----------+