现在数据库有字段  YY                    DD
数据内容为
数据1            4213432
数据2            32423A1
数据3            45343B
数据4            978A2D3如何判断字段yy里面只要包含单个字母 A的则更新 DD的数据为 “啊”
字段yy里面只要包含单个字母B的则更新 DD的数据为 “笔”
字段yy里面只要包含单个字母C的则更新 DD的数据为 "吃"
以次类推,其它字母也有更改的字段YY只有数字的不更改,拥有多个字母的也不更改,只更改单个字母

解决方案 »

  1.   

    --> 测试数据: @tb
    declare @tb table (id varchar(5),yy varchar(7),dd sql_variant)
    insert into @tb
    select '数据1','4213432',null union all
    select '数据2','32423A1',null union all
    select '数据3','45343B',null union all
    select '数据4','978A2D3',null
    update @tb
    set dd=case when len(yy)-len(replace(yy,'A',''))=1 then '啊'
    when len(yy)-len(replace(yy,'B',''))=1 then '笔'
    when len(yy)-len(replace(yy,'C',''))=1 then '吃'
    --when 。省略
    else null
    end 
    select * from @tbid    yy      dd
    ----- ------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    数据1   4213432 NULL
    数据2   32423A1 啊
    数据3   45343B  笔
    数据4   978A2D3 啊(4 行受影响)
      

  2.   

    ----建个转换表tb2
    --tb2 zimu  zhuanhua
    --     'A'    N'啊'
    --     'B'    N'笔'
    --     'C'    N'吃'
         
     update k  
     set YY=tb2.zhuanhua
     from tb2 ,tb1 k
     where CHARINDEX(zimu,YY)>0 and 
           (select COUNT(*) from tb2 where CHARINDEX(zimu,k.yy)>0 )>1
      

  3.   

    --DROP TABLE TB1 ,TB2
    create table tb2 (zimu varchar(3),  zhuanhua nvarchar(10))
    insert tb2 select 
         'A'  ,  N'啊' union select 
         'B'  ,  N'笔' union select 
         'C'  ,  N'吃' UNION SELECT 
         'D'  ,  N'低调'
    create table tb1 (id varchar(5),yy varchar(17),dd sql_variant)
    insert into tb1
    select '数据1','42134AAA32',null union all
    select '数据2','32423A1',null union all
    select '数据3','45343B',null union all
    select '数据4','978D3',null UNION ALL 
    select '数据5','978A2DSADSAD3',null UNION ALL 
    select '数据6','9783',null   
    GO
    update  k
    SET dd = tb2.zimu
    from tb2 join tb1 k on CHARINDEX(zimu,k.yy)>0  
    join (select *
    from (
    select ID,yy=STUFF(yy,PATINDEX('%[a-z A-Z]%',yy),1,'')
    from tb1 
    where PATINDEX('%[a-z A-Z]%',yy)>0) z 
    where PATINDEX('%[a-z A-Z]%',yy)=0) p on k.id=p.id
    go
    select * from tb1
    /*
    id    yy                dd
    ----- ----------------- --------------------
    数据1   42134AAA32        NULL
    数据2   32423A1           A
    数据3   45343B            B
    数据4   978D3             D
    数据5   978A2DSADSAD3     NULL
    数据6   9783              NULL*/
      

  4.   

    set nocount on 
    --> 测试数据: @tb
    declare @tb table (id varchar(5),yy varchar(7),dd sql_variant)
    insert into @tb
    select '数据1','4213432',null union all
    select '数据2','32423A1',null union all
    select '数据3','45343B',null union all
    select '数据5','453B43B',null union all
    select '数据4','978A2D3',null
    declare @tc table (col1 varchar(5),col2 varchar(7) )
    insert into @tc
    select 'A','啊'  union all
    select 'B','笔'  union all
    select 'C','吃'  union all
    select 'D','的' 
     
    update @tb
    set dd= T2.col2 
    from @tb T1 ,@TC T2,
    (
    SELECT YY FROM @TB A ,@TC B 
    WHERE len(yy)-len(replace(yy,b.col1,''))=1
    GROUP BY YY  HAVING(COUNT(*)=1)
    )T3
    where T1.YY=T3.YY AND CHARINDEX(T2.COL1,T3.YY)>0 
     
      
    select * from @tbid    yy      dd
    ----- ------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    数据1   4213432 NULL
    数据2   32423A1 啊
    数据3   45343B  笔
    数据5   453B43B NULL
    数据4   978A2D3 NULL