views:

24

answers:

2

So,please help me to split value from table like below

field A has value below

13974;14098;14237;14269;14317;14319;14392;14393;13 257;13983;13820

and i need to split and down value in row like below

file B
13974
14098
14237
14269
....

please help me to supports many thanks

+4  A: 

Placing a 1:N relation into one column is considered bad database design, and SQL Server doesn't even try to support it.

Despite all that, it's fairly common. You can search around the web for one of the many fnSplit implementations. Here's one example:

CREATE FUNCTION dbo.fnSplit(
    @sInputList VARCHAR(8000) -- List of delimited items
  , @sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
) RETURNS @List TABLE (item VARCHAR(8000))

BEGIN
DECLARE @sItem VARCHAR(8000)
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
 BEGIN
 SELECT
  @sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
  @sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))

 IF LEN(@sItem) > 0
  INSERT INTO @List SELECT @sItem
 END

IF LEN(@sInputList) > 0
 INSERT INTO @List SELECT @sInputList -- Put the last item in
RETURN
END
GO
--Test
select * from fnSplit('1,22,333,444,,5555,666', ',')
select * from fnSplit('1##22#333##444','##')  --note second item has embedded #
select * from fnSplit('1 22 333 444  5555 666', ' ')
Andomar
A: 

Thanks very helpful , I'll try to applied.

lonjovy