Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split String into Columns

Status
Not open for further replies.

RJL1

Technical User
Oct 3, 2002
228
US
Hello,

I have the following string that I would like to split in to columns so I can betted deliver the data in a report

Code:
Sequence 50, Shipment 81449098, Item E0191901 - No Record.

I like the end result ot be something like this. My SQL is not up to this level yet so I'm reaching out to the SQL gurus to help me with this.

Code:
column1       Column2            Column3        Column4
Sequence 50   Shipment 81449098  Item E0191901  No Record.


Thanks
Any assistance is apreaicted

RJL
 
declare @column1 varchar(max), @column2 varchar(max), @column3 varchar(max), @column4 varchar(max), @pos int

set @pos = CHARINDEX(',', @STR)
IF @pos > 0
begin
SET @Column1 = LEFT(@STR, @pos -1 )
SET @STR = SUBSTRING(@STR, @pos + 1, 8000)
set @pos = CHARINDEX(',', @STR)
IF @pos > 0
begin
SET @Column2 = LEFT(@STR, @pos -1 )
SET @STR = SUBSTRING(@STR, @pos + 1, 8000)
set @pos = CHARINDEX(' - ', @STR)
if @pos > 0
set @Column3 = LEFT(@STR, @pos -1 )
set @Column4 = SUBSTRING(@STR, @pos + 1, 8000)
end
end
end

select @Column1 as Column1, @Column2 as Column2, @Column3 as Column3, @Column4 as Column4

From the top of my head.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top