473,503 Members | 8,959 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL 2000 and UTF-16 encoding

Hi all,

I have an issue on querying against UTF-16 encoded characters in
SQL2000 database: For example the "López" is saved into database as
"López" (due to the UTF-16 encoding); somehow, when I query data
with conditions of "like 'lop%'" or "like 'Lóp%'", the row of López
would not return.

NOTE: the accent insensitive collation can not help in this case.

Thank you,

Albion(052X)

Jul 23 '05 #1
8 6041
[posted and mailed, please reply in news]

dBlue (zk*****@hotmail.com) writes:
I have an issue on querying against UTF-16 encoded characters in
SQL2000 database: For example the "López" is saved into database as
"López" (due to the UTF-16 encoding); somehow, when I query data
with conditions of "like 'lop%'" or "like 'Lóp%'", the row of López
would not return.

NOTE: the accent insensitive collation can not help in this case.


I answered a very similar question yesterday, and while the username
and e-mail address, I would assume that you are the same person. I cannot
do much more but repeat my answer from yesterday:

I was not able to repeat this. If you can produce a script similar
to the one below that demonstrates the problem, it's a little easier
to get an idea of what is going on. Don't forget to include the collation
of the column.

CREATE TABLE tbl
(lastname nvarchar(30) COLLATE Finnish_Swedish_BIN NOT NULL)
go
INSERT tbl (lastname) VALUES ('Mopez')
INSERT tbl (lastname) VALUES ('Mópez')
INSERT tbl (lastname) VALUES ('lopez de la serra')
INSERT tbl (lastname) VALUES ('x lópez')
go
SELECT * FROM tbl WHERE lastname LIKE '%[oó]pez%'
SELECT * FROM tbl WHERE lastname LIKE '%l[oó]pez%'
SELECT * FROM tbl WHERE lastname LIKE N'%[oó]pez%'
SELECT * FROM tbl WHERE lastname LIKE N'%l[oó]pez%'
go
drop table tbl
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
Thank you Erland for the reply, unfortunatly, the problem remains.

I understand your example well, and it is exactly the sql query we are
using now. The problem is not at the accent. The query does not
return me the data row, because the character is save as "López" in
the database, and the [oó] does not help on it.

BTW, the accent insensitive insensitive does not help either.

Thank you,

Albion(052X)

Jul 23 '05 #3
Hi,

Please try the script below. It is a bit weired to me.

CREATE TABLE tbl
(lastname nvarchar(30) NOT NULL)
go
INSERT tbl (lastname) VALUES ('MópeX')
INSERT tbl (lastname) VALUES ('MópeY')
INSERT tbl (lastname) VALUES ('MopeZ')
go

SELECT * FROM tbl WHERE lastname Collate
SQL_Latin1_General_CP1_CI_AI LIKE N'%[óo]pe%'
SELECT * FROM tbl WHERE lastname Collate
SQL_Latin1_General_CP1_CI_AI LIKE N'%M[óo]pe%'
SELECT * FROM tbl WHERE lastname Collate
SQL_Latin1_General_CP1_CI_AI LIKE N'%M%[óo]pe%'
GO

Drop table tbl

The frist two select statement does not return same result as they
suppose to. Any idea?

Thank you,

Albion(052X)

Jul 23 '05 #4
BYTW, I found an very instereting stuff. Please the script below,

The first 1 and 2 select statement should return same result, but they
do not; and only difference between them is 1 statement does not have
the M character inside the like statement.
CREATE TABLE tbl
(lastname nvarchar(30) NOT NULL)
go
INSERT tbl (lastname) VALUES ('MópeX')
INSERT tbl (lastname) VALUES ('MópeY')
INSERT tbl (lastname) VALUES ('MopeZ')
go
SELECT * FROM tbl WHERE lastname Collate
SQL_Latin1_General_CP1_CI_AI LIKE N'%[óo]pe%'
SELECT * FROM tbl WHERE lastname Collate
SQL_Latin1_General_CP1_CI_AI LIKE N'%M[óo]pe%'
SELECT * FROM tbl WHERE lastname Collate
SQL_Latin1_General_CP1_CI_AI LIKE N'%M%[óo]pe%'
SELECT * FROM tbl WHERE lastname Collate
SQL_Latin1_General_CP1_CI_AI LIKE N'%[o]pe%'
GO

