Ошибка string data right truncation

We are doing some performance tests on our website and we are getting the following error a lot:

*** 'C:inetpubfoo.plex' log message at: 2008/10/07 13:19:58
DBD::ODBC::st execute failed: [Microsoft][SQL Native Client]String data, right truncation (SQL-22001) at C:inetpubfoo.plex line 25.

Line 25 is the following:

SELECT DISTINCT top 20 ZIP_CODE, CITY, STATE FROM Zipcodes WHERE (ZIP_CODE like ?) OR (CITY like ?) ORDER BY ZIP_CODE

And lastly, this is perl code.

Any ideas?

EDIT: the issue here was that I was searching in the zip file with the string «74523%» which is too long. I ended up just not adding the % if they give five digits.

Either the parameter supplied for ZIP_CODE is larger (in length) than ZIP_CODEs column width or the parameter supplied for CITY is larger (in length) than CITYs column width.

It would be interesting to know the values supplied for the two ? placeholders.

answered Oct 8, 2008 at 15:58

Chris Driver's user avatar

Chris DriverChris Driver

2,2452 gold badges19 silver badges14 bronze badges

1

This is a known issue of the mssql ODBC driver. According to the Microsoft blog post:

The ColumnSize parameter of SQLBindParameter refers to the number of characters in the SQL type, while BufferLength is the number of bytes in the application’s buffer. However, if the SQL data type is varchar(n) or char(n), the application binds the parameter as SQL_C_CHAR or SQL_C_VARCHAR, and the character encoding of the client is UTF-8, you may get a «String data, right truncation» error from the driver even if the value of ColumnSize is aligned with the size of the data type on the server. This error occurs since conversions between character encodings may change the length of the data. For example, a right apostrophe character (U+2019) is encoded in CP-1252 as the single byte 0x92, but in UTF-8 as the 3-byte sequence 0xe2 0x80 0x99.

You can find the full article here.

1

I got around the issue by using a convert on the «?», so my code looks like convert(char(50),?) and that got rid of the truncation error.

0

I was facing the same issue. So, i created a stored Procedure and defined the size like
@FromDate datetime,
@ToDate datetime,
@BL varchar(50)

After defining the size in @BL varchar(50), i did not face any problem. Now it is working fine

If the connection is done via PHP, we solved with the connection parameter «CharacterSet»:

sqlsrv_connect(DB_PTH_HOST, array(
                                "Database" => ***,
                                "UID" => ***,
                                "PWD" => ***,
                                "CharacterSet" => "UTF-8"));

I experienced this today on an application that has been running for years. My cause was a little different, so I figured I’d share to help anyone in the future that this happens to.

For the issue I experienced, we have an application that runs on Client machines and the clients talk to the DB server via ODBC connection. For whatever reason, the ODBC driver on the client had updated to a newer version than the server had, (which usually didn’t matter in the past). The version was only 1 month apart but was the cause of the issue. I went to windows patch history and uninstalled the patch whose date most closely matched the ODBC driver date, and this resolved the issue. (to check ODBC driver version, just search for ODBC in windows start, open ODBC 32, click on the drivers tab, scroll to the driver for the connection type you use, and the date is listed far right.)

Similar to the solution provided by luca.vercelli: in R, using dbConnect from the odbc package, you can specify the encoding used by the database. My database uses the SQL_Latin1_General_CP1_CI_AS collation, and specifying the latin1 encoding in dbConnect solved the problem.

We are doing some performance tests on our website and we are getting the following error a lot:

*** 'C:inetpubfoo.plex' log message at: 2008/10/07 13:19:58
DBD::ODBC::st execute failed: [Microsoft][SQL Native Client]String data, right truncation (SQL-22001) at C:inetpubfoo.plex line 25.

Line 25 is the following:

SELECT DISTINCT top 20 ZIP_CODE, CITY, STATE FROM Zipcodes WHERE (ZIP_CODE like ?) OR (CITY like ?) ORDER BY ZIP_CODE

And lastly, this is perl code.

Any ideas?

EDIT: the issue here was that I was searching in the zip file with the string «74523%» which is too long. I ended up just not adding the % if they give five digits.

Either the parameter supplied for ZIP_CODE is larger (in length) than ZIP_CODEs column width or the parameter supplied for CITY is larger (in length) than CITYs column width.

It would be interesting to know the values supplied for the two ? placeholders.

answered Oct 8, 2008 at 15:58

Chris Driver's user avatar

Chris DriverChris Driver

2,2452 gold badges19 silver badges14 bronze badges

1

This is a known issue of the mssql ODBC driver. According to the Microsoft blog post:

The ColumnSize parameter of SQLBindParameter refers to the number of characters in the SQL type, while BufferLength is the number of bytes in the application’s buffer. However, if the SQL data type is varchar(n) or char(n), the application binds the parameter as SQL_C_CHAR or SQL_C_VARCHAR, and the character encoding of the client is UTF-8, you may get a «String data, right truncation» error from the driver even if the value of ColumnSize is aligned with the size of the data type on the server. This error occurs since conversions between character encodings may change the length of the data. For example, a right apostrophe character (U+2019) is encoded in CP-1252 as the single byte 0x92, but in UTF-8 as the 3-byte sequence 0xe2 0x80 0x99.

You can find the full article here.

1

I got around the issue by using a convert on the «?», so my code looks like convert(char(50),?) and that got rid of the truncation error.

0

