"String data, right truncation" warning on a select statement

19,092

Solution 1

I'd agree that this is driver related. The {SQL Server} driver was introduced for use with SQL 2000. {SQL Native Client} came along with 2005. Ideally, for your 2008 database, you should use the newest {SQL Server Native Client 10.0}. The newer drivers are backward compatible with older versions of SQL Server.

Solution 2

Changing my driver from "Driver={SQL Server};" to Driver={SQL Native Client};

has made the problem go away, but I'm not sure what was going on. I'm going to keep looking into it

Solution 3

From a bit of Googling, I've learned that apparently, at times, particularly when "Use Regional Settings" is checked in the MS SQL Server ODBC driver DSN setup dialog, ODBC will treat a string made up of all digits, as a number, and return it like "12345678.00" which doesn't fit into the space you've given it. The solution is to turn that setting off, either in the dialog box, or, more permanently, in the connection string:

 CString sDsn = "Driver={SQL Server};Server=server\\db;Database=Boards;"
               +"Uid=uid;Pwd=pwd;Trusted_Connection=False;Regional=No;"
Share:
19,092
JonDrnek
Author by

JonDrnek

I started coding professionally in 97. I started in the mainframe world with Cobal and Sapiens, moved on to the web with Java, ASP and JavaScript then to C++ and MFC and now working with C# and .Net

Updated on June 13, 2022

Comments

  • JonDrnek
    JonDrnek almost 2 years

    I am upscaling an access 2003 database to SQL Server Express 2008. The tables appear to be created ok and the data looks ok.

    I have an MFC application that connects to this database. It worked fine connecting to access, but when I connect to SQL Server I am getting the following error on a select statement.

    DBMS: Microsoft SQL Server
    Version: 10.50.1600
    ODBC Driver Manager Version: 03.80.0000
    Warning: ODBC Success With Info on field 0.
    String data, right truncation
    
    State:01004,Native:0,Origin:[Microsoft][ODBC SQL Server Driver]
    

    The data that is returned should be 8 characters but is only 7 with the right most character truncated.

    The access front end can read the data from SQL Server correctly.

    The field in the SQL Server table is defined as nvarchar with a length of 8.

    The code to read the field looks something like

    CDatabase Database;
    CString sSerialNumber = "00000000";
    CString SqlString;
    
    CString sDsn = "Driver={SQL Server};Server=server\\db;Database=Boards;Uid=uid;Pwd=pwd;Trusted_Connection=False";
    Database.Open(NULL,false,false,sDsn);
    
    CRecordset recset( &Database );
    SqlString.Format("Select SerialNumber from boards where MACAddress = '%s'",mac);
    recset.Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
    recset.GetFieldValue("SerialNumber",sSerialNumber);
    

    After this, sSerialNumber should be 12345678 but its 1234567

    Thanks for the help