drop table tbl
Thank you,

Albion(052X)

Jul 23 '05 #5
| "dBlue" <zk*****@hotmail.com> schrieb im Newsbeitrag
news:11**********************@g49g2000cwa.googlegr oups.com...
|Hi all,
|
|I have an issue on querying against UTF-16 encoded characters in
|SQL2000 database: For example the "López" is saved into database as
|"López" (due to the UTF-16 encoding);

This is not a UTF-16 representation but UTF-8.

--

Vincent Wehren


|somehow, when I query data
|with conditions of "like 'lop%'" or "like 'Lóp%'", the row of López
|would not return.

|NOTE: the accent insensitive collation can not help in this case.

|Thank you,

|Albion(052X)
Jul 23 '05 #6
dBlue (zk*****@hotmail.com) writes:
BYTW, I found an very instereting stuff. Please the script below,

The first 1 and 2 select statement should return same result, but they
do not; and only difference between them is 1 statement does not have
the M character inside the like statement.
CREATE TABLE tbl
(lastname nvarchar(30) NOT NULL)
go
INSERT tbl (lastname) VALUES ('MópeX')
INSERT tbl (lastname) VALUES ('MópeY')
INSERT tbl (lastname) VALUES ('MopeZ')
go
SELECT * FROM tbl WHERE lastname Collate
SQL_Latin1_General_CP1_CI_AI LIKE N'%[óo]pe%'
SELECT * FROM tbl WHERE lastname Collate
SQL_Latin1_General_CP1_CI_AI LIKE N'%M[óo]pe%'
SELECT * FROM tbl WHERE lastname Collate
SQL_Latin1_General_CP1_CI_AI LIKE N'%M%[óo]pe%'
SELECT * FROM tbl WHERE lastname Collate
SQL_Latin1_General_CP1_CI_AI LIKE N'%[o]pe%'
GO

drop table tbl


But "MópeX" is not the same as "MópeX". The first string has six
characters, the second has five. Both strings match N'%[óo]pe%' -
that is first any number of characters and then exactly one of
"Ã", "³" or "o" and then "pe" followed by any number of chars.

On the other hand, the string N'%M[óo]pe%', you say that there
should be an M and then exactly one of "Ã", "³" or "o" and then "pe",
which there is in the six-char string.

OK, so that "MòpeZ" is really a representation of "MópeX", to whit
the bit pattern that you have in UTF-8, but stored in a string which
is supposed to hold UTF_16 values, and then it goes downhill from
there.

So I guess you really problem is why you have UTF-8 encodings in the
wrong place. When I saw your first posting, I assumed that the mangled
UTF-8 came from the news posting itself, but it appears now that it
comes from the database.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #7
Hi,

Thank you for the reply.

It seems like it is the problem on querying for the 'MópeX' from the
SQL, but getting correct 'MópeX', rather than getting the 'MópeX'.
Is that right?

The following is the function we use to do the decoding for the
URLstring. I can add the WideCharToMultiByte and MultiByteToWideChar
to get correct data. But is there a more efficient way?

str = Replace(str, "+", " ")

i = 0
Do
i = InStr(i + 1, str, "%")
If i = 0 Then
Exit Do
End If
var_char = Chr(CStr("&H" + Mid(str, i + 1, 2)))
If Err.Number = 0 Then
str = Mid(str, 1, i - 1) _
& var_char _
& Mid(str, i + 3)
End If
Loop

thanks again.
Erland Sommarskog wrote:
dBlue (zk*****@hotmail.com) writes:
BYTW, I found an very instereting stuff. Please the script below,

The first 1 and 2 select statement should return same result, but they
do not; and only difference between them is 1 statement does not have
the M character inside the like statement.
CREATE TABLE tbl
(lastname nvarchar(30) NOT NULL)
go
INSERT tbl (lastname) VALUES ('MópeX')
INSERT tbl (lastname) VALUES ('MópeY')
INSERT tbl (lastname) VALUES ('MopeZ')
go
SELECT * FROM tbl WHERE lastname Collate
SQL_Latin1_General_CP1_CI_AI LIKE N'%[óo]pe%'
SELECT * FROM tbl WHERE lastname Collate
SQL_Latin1_General_CP1_CI_AI LIKE N'%M[óo]pe%'
SELECT * FROM tbl WHERE lastname Collate
SQL_Latin1_General_CP1_CI_AI LIKE N'%M%[óo]pe%'
SELECT * FROM tbl WHERE lastname Collate
SQL_Latin1_General_CP1_CI_AI LIKE N'%[o]pe%'
GO

