Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Insert into not working correctly.

Author  Topic 

zydjohn
Starting Member

2 Posts

Posted - 2010-06-13 : 04:26:50
Dear All:
I have just installed SQL Server 2008 R2, and want to create a simple table and insert some data.
Here is my table:
CREATE TABLE [dbo].[Table1]([Field1] [nvarchar] (10) NOT NULL)
Now, I want to insert one value containg "." and 2 more chars, but I got the warning that the string/value may be truncated.
INSERT INTO TABLE1 VALUES('CHC.WS')
After the insert, I check with "SELECT * FROM TABLE1".
I have found the value is 'CHC.W'
The second char after "." is gone, but the first char after "." is still there.
Any idea?
PS: My database: SQL Server 2008 R2 Enterprise.
OS: Windows 2008 R2 Enterprise
Thanks,

Kristen
Test

22859 Posts

Posted - 2010-06-13 : 06:07:44
Not seen that before

Does this fix it?

INSERT INTO TABLE1 VALUES(N'CHC.WS')

where N explicitly states that the 'String' is to be treated as Unicode/NVarchar

NVarchar uses 2 bytes per character. You have specified the column to be NVarchar(10), so it will hold 10 characters, but the fact that it is "truncating" at 5 characters suggests it thinks the data you are providing is 2-bytes-per-character

If your example was run from an Application, rather than SSMS or some other similar SQL tool, then it might be that the application is converting the data to Unicode in a way tha SQL is not expecting.
Go to Top of Page

zydjohn
Starting Member

2 Posts

Posted - 2010-06-13 : 12:36:38
Hi, Kristen:
Thank you very much, your way works.
It is not easy to know how to input data like this.
Thanks again!
John
Go to Top of Page
   

- Advertisement -