我有一个输入字符串
public void ModifyColorsCollection(int number)
{
//Logic to modify Colors collection here only
}
我想解析并将其添加到包含3列的表中,因此它应该是f.x
100|2|3,101|2|1,103|2|3.
类似的其他数据,以逗号分隔为记录, col1 col2 col3
100 2 3
为列。
由于
NIK
答案 0 :(得分:0)
试试这种方式
DECLARE @TAB TABLE(COLUMN1 INT, COLUMN2 INT, COLUMN3 INT)
DECLARE @STRING VARCHAR(MAX)='100|2|3,101|2|1,103|2|3,'
SELECT @STRING = 'SELECT ' + REPLACE( REPLACE (@STRING, ',','
UNION ALL
SELECT '),'|',',')
SELECT @STRING = SUBSTRING(@STRING,1,LEN(@STRING)-18)
INSERT INTO @TAB
EXEC(@STRING)
SELECT * FROM @TAB
结果将是
+---------+---------+---------+
| COLUMN1 | COLUMN2 | COLUMN3 |
+---------+---------+---------+
| 100 | 2 | 3 |
| 101 | 2 | 1 |
| 103 | 2 | 3 |
+---------+---------+---------+
答案 1 :(得分:0)
或者这样:
SELECT
LEFT(value, Charindex('|', value) - 1),
SUBSTRING(value, Charindex('|', value) + 1,Len(value) - Charindex('|', Reverse(value)) - Charindex('|', value)),
RIGHT(value, Charindex('|', Reverse(value)) - 1)
FROM
string_split('100|2|3,101|2|1,103|2|3',',')
答案 2 :(得分:0)
Declare @temp table(col1 int,col2 int ,col3 int)
Declare @pos int,@str nvarchar(max),@len int
Set @str='100|2|3'
Set @pos=1
Select @len=len(@str)
Insert into @temp
Select substring(@str,@pos,charindex('|',@str,@pos)-1),
substring(@str,charindex('|',@str,@pos)+1,charindex('|',@str,@pos)-3),
substring(@str,charindex('|',@str,charindex('|',@str,@pos)+1)+1,@len)
Select * from @temp
答案 3 :(得分:0)
;WITH tb(s)AS(
SELECT '100|2|3' UNION
SELECT '101|2|1' UNION
SELECT '103|2|3'
)
SELECT PARSENAME(REPLACE(s,'|','.'),3)
,PARSENAME(REPLACE(s,'|','.'),2)
,PARSENAME(REPLACE(s,'|','.'),1)
FROM tb
(No column name) (No column name) (No column name) 100 2 3 101 2 1 103 2 3