如何基于逗号分隔更新字段 [英] How to update field based on comma separated

查看:70
本文介绍了如何基于逗号分隔更新字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

无法根据逗号分隔更新  字段 

样本数据

mycode     mysource
1,1,1289    DHM
1,4,3490    DHM
2,6,5678    DHM

我突然发现我的代码错误,不足以区分记录数据

I suddenly i found my code is wrong and not enough to make distinguish between records data

这样我决定修改任何记录都有 我的源DHM和代码分隔逗号2逗号

so that i decided to modify any record have  my source DHM and code separated comma 2 comma

如下:

我需要从

mycode     mysource  
1,1,1289    DHM        will change to 1,1,2019,1289
1,4,3490    DHM        will change to 1,4,2019,3490
2,6,5678    DHM        will change to 2,6,2019,5678

以便如何更新上述记录将2019添加到我的代码中 当mysource = DHM

so that how to update records above by add 2019 to my code  when mysource =DHM

作为例子1,1,2019,1289等等

as example above 1,1,2019,1289 ,etc,etc

推荐答案

create table concat
(
 mycode varchar(100)
);
  
  insert into concat values('1,1,1289');
  
 
  
  update concat set mycode=stuff(mycode,second,1,',2019,') from concat 
  cross apply(values (charindex(',',mycode))) A([First])
  cross apply(values (charindex(',',mycode,First)+First)) B([second])
  
  
  select * from concat;





这篇关于如何基于逗号分隔更新字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