I was facing the same issue. So, i created a stored Procedure and defined the size like
@FromDate datetime,
@ToDate datetime,
@BL varchar(50)

After defining the size in @BL varchar(50), i did not face any problem. Now it is working fine

If the connection is done via PHP, we solved with the connection parameter «CharacterSet»:

sqlsrv_connect(DB_PTH_HOST, array(
                                "Database" => ***,
                                "UID" => ***,
                                "PWD" => ***,
                                "CharacterSet" => "UTF-8"));

I experienced this today on an application that has been running for years. My cause was a little different, so I figured I’d share to help anyone in the future that this happens to.

For the issue I experienced, we have an application that runs on Client machines and the clients talk to the DB server via ODBC connection. For whatever reason, the ODBC driver on the client had updated to a newer version than the server had, (which usually didn’t matter in the past). The version was only 1 month apart but was the cause of the issue. I went to windows patch history and uninstalled the patch whose date most closely matched the ODBC driver date, and this resolved the issue. (to check ODBC driver version, just search for ODBC in windows start, open ODBC 32, click on the drivers tab, scroll to the driver for the connection type you use, and the date is listed far right.)

Similar to the solution provided by luca.vercelli: in R, using dbConnect from the odbc package, you can specify the encoding used by the database. My database uses the SQL_Latin1_General_CP1_CI_AS collation, and specifying the latin1 encoding in dbConnect solved the problem.

I have similar problem, too.
Looks like executing query with emoji (or maybe other surrogate paired unicode?) parameter doesn’t work in 4.0.23, but works on 4.0.22 which had another parameter handling bug.

In this case, with 4.0.23,

CREATE TABLE [dbo].[😁] (
    ID INT NULL,
    NV NVARCHAR(256) NULL,
    FL FLOAT NULL,
    DM DECIMAL(21, 6) NULL
)
cnxn.execute(u'INSERT INTO [😁] (ID, NV, FL, DM) VALUES (?, ?, ?, ?)', 5, u'😁', None, None) # ✖ doesn't work
cnxn.execute(u'INSERT INTO [😁] (ID, NV, FL, DM) VALUES (5, N'😁', NULL, NULL)')             # ✔ works like expected
cnxn.execute(u'INSERT INTO [😁] (ID, NV, FL, DM) VALUES (5, '😁', NULL, NULL)')              # ✔ works, but have encoding issue

I tested with unixODBC 2.3.7pre and Microsoft ODBC Driver 17 for SQL Server

Problem Summary
DG4ODBC: STRING DATA, RIGHT TRUNCATION ERROR WHILE ACCESSING VARCHAR(MAX) COLUMN

Driver
Microsoft® ODBC Driver 11 for SQL Server® — RedHat Linux

Problem Description
When selecting a MS SQL VARCHAR (max) column over a ODBC Gateway database connection I am getting this error from Oracle:

ORA-28500: connection from ORACLE to a non-Oracle system returned this message:

[Microsoft][ODBC Driver 11 for SQL Server]String data, right truncation {01004}
[Microsoft][ODBC Driver 11 for SQL Server]String data, right truncation {01004}
[Microsoft][ODBC Driver 11 for SQL Server]String data, right truncation {01004}
[Microsoft][ODBC Driver 11 for SQL Server]String data, right truncation {01004}
[Microsoft][ODBC Driver 11 for SQL Server]String data, right truncation {01004}
ORA-02063: preceding 2 lines from <LINK_NAME>

The ODBC driver should map the varchar(max) column to SQL_LONGVARCHAR which would be appropriate for Oracle but the column is getting truncated

Issue
By default the SQL Server ODBC driver exposes the varchar(max) data type as a SQL_VARCHAR. When reporting the maximum size of a varchar(max) column, the driver returns 0, which is the Microsoft convention for «unlimited».

  [ODBC][25518][1399527750.588980][SQLDescribeCol.c][497]
  Exit:[SQL_SUCCESS]                

  Column Name = [raw_response]                

  Data Type = 0x7fffe3cbe1a4 -> 12                

  Column Size = 0x7fffe3cbe158 -> 0                

  Decimal Digits = 0x7fffe3cbe1ac -> 0                

  Nullable = 0x7fffe3cbe1b0 -> 1

DG4ODBC is unable to interpret a zero length as an «unlimited» size and returns an error when retrieving varchar(max) data.

FreeTDS and DataDirect ODBC drivers  return SQL_LONGVARCHAR instead of SQL_VARCHAR with 0 precision. So there is no problem reported for these drivers.

Is there a fix for this or is the Microsoft ODBC driver team working on a fix for the driver regarding varchar(max)?

Regards,
James

Всем привет

Проблема собственно в длине строки

SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'title' at row 1 (SQL: update... ... ...

Везде пишут что надо в файл AppServiceProvider.php добавить:

use IlluminateSupportFacadesSchema;

public function boot()
{
        Schema::defaultStringLength(191);
}

Однако все равно ошибка, вставлял и Schema::defaultStringLength(50); и Schema::defaultStringLength(10); результат одинаковый — ошибка никуда не делать

У меня стоят различные лимиты на колонки 50 / 150 / 250 в зависимости от назначения

5e91ca947b058882496659.jpeg

Подскажите как это лечить?

  • Ошибка string or binary data would be truncated the statement has been terminated
  • Ошибка string cannot be converted to string
  • Ошибка stream read error
  • Ошибка stream not found
  • Ошибка stray 321 in program