SQL SERVER 特殊需求的一个替换实例 --【叶子】

需求贴:

http://topic.csdn.net/u/20120204/10/8b902fd2-8909-4ed9-b534-4a1a72454eff.html#r_77443331

需求简介:

比如:YYYYN,  结果处理为:人1,人2,人3,人4

分析:

貌似就是一个字符串的替换,如果是'Y'替换成'人'+Y的所在位置的编号。

解决方案:

  1. declare @T table (id int,col varchar(5))  
  2. insert into @T  
  3. select 1,'YYYYN' union all  
  4. select 2,'YNNNY' union all  
  5. select 3,'NNNYN' union all  
  6. select 4,'YYNYN'  
  7.   
  8. --1.第一种方法:case when  
  9. --最直观的处理方式   
  10. select id,col=left(col,len(col)-1) from   
  11. (  
  12.  select id,col=  
  13.  case when left(col,1)='Y' then '人1,' else '' end+  
  14.  case when substring(col,2,1)='Y' then '人2,' else '' end+  
  15.  case when substring(col,3,1)='Y' then '人3,' else '' end+  
  16.  case when substring(col,4,1)='Y' then '人4,' else '' end+  
  17.  case when substring(col,5,1)='Y' then '人5,' else '' end  
  18.  from @T  
  19. ) a  
  20. /*  
  21. id          col  
  22. ----------- --------------------  
  23. 1           人1,人2,人3,人4  
  24. 2           人1,人5  
  25. 3           人4  
  26. 4           人1,人2,人4  
  27. */  
  28.   
  29.   
  30. --2.使用自定义函数   
  31. --创建函数  
  32. create function fn_GetPersonCount  
  33. (  
  34.     @str varchar(5)  
  35. )  
  36. returns varchar(20)  
  37. as  
  38. begin  
  39.     declare @result varchar(20) set @result=''  
  40.     declare @i int set @i=0  
  41.     while(charindex('Y',@str)>0)  
  42.     begin  
  43.         set @result=@result+'人'+ltrim(charindex('Y',@str)+@i)+','  
  44.         set @str=right(@str,len(@str)-charindex('Y',@str))  
  45.         set @i=@i+1  
  46.     end  
  47.     set @result=left(@result,len(@result)-1)  
  48.     return @result  
  49. end  
  50.   
  51. declare @T table (id int,col varchar(5))  
  52. insert into @T  
  53. select 1,'YYYYN' union all  
  54. select 2,'YNNNY' union all  
  55. select 3,'NNNYN' union all  
  56. select 4,'YYNYN'  
  57.   
  58. select id,col=dbo.fn_GetPersonCount(col) from @T  
  59. /*  
  60. id          col  
  61. ----------- --------------------  
  62. 1           人1,人2,人3,人4  
  63. 2           人1,人5  
  64. 3           人4  
  65. 4           人1,人2,人4  
  66. */  
  67.   
  68.   
  69. --3.合并列值  
  70.   
  71. declare @T table (id int,col varchar(5))  
  72. insert into @T  
  73. select 1,'YYYYN' union all  
  74. select 2,'YNNNY' union all  
  75. select 3,'NNNYN' union all  
  76. select 4,'YYNYN'  
  77.   
  78. ;with maco as  
  79. (  
  80. select a.*,'人'+ltrim(b.number+1) as c1 from @T a,master..spt_values b   
  81. where b.type='p' and b.number <5 and substring(col,0+right(number+1,1),1)='Y'  
  82. )  
  83.   
  84. select id, col=  
  85. stuff((select ','+c1 from maco t where id=maco.id for xml path('')), 1, 1, '')  
  86. from maco group by id  
  87.   
  88. /*  
  89. id          col  
  90. ----------- --------------------  
  91. 1           人1,人2,人3,人4  
  92. 2           人1,人5  
  93. 3           人4  
  94. 4           人1,人2,人4  
  95. */  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值