Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
T-SQL 6
SQL Server 5
#SQL Server 5
#TSQL 4
BRH 3

Archive · View All
October 2010 2
April 2012 1
October 2011 1
September 2011 1
May 2011 1
April 2010 1
March 2010 1

Cleanse all character fields in a table

Sep 8 2011 11:56AM by Naomi   

This blog post will present a T-SQL solution for a 'cleansing' problem. Suppose, we need to remove all commas, single and double quotes from all character fields in a table. The presented stored procedure solves this problem, although for big tables and many characters to remove I will not recommend to use the presented solution as the performance may suffer.

I solved this problem based on the following MSDN thread 

CREATE PROCEDURE dbo.RemoveCharacters(@TableName sysname,
@BadChars NVARCHAR(20))

AS
 BEGIN
  IF OBJECT_ID('TempDB..#Columns','U') IS NOT NULL DROP TABLE #Columns
   SELECT Column_Name INTO #Columns 
   FROM INFORMATION_SCHEMA.COLUMNS C
   WHERE C.TABLE_NAME = @TableName 
   AND C.DATA_TYPE LIKE '%char' -- get list of all character columns in a table
   
   IF @@ROWCOUNT > 0 -- if we don't have char columns, no need to continue
   BEGIN 
   DECLARE @Loop INT, @ReplaceLine NVARCHAR(MAX), @Update NVARCHAR(MAX)
   SET @Loop = 2
   SET @ReplaceLine = 'REPLACE(,' + QUOTENAME(SUBSTRING(@BadChars,1,1),'''') + ','''')' -- construct replace with the placeholder
   WHILE @Loop <= LEN(@BadChars)
     BEGIN 
        SET @ReplaceLine = 'REPLACE(' + @ReplaceLine + ',' + QUOTENAME(SUBSTRING(@BadChars,@Loop,1),'''') + ','''')'
        SET @Loop = @Loop + 1
     END
    PRINT @ReplaceLine 
    SET @Update = ''
    
    SELECT @Update = @Update + ', ' + QUOTENAME(Column_Name) + 
    ' = ' +  REPLACE(@ReplaceLine, '', QUOTENAME(Column_Name))
    FROM #Columns 
    
    SET @Update = 'Update ' + QUOTENAME(@TableName) + '
    SET ' + STUFF(@Update,1,2,'')
    
    PRINT @Update
    
    EXECUTE(@Update)
    END
    END
    GO

And this is a sample of usage

EXECUTE dbo.RemoveCharacters 'BadChars', N',"'''                 
    SELECT * FROM dbo.BadChars

 

 

Tags: T-SQL, #SQL Server, #TSQL, SQL Server,


Naomi
33 · 6% · 1774
2
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

1  Comments  

  • Another way is to have a table with all characters to be deleted and use a function that cleans those characters http://beyondrelational.com/blogs/madhivanan/archive/2009/05/11/removing-unwanted-characters.aspx

    commented on Sep 14 2011 8:36AM
    Madhivanan
    3 · 39% · 12419

Your Comment


Sign Up or Login to post a comment.

"Cleanse all character fields in a table" rated 5 out of 5 by 2 readers
Cleanse all character fields in a table , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]