Not Able to Insert Data into Database String or binary data would be truncated - sql

INSERT INTO [CVSUAT].[dbo].[UserLevel ](
[Client_ID]
,[User_Level_Name]
,[User_Level_Description]
,[Created_by]
,[Created_Date]
,[Modified_by]
,[Modified_Date]
,[Delete_Flag]
,[Deactivate_Flag]) VALUES ('sndbsndbsdnbsndbsnbdnsbdn23','Client','','Client','2013-03-12 21:31:38.437','Client','2013-03-12 21:31:38.437','0','0')
Msg 8152, Level 16, State 4, Line 1
String or binary data would be truncated.
The statement has been terminated.
NOTE: My Table has a space [UserLevel ] as it was made that way from before

This is caused by attempting to put too much data into a column.
The trouble is, none of the values specified in your example are too large for the columns indicated in your schema picture. I'd therefore assume that the values you've given us either aren't the true values, or you've got a trigger on that table, which is actually what is causing the error.
As an aside, shouldn't your Delete_Flag and Deactivate_Flag columns be of datatype bit, rather than char(1)?
Edit:
Oh, and one more thing - as Client_ID is an nvarchar, you probably want to store unicode data in there. To indicate this in your script, you should use the "N" prefix on your strings, like so:
INSERT INTO [CVSUAT].[dbo].[UserLevel ](
[Client_ID]
,[User_Level_Name]
,[User_Level_Description]
,[Created_by]
,[Created_Date]
,[Modified_by]
,[Modified_Date]
,[Delete_Flag]
,[Deactivate_Flag]) VALUES (N'sndbsndbsdnbsndbsnbdnsbdn23','Client','','Client','2013-03-12 21:31:38.437','Client','2013-03-12 21:31:38.437','0','0')

Related

Binary or string data would be truncated

I am running an insert statement to populate records into a table using SQL Server 2012. In table 1 that has all the records, the datatype is VARCHAR(5000), and I have done a max(len) to determine that the maximum length of data it contains is about 3000.
In table 2 that the records should go into, the datatype for the field is VARCHAR(5000), which mirrors what's in Table 1.
I am getting the dreaded binary or string data would be truncated message, but my destination table field is large enough to store this data.
When I remove this field from the insert statement, the insert statement works fine and my data moves from Table 1 to Table 2 as expected, but including the field causes this error.
Has anyone come across this peculiar case before? Is it possible that the string field has some sort of weird characters in it that could be causing this error.
Thanks

How do I convert a nvarchar to a uniqueidentifer?

I have a table (RPT.table1) that contains data that was exported from an ArcGIS Online application. One of the columns in the table (GlobalID) was exported as a nvarchar(255) datatype. I need to convert this column to a uniqueidentifier datatype before I insert this data into another table (CFAdmin.table2).
The values in the GlobalID column were once unique identifiers and already contain hyphens (B4A6AA96-42DF-48D9-A3E0-4C7F88ED3E1D).
I've tried using
ALTER TABLE RPT.table1
ALTER COLUMN GlobalID uniqueidentifier;
and in an INSERT INTO statement
INSERT INTO CFAdmin.table2 (GlobalID)
SELECT CAST(GlobalID AS uniqueidentifier)
FROM RPT.table1;
For both methods I get an error
Msg 8169, Level 16, State 2, Line 1
Conversion failed when converting from a character string to uniqueidentifier.
I'm relatively new to SQL Server and am guessing there is a pretty simple solution.
Thanks for reading.
Chances are there's an issue with your data, not with your code/syntax. There are probably values in the source table that are invalid as unique identifiers. I would investigate by just looking at the data in your source table and trying to find the values that would cause the error. See what this query returns:
Select GlobalID from RPT.table1 WHERE GlobalID NOT LIKE '________-____-____-____-____________'
(Those underscores blur together, so for the sake of being explicit: 8 underscores, dash, 4 underscores, dash, 4 underscores, dash, 4 underscores, dash, 12 underscores)
And maybe check
Select GlobalID from RPT.table1 WHERE GlobalID IS NULL

What is the easiest way to track down an Insert Into error?

