How to replace this special character at all the places - sql

I tried the following the ways, but result could not be as per requirement( I mean getting same after running replace query). how to replace all the places that special character only?
Here is the Query.
select
REPLACE(description,'‚'COLLATE Latin1_General_BIN, N'‚')
from Zstandars_25Feb2015 where Colname = 56
Result:
.
....the form 𝘹 + 𝘱 = 𝘲 and 𝘱𝘹 = 𝘲 for cases in which 𝘱, 𝘲 and
𝘹 are all nonnegative ..........

whats the type of your description field?
if its varchar, you shouldnt face any problem. I have run your query in my test table and i didnt get such a error, it might be something with your collation, remove collate and try it again or change the collation to something else.

-- Removes special characters from a string value.
-- All characters except 0-9, a-z and A-Z are removed and
-- the remaining characters are returned.
-- Author: Christian d'Heureuse, www.source-code.biz
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

By keeping N, I am able to fetch the data..
select
REPLACE(description,N'‚'COLLATE Latin1_General_BIN, N'‚')
from Zstandars_25Feb2015 where Colname = 56
Thanks all for the help.

Related

SQL Server to Excel using Microsoft Query varchar not showing

I have read a lot of posts that the simplest answer to the issue of special character is to replace your newline characters before running your query. I tried:
REPLACE(REPLACE(Field1, CHAR(10), ' '), CHAR(13), ' ')
I pasted the value into Notepad++ and there is no longer any special characters at least what I can see, I even did a TOP 1 to try only 1 row yet when I run the same query in the Microsoft Query Editor window in Excel the value comes out blank can any shed some light or direct me to another post answer I might have missed.
Not pretty but does the job.
CREATE Function dbo.ufn_RemoveSpecialChars (#str varchar(256)) returns varchar(256)
with schemabinding
begin
if #str is null
return null
declare #str2 varchar(256)
set #str2 = ''
declare #strlen int
set #strlen = len(#str)
declare #p int
set #p = 1
while #p <= #strlen begin
declare #code int
set #code = ascii(substring(#str, #p, 1))
if #code between 32 and 127
set #str2 = #str2 + char(#code)
set #p = #p + 1
end
if len(#str2) = 0
return null
return #str2
end

SQL REPLACE special characters with value from another table

All,
I am trying to replace the special characters in a string with the URL
encoding values to which they correspond. Below is some example code I have
been working with.
Thanks for the help.
create table #url_encoding_lookup(character varchar(10), code varchar (20))
insert into #url_encoding_lookup (character, code)
values
('!', '%21'),
('"', '%22'),
('#', '%23'),
('$', '%24'),
('%', '%25'),
('&', '%26'),
('''', '%27'),
('(', '%28'),
(')', '%29'),
('*', '%2A'),
('+', '%2B'),
(',', '%2C'),
('-', '%2D'),
('.', '%2E'),
('/', '%2F')
Create table #data
(string varchar (200))
insert into #data
values
('Jim (BoB)'),
('Will''s Place'),
('Auto-Mart')
select * from #data
select * from #url_encoding_lookup
desired results would be
Jim %28Bob%29
Will%27s Place
Auto%2DMart
Create procedure
BEGIN
DECLARE _end BOOLEAN DEFAULT FALSE;
DECLARE _result CHAR(200) DEFAULT str;
DECLARE _find VARCHAR(32);
DECLARE _replace VARCHAR(32);
DECLARE _cur CURSOR FOR SELECT _character, _code FROM url_encoding_lookup;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET _end = TRUE;
OPEN _cur;
_loop: LOOP
FETCH _cur INTO _find, _replace;
IF _end THEN
LEAVE _loop;
END IF;
SET _result = REPLACE(_result, _find, _replace);
END LOOP _loop;
CLOSE _cur;
RETURN _result;
END
Then
SELECT _replace_chars(name) FROM `data`
Result
Jim %28BoB%29
Will%27s Place
Auto%2DMart
ALTER FUNCTION [dbo].[udf_ReplaceYouCoded]
(
#the_string nvarchar(max)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
declare #temp_field nvarchar(max)
create table #url_encoding_lookup(character varchar(10), code varchar (20))
insert into #url_encoding_lookup (character, code)
values
('!', '%21'),
('"', '%22'),
('#', '%23'),
('$', '%24'),
('%', '%25'),
('&', '%26'),
('''', '%27'),
('(', '%28'),
(')', '%29'),
('*', '%2A'),
('+', '%2B'),
(',', '%2C'),
('-', '%2D'),
('.', '%2E'),
('/', '%2F')
declare #x as int
set #x = 1
--LOOP #the_string
while #i < len(#the_string)
if(substring(#the_string,x,1) = (select character from #url_encoding_lookup where character = substring(#the_string,x,1)))
begin
#temp_field = #temp_field + (select code from #url_encoding_lookup where character = substring(#the_string,x,1))
end
else
begin
#temp_field = #temp_field + substring(#the_string,x,1)
end
select #x = #x + 1
end
RETURN #temp_field
END
ALTER function [dbo].[udf_ReplaceSpecialChars]
(#s varchar(256))
returns varchar(256)
as
begin
-- declare #s varchar(256) set #s = 'Jim (P)' --test
if #s is null
return null
declare #s2 varchar(256)
set #s2 = '' --set variable to empty string. ready to recieve values
declare #l int
set #l = len(#s) --determin the number of characters in #s
declare #p int
set #p = 1 --set beginning string position
while #p <= #l begin
declare #c int
set #c = ascii(substring(#s, #p, 1)) --find the ascii number for 1st
character
declare #nc varchar(256)
set #nc = (select code from url_encoding_lookup where ascii_code = #c) --
get corresponding URL encoding string from lookup table
if #c between 33 and 47 or #c between 58 and 64 or #c between 91 and 96
or #c between 123 and 255 --when looping through each character, if special
character
set #s2 = #s2 + #nc --then use string from lookup table
else if #c = 32 or #c between 48 and 57 or #c between 65 and 90 or #c
between 97 and 122 --if character is not special
set #s2 = #s2 + char(#c) --then find char value of character
set #p = #p + 1 --set position to next charachter for loop to look at
end
if len(#s2) = 0
return null
return #s2 --return rebuilt string
end
You can replace the special characters using the following regular expression [\u0100-\uffff]
select regexp_replace(column, '[\u0100-\uffff]', '')

Order By Clause return wrong order when there are special characters in record set

I am using SQL server. I am fetching student information from database. I order by lastname and then first name like ORDER BY lastname, firstname it works until I have simple data. Issue occurs when there is '~' or any other character in name occur. It disturb order.
UPDATE
Here is what I am getting currently
and on front end I have this
Expectation is 2 and 3 should be after 4 and 5 as k comes first order then ñ
Use Collate with your query. Hope that can help! Like:
SELECT FIRSTNAME, LASTNAME
FROM TABLE
ORDER BY FIRSTNAME Collate SQL_Latin1_General_CP1253_CI_AI
Make this function in sql server-----
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
now you can order by like ---
SELECT *
FROM yourtablename
ORDER BY dbo.RemoveSpecialChars(yourcolumnname)

Find and Replace All Special Character in SQL [duplicate]

This question already has answers here:
How to strip all non-alphabetic characters from string in SQL Server?
(21 answers)
Closed 7 years ago.
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
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

How to replace all nonnumeric values?

This is TERADATA (not SQL Server, not Oracle )
I have a column of phone numbers:
(312)9879878
(298)989-9878
430-394-2934
394s9048ds987
..........
I need to clean this column into
3129879878
2989899878
4303942934
3949048987
..........
So that only numbers should stay. All other letters, special characters, hyphens ... should be removed. How can I do this?
Which release of TD is running at your site?
If it's 14 or you got the oTranslate UDF installed you can simply do an old trick nesting Translate:
oTranslate(phonenum, oTranslate(phonenum, '0123456789', ''), '')
Answer :
DECLARE #Input varchar(1000)
SET #Input = '01 vishal 98-)6543'
DECLARE #pos INT
SET #Pos = PATINDEX('%[^0-9]%',#Input)
WHILE #Pos > 0
BEGIN
SET #Input = STUFF(#Input,#pos,1,'')
SET #Pos = PATINDEX('%[^0-9]%',#Input)
END
SELECT #Input
Thank You,
Vishal Patel
I have this function to pull numerics (0-9) from a string:
CREATE FUNCTION NumbersOnly(#STR VARCHAR(2000))
RETURNS VARCHAR(1000)
AS
BEGIN
DECLARE #N INT
DECLARE #NN VARCHAR(1000)
SET #N = 0
SET #NN = ''
WHILE #N <= LEN(#STR)
BEGIN
IF SUBSTRING(#STR,#N,1) >= '0'
AND SUBSTRING(#STR,#N,1) <= '9'
BEGIN
SET #NN = #NN + SUBSTRING(#STR,#N,1)
END
SET #N = #N + 1
END
RETURN #NN
END