sql server用null替换特殊字符

时间:2015-05-06 12:33:08

标签: sql sql-server netezza

我无法替换特殊字符。你能帮帮我吗?

我的意见是:

Mrs঩Montero
输出应该是:

Mrs Montero

特殊字符未正确显示。请看下面的图片。  http://i.stack.imgur.com/b2SdY.jpg

4 个答案:

答案 0 :(得分:2)

如果您只是选择,请执行:

Update mytable
Set textct = Replace(textct, Special_char, ' ')

如果您要更新,请执行以下操作:

Select Replace(cast(textct as varchar),cast(Special_char as varchar), ' ')
from mytable

假设这些是nvarchars,那么这样做:

Create Function RemoveSpecialCharacters (@text nvarchar(max))
Returns varchar(4000)
AS
BEGIN
Declare @Return varchar(4000) = Cast(@text as varchar(4000))

While PatIndex('%[^a-z ]%', @Return) > 0
        Set @Return = Stuff(@Return, PatIndex('%[^a-z ]%', @text), 1, ' ')

Return @Return
END

Select RemoveSpecialCharacters(textct) from mytable

如果要删除所有特殊字符,则需要使用如下函数:

id

答案 1 :(得分:2)

创建此功能:

CREATE function RemoveSpecialCharacters(@Temp nvarchar(4000))
returns varchar(4000)
as
BEGIN
  DECLARE @KeepValues as varchar(100)
  -- you can add more characters like hyphen if you also want to keep those
  SET @KeepValues = '%[^A-Z0-9 ]%'
  WHILE PatIndex(@KeepValues, @Temp) > 0
    SET @Temp = Stuff(@Temp, PatIndex(@KeepValues, 1, ' ')
  RETURN @Temp
END

现在您可以替换字符。

SELECT dbo.RemoveSpecialCharacters('abc!"¤# ?123')

结果:

abc      123

答案 2 :(得分:1)

source你可以创建一个这样的函数:

create function dbo.RemoveSpecialChars (@s varchar(256)) returns varchar(256)
   with schemabinding
begin
   if @s is null
      return null
   declare @s2 varchar(256)
   set @s2 = ''
   declare @l int
   set @l = len(@s)
   declare @p int
   set @p = 1
   while @p <= @l begin
      declare @c int
      set @c = ascii(substring(@s, @p, 1))
      if @c between 48 and 57 or @c between 65 and 90 or @c between 97 and 122
         set @s2 = @s2 + char(@c)
      set @p = @p + 1
      end
   if len(@s2) = 0
      return null
   return @s2
   end

<强> SQL FIDDLE DEMO

如果您只有那个特殊字符,那么您可以使用它:

SELECT replace('Mrs঩Montero', '঩', ' ') 

<强> SQL FIDDLE DEMO

答案 3 :(得分:1)

你可以

select 
    textct,
    replace(textct collate Latin1_General_BIN, nchar(2473), ' ')
from
    mytable