Skip to main content

Troubleshooting String Binary Truncation Errors or Data type mismatch when updating SQL tables.

 Troubleshooting String Binary Truncation Errors in SQL Server

String binary truncation errors in Microsoft SQL Server can be frustrating to deal with, especially when you're dealing with large datasets or complex queries. These errors occur when a string value is too long to fit into a specified column, leading to data loss or corruption. Fortunately, there are steps you can take to identify and resolve these errors effectively.

1. Enable Trace Flags

The first step in troubleshooting string binary truncation errors is to enable trace flags to pinpoint which fields are causing the issue. This can be done using the DBCC TRACEON command in SQL Server. By enabling trace flags, you activate specific diagnostic features that can help identify the source of the error. I would recommend doing this on a test server to avoid any performance issues.

sql
DBCC TRACEON(460, -100); -- Enable errors DBCC TRACEOFF(460, -100); -- Disable errors

2. Extract Data into a Temporary Table

Once you've enabled trace flags, run your query to identify the offending column. If this does not work extract the data from your source table into a temporary table schema. This allows you to compare the field names and data types between the source table and the temporary table to identify any discrepancies.

sql
SELECT * INTO #temptable FROM YourSourceTable;

3. Compare Schemas for Variances

After extracting the data into a temporary table, compare the schemas of the source table and the temporary table for any variances in field names or data types. This can be done using a SQL query that retrieves column information from the INFORMATION_SCHEMA.COLUMNS system view.

sql
DECLARE @TableName1 NVARCHAR(128) = 'YourSourceTable'
DECLARE @TableName2 NVARCHAR(128) = '#temptable'
-- Compare field names and data types WITH FieldComparison AS ( SELECT @TableName1 AS TableName, COLUMN_NAME AS FieldName, DATA_TYPE AS DataType FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @TableName1 UNION ALL SELECT @TableName2 AS TableName, COLUMN_NAME AS FieldName, DATA_TYPE AS DataType FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @TableName2 ) SELECT T1.TableName AS TableName1, T1.FieldName AS FieldName1, T1.DataType AS DataType1, T2.TableName AS TableName2, T2.FieldName AS FieldName2, T2.DataType AS DataType2, iif(T1.FieldName <> T2.FieldName, 'X', '') AS IsFieldMatch, iif(T1.DataType <> T2.DataType, 'X', '') AS IsDataTypeMatch FROM (SELECT * FROM FieldComparison WHERE TableName = @TableName1) AS T1 FULL JOIN (SELECT * FROM FieldComparison WHERE TableName = @TableName2) AS T2 ON T1.FieldName = T2.FieldName ORDER BY COALESCE(T1.FieldName, T2.FieldName);

4. Analyze Execution Plans

Additionally, you can analyze the execution plans of your queries to identify any implicit conversions that may be causing string binary truncation errors. This can be done by querying the sys.dm_exec_query_stats dynamic management view and examining query plans for implicit conversions.

sql
SELECT TOP(50) DB_NAME(t.[dbid]) AS [Database Name], t.text AS [Query Text], qs.total_worker_time AS [Total Worker Time], qs.total_worker_time/qs.execution_count AS [Avg Worker Time], qs.max_worker_time AS [Max Worker Time], qs.total_elapsed_time/qs.execution_count AS [Avg Elapsed Time], qs.max_elapsed_time AS [Max Elapsed Time], qs.total_logical_reads/qs.execution_count AS [Avg Logical Reads], qs.max_logical_reads AS [Max Logical Reads], qs.execution_count AS [Execution Count], qs.creation_time AS [Creation Time], qp.query_plan AS [Query Plan] FROM sys.dm_exec_query_stats AS qs WITH (NOLOCK) CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS t CROSS APPLY sys.dm_exec_query_plan(plan_handle) AS qp WHERE CAST(query_plan AS NVARCHAR(MAX)) LIKE ('%CONVERT_IMPLICIT%') AND t.[dbid] = DB_ID() ORDER BY qs.total_worker_time DESC OPTION (RECOMPILE);

By following these steps and analyzing the results, you can effectively troubleshoot string binary truncation errors in SQL Server and ensure the integrity of your data.

Comments

Popular posts from this blog

Using SSRS web services to render a report as a PDF

I have been looking around the net for some decent code which would explain how I could render a report, using SSRS 2008 web services as a PDF.   The need was to extract reports sitting on a SSRS 2008 server sitting on a NT domain on a trusted network, whereas my web server was sitting in a DMZ. Where the only communication allowed by the network admin was port 80. To do this you will need to use the SSRS2008   ReportExecution2005.asmx web service. This could be accesses using the following URL assuming your SSRS server was installed using the default settings. http://YourServerIP/reportserver/reportexecution2005.asmx?wsdl 1.        Create a user on your AD domain with the least amount of privileges (say ReportUser) 2.        Give this account browse access on the reporting server for the desired reports. 3.        To get this working in visual studio 2010 (I am using the Premium edition)   Right click on your project and add a service reference to the above web serv

Integrating MIGS eGate Payment gateway using C# for Oz businesses (Server-Hosted Payment method)

If you ever wanted to include the eGate payment gateway in your ASP.Net site and want to save yourself the time in creating the class, here’s how to do it. First things first you will need a merchant account with ANZ who will give you the following ·          Merchant Account ·          User name ·          password The details could be downloaded from the ANZ web site http://www.anz.com.au/australia/business/merchant/DownloadDevKit.asp I am doing this for an Aus base client thus the process might be different to your own country. Please check with the issuing bank for details. Have a read of the Merchant Admin guide. Login to the merchant account and create an operator ID, Login as the operator and obtain the access code. I will not going to the details as this is all documented in the admin guide. What you will need for the implementation would be vpc_AccessCode vpc_Merchan MIGS gateway URL:https://migs.mastercard.com.au/vpcdps The site has numerous

How to Query SQL server for T-SQL Calendar using GetFullMonth using Pivot

After listing sql calendar date values in a month as rows, we need to display them now in a format similar to frequently used in printed calendars. The day names are listed as column names and dates are shown under the related columns. The first solution is supplied by Plamen Ratchev from SQLStudio.com using the t-sql PIVOT. Since I liked the t-sql code he coded much I want to share it with you here. SELECT [Sunday], [Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday] FROM ( SELECT DATEPART(WEEK, [date]) AS week_number, [date], [days] FROM dbo.getFullmonth('20080229') ) AS Dates PIVOT ( MIN([date]) FOR [days] IN ( [Sunday], [Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday] ) ) AS Pivots And the output of this sql pivot statement will return the following result set as output which is displaying sql dates in a calendar format.