Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and Replace All Special Character in SQL [duplicate]

I want to copy all row in new column with replacing all special character with -. my code is below.

My table design

enter image description here

 select * from mycode
    UPDATE mycode
    SET newName = Replace(myname, '%[^0-9a-zA-Z]%', '-')

It's getting copy with my code but the special character are not replaced

Result enter image description here

like image 681
Sunil Acharya Avatar asked Jun 25 '15 12:06

Sunil Acharya


People also ask

How do I replace multiple special characters in SQL?

SELECT REPLACE(REPLACE(REPLACE(REPLACE('3*[4+5]/{6-8}', '[', '('), ']', ')'), '{', '('), '}', ')'); We can see that the REPLACE function is nested and it is called multiple times to replace the corresponding string as per the defined positional values within the SQL REPLACE function.

How remove all special characters in a column in SQL?

You can remove special characters from a database field using REPLACE() function. The special characters are double quotes (“ “), Number sign (#), dollar sign($), percent (%) etc.

How do you treat special characters in SQL?

Use braces to escape a string of characters or symbols. Everything within a set of braces in considered part of the escape sequence. When you use braces to escape a single character, the escaped character becomes a separate token in the query. Use the backslash character to escape a single character or symbol.

How to replace special characters in a string in Oracle SQL?

In Oracle SQL, you have three options for replacing special characters: Using the REPLACE function; Using the REGEXP_REPLACE function; Using the TRANSLATE function; Each of them has their pros and cons. REPLACE allows you to replace a single character in a string, and is probably the simplest of the three methods.

How do I find all special characters in SQL Server?

Finding special characters in SQL Server is easy to do using the inverse wildcard logic. Replacing all special characters found can be tedious if using multiple nested REPLACE statements. However, SQL Server has a similar function called TRANSLATE, which makes this effort much cleaner and simpler.

How to remove all special characters from a column in SQL?

If you want to just remove all special characters, you can use a function like this: 1 2 SELECT REGEXP_REPLACE(your_column, '[^0-9A-Za-z]' , '' ) FROM table ;

How do you replace a character in a list?

If you want to replace a long list of characters, you’ll need to specify a replacement character each time. There are 10 characters in the second parameter, so there needs to be 10 characters in the third parameter. You can also use the REGEXP_REPLACE function to replace special characters.


2 Answers

Try to create this function

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

and then do your UPDATE

   UPDATE mycode
    SET newName = dbo.RemoveSpecialChars(mycode)
like image 78
Galma88 Avatar answered Oct 07 '22 03:10

Galma88


Try this query

DECLARE @specialchar varchar(15)
DECLARE @getspecialchar CURSOR
SET @getspecialchar = CURSOR FOR
SELECT DISTINCT poschar 
    FROM   MASTER..spt_values S 
           CROSS APPLY (SELECT SUBSTRING(newName ,NUMBER,1) AS poschar from mycode ) t 
    WHERE  NUMBER > 0 

           AND NOT (ASCII(t.poschar) BETWEEN 65 AND 90 
                     OR ASCII(t.poschar) BETWEEN 97 AND 122 
                     OR ASCII(t.poschar) BETWEEN 48 AND 57)

OPEN @getspecialchar 
FETCH NEXT
FROM @getspecialchar INTO @specialchar 
WHILE @@FETCH_STATUS = 0
BEGIN
    UPDATE mycode 
        SET newName =Replace(myname,@specialchar,'')
FETCH NEXT
FROM @getspecialchar INTO @specialchar 
END
CLOSE @getspecialchar 
DEALLOCATE @getspecialchar 
like image 30
Mukesh Kalgude Avatar answered Oct 07 '22 03:10

Mukesh Kalgude