-2

I have a string like val1_val2,val3_val4 and I need to split these values to tabled data as follows.

column1 column2
 Val1     val2 
 Val3     val4

Thanks in advance....

Darren
  • 66,506
  • 23
  • 132
  • 141
Rajesh P
  • 19
  • 1
  • 6

2 Answers2

3

A split function can be found here

declare @str varchar(100) 
set @str = "val1_val2,val3_val4"


declare @str varchar(100) = 'val1_val2,val3_val4'

select substring(f.value, 0, charindex('_', f.value)) as val1
      ,substring(f.value, charindex('_', f.value) + 1, LEN(f.value) ) as val2
from dbo.fnSplitString(@str, ',') f
Community
  • 1
  • 1
Adrian Iftode
  • 15,175
  • 3
  • 44
  • 71
0

There is a nice answer here:

http://www.codeproject.com/Articles/7938/SQL-User-Defined-Function-to-Parse-a-Delimited-Str

Using this function you could just use:

SELECT fn_ParseText2Table 'val1_val2,val3_val4', '_'
Darren
  • 66,506
  • 23
  • 132
  • 141