从2个符号中拆分数据

时间:2013-11-26 08:45:02

标签: sql sql-server

有表

CREATE TABLE #tbl 
(
   id int identity(1,1),
   obj_type int ,
   obj_id nvarchar(50)
)

有以下数据: 153:0 | 114:0 | 147:0 | 148:0 | 152:0 | 155:0 想要在“:”之前插入哪些数据到obj_id,哪些数据在“旁边:”插入tu obj_type。它必须像

id       obj_type     obj_id
1           0           153
2           0           114
3           0           147
4           0           148
5           0           152
6           0           155

存储过程如何?不起作用

4 个答案:

答案 0 :(得分:2)

declare @S varchar(100) = '153:0|114:0|147:0|148:0|152:0|155:0'

declare @xml xml

select @xml = '<item><value>'+replace(replace(@s, ':','</value><value>'), '|','</value></item><item><value>')+'</value></item>'

select N.value('value[1]', 'int') as obj_id,
       N.value('value[2]', 'int') as obj_type
from @xml.nodes('item') as T(N)

SQL Fiddle

答案 1 :(得分:0)

你可以等一些专家回答 到那时你可以给它一次机会

insert into #tbl 
SELECT LEFT(splitdata, CHARINDEX(':', splitdata) - 1) AS obj_id, 
       RIGHT(splitdata, CHARINDEX(':', REVERSE(splitdata)) - 1) AS obj_type from (select splitdatafrom fnSplitString(parameterName,'|')

现在您可以像这样编写stringsplit函数


CREATE FUNCTION [dbo].[fnSplitString] 
( 
    @string NVARCHAR(MAX), 
    @delimiter CHAR(1) 
) 
RETURNS @output TABLE(splitdata NVARCHAR(MAX) 
) 
BEGIN 
    DECLARE @start INT, @end INT 
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
    WHILE @start < LEN(@string) + 1 BEGIN 
        IF @end = 0  
            SET @end = LEN(@string) + 1

        INSERT INTO @output (splitdata)  
        VALUES(SUBSTRING(@string, @start, @end - @start)) 
        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @string, @start)

    END 
    RETURN 
END

答案 2 :(得分:0)

另一种解决方案:

Create FUNCTION [dbo].[SplitString]
    (
        @List NVARCHAR(MAX),
        @Delim VARCHAR(255)
    )
    RETURNS TABLE
    AS
        RETURN ( SELECT [Value] FROM 
          ( 
            SELECT 
              [Value] = LTRIM(RTRIM(SUBSTRING(@List, [Number],
              CHARINDEX(@Delim, @List + @Delim, [Number]) - [Number])))
            FROM (SELECT Number = ROW_NUMBER() OVER (ORDER BY name)
              FROM sys.all_objects) AS x
              WHERE Number <= LEN(@List)
              AND SUBSTRING(@Delim + @List, [Number], LEN(@Delim)) = @Delim
          ) AS y
        );

采取的形式:T-SQL split string

然后选择值:

Declare 
@Text varchar (100) = '153:0|114:0|147:0|148:0|152:0|155:0',
@Delim varchar(50) = ':0|'

select case when CHARINDEX(':0', Value) > 0 then Left(Value, Len(Value)-2) else Value   End AS Result  from dbo.SplitString(@Text, @Delim) 

答案 3 :(得分:0)

CREATE procedure   [dbo].[Insert_procedure] 
    @inputString varchar(max)
    AS
    BEGIN   
    set @inputString  ='2153:770|114:0|147:0|148:0|152:0|155:0' Declare @delimiter char(1) = '|' Declare @delimiter_Colon char(1) = ':' 
    DECLARE @chIndex int DECLARE @chIndex1 int DECLARE @item varchar(100)Declare @ReverseString varchar(max)    
    SELECT @ReverseString =   Reverse(substring(reverse(@inputString), 1, 1))
    IF(@ReverseString <> '|')
    set @inputString = @inputString +'|'    
    WHILE CHARINDEX(@delimiter, @inputString, 0) <> 0
        BEGIN            
            SET @chIndex = CHARINDEX(@delimiter, @inputString, 0)            
            SELECT @item = SUBSTRING(@inputString, 1, @chIndex - 1)           
            IF LEN(@item) > 0
                BEGIN
                 set @chIndex1 = CHARINDEX(@delimiter_Colon, @item, 0)
                 Declare @obj_type int Declare @obj_id varchar(50)
                 SELECT @obj_id = SUBSTRING(@item, @chIndex1+1,len(@item)) SELECT @obj_type = SUBSTRING(@item,1,@chIndex1-1)                  
                 Insert into TEST(obj_type,obj_id) values (@obj_type,@obj_id)                                                                             
                END           
            SELECT @inputString = SUBSTRING(@inputString, @chIndex + 1, LEN(@inputString))
        END
   END