简体   繁体   中英

Split function in SQL Server 2005

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM