Replace Ambigious / Invalid Text Characters from String - sql

I am inserting the string into table. But there are some ambiguous, illegal text characters like 'ÔÇô' , '├®' appearing in the string. I want to replace all these invalid characters from the table and update my table. Is there any way to replace such characters in SQL. I am using SQL server 2008.

You could use one of the functions here:
How to strip all non-alphabetic characters from string in SQL Server?
You haven't included your insert statement, so I'm going to guess you've done it similar to
insert into table2
SELECT dbo.fn_StripCharacters(myfield1, 'a-z0-9'), myfield2, myfield3 from table1

Like you said... "Replace".
Replace documentation
Hint: Replace the ambigous character with an empty string.

Also you can go for nvarchar, nchar or ntext to support these unicode chars in case you need those. If its really needs to replace the yous hould go for Replace() of Sql Server

Related

SQL query with german special char and Latin1_General_CI_AS

I am trying to read some entries out of a microsoft sql database.
The problem ist, that one of the columns has a name with special characters "bedürfnisse" and the entries are all in Latin1_General_CI_AS.
I need to select this column.
Select nameName.bedürfnisse
FROM nameName
This is not working. I also tried
Select nameName.bedürfnisse COLLATE Latin1_General_CI_AS
FROM nameName
but this is also not working. How can i select this column?
You need to escape your column names if they contain special characters. This also includes whitespace or other non-standard ascii characters in column names.
For standard ANSI SQL the syntax to do this is:
SELECT nameName."bedürfnisse"
FROM nameName
For SQL Server this also works:
SELECT nameName.[bedürfnisse]
FROM nameName
For MySQL, this also works:
SELECT nameName.`bedürfnisse`
FROM nameName
What really helped was to define UTF-8 in the connection. Its one of the option parameter

How do I deal with SQL tablenames with hyphen (-) when writing raw queries? i.e project-users

I have a table called project-users and want to write a SQL query like SELECT * FROM project-users I get this error ERROR: syntax error at or near "-".
I cannot change the table name at this point.
According to http://www.postgresql.org/docs/9.0/static/sql-syntax-lexical.html, you should use double quotes.
In your case, for PostgreSQL the query should be:
SELECT * FROM "project-users";
It is good practice to avoid the use of characters that need escaping or that contain spaces in identifiers.

Regex to get data with special characters

I have some data in my table's column upn.
Here is a small sample set of this data.
Pasquale.Rombolà#it.eurw.domain.net
JuanMaria.RomanGonçalves#eurs.domain.net
Santo.Paternò#it.eurw.domain.net
Peter.Browne#UK.EURW.domain.net
François.ESTIN#fr.eurw.domain.net
Frédéric.Huynh#fr.eurw.domain.net
Frédérique.Psaume#fr.eurw.domain.net
Laura.PiñeiroGomez#eurs.domain.net
Maria.AranzabalSaldaña#eurs.domain.net
Alberto.RubioMuñoz#eurs.domain.net
Peter.Brüggemann#UK.EURW.domain.net
Russel.Peters#CA.domain.net
I want to query this table for UPN values where I have some special characters in the UPN. So my query should not return upns such as:
Peter.Browne#UK.EURW.domain.net
and
Russel.Peters#CA.domain.net
But returns everything else with special characters such as [à,ò,ñ,ü ...etc]
I have tried this query but it doesn't work.
Select * from TableName
Where [UPN] like %[a-z,0-9,#,\.,-,A-Z]%
It returns everything including those which don't have any special characters.
Please help.
If I understand correctly, I think you'll just need to add a "^" as the first character inside the square brackets.
At present you're saying you want to return all those UPNs where one or more characters is in the list you give (i.e. the "ordinary" characters). The "^" should reverse that and give you all the UPNs where at least one of the characters is not in the list you give.
Update: After testing locally ... Make sure your collation is "Accent Sensitive" (if necessary add "Latin1_General_CI_AS" or similar after your "like" clause.
I found it only worked if rather than "A-Z", I actually typed out the whole alphabet.
You need to add binary collate clause in it. Chose necessary collation as per your data. For given sample data Latin1_General_BIN works. Here is the link for collation in sql server.
This snippet worked for me on my machine-
create table #t (name varchar(100));
insert into #t values
('Pasquale.Rombolà#it.eurw.domain.net'),
('JuanMaria.RomanGonçalves#eurs.domain.net'),
('Santo.Paternò#it.eurw.domain.net'),
('Peter.Browne#UK.EURW.domain.net'),
('François.ESTIN#fr.eurw.domain.net'),
('Frédéric.Huynh#fr.eurw.domain.net'),
('Frédérique.Psaume#fr.eurw.domain.net'),
('Laura.PiñeiroGomez#eurs.domain.net'),
('Maria.AranzabalSaldaña#eurs.domain.net'),
('Alberto.RubioMuñoz#eurs.domain.net'),
('Peter.Brüggemann#UK.EURW.domain.net'),
('Russel.Peters#CA.domain.net');
select * from #t where name not like '%[^a-zA-Z0-9#.]%' COLLATE Latin1_General_BIN;
Output-
Peter.Browne#UK.EURW.domain.net
Russel.Peters#CA.domain.net

insert control characters in nvarchar or varchar from SQL script?

Given SQL Server 2012, how can I insert control characters (the ones coded under ASCII 32, like TAB, CR, LF) in a nvarchar or varchar column from a SQL script?
Unless I miss something in the question you can do this using TSQL CHAR() function:
INSERT INTO MyTable(ColName) VALUES(CHAR(13) + CHAR(10))
Will insert CR/LF. Same for other codes.
Edit there is TSQL NCHAR() as well for Unicode characters.
Please note that the function may vary depending on the type of your column, using the wrong function can result in wrong encoding.
nchar/nvarchar
http://technet.microsoft.com/en-us/library/ms186939.aspx
char/varchar
http://technet.microsoft.com/en-us/library/ms176089.aspx

Replace NOT working!

I have column with values that have wrong character ?. Now I want to change it to character b. For this I am using this statement:
SELECT REPLACE(name,'?','b') from contacts;
But when I do this nothing's happening, it return value with ?.
What I am doing wrong? How I can replace this?
Are you actually trying to change the values in the table? In that case, you'll need to do an UPDATE:
UPDATE contacts
SET name = Replace(name,'?','b')
Otherwise, if you are simply trying to retrieve a modified value, your syntax should work just fine. (I tested it, ? doesn't have to be escaped or anything):
SELECT name, Replace(name,'?','b') as Fixed
FROM contacts
Another possibility that I've seen before is that the character looks like a regular old ASCII question mark but it's not really. It's actually a different character. I'd select the text and paste it into Notepad and then copy and paste it into my query.
If your name column data type is NVARCHAR you should use N prefix. NVARCHAR and VARCHAR types have unicode differance. Look at this link for more information about differance between NVARCHAR and VARCHAR types.
SELECT REPLACE(name,N'?', N'b') from contacts;
Try this
update contacts set name=replace(name, '?', 'b')