?
Solved

SQl 2000 User defined function to loop through many text replace commands

Posted on 2010-01-12
4
Medium Priority
?
426 Views
Last Modified: 2012-06-27
I had hoped to be able to do something like the code below but if I remove all the comments it does nothing, if I leave just one line uncommented it works fine.  Now I'm not a SQL coder so I am not sure whats missing!

Here's the function.


CREATE FUNCTION dbo.fnReturnCleanedtext (@Text VARCHAR(5000))
RETURNS VARCHAR(5000) AS
BEGIN
      DECLARE @Result VARCHAR(5000)
--      Set @Result = REPLACE(@Text,'"', '''');
--      Set @Result = REPLACE(@Text,'&', '');
--      Set @Result = REPLACE(@Text,'[', '');
--      Set @Result = REPLACE(@Text,']', '');
--      Set @Result = REPLACE(@Text,' O ', '');
--      Set @Result = REPLACE(@Text,' Q ', '');
--      Set @Result = REPLACE(@Text,' G ', '');
--      Set @Result = REPLACE(@Text,' R ', '')
--      Set @Result = REPLACE(@Text,' ) ', '');
      Set @Result = REPLACE(@Text,'<FP>B',' ');
--      Set @Result = REPLACE(@Text,'<FP>I',' ');
--      Set @Result = REPLACE(@Text,'<FP>S',' ');
--      Set @Result = REPLACE(@Text,'<FP>T',' ');
--      Set @Result = REPLACE(@Text,'<FP>U',' ');
--      Set @Result = REPLACE(@Text,'<FP>W',' ');
--      Set @Result = REPLACE(@Text,'<FP>-','-');
--      Set @Result = REPLACE(@Text,CHAR(13),' ');
--      Set @Result = REPLACE(@Text,CHAR(10),' ');
--      Set @Result = REPLACE(@Text,CHAR(9),' ');
--      Set @Result = LTRIM(@Text);
--      Set @Result = RTRIM(@Text);
      RETURN @Result
END
0
Comment
Question by:shanedixon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 26295575
CREATE FUNCTION dbo.fnReturnCleanedtext (@Text VARCHAR(5000))
RETURNS VARCHAR(5000) AS
BEGIN
      DECLARE @Result VARCHAR(5000)
      Set @Result = REPLACE(@Text,'"', '''');
      Set @Result = REPLACE(@Result,'&', '');
      Set @Result = REPLACE(@Result,'[', '');
      Set @Result = REPLACE(@Result,']', '');
      Set @Result = REPLACE(@Result,' O ', '');
      Set @Result = REPLACE(@Result,' Q ', '');
      Set @Result = REPLACE(@Result,' G ', '');
      Set @Result = REPLACE(@Result,' R ', '')
      Set @Result = REPLACE(@Result,' ) ', '');
      Set @Result = REPLACE(@Result,'<FP>B',' ');
      Set @Result = REPLACE(@Result,'<FP>I',' ');
      Set @Result = REPLACE(@Result,'<FP>S',' ');
      Set @Result = REPLACE(@Result,'<FP>T',' ');
      Set @Result = REPLACE(@Result,'<FP>U',' ');
      Set @Result = REPLACE(@Result,'<FP>W',' ');
      Set @Result = REPLACE(@Result,'<FP>-','-');
      Set @Result = REPLACE(@Result,CHAR(13),' ');
      Set @Result = REPLACE(@Result,CHAR(10),' ');
      Set @Result = REPLACE(@Result,CHAR(9),' ');
      Set @Result = LTRIM(@Result);
      Set @Result = RTRIM(@Result);
      RETURN @Result
END
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 2000 total points
ID: 26295603
Or simply:

CREATE FUNCTION dbo.fnReturnCleanedtext (@Text VARCHAR(5000))
RETURNS VARCHAR(5000) AS
BEGIN
      Set @Text = REPLACE(@Text,'"', '''');
      Set @Text = REPLACE(@Text,'&', '');
      Set @Text = REPLACE(@Text,'[', '');
      Set @Text = REPLACE(@Text,']', '');
      Set @Text = REPLACE(@Text,' O ', '');
      Set @Text = REPLACE(@Text,' Q ', '');
      Set @Text = REPLACE(@Text,' G ', '');
      Set @Text = REPLACE(@Text,' R ', '')
      Set @Text = REPLACE(@Text,' ) ', '');
      Set @Text = REPLACE(@Text,'<FP>B',' ');
      Set @Text = REPLACE(@Text,'<FP>I',' ');
      Set @Text = REPLACE(@Text,'<FP>S',' ');
      Set @Text = REPLACE(@Text,'<FP>T',' ');
      Set @Text = REPLACE(@Text,'<FP>U',' ');
      Set @Text = REPLACE(@Text,'<FP>W',' ');
      Set @Text = REPLACE(@Text,'<FP>-','-');
      Set @Text = REPLACE(@Text,CHAR(13),' ');
      Set @Text = REPLACE(@Text,CHAR(10),' ');
      Set @Text = REPLACE(@Text,CHAR(9),' ');
      Set @Text = LTRIM(@Text);
      Set @Text = RTRIM(@Text);
      RETURN @Text
END
0
 

Author Comment

by:shanedixon
ID: 26295921
AC-

What if I wanted to store those replacement codes in a table could that UDF then read the table and loop through all the changes on each  @text?

say the changes were stored in a table with field Tags.  Is there a way to create the sql query and data loop in the UDF?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 26299699
I am sorry I just saw your question, but it looks like you have a solution in another thread.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

770 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