Sql Server去除表中的不可見字元
SET NOCOUNT ON
DECLARE @TblName VARCHAR(100)
DECLARE @UpdateString NVARCHAR(1000)
DECLARE @SelectString NVARCHAR(1000)
DECLARE @COlName VARCHAR(100)
DECLARE @COUNT INT
SET @TblName = '表名稱'--指定想要修改的表名
--定義遊標取出指定表內的資料型別是VARCHAR,char,nVARCHAR的欄位名稱
DECLARE cur_ColName CURSOR
FOR
SELECT col.name
FROM syscolumns AS col
inner join sysobjects AS obj ON col.ID = obj.ID
INNER join systypes AS typ ON col.xtype = typ.xtype
WHERE obj.xtype ='U'
AND obj.name = @TblName
AND typ.name IN ('VARCHAR','CHAR','NVARCHAR','NCHAR')
FOR READ ONLY
--開啟遊標
OPEN cur_ColName
FETCH NEXT FROM cur_ColName INTO @ColName
IF @@FETCH_STATUS<>0
BEGIN
PRINT '沒有對應表或欄位,'PRINT '請確認當前資料庫內有' + @TblName + '表,' PRINT '或該表內有VARCHAR、CHAR、NVARCHAR、NCHAR型別的欄位!' GOTO LABCLOSE
END--迴圈修改
WHILE @@FETCH_STATUS=0
BEGIN
--拼修改字串
--去掉左邊的不可見字元
SET @SelectString = 'SELECT @COU=COUNT(*)
FROM ' + @TblName +'
WHERE ASCII(LEFT(' + @ColName +',1))<32
AND '+ @ColName + ' IS NOT NULL'
EXEC sp_executesql @SelectString,N'@COU INT OUTPUT',
@COUNT OUTPUT WHILE @COUNT>0
BEGIN
SET @UpdateString =
' UPDATE ' + @TblName +
' SET ' + @ColName + '=RIGHT(' + @ColName + ',LEN(' + @ColName + ')-1)
WHERE ASCII(LEFT(' + @ColName + ',1))<32
AND ' + @ColName + ' IS NOT NULL'
EXEC sp_executesql @UpdateString
EXEC sp_executesql @SelectString,N'@COU INT OUTPUT',@COUNT OUTPUT
END
--去掉右邊的不可見字元
SET @SelectString = 'SELECT @COU=COUNT(*)
FROM ' + @TblName +'
WHERE ASCII(RIGHT(' + @ColName +',1))<32
AND '+ @ColName + ' IS NOT NULL'
EXEC sp_executesql @SelectString,N'@COU INT OUTPUT',
@COUNT OUTPUT WHILE @COUNT>0
BEGIN
SET @UpdateString = ' UPDATE ' + @TblName + ' SET '
+ @ColName + '=LEFT(' + @ColName + ',LEN(' + @ColName + ')-1)
WHERE ASCII(RIGHT(' + @ColName + ',1))<32
AND ' + @ColName + ' IS NOT NULL'
EXEC SP_EXECUTESQL @UpdateString
EXEC sp_executesql @SelectString,N'@COU INT OUTPUT',
@COUNT OUTPUT
END
PRINT 'column: ' + @ColName + '---ok'
FETCH NEXT FROM cur_ColName INTO @ColName
END
--關閉、釋放遊標
LABCLOSE: CLOSE cur_ColName
DEALLOCATE cur_ColName
GO
DECLARE @TblName VARCHAR(100)
DECLARE @UpdateString NVARCHAR(1000)
DECLARE @SelectString NVARCHAR(1000)
DECLARE @COlName VARCHAR(100)
DECLARE @COUNT INT
SET @TblName = '表名稱'--指定想要修改的表名
--定義遊標取出指定表內的資料型別是VARCHAR,char,nVARCHAR的欄位名稱
DECLARE cur_ColName CURSOR
FOR
SELECT col.name
FROM syscolumns AS col
inner join sysobjects AS obj ON col.ID = obj.ID
INNER join systypes AS typ ON col.xtype = typ.xtype
WHERE obj.xtype ='U'
AND obj.name = @TblName
AND typ.name IN ('VARCHAR','CHAR','NVARCHAR','NCHAR')
FOR READ ONLY
--開啟遊標
OPEN cur_ColName
FETCH NEXT FROM cur_ColName INTO @ColName
IF @@FETCH_STATUS<>0
BEGIN
PRINT '沒有對應表或欄位,'PRINT '請確認當前資料庫內有' + @TblName + '表,' PRINT '或該表內有VARCHAR、CHAR、NVARCHAR、NCHAR型別的欄位!' GOTO LABCLOSE
END--迴圈修改
WHILE @@FETCH_STATUS=0
BEGIN
--拼修改字串
--去掉左邊的不可見字元
SET @SelectString = 'SELECT @COU=COUNT(*)
FROM ' + @TblName +'
WHERE ASCII(LEFT(' + @ColName +',1))<32
AND '+ @ColName + ' IS NOT NULL'
EXEC sp_executesql @SelectString,N'@COU INT OUTPUT',
@COUNT OUTPUT WHILE @COUNT>0
BEGIN
SET @UpdateString =
' UPDATE ' + @TblName +
' SET ' + @ColName + '=RIGHT(' + @ColName + ',LEN(' + @ColName + ')-1)
WHERE ASCII(LEFT(' + @ColName + ',1))<32
AND ' + @ColName + ' IS NOT NULL'
EXEC sp_executesql @UpdateString
EXEC sp_executesql @SelectString,N'@COU INT OUTPUT',@COUNT OUTPUT
END
--去掉右邊的不可見字元
SET @SelectString = 'SELECT @COU=COUNT(*)
FROM ' + @TblName +'
WHERE ASCII(RIGHT(' + @ColName +',1))<32
AND '+ @ColName + ' IS NOT NULL'
EXEC sp_executesql @SelectString,N'@COU INT OUTPUT',
@COUNT OUTPUT WHILE @COUNT>0
BEGIN
SET @UpdateString = ' UPDATE ' + @TblName + ' SET '
+ @ColName + '=LEFT(' + @ColName + ',LEN(' + @ColName + ')-1)
WHERE ASCII(RIGHT(' + @ColName + ',1))<32
AND ' + @ColName + ' IS NOT NULL'
EXEC SP_EXECUTESQL @UpdateString
EXEC sp_executesql @SelectString,N'@COU INT OUTPUT',
@COUNT OUTPUT
END
PRINT 'column: ' + @ColName + '---ok'
FETCH NEXT FROM cur_ColName INTO @ColName
END
--關閉、釋放遊標
LABCLOSE: CLOSE cur_ColName
DEALLOCATE cur_ColName
GO
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16436858/viewspace-609100/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- C#移除字串中的不可見Unicode字元C#字串Unicode字元
- SQL Server安全審計中的常見疏忽NFSQLServer
- SQL Server中row_number函式的常見用法SQLServer函式
- SQL server 特殊字元"u0000"處理SQLServer字元
- 如何去除特殊字元字元
- SQL SERVER之分割槽表SQLServer
- SQL server 修改表資料SQLServer
- SQL Server 2008中Analysis Services的新特性——深入SQL Server 2008SQLServer
- 【SQL Server】常見系統函式SQLServer函式
- SQL Server 中的 ACID 屬性SQLServer
- 2.去除字元strip方法字元
- DbForge Studio for SQL Server入門教程:在表編輯器中建立表SQLServer
- Shell命令列中的特殊字元及其轉義(去除特殊含義)命令列字元
- SQL Server 操作要重建表被禁止SQLServer
- SQL Server 中的一些概念SQLServer
- SQL Server中的版本號如何理解SQLServer
- 【SQL Server】常見系統儲存過程SQLServer儲存過程
- SQL Server中常見負載均衡方法(下)VQSQLServer負載
- SQL Server中常見負載均衡方法(上)LNSQLServer負載
- SQL server根據表名查詢表主鍵SQLServer
- SQL Server中GROUP BY(連結)SQLServer
- .sql檔案匯入到sql server中SQLServer
- 【Case】SQL SERVER 屬性OWNER不可用於資料庫xxx。SQLServer資料庫
- SQL Server 表分割槽注意事項HXSQLServer
- SQL Server中的日期和時間:DATEADD()SQLServer
- 淺談SQL Server中的快照問題SQLServer
- 正則替換 修改字元 去除空格字元
- SQL Server資料庫恢復常見問題SQLServer資料庫
- 輸入一段字串,去除字串中重複的字元,並輸出字串字元
- SQL Server 批量生成資料庫內多個表的表結構SQLServer資料庫
- Sql Server資料庫類似正規表示式的字元處理問題SQLServer資料庫字元
- sql server中巢狀事務*SQLServer巢狀
- 正規表示式去除連續重複的字元字元
- 去除重複字母(不同字元的最小序列)問題字元
- Oracle資料庫中的不可見索引 invisible indexOracle資料庫索引Index
- SQL Server 資料表程式碼建立約束SQLServer
- SQL Server表分割槽刪除詳情DSCCSQLServer
- SQL Server 查詢表註釋和欄位SQLServer
- sql serverSQLServer