-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
  • 68,902
  • 24
  • 138
  • 144
Rajesh P
  • 39
  • 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,465
  • 4
  • 48
  • 73
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
  • 68,902
  • 24
  • 138
  • 144