utf-8 in database varcharhttp://forums.asp.net/t/1112013.aspx/1?utf+8+in+database+varcharWed, 23 May 2007 06:37:00 -040011120131713900http://forums.asp.net/p/1112013/1713900.aspx/1?utf+8+in+database+varcharutf-8 in database varchar <p>We have existing SQL Server databases with all text fields defined as varchar (not nvarchar) and the text in them is actually UTF-8. Our old system (written in Delphi) will read and write those strings correctly and display the international characters on a web page etc. We are rewriting our system in ASP.NET and using Datasets to access the data. Reading/writing the data does not treat the data as utf-8. How can I get it to treat it as utf-8?</p> <p>&nbsp;</p> 2007-05-18T04:29:28-04:001715932http://forums.asp.net/p/1112013/1715932.aspx/1?Re+utf+8+in+database+varcharRe: utf-8 in database varchar <p>Why not upgrade the VARCHAR to NVARCHAR? If you used stored procedures for your existing Delphi programe, leave the existimg sp with VARCHAR datatype and create a new set with NVARCHAR arguments.&nbsp;</p> 2007-05-19T22:22:20-04:001718333http://forums.asp.net/p/1112013/1718333.aspx/1?Re+utf+8+in+database+varcharRe: utf-8 in database varchar <p>So can the stored procedure return a field as a different datatype to the one stored? How is this conversion handled? Can you ensure that the varchar is encoded as utf-8 and not using a codepage?</p> 2007-05-21T23:31:40-04:001718751http://forums.asp.net/p/1112013/1718751.aspx/1?Re+utf+8+in+database+varcharRe: utf-8 in database varchar <p>&gt;So can the stored procedure return a field as a different datatype to the one stored? <br> Yes!</p> <p>&gt;How is this conversion handled? <br> Explicitly using using CONVERT (although CAST can also be used)</p> <p>&gt;Can you ensure that the varchar is encoded as utf-8 and not using a codepage?<br> You can also specify an explicit collating sequence using COLLATE</p> <p>The existing table will need to be changed from VARCHAR to NVARCHAR</p> 2007-05-22T06:47:11-04:001720666http://forums.asp.net/p/1112013/1720666.aspx/1?Re+utf+8+in+database+varcharRe: utf-8 in database varchar <p>I don't use stored procedures in the existing application. And there are two many tables to create stored procedures for them all. The code would have to change too much. So for now I am stuck with varchar fields. Is it not possible to cast the nvarchar to a varchar when saving the data to the database? I tried to write a stored function and it gives and exception. My function was&nbsp; :-</p> <p>CREATE FUNCTION dbo.fnStringWrite<br> (<br> &nbsp; @S nvarchar(100)<br> )<br> RETURNS varchar(100)<br> AS<br> BEGIN<br> &nbsp; DECLARE @RETURN varchar(100)<br> &nbsp; SET @RETURN = cast(@S as varchar(100))<br> &nbsp; RETURN @RETURN<br> END<br> </p> 2007-05-23T04:21:44-04:001720673http://forums.asp.net/p/1112013/1720673.aspx/1?Re+utf+8+in+database+varcharRe: utf-8 in database varchar <p>Is it possible for a function to return a varchar with different lengths?</p> <p>It seems as though I need to decalre the return variable as say type varchar(100). I need to be able to handle various string lengths. Can I pass in the length of the string and use that when defining the varchar inoput and output lengths? Otherwise I need a function for every possible string field length.</p> 2007-05-23T04:26:32-04:001720678http://forums.asp.net/p/1112013/1720678.aspx/1?Re+utf+8+in+database+varcharRe: utf-8 in database varchar <p>How do you use COLLATE? I thought the collate only affected sort order or comparisons?</p> <p>Does it also affect how the data is stored? If it does I assume it only affects which codepage is used to convert characters. This is not the same as UTF-8 encoding. I don't think there is a collation to force storage in utf-8 format.</p> 2007-05-23T04:31:10-04:001720904http://forums.asp.net/p/1112013/1720904.aspx/1?Re+utf+8+in+database+varcharRe: utf-8 in database varchar <p>COLLATE can be used in a WHERE clause to compare data in different collationg sequences as in </p> <p>WHERE TABLE1.A = TABLEB.A COLLATE <font color="#ff0000" size="2">Latin1_General_CI_AS </font></p> <p><font color="#ff0000" size="2"><font color="#000000">Make a copy of the database and&nbsp;convert all the database VARCHAR to NVARCHAR. The casting should be in the old set of stored procedures. The simplest way forward however would be to update the delphi program to use NVARCHAR. That way the conversion issue will no longer arise.</font></p> </font> 2007-05-23T06:37:00-04:00