I want to copy all row in new column with replacing all special character with -. my code is below.
My table design
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
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.
You can remove special characters from a database field using REPLACE() function. The special characters are double quotes (“ “), Number sign (#), dollar sign($), percent (%) etc.
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.
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.
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.
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 ;
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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With