Actually, I can't insert data into a table with big number.
The primary key has type of BIGINT, when I try to insert a row I'm getting this error.
Same thing happens when I try it manually with HeidiSQL.
It seems you are going to insert string/varchar/nvarchar data to your BIGINT type column. please check your data.
The problem is solved. It's been caused by a trigger in the database, which tried to put a string with "8001049473-A1-R" into a bigint field. After deleting that row containing the "8001049473-A1-R", everything works.
Related
Backend code = java, hibernate, maven, hosted in AEM.
DB = SQL db
Existing table had column of type INT.
Backup of the original table containing that column was made by Select * into backupTable from originalTable
Backup of the audit table (for the original table) containing that column was made by Select * into backupTable_AUDIT from originalTable_AUDIT
Column type INT was changed to VARCHAR(255) by dropping-recreating that column in originalTable.
Column type INT was changed to VARCHAR(255) by dropping-recreating that column in originalTable_AUDIT.
All places in the code that used that column has been changed to accomodate VARCHAR.
BE Code was rebuilt.
Code was deployed.
When trying to run app getting error: "wrong column type encountered in column [mycolumn] in table [originalTable]; found [nvarchar (Types#NVARCHAR)], but expecting [int (Types#INTEGER)])"
After deleting backupTable_AUD => no error any more, all works fine.
As much as I know each table in the db schema has an id.
It seems BE-code/Hibernate was looking at backup table id?
Can somebody please explain more why deleting backup tables did help to eliminate the error,
and which step was missed during deployment/backup to avoid this error?
Many thanks
I am trying to concat two integers together and then cast it as bigint, but SQL Server 2016 is throwing an error:
Error converting data type varchar to bigint
My table has two columns idnum int not null and innumdb not null.
In my select query, I am doing the following:
SELECT CAST(CONCAT(idnum, idnumdb) AS BIGINT)
FROM TABLE
When I execute the query, some data will be display but then while the query is displaying data, I get the error mentioned above.
I don't understand why since both idnum and idnumdb are int datatype in the table. I am assuming the concat function is messing up the data. Any ideas what's going on here? Thanks in advance
thanks everyone for helping out. i tried Udai solution of executing
SELECT CONCAT(idnum, idnumdb) FROM TABLE WHERE ISNUMERIC(CONCAT(idnum, idnumdb) ) <> 1
it turns out that idnumdb has a - sign for a few rows. i test on idnum only and that was fine. idnumdb was the problem. thanks all
Good Day,
I am trying to insert records from csv file into my database table. Problem is in inserting alphanumeric values.
My column datatype is set to NUMERIC(19,0), in this column I am expecting some numeric values to be inserted from. For some specific reasons I am getting alphanumeric values in my csv file. For example:
I am getting value: GBS1182000945008.
My goal here is to remove those three characters and cast the remaining string as Numeric and get it inserted inside my table.
So far I have tried:
CAST((select substring(?,4,30)) AS NUMERIC)
But, I am still getting that annoying error, I cannot just ignore those values by using TRY_CONVERT as I do need those records in my database. What am I missing here?
Edit: I have tested this code separately and it is working as expected, only problem is in using it while inserting values. What I have done is that, I checked whether the given parameter is numeric or not, if it is I am just inserting the param if not then I am converting that param into numeric.
So here is my whole scenario:
If (SELECT ISNUMERIC(?)) = 1 {
// Just insert the parameter as:
Insert INTO table (NUMERIC_FIELD) VALUE(?)
}
ELSE {
Insert INTO table (NUMERIC_FIELD) VALUE(CAST((select substring(?,4,30)) AS NUMERIC))
}
Here ? represents the value from CSV.
Try AS NUMERIC(19,0) instead of AS NUMERIC
Also, please note you can have 30 digits in the extracted substring (it will not fit your 19 digis of the column datatype.
I'm working on a legacy system using SQL Server in 2000 compatibility mode. There's a stored procedure that selects from a query into a virtual table.
When I run the query, I get the following error:
Error converting data type varchar to numeric
which initially tells me that something stringy is trying to make its way into a numeric column.
To debug, I created the virtual table as a physical table and started eliminating each column.
The culprit column is called accnum (which stores a bank account number, which has a source data type of varchar(21)), which I'm trying to insert into a numeric(16,0) column, which obviously could cause issues.
So I made the accnum column varchar(21) as well in the physical table I created and it imports 100%. I also added an additional column called accnum2 and made it numeric(16,0).
After the data is imported, I proceeded to update accnum2 to the value of accnum. Lo and behold, it updates without an error, yet it wouldn't work with an insert into...select query.
I have to work with the data types provided. Any ideas how I can get around this?
Can you try to use conversion in your insert statement like this:
SELECT [accnum] = CASE ISNUMERIC(accnum)
WHEN 0 THEN NULL
ELSE CAST(accnum AS NUMERIC(16, 0))
END
I'm working on SQL Server 2008.
I delete all data from a table and then I try to insert value to the table. Here's the code:
TRUNCATE TABLE [dbo].[STRAT_tmp_StratMain]
INSERT INTO [dbo].[STRAT_tmp_StratMain] ([FileNum])
SELECT [dbo].[STRAT_tmp_Customer].[NumericFileNumber]
FROM [dbo].[STRAT_tmp_Customer];
The FileNum in STRAT_tmp_StratMain is float number and is also index and can't be null.
NumericFileNumber is float and can be null but is never null and there are no duplicates in it (each row is unique number).
The table STRAT_tmp_StratMain contain much more fields but all can be null and also has a defualt values.
When I try to run this query I get the error:
Msg 8152, Level 16, State 4, Line 1 String or binary data would be
truncated. The statement has been terminated.
I tried also to do simply:
INSERT INTO [dbo].[STRAT_tmp_StratMain] ([FileNum]) Values (1);
Still get the same error.
Any ideas?
Thanks,
Ilan
I am not able to reproduce your issue. When I run this code on SQL Server 2008, I get no error:
DECLARE #tt TABLE (FileNum float NOT NULL);
INSERT INTO #tt (FileNum) VALUES (1);
Check the Default constraints on all the columns in your target table and make sure none of them would try to insert a string value that would truncated by the datatype limitations of the column.
example: SomeColumn varchar(1) DEFAULT 'Hello'
This due to the data you are trying to insert does not fit in the field: if you have a defined length of (say) 10 or 50 characters but the data you are trying to insert is longer than that.