drop table tbl


But "MópeX" is not the same as "MópeX". The first string has six
characters, the second has five. Both strings match N'%[óo]pe%' -
that is first any number of characters and then exactly one of
"Ã", "³" or "o" and then "pe" followed by any number of chars.

On the other hand, the string N'%M[óo]pe%', you say that there
should be an M and then exactly one of "Ã", "³" or "o" and then "pe",
which there is in the six-char string.

OK, so that "MòpeZ" is really a representation of "MópeX", to whit
the bit pattern that you have in UTF-8, but stored in a string which
is supposed to hold UTF_16 values, and then it goes downhill from
there.

So I guess you really problem is why you have UTF-8 encodings in the
wrong place. When I saw your first posting, I assumed that the mangled
UTF-8 came from the news posting itself, but it appears now that it
comes from the database.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp


Jul 23 '05 #8
dBlue (zk*****@hotmail.com) writes:
It seems like it is the problem on querying for the 'MópeX' from the
SQL, but getting correct 'MópeX', rather than getting the 'MópeX'.
Is that right?
The problem is that you have garbage in your database, yes.
The following is the function we use to do the decoding for the
URLstring. I can add the WideCharToMultiByte and MultiByteToWideChar
to get correct data. But is there a more efficient way?

str = Replace(str, "+", " ")

i = 0
Do
i = InStr(i + 1, str, "%")
If i = 0 Then
Exit Do
End If
var_char = Chr(CStr("&H" + Mid(str, i + 1, 2)))
If Err.Number = 0 Then
str = Mid(str, 1, i - 1) _
& var_char _
& Mid(str, i + 3)
End If
Loop


I don't know what URL string you have, and I am not an expert on how
to write effecient VB code.

I have however used WideCharToMultiByte and MultiByteToWideChar (from
C++) to convert between UTF-8 and UTF-16, and found them to work well.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
22447
by: Michael Walton | last post by:
I am trying to write some code that inserts or updates a text field in my SQL Server 2000 database. The SQL statement is created using a submit from a form, and the text is coming from a...
2
15177
by: James Allan | last post by:
Hello -- I'm trying to get SQL Server 2000 on a Windows 2000 Server to be able to query an Active Directory. We've got two domain servers one Win2000 and one Win2003. However, I'm having...
3
3317
by: Michael | last post by:
Is there a GUI to do like drag and drop and very powerful , but not DOT NET dependant. I have found some programs that are GUI rich in scripting for SQL 2000 and even MY SQL, but they are...
12
9994
by: M Wells | last post by:
Hi All, I have a table that holds pregenerated member IDs. This table is used to assign an available member id to web site visitors who choose to register with the site So, conceptually the...
2
1168
by: Graham Carr | last post by:
Hi, My client has the need to upgrade to Windows 2000 Server from NT4 but are concerned about whether there existing custom code (ASP,VB6,MTS, etc) will work correctly once migrated. Does...
3
1920
by: Les Juby | last post by:
Getting really stumped here..... Using Classic.ASP and after an editing page has been completed I need to write the values back to an Access 2000 database. But I'm battling with a checkbox...
3
3133
by: Mark | last post by:
Any Visual C++ source code available for disabling the following keys in windows 2000. Alt + Ctrl + Del Ctrl + Esc Windows Key to Remove task bar Function keys (or Alt + Function keys or Ctrl...
7
1212
by: Axel Dahmen | last post by:
I've just installed Windows 2000 Service Pack 4 (SP4) on my Windows 2000 Server. Now I can't debug anymore! I'm using Visual Studio 7.0 (2002), ..NET Framework 1.0. The error message I get is...
1
2059
by: Ohaya | last post by:
Hi, We are running a website on a Windows 2000 Advanced Server machine with 4 CPUs and Hyperthreading enabled (i.e., 8 virtual processors). The web server is IIS5. Our application uses...
5
2028
by: znubbe | last post by:
Hi, I hope anyone can help me with this problem. I have a field of image type in a SQL 2000 database. I'm using this code to insert a document: Dim conn Dim rs Dim oStream
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.