Avatar of gloriagalvez
gloriagalvez
 asked on

how do I convert tab to spaces in an sql field

Hello:
I have the following function which I found in the internet and modified for my purposes, and I cannot make it to work.  I keep getting the same error-
"Invalid length parameter passed to the LEFT or SUBSTRING function."
I tried everything as shrinking the field size for the parameter and matching all variables to the same size, also changing the varchar for nvarchar, etc.
Please help!

ALTER FUNCTION [dbo].[ConvertTabsToSpaces] (@String varchar(7000))  
RETURNS varchar(7000) AS  
BEGIN
DECLARE
@start int,
@tempnote varchar(7000),
@temp char(1)


set @start=CHARINDEX(char(9), @String)
set @tempnote=substring(@String,1,(@start-1)) + ' '

While len(@String) > @start
begin
set @temp=substring(@String,@start,1)

IF @temp <> char(9)
begin
set @tempnote=@tempnote + @temp
end
else
begin
set @tempnote=@tempnote + ' '
end
SET @start=@start+1

END

RETURN(@tempnote)
end

thank you.
Microsoft SQL ServerDB Reporting ToolsSQL

Avatar of undefined
Last Comment
gloriagalvez

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Kyle Abrahams

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
gloriagalvez

ASKER
Hello:
Thank you very much.  Since the field had different kind of characters, I had to use the following:

 Replace(Replace(Replace(Replace(Replace(Replace(Replace('|' + FTRN_HISTORY.EXTENDED_DESCRIPTION + '|', char(9), ''), char(35), ''), char(10), ''), char(11), ''), char(12), ''), char(44), ''), char(13), '') AS 'DESCRIPTION'

Thank you again.
Your help has saved me hundreds of hours of internet surfing.
fblack61