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....
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....
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
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', '_'