SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Writing strings with chars 128-160 to an varchar column


Writing strings with chars 128-160 to an varchar column

Author
Message
martin.vd.graaff
martin.vd.graaff
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 17
I am trying to write a string containing chars in the range 128-160 to an varchar column.
Chars in the range 128-160 are however converted to char 63.
How can I write chars in the 128-160 to the db?
I guess this shouldn't be too difficult, you just have to know how to.

Thanks in advance for your help!

Martin
dmoldovan
dmoldovan
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1512 Visits: 1479
Please post your code.
bitbucket-25253
bitbucket-25253
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15225 Visits: 25280
Might I suggest you read this from Books On Line:
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/e123fb74-022e-4a62-9639-b2053a9b4d7e.htm

Is it possible for you to change your columns data type from varchar to nvarchar (unicode character)?

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
tcnolan
tcnolan
Right there with Babe
Right there with Babe (765 reputation)Right there with Babe (765 reputation)Right there with Babe (765 reputation)Right there with Babe (765 reputation)Right there with Babe (765 reputation)Right there with Babe (765 reputation)Right there with Babe (765 reputation)Right there with Babe (765 reputation)

Group: General Forum Members
Points: 765 Visits: 164
You shouldn't have a problem with using varchar and CHAR for the characters you are talking about... see the following...

Running this code...


DECLARE @i int
DECLARE @str varchar(222)
SET @i = 33 /*Start at 33 to skip SPACE and non-printable characters */
SET @str = ''

WHILE (SELECT @i) < 256
BEGIN
SET @str = @str + CHAR(@i)
SET @i = @i + 1
END

PRINT @str




Returns the following...


!"#$%&'()*+,-./0123456789:; ?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ


martin.vd.graaff
martin.vd.graaff
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 17
I attached the C# code of the test application including the SQL command to create a test table

It works if I use an nchar column, but I know it is possible to store the data in a varchar column without loss of data, because this is done somehow from a C++ application using ODBC.

So I want to know how to do it while using a varchar column.

Thanks,
Martin
Attachments
Program.cs.txt (22 views, 2.00 KB)
Mark Cowne
Mark Cowne
SSCertifiable
SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)

Group: General Forum Members
Points: 6249 Visits: 25529
I think the problem is the unicode->ascii conversion that is happening somewhere along the way. C# strings are always 16-bit.

Try changing

parameters.Add("@TestColumn", SqlDbType.NVarChar);
parameters["@TestColumn"].Value = str;

to

parameters.Add("@TestColumn", SqlDbType.VarBinary);
parameters["@TestColumn"].Value = Encoding.GetEncoding(28591).GetBytes(str);

____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search