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
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 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
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2320 Visits: 1481
Please post your code.
bitbucket-25253
bitbucket-25253
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24271 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
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1031 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
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 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 (24 views, 2.00 KB)
Mark Cowne
Mark Cowne
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10160 Visits: 26054
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