Error converting data type varchar to bigint: INSERT INTO SELECT - sql

I keep getting this error when trying to execute a stored procedure and am not sure why:
Msg 8114:
Error converting data type varchar to bigint.
Please see my SQL query below:
INSERT INTO StagingArea.dbo.DimStudentsTEST
(StudentCode, Module, Year, UniqueStudentID)
SELECT DISTINCT
RTRIM(S.STUDENT_Student_ID) +
RTRIM(SUBSTRING(AY.ACADEMYR_Academic_Year_Code, 3, 2) +
SUBSTRING(AY.ACADEMYR_Academic_Year_Code, 8, 2)) AS 'UniqueStudentID',
...
FROM
...
INNER JOIN
...
I believe the error is emerging because of the UniqueStudentID I have tried using CAST & CONVERT around the SELECT line but still no luck. Perhaps I am using in the wrong way. I have a feeling it is maybe because the column of the table I am pulling the data from ("AY.ACADEMYR..") is not of datatype 'bigint' so the error message keeps occuring. The datatype of the 'UniqueStudentID' column is of datatype 'bigint'
Can anyone see where the problem lies or if I am meant to use the CAST/CONVERT function then how best to use in this scenario.
Many thanks,

Assuming the columns are in the proper order during the INSERT.
I suspect you there may be some unexpected data/strings in the underlying components.
To identify the bogus records, try the following using try_convert(). As you may know, try_convert() will return a NULL if the conversion fails.
Example
Select *
From ...
Where try_convert(bigint,RTRIM(S.STUDENT_Student_ID)+RTRIM(SUBSTRING(AY.ACADEMYR_Academic_Year_Code,3,2)+SUBSTRING(AY.ACADEMYR_Academic_Year_Code,8,2))) is null

Related

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.

sql conversion of data types cast and convert

I have two columns in my table: AmountIncl_LC, AmountExcl_LC which are nvarchar data type.
Now O want to change data type of these two columns and sum two columns into a one column result us.
select cast(AmountIncl_LC as int)int [AmountIncl_LC]
from dbo.table2
I used the above query and it is throwing me an error saying
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'AmountIncl_LC'.
Try this:
select cast(AmountIncl_LC as numeric(16,8))+cast(AmountExcl_LC as numeric(16,8)) from dbo.table2
try this
select cast(AmountIncl_LC as int)+cast(AmountExcl_LC as int) from dbo.table2
The question doesn't contain all the details. The DB seems to hold ints in decimal format stored as Nvarchar, which in the first place isn't a great approach. You should try cast:
select try_cast(AmountIncl_LC as decimal)+try_cast(AmountExcl_LC as decimal) from dbo.table2
CAST function could be helpful to change the data type while Querying, and use the
addition operator "+" to add both columns. Further, you can use the "AS" command to create a new column ;
As far as my understanding, the only error in your code is variable mismatch and Syntax error;
select CAST(AmountIncl_LC AS int) + CAST(AmountExcl_LC AS int) as New_Column from dbo.table2;

UNION causes "Conversion failed when converting the varchar value to int"

I tried to search for previous articles related to this, but I can't find one specific to my situation. And because I'm brand new to StackOverflow, I can't post pictures so I'll try to describe it.
I have two datasets. One is 34 rows, 1 column of all NULLs. The other 13 rows, 1 column of varchars.
When I try to UNION ALL these two together, i get the following error:
Conversion failed when converting the varchar value to data type int.
I don't understand why I'm getting this error. I've UNIONed many NULL columns and varchar columns before, among many other types and I don't get this conversion error.
Can anyone offer suggestions why this error occurs?
The error occurs because you have corresponding columns in the two of the subqueries where the type of one is an integer and the type of the other is a character. Then, the character value has -- in at least one row -- a value that cannot be automatically converted to an integer.
This is easy to replicate:
select t.*
from (select 'A' as col union all
select 1
) t;
Here is the corresponding SQL Fiddle.
SQL Server uses pretty sophisticated type precedence rules for determining the destination type in a union. In practice, though, it is best to avoid using implicit type conversions. Instead, explicitly cast the columns to the type you intend.
EDIT:
The situation with NULL values is complicated. By itself, the NULL value has no type. So, the following works fine:
select NULL as col
union all
select 'A';
If you type the NULL, then the query will fail:
select cast(NULL as int) as col
union all
select 'A';
Also, if you put SQL Server in a position where it has to assign a type, then SQL Server will make the NULL an integer. Every column in a table or result set needs a type, so this will also fail:
select (select NULL) as col
union all
select 'A';
Perhaps your queries are doing something like this.
I have also encountered this error when I accidentally had the fields out of sequence in the 2 SELECT queries that I was unioning. Adjusting the fields' sequence fixed the problem.

SQL Server Ce 2005 Data Conversion fails based on data in table

I have a query like :
select * from table where varchar_column=Numeric_value
that is fine until I run an insert script. After the new data is inserted, I must use this query:
select * from table where varchar_column='Numeric_value'
Can inserting a certain kind of data cause it to no longer implicitly convert?
After the insert script, the error is Data conversion fails OLEDB Status = 2
And the second query does work
I'm not certain of this... the first may be doing an implicit conversion of the varchar_column to a numeric value. Not the other way around. But when you insert values into that column that's no longer convertable, it fails. However, with the second, you're doing a varchar to varchar comparison and all is right again with the world. My guess.

Conversion error on host variable or parameter *N

I am getting this Error on Insert statement to AS400 database, using Java with JDBC.
I figured it out, I had a numeric field with length 4 and decimal digits 2, In my insert I was trying to insert 100 in this numeric field which gave this error.
I altered the table and made it numeric with length 9 and decimal digits 2 and the insert worked.
even though the error says "Conversion error", actually its the wrong field length.
I ran in to this issue when a table was updated from 2 digits to 3 for user ID's and the query inserts in to a history table where the history table was not updated to 3 digits for the user id. Used Alter Table to correct the history table and all is well.
alter table "Table Name" ALTER COLUMN "COLUMN NAME" SET DATA TYPE NUMERIC(3)
The error message is not intuitive:
Conversion error on variable or parameter *N (#-302) [IBM][System
iAccess ODBC Driver][DB2 for i5/OS]SQL0012 - Correlation without
qualification occurred for column "columnname" to table "Tablename".
(#12)
I got this error today running the following query:
select *
from mytable
where invoice_date >= '2019-01-01'
Turned out invoice "date" is not a date... it's a decimal that impersonates a date. Poor database design, perhaps, but nonetheless the simple fix was:
select *
from mytable
where invoice_date >= 20190101
My issue was I overlooked the fact that I had single quotes around my parameter place holder. So I was getting a, 6 -- Numeric data that is not valid. error.
So, for example:
select someColumn
From favTable
where someOtherColumn = '?'
command.Parameters.Add("decVal", OleDbType.Decimal, 10).Value = model.someDecVal;
Correcting to:
select someColumn
From favTable
where someOtherColumn = ?
solved my issue.
Looks easy to catch here, but with big queries, this can be easily overlooked. Wasted about an hour on this.