1

I can't add data into my database.

I tried using quotation marks and apostrophes but still, I can't find the error.

insert into cliente(name, email, phone, adress, postalCode)
values ('Filipe', '[email protected]', 912345678, 'Red Street', '1234-567')

ERROR :

"String or binary data would be truncated"

I hope I can fix this quickly.

4
  • 2
    Your'e trying to insert something that wont fit in the column (e.g. if name is declared varchar(2) then 6 character long name won't fit. Commented Jan 8, 2019 at 11:23
  • It seems you don't know what the error means. The error is telling you that the varchar value you are trying to INSERT is too large for one of the columns. Which column, we don't know (we don't have the DDL for your table). You'll need to check the lengths of your varchar columns and ensure the literal strings you are passing are small enough to fit, or increase the size of your column's length. If you post the DDL of your table, we'll be able to be more explicit. Commented Jan 8, 2019 at 11:24
  • Also, your INSERT statement implies you're storing phone as an int or bigint. This is actually a bad idea. Although Phone numbers are (normally) made up of numerical characters they aren't actually "numbers" that can be stored in a numerical data type, like int. Phone Number can have leading zeros, which would be lost with a numerical data type, can have country codes (which can/do start with a +), extensions and often formatting attached to them. For example 020 7123 4567 and 01234 567890 for UK numbers. You would be far better off storing phone as a varchar. Commented Jan 8, 2019 at 11:29
  • This question is similar to: SQL Server String or binary data would be truncated. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jul 29, 2024 at 20:46

2 Answers 2

3

You should check the columns and their datatypes. One of the columns is smaller than the data you are trying to insert.

So for instance if email is a VARCHAR (10) this would break, because the input exceeds the max characters of 10.

Sign up to request clarification or add additional context in comments.

1 Comment

If you for some reason do not have control over the length of the input data, you can cast them to the appropriate length before inserting. INSERT INTO clientele(Name,Email) SELECT CAST(Name as nvarchar(20)),CAST(Email AS NVARCHAR(128)). I sometimes do this when importing from excel
1

Given below is the stored procedure that can find the exact column name and its data which is exceeding the limit of column width.

--DROP PROCEDURE usp_String_or_binary_data_truncated
--GO
CREATE PROCEDURE usp_String_or_binary_data_truncated
@String VARCHAR(MAX)
AS

DECLARE @VARCHAR AS VARCHAR(MAX)
DECLARE @Xml AS XML
DECLARE @TCount AS INT
SET @String= REPLACE(REPLACE(REPLACE(REPLACE(@String,'''','')
             ,'[',''),']',''),CHAR(13) + CHAR(10),'')
SET @Xml = CAST(('<a>'+REPLACE(@String,'(','</a><a>')
           +'</a>') AS XML)

SELECT @TCount=COUNT(*)
FROM @Xml.nodes('A') AS FN(A)

;WITH CTE AS
     (SELECT
     (CASE
     WHEN (CHARINDEX('INSERT INTO',A.value('.', 'varchar(max)'))>0)
     THEN 1
     WHEN CHARINDEX('VALUES',A.value('.', 'varchar(max)'))>0
     THEN 2
     WHEN (CHARINDEX('INSERT INTO',A.value('.', 'varchar(max)'))=0
     AND CHARINDEX('VALUES',A.value('.', 'varchar(max)'))=0)
     AND @TCount=2  THEN 2
     WHEN (CHARINDEX('INSERT INTO',A.value('.', 'varchar(max)'))=0
     AND CHARINDEX('VALUES',A.value('.', 'varchar(max)'))=0)
     AND @TCount=3  THEN 3
     END) AS[Batch Number],
     REPLACE(REPLACE(A.value('.', 'varchar(max)')
     ,'INSERT INTO',''),'VALUES ','') AS [Column]
     FROM @Xml.nodes('A') AS FN(A))

, [CTE2] AS
(
    SELECT
    [Batch Number],
    CAST('' + REPLACE([Column], ',' , '')
    + '' AS XML)
    AS [Column name And Data]
    FROM  [CTE]
)
,[CTE3] AS
(
    SELECT [Batch Number],
    ROW_NUMBER() OVER(PARTITION BY [Batch Number]
    ORDER BY [Batch Number] DESC) AS [Row Number],
    Split.a.value('.', 'VARCHAR(MAX)') AS [Column name And Data]
FROM [CTE2]
CROSS APPLY [Column name And Data].nodes('/M')Split(A))

SELECT
 ISNULL(B.[Column name And Data],C.name) AS [Column Name]
,A.[Column name And Data] AS [Column Data]
,C.max_length As [Column Length]
,DATALENGTH(A.[Column name And Data])
AS [Column Data Length]

FROM [CTE3] A
LEFT JOIN [CTE3] B
ON A.[Batch Number]=2 AND B.[Batch Number]=3
AND A.[Row Number] =B.[Row Number]
LEFT JOIN sys.columns C
ON C.object_id =(
    SELECT object_ID(LTRIM(RTRIM([Column name And Data])))
    FROM [CTE3] WHERE [Batch Number]=1
)
AND (C.name = B.[Column name And Data]
OR  (C.column_id =A.[Row Number]
And A.[Batch Number]<>1))
WHERE a.[Batch Number] <>1
AND DATALENGTH(A.[Column name And Data]) >C.max_length
AND C.system_type_id IN (167,175,231,239)
AND C.max_length>0

GO

1 Comment

That's a lot of work to make up for an inadequate error message

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.