2008-09-09 72 views
7

我們有一些輸入數據,有時在末尾出現& nbsp字符。T-SQL trim&nbsp(以及其他非字母數字字符)

數據作爲varchar()從源系統進入,我們試圖將其轉換爲十進制失敗b/c這些字符。

LTRIM和RTRIM不刪除字符,所以我們不得不做這樣的事情:

UPDATE myTable 
SET myColumn = replace(myColumn,char(160),'') 
WHERE charindex(char(160),myColumn) > 0 

該方法適用於& NBSP,但有向任何非這樣做的好方法 - 字母數字(或在這種情況下數字)字符?

回答

9

This page有如何刪除非字母數字字符的示例:

-- Put something like this into a user function: 
DECLARE @cString VARCHAR(32) 
DECLARE @nPos INTEGER 
SELECT @cString = '90$%45623 *6%}~:@' 
SELECT @nPos = PATINDEX('%[^0-9]%', @cString) 

WHILE @nPos > 0 
BEGIN 
SELECT @cString = STUFF(@cString, @nPos, 1, '') 
SELECT @nPos = PATINDEX('%[^0-9]%', @cString) 
END 

SELECT @cString 
+0

這將刪除所有,但0-9(數字)的功能。 ....所以它不是真的在做_alpha_ numberic:P .. – 2010-02-19 04:54:35

1

如何被填充表?雖然可以在sql中進行清理,但更好的方法是將數據類型更改爲int,並在數據加載到數據庫之前清理數據(SSIS)。這是一個選項嗎?

17

這將刪除所有非字母數字chracters

CREATE FUNCTION [dbo].[fnRemoveBadCharacter] 
(
    @BadString nvarchar(20) 
) 
RETURNS nvarchar(20) 
AS 
BEGIN 

      DECLARE @nPos INTEGER 
      SELECT @nPos = PATINDEX('%[^a-zA-Z0-9_]%', @BadString) 

      WHILE @nPos > 0 
      BEGIN 
         SELECT @BadString = STUFF(@BadString, @nPos, 1, '') 
         SELECT @nPos = PATINDEX('%[^a-zA-Z0-9_]%', @BadString) 
      END 

      RETURN @BadString 
END 

使用的功能等:

UPDATE TableToUpdate 
SET ColumnToUpdate = dbo.fnRemoveBadCharacter(ColumnToUpdate) 
WHERE whatever 
+0

這比接受的答案要好 – 2014-03-24 00:17:02

0

對於大型數據集我有更好的運氣使用此功能來檢查的ASCII值。我已添加選項以僅保留字母,數字或字母數字基於參數。

--CleanType 1 - Remove all non alpanumeric 
--   2 - Remove only alpha 
--   3 - Remove only numeric 
CREATE FUNCTION [dbo].[fnCleanString] (
     @InputString varchar(8000) 
    , @CleanType  int 
    , @LeaveSpaces bit 
) RETURNS varchar(8000) 
AS 
BEGIN 

    -- // Declare variables 
    -- =========================================================== 
    DECLARE @Length  int 
     , @CurLength int = 1 
     , @ReturnString varchar(8000)='' 

    SELECT @Length = len(@InputString) 

    -- // Begin looping through each char checking ASCII value 
    -- =========================================================== 
    WHILE (@CurLength <= (@Length+1)) 
    BEGIN 
     IF (ASCII(SUBSTRING(@InputString,@CurLength,1)) between 48 and 57  AND @CleanType in (1,3)) 
     or (ASCII(SUBSTRING(@InputString,@CurLength,1)) between 65 and 90 AND @CleanType in (1,2)) 
     or (ASCII(SUBSTRING(@InputString,@CurLength,1)) between 97 and 122 AND @CleanType in (1,2)) 
     or (ASCII(SUBSTRING(@InputString,@CurLength,1)) = 32 AND @LeaveSpaces = 1) 
     BEGIN 
      SET @ReturnString = @ReturnString + SUBSTRING(@InputString,@CurLength,1) 
     END 
     SET @CurLength = @CurLength + 1 
    END 

    RETURN @ReturnString 
END 
0

如果移動可以用一個加號(+)開始,我將使用這樣

CREATE FUNCTION [dbo].[Mobile_NoAlpha](@Mobile VARCHAR(1000)) 
RETURNS VARCHAR(1000) 
AS 
BEGIN 
    DECLARE @StartsWithPlus BIT = 0 

    --check if the mobile starts with a plus(+) 
    IF LEFT(@Mobile, 1) = '+' 
    BEGIN 
     SET @StartsWithPlus = 1 

     --Take out the plus before using the regex to eliminate invalid characters 
     SET @Mobile = RIGHT(@Mobile, LEN(@Mobile)-1) 
    END 

    WHILE PatIndex('%[^0-9]%', @Mobile) > 0 
     SET @Mobile = Stuff(@Mobile, PatIndex('%[^0-9]%', @Mobile), 1, '') 

    IF @StartsWithPlus = 1 
     SET @Mobile = '+' + @Mobile 
    RETURN @Mobile 
END 
相關問題