Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add split between duplicate value in SQL

I have SQL Server table that have duplicated value in field and need to split by slash character. All field start with alphabetical char end with ‘M’ char

For example:

     X123X123M
     Y1515Y1515M

and need to convert to:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

     X123/X123M
     Y1515/Y1515M

Thanks

>Solution :

Probably many ways to skin this cat, assuming you’re always looking for the same initial character repeated within the string, the following uses stuff to insert a slash at the appropriate location:

with t as (
    select 'X123X123M' val union all
    select 'Y1515Y1515M'
)
select val, Stuff(val,v,0,'/')
from t
cross apply(values(CharIndex(Left(val,1),val,2)))v(v)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading