ahmed elbarbary

ahmed elbarbary

  • 597
  • 1.6k
  • 282.4k

When split comma separated to text and value not working

Jun 28 2020 10:47 PM
I work on SQL server 2012 I face issue : I can't split Value column to Text Unit and Value Unit in case I have on Value column multiple value separated by comma .
as example
  1. Value ValueUnit TextUnit  
  2. 1.71V, 2.375V, 3.135V 1.71 V, 2.375V, 3.135V     
  3. have issue when separate it to text and value  
correct must be as line below:
  1. Value ValueUnit TextUnit  
  2. 1.71V, 2.375V, 3.135V 1.71,2.375,3.135 V  
sample Data as below :
  1. create table #finaltable  
  2. (  
  3. Value nvarchar(50),  
  4. TextUnit nvarchar(50),  
  5. ValueUnit nvarchar(50)  
  6. )  
  7. insert into #finaltable(Value)  
  8. values  
  9. ('1.71V, 2.375V, 3.135V'),  
  10. ('1.89V, 2.625V, 3.465V'),  
  11. ('1.8V')  
  12. update ft set ValueUnit=substring(ft.Value,1,ca.Posit),TextUnit=substring (ft.Value,Posit+1,50) from #FinalTable ft  
  13. cross apply (select PATINDEX('%[0-9.][^0-9.]%',ft.Value))ca (Posit)  
  14. select * from #finaltable  
when you run statement above it will display issue on value have comma separated
on record number 1 and number 2 but number 3 it work perfect
so How to solve issue on records 1 and 2 have values with separated comma ?
Expected Result it must be as below
  1. Value ValueUnit TextUnit  
  2. 1.71V, 2.375V, 3.135V 1.71,2.375,3.135 V  
  3. 1.89V, 2.625V, 3.465V 1.89,2.625,3.465 V  
  4. 1.8V 1.8 V  
wrong values as below AND I don't need Below :
  1. Value TextUnit ValueUnit  
  2. 1.71V, 2.375V, 3.135V V, 2.375V, 3.135V 1.71 --have issue on this line  
  3. 1.89V, 2.625V, 3.465V V, 2.625V, 3.465V 1.89 --have issue on this line  

Answers (3)