Parse SQL string

I have a table with a column that has string text with 4 tab characters separating values in the string. I need to take the content of the column and   create a temp table with the delimited values from that column (i.e. Column1, column2,column3, column4).

Any ideas?
jdr0606Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leo TorresSQL DeveloperCommented:
I would need to know more so the 10, 20, 30 are dynamic variables. But this is the general idea


Select substring(replace(Col1,char(9),''), 0, len(col1))
,substring(replace(Col1,char(9),''), 10, len(col2))
,substring(replace(Col1,char(9),''), 20, len(col3))
,substring(replace(Col1,char(9),''), 30, len(col4))

Open in new window

jdr0606Author Commented:
String content (Assume space is the tab)
LOCATION #20 58 $550.42
LOCATION #2001 13 $561.34
LOCATION #2002 26 $1,122.68
LOCATION #2003 7 $302.26
LOCATION #2004 8 $345.44

TempTable
Column1            Column2            Column3            Column4
LOCATION              #20                            58                            $550.42
LOCATION              #2001                    13                            $561.34
LOCATION              #2002                    26                            $1,122.68
LOCATION              #2003                    7                            $302.26
LOCATION              #2004                    8                            $345.44
Leo TorresSQL DeveloperCommented:
Easiest way to do this is

Select * into #temp
from (
Select 'LOCATION #20 58 $550.42' as string 
Union
Select 'LOCATION #2001 13 $561.34'
union
Select 'LOCATION #2002 26 $1,122.68'
union
Select 'LOCATION #2003 7 $302.26'
union
select 'LOCATION #2004 8 $345.44'
) a

Select SUBSTRING(string,0,dbo.CHARINDEX2(' ',string,1)) location
,SUBSTRING(string,dbo.CHARINDEX2(' ',string,1)+1,dbo.CHARINDEX2(' ',string,2)-dbo.CHARINDEX2(' ',string,1)) location_Num
,SUBSTRING(string,dbo.CHARINDEX2(' ',string,2)+1,dbo.CHARINDEX2(' ',string,3)-dbo.CHARINDEX2(' ',string,2)) Quantity
,SUBSTRING(string,dbo.CHARINDEX2(' ',string,3)+1,Len(string)-dbo.CHARINDEX2(' ',string,3)) Price
from #temp

Open in new window



Here is the custom function
/****** Object:  UserDefinedFunction [dbo].[CHARINDEX2]    Script Date: 6/18/2015 10:58:15 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[CHARINDEX2]
(
@TargetStr varchar(8000), 
@SearchedStr varchar(8000), 
@Occurrence int
)

RETURNS int

as
begin

declare @pos int, @counter int, @ret int, @posLen int

set @pos = CHARINDEX(@TargetStr, @SearchedStr,0)
set @counter = 1

if @Occurrence = 1 set @ret = @pos

else
begin

while (@counter < @Occurrence)
	begin

		select @ret = CHARINDEX(@TargetStr, @SearchedStr, @pos + 1)
	
		set @counter = @counter + 1

		set @pos = @ret
			
	if 	@pos = 0
		Begin
			set @pos = @ret
			Break 	
		END
	end


end

RETURN(@ret)

end

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jdr0606Author Commented:
Excellent!

Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.