清理sql server中的数据

时间:2017-03-06 06:25:35

标签: sql sql-server data-cleansing

有一个表格包含一些字段,我想清理字段并从字符串中删除一些字符。我使用了replace语句,但是对于某些字符不起作用所以我写了一个带2循环的函数并检查字符串的字符,但是它太慢了。在这方面你能帮我吗? 我想提一下,有一个表格,我插入了所有普通字符并用它检查字符串。 代码如下:

CREATE FUNCTION [dbo].[replace_char_Clean] (@str1 nvarchar(max))
RETURNS nvarchar(max)
AS
BEGIN
  DECLARE @i int = 1;
  DECLARE @string nvarchar(max);
  SET @string = @str1;
  SET @string = REPLACE(@string, ' ', '<>');
  DECLARE @count int;
  DECLARE @row int;
  SELECT
    @count = COUNT([DBO].[alaem].num)
  FROM [DBO].[alaem];
  DECLARE @VRB int;

  -- replace all arabic with persian characters
  SET @string = LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
  REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
  REPLACE(@string, Nchar(1610), Nchar(1740)), N'ئ', N'ی'), N'ي', Nchar(1740)), Nchar(1571), nchar(1575)), NCHAR(1603), NCHAR(1705)), Nchar(1573), nchar(1575)), NCHAR(1574), NCHAR(1740)), N'ی', NCHAR(1740))
  , N'‍‍‍‍‍‍‍‍‍‍ژ', NCHAR(01688)), NCHAR(1572), Nchar(1608)), NCHAR(01577), Nchar(1607)), NCHAR(1603), NCHAR(1705)), NCHAR(1597), NCHAR(1740)), NCHAR(1598), NCHAR(1740)), NCHAR(1599), NCHAR(1740)),
  nchar(1650), nchar(1575)), nchar(1651), nchar(1575)), NCHAR(1654), NCHAR(1608)), NCHAR(1655), NCHAR(1608)), NCHAR(1656), NCHAR(1740)), NCHAR(1728), NCHAR(1607)), NCHAR(1729), NCHAR(1607)),
  NCHAR(1730), NCHAR(1607)), NCHAR(1731), NCHAR(1607)), NCHAR(
  1735), NCHAR(1608)), NCHAR(1736), NCHAR(1608)), NCHAR(1737), NCHAR(1608)), NCHAR(1738), NCHAR(1608)), NCHAR(1739), NCHAR(1608)), NCHAR(1717), NCHAR(1604)), NCHAR(1718), NCHAR(1604)),
  NCHAR(1719), NCHAR(1604)), NCHAR(1720), NCHAR(1604)), NCHAR(1708), NCHAR(1705)), NCHAR(1709), NCHAR(1705)), NCHAR(1710), NCHAR(1705)), NCHAR(1664), NCHAR(1662)), NCHAR(1659), NCHAR(1576)),
  NCHAR(1654), NCHAR(1608)), NCHAR(1681), NCHAR(1688)), NCHAR(1721), NCHAR(1606)), NCHAR(1723), NCHAR(1606)), NCHAR(1724), NCHAR(1606))));
  SET @row = 1;


  WHILE @i <= LEN(@string)
  BEGIN

    SET @row = 1

    WHILE (@row <= @count)
    BEGIN
      SELECT
        @VRB = num
      FROM [DBO].[alaem]
      WHERE @row = id;
      IF UNICODE(SUBSTRING(@string, @i, 1)) = @VRB
      BEGIN
        SET @string = @string;
        SET @i = @i + 1;
        SET @row = 1
        CONTINUE;
      END --if
      ELSE
      BEGIN
        SET @row = @row + 1;
      END --else
    END   -- while
    IF @row > @count
    BEGIN
      IF @i <= LEN(@string)
        SET @string = STUFF(@string, @i, 1, '');
    END
  END

  SET @string = REPLACE(REPLACE(REPLACE(@string, ' ', '<>'), '><', ''), '<>', ' ');

  RETURN @string;

END

由于

0 个答案:

没有答案