Solved

how do I convert tab to spaces in an sql field

Posted on 2012-03-14
2
337 Views
Last Modified: 2012-03-14
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.
0
Comment
Question by:gloriagalvez
2 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 37721489
ConvertTabsToSpaces

declare @tab as nvarchar(1)
set @tab = char(9)

return replace(@tempnote, @tab, ' ')
0
 

Author Comment

by:gloriagalvez
ID: 37722478
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.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
query question 12 32
SQL trigger 5 20
Acces SQL Insert Problem 6 31
Get rid of the last Last comma and space in a SQL statement. 7 14
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question