Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Writing strings with chars 128-160 to an varchar column Expand / Collapse
Author
Message
Posted Tuesday, March 3, 2009 1:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, February 26, 2010 3:43 AM
Points: 2, 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

Post #667087
Posted Tuesday, March 3, 2009 2:56 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 2:52 AM
Points: 363, Visits: 1,318
Please post your code.
Post #667120
Posted Tuesday, March 3, 2009 9:00 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 6:19 AM
Points: 5,586, Visits: 24,925
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
Post #667356
Posted Tuesday, March 3, 2009 11:41 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, April 7, 2010 2:37 PM
Points: 315, 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{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ

Post #667545
Posted Thursday, March 5, 2009 3:30 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, February 26, 2010 3:43 AM
Points: 2, 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






  Post Attachments 
Program.cs.txt (9 views, 2.78 KB)
Post #669040
Posted Thursday, March 5, 2009 4:19 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, April 8, 2014 6:13 AM
Points: 1,694, Visits: 19,550

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);


____________________________________________________

How to get the best help on a forum

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

Never approach a goat from the front, a horse from the rear, or a fool from any direction.
Post #669070
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse