Wayne29
asked on
Split Text Into different Columns :
Hi,
SELECT ReferenceNumber, ......... FROM Table1 WHERE id=1
Where in ReferenceNumber is : AB/1234/23/DE
What I would like to get the OUTPUT is :
AB/1234/23/DE AB 1234 23 DE
I would like to split that lone string into 4 columns, I knew my format is fixed i.e. delimited by /
I have seen many examples where in text splitted into rows. But I would like the text to be splitted into columns.
Your help as before is much appreciated.
Regards
Wayne
SELECT ReferenceNumber, ......... FROM Table1 WHERE id=1
Where in ReferenceNumber is : AB/1234/23/DE
What I would like to get the OUTPUT is :
AB/1234/23/DE AB 1234 23 DE
I would like to split that lone string into 4 columns, I knew my format is fixed i.e. delimited by /
I have seen many examples where in text splitted into rows. But I would like the text to be splitted into columns.
Your help as before is much appreciated.
Regards
Wayne
drop function dbo.CSV_Values
go
create function dbo.CSV_Values(@str varchar(1000))
returns
@Table Table (value varchar(100))
as
begin
declare @ptr as smallint,
@counter smallint
declare @substr varchar(1000)
set @substr = ''
set @ptr = 1
set @counter = 0
while @ptr <=len (@str)+1
begin
if (substring(@str, @ptr, 1) = '/' ) or @ptr = len(@str) +1
begin
insert into @table values(substring(@str,len( @substr)+1 ,@counter) )
set @substr = substring(@str,1,@ptr)
set @counter = 0
end
else
set @counter = @counter +1
set @ptr = @ptr + 1
end
Return
end
go
SELECT dbo.csv_values(ReferenceNu mber), ......... FROM Table1 WHERE id=1
go
create function dbo.CSV_Values(@str varchar(1000))
returns
@Table Table (value varchar(100))
as
begin
declare @ptr as smallint,
@counter smallint
declare @substr varchar(1000)
set @substr = ''
set @ptr = 1
set @counter = 0
while @ptr <=len (@str)+1
begin
if (substring(@str, @ptr, 1) = '/' ) or @ptr = len(@str) +1
begin
insert into @table values(substring(@str,len(
set @substr = substring(@str,1,@ptr)
set @counter = 0
end
else
set @counter = @counter +1
set @ptr = @ptr + 1
end
Return
end
go
SELECT dbo.csv_values(ReferenceNu
ASKER
Hi Imran,
Your function is created successfully but when I try to run, I get the below message.
SELECT dbo.csv_values('AB/12/DE/C E')
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'dbo.csv_values'.
What am I missing?
Regards
Wayne
Your function is created successfully but when I try to run, I get the below message.
SELECT dbo.csv_values('AB/12/DE/C
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'dbo.csv_values'.
What am I missing?
Regards
Wayne
Hi Wayne,
Did you tried my solution???
Does it work??
Regards,
dduser
Did you tried my solution???
Does it work??
Regards,
dduser
ASKER
Hi ddUser,
Somehow your PROC/FUNC is not getting created due to the below errors.
can you correct so that I can try again?
Server: Msg 170, Level 15, State 1, Line 10
Line 10: Incorrect syntax near '@ReferenceNumber'.
Server: Msg 170, Level 15, State 1, Line 13
Line 13: Incorrect syntax near '@TempVal'.
Server: Msg 170, Level 15, State 1, Line 16
Line 16: Incorrect syntax near '@TempVal'.
Wayne
ASKER
Ok you missed the commas. now it runs atleast.
Hi,
it is returning a table try
SELECT * FROM dbo.csv_values('AB/12/DE/C E')
it is returning a table try
SELECT * FROM dbo.csv_values('AB/12/DE/C
ASKER
Hi Imran,
Thanks. But I need them as Columns. It is returning rows.
Wayne
Thanks. But I need them as Columns. It is returning rows.
Wayne
Create Procedure TempProc(@ReferenceNumber Varchar(100)) As
Declare @Col1 as Varchar(20)
Declare @Col2 as Varchar(20)
Declare @Col3 as Varchar(20)
Declare @Col4 as Varchar(20)
Declare @TempVal as Varchar(100)
Set @Col1 = Left(@ReferenceNumber,PatI ndex('%/%' ,@Referenc eNumber) - 1)
Set @TempVal = Right(@ReferenceNumber,len (@REferenc eNumber) - PatIndex('%/%'@ReferenceNu mber))
Set @Col2 = Left(@TempVal,PatIndex('%/ %',@TempVa l) - 1)
Set @TempVal = Right(@TempVal,len(@TempVa l) - PatIndex('%/%',@TempVal))
Set @Col3 = Left(@TempVal,PatIndex('%/ %',@TempVa l) - 1)
Set @TempVal = Right(@TempVal,len(@TempVa l) - PatIndex('%/%',@TempVal))
Set @Col4 = @TempVal
Select @Col1,@Col2,@Col3,@Col4
Return
Now it workss????
Declare @Col1 as Varchar(20)
Declare @Col2 as Varchar(20)
Declare @Col3 as Varchar(20)
Declare @Col4 as Varchar(20)
Declare @TempVal as Varchar(100)
Set @Col1 = Left(@ReferenceNumber,PatI
Set @TempVal = Right(@ReferenceNumber,len
Set @Col2 = Left(@TempVal,PatIndex('%/
Set @TempVal = Right(@TempVal,len(@TempVa
Set @Col3 = Left(@TempVal,PatIndex('%/
Set @TempVal = Right(@TempVal,len(@TempVa
Set @Col4 = @TempVal
Select @Col1,@Col2,@Col3,@Col4
Return
Now it workss????
ASKER
Yes in the 1st PatIndex, a comma is required.
You are very close to my solution.
Consider, I already have an existing SELECT statement that is retrieving 10 fields from Various tables using Joins etc.
Now How can I get these additional col1,col2,col3,col4 in the same select statement as it is a PROC.
Wayne
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many Thanks DdUser.
ASKER
Sorry. Thanks to Imran too.
Declare @Col1 as Varchar(20)
Declare @Col2 as Varchar(20)
Declare @Col3 as Varchar(20)
Declare @Col4 as Varchar(20)
Declare @TempVal as Varchar(100)
Set @Col1 = Left(@ReferenceNumber,PatI
Set @TempVal = Right(@ReferenceNumber,len
Set @Col2 = Left(@TempVal,PatIndex('%/
Set @TempVal = Right(@TempVal,len(@TempVa
Set @Col3 = Left(@TempVal,PatIndex('%/
Set @TempVal = Right(@TempVal,len(@TempVa
Set @Col4 = @TempVal
Select @Col1,@Col2,@Col3,@Col4
Return