I'm working with a rather large Insert Into . . Select . . From . .
I have over 500+ lines of SQL in this script and I'm getting this error:
INSERT INTO MtgeMaster ( [Col1]
,[Col2]
,[Col3]
, etc., etc. )
SELECT [Col1]
,[Col2]
,[Col3]
, etc., etc.
FROM MtgeMktg
When I run the code above I get this error:
Msg 8114, Level 16, State 5, Line 164
Error converting data type varchar to numeric.
It looks like the error comes from line 164, but line 164 is literally my [Col1] field, and this is VARCHAR. So, I'm going from VARCHAR to VARCHAR. There is no VARCHAR to NUMERIC.
Also, if I add a couple of blank lines and re-run the process, I get this:
Msg 8114, Level 16, State 5, Line 166
Error converting data type varchar to numeric.
All it's really doing is going to the line with the INSERT INTO clause.
The error must be coming from another line, but it's hard to tell what's throwing the error when I have 500+ lines of SQL to go through.
SQL Server does not makes this easy. I have found that a brute force approach is necessary. I like to start by loading the data into a staging table where all the columns are strings. This makes it easier to manipulate.
You can use one of two methods to find the error. The first is to use try_convert() on each column to determine where the error is.
The second is to do a binary search to find the offending row. Load the first half of the data to see if the error is there. Then divide that half in half. And so on.
It looks like the error before and after adding few blank lines is same. It is probably a datatype conversion issue. You could try using cast() function and convert the data type of the [Col1] in select field of MtgeMktg table to match with the datatype of [Col1] of the MtgeMaster table.
So, here's my attempt to answer this question, based on what is given.
I would review the table structure of MtgeMaster, and see what columns are supposed to be numeric. Let's just say, for this example, Col3 of MtgeMaster is numeric. You may have multiple numeric columns.
I would then query MtgeMktg and check whether the column you're trying to save from MtgeMktg to MtgeMaster is numeric or not. You may have to do this for each column that is numeric in MtgeMaster. If I were doing it (which I did in SQLFiddle), it would be something to the nature of the following:
select * from MtgeMktg where ISNUMERIC(Col3) = 0
Anything that returns from this query will tell you what rows have a non numeric column.
Simple fiddle listed here:
Obviously, you would have 1 of 2 decisions to make at this point. Fix the data, or filter out the rows that have the non-numeric data in it. I presume you'd need the rows though.

String or Binary Data would be truncated message during SQL Server insert command

I'm trying to insert some data into a SQL Server table I created from scratch and cannot add the two values I would like to add which is 'Technology Question' under the column technology questions nor am I able to enter a time stamp under my time_entered column.
I'm basically trying to create a Microsoft SQL Server database to eventually take over the existence of an existing SQLite3 database so in my early test case here I am attempting to pull in one piece of data from the existing SQLite3 table into the SQL Server table.
I have tried changing the syntax around in as many ways as I can think of but am failing to get anywhere e.g. ensuring single quote tick around data values etc.
select * from questiontype
select [Technology Questions], time_entered
from questiontype
INSERT INTO questiontype ([Technology Questions], time_entered)
VALUES ('Technology Question', '2019-03-23 16:59')
I was hoping to see the data values 'Technology Question', '2019-03-23 16:59' in their respective columns within the SQL Server table 'questiontype'
When I try to do above I get the following,
Msg 8152, Level 16, State 4, Line 6
String or binary data would be truncated
Multiple issues but had to set column name to be ID instead of 'INT' and had to make sure it held the identity property. Also had to increase the character limitation for each column.

Column of Date type and inserting value into it

Hi I created a table in which one column is of date type and also works as PK. I tried to insert value 2009-01-07 into this column and had this error. Isn't Date default format yyyy-mm-dd? I don't understand this.
Msg 241, Level 16, State 1, Line 3
Conversion failed when converting date and/or time from character string.
This is my query:
INSERT INTO Table_Name
Values ('2009-01-07', other column values)
Your value '2009-01-07' should be converted.
Date literals are always a deep source of troubles... Best was, to use either
Unseparated: 20090107
ODBC : {d'2009-01-07'}
ISO8601 : 2009-01-07T00:00:00
But your format is short ISO 8601 and should work...
Some possible reasons:
Other values in your VALUES list
a trigger
a constraint
As a_horse_with_no_name stated in comment: Without a column list after INSERT INTO Table(col1, col2, ...) There is a great risk to state your values in a different order thus pairing values with the wrong columns...
Invalid (but good looking) dates such as 2016-06-31
Or a - well known - issue with SQL-Server. Sometimes the order of execution is absolutely not the way one expected it. There are several issues with conversion errors...
What you can try
Use ODBC format (which is treated as DATETIME immediately)
DECLARE a variable with this value and put it in place
Thank you all for the prompt replies. I read and tried all of them and found out why.
'2009-01-07' can be inserted into a Column with "Date" as data type if no CONSTRAINT has issue with that;
my problem was caused by a CHECK constraint on that column.
Originally I set CONSTRAINT as
Column_Name = 'Wednesday'
After I modified it to
DATEName(dw,[Column_Name]) = 'Wednesday'
the inserting began to work.
Thanks again.