Re: Unicode in SQL queries

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Erland Sommarskog (esquel_at_sommarskog.se)
Date: 06/05/04

  • Next message: Erland Sommarskog: "Re: Recordset opens as read-only -- WHY??"
    Date: Sat, 5 Jun 2004 22:38:08 +0000 (UTC)
    
    

    [posted and mailed, please reply in news]

    Silvio Lopes de Oliveira (silviol@aaesys.com) writes:
    > I use Unicode in my program to enter data in Chinese into a nvarchar
    > field. When inserting or updating records, my application does not use
    > Unicode escape sequences but rather the Unicode characters themselves.
    > Thus, my app may have SQL statements like:
    >
    > UPDATE table SET field1 = '<some string in Chinese>';
    >
    > The syntax above works both in my app and through SQL Server Enterprise
    > Management. However, if I try the SELECT query below, it fails to match
    > the record I inserted both in my app and through Enterprise manager:
    >
    > SELECT * FROM table WHERE field1 = '<some string in Chinese>';

    I would expect that you would have to use N'<some string in Chinese>',
    or else the string would be converted to fallback characters in your
    ANSI character set.

    When you look at the data as you updated it, do you see it correctly?
     
    > I tried changing the UPDATE statement and the SELECT statement to use
    > escape sequences instead of the actual unicode characters, but that made
    > things worse. For example, I executed:
    >
    > UPDATE table SET field1 = '\u4E0A\u6D77';
    > SELECT * FROM table WHERE field1 = '\u4E0A\u6D77';

    Of course. This is nothing which is supported by SQL Server. The way
    to express that string through codes would be:

          select nchar(0x4E0A) + nchar(0x6D77)
     
    > Why can I use the Unicode chars explicitly when adding or updating
    > records, but cannot use them to match a record with a SELECT statement?
    > Is there something I'm missing when using the escape sequence (maybe
    > some conversion function) ?

    There is no difference to UPDATE and SELECT as far as SQL Server is
    concerned. But since you talk about application and Enterprise Manager
    there may be things going on at client level.

    The best tool to use for simple tests is Query Analyzer, since it
    just sends the query to SQL Server and presents the result.

    -- 
    Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
    Books Online for SQL Server SP3 at
    http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp
    

  • Next message: Erland Sommarskog: "Re: Recordset opens as read-only -- WHY??"

    Relevant Pages

    • Re: RosAsm - right click
      ... If in Unicode, little-endian, or big-endian? ... Do you want the string null-terminated, ... (Well - In case you prefer Unicode characters, ...
      (alt.lang.asm)
    • Re: sendStringParameterAsUnicode: How to insert unicode data corre
      ... After checking with Microsoft it turns out that transmitting Unicode ... requires a change in the data format sent to the JDBC Receiver channel. ... We have contacted SAP and Microsoft. ... We are using SAP XI and connecting it to SQL Server 2005 using JDBC. ...
      (microsoft.public.sqlserver.jdbcdriver)
    • Re: java string encoding
      ... > unicode and use a printBytes method to display the hex value to System.out. ... This says "take the sequence of 16-bit Unicode characters living in the ... String 'cname' and convert it to a byte array using the default platform ... "convert this Java char sequence to bytes using 'BIG5' to do the conversion. ...
      (comp.lang.java.programmer)
    • Re: How to store HTML code in SQL server table
      ... but varchar cannot be properly used with unicode ... and has a maximum length of 8000 characters. ... > I support the Professional Association of SQL Server and it's> community of SQL Server professionals. ... >> I'm creating something like a web site builder in ASP.Net, and I need to>> store an HTML code in the SQL server table. ...
      (microsoft.public.sqlserver.server)
    • Re: How to store HTML code in SQL server table
      ... but varchar cannot be properly used with unicode ... and has a maximum length of 8000 characters. ... > I support the Professional Association of SQL Server and it's> community of SQL Server professionals. ... >> I'm creating something like a web site builder in ASP.Net, and I need to>> store an HTML code in the SQL server table. ...
      (microsoft.public.sqlserver.programming)