The conversion of the varchar value '5035899999' overflowed an int column - sql

Select Distinct
E.EmpNo,
convert(date,R.APPLYDATE) AS DateWorked,
R.PAYCODENAME
From
TableExportEmployees E
INNER JOIN VP_RTIMEDTLTOTALS R
ON R.PERSONNUM = E.EmpNo
Inner Join VP_SCHEDULE V
ON V.PERSONNUM = E.EmpNo
When I am executing this I am getting this error
Msg 248, Level 16, State 1, Line 1 The conversion of the varchar value
'5035899999' overflowed an int column.

When you make joins, you need to ensure that type of the columns used in the join haves the same type in the two tables.
It may cause these kind of errors.
Verify and tell us what are the column types of:
- R.PERSONNUM
- E.EmpNo
- V.PERSONNUM
If you are using a int column, you should alter it, using bigint or numeric

The reason is in the error code. It is trying to evaluate that value as type int.
The value is beyond the range supported by int.
See: https://learn.microsoft.com/en-us/sql/t-sql/data-types/int-bigint-smallint-and-tinyint-transact-sql
Find the violating column, and if you do need to store that large value as a number you'll need to redefine the column to be of type bigint. Or if it is ok to store as string redefine your column to be of type varchar(greater than 10) since your value is already 10 digits long.
Based on your comment, the violating column is actually PersonNum. SQL is trying to convert it to int to compare to the EmployeeNum. To fix it, you need to redefine EmployeeNum to be the same datatype as PersonNum, or redefine them both to be bigint.

Related

Msg 245, Level 16, State 1, Line 4 Conversion failed when converting the nvarchar value '239.6' to data type int

I have this query:
SELECT SerialNumber
FROM [ETEL-PRDSQL].[ERP10DBLIVE].[ERP].[SerialNo]
WHERE CustNum IN (2);
It's causing this error:
Msg 245, Level 16, State 1, Line 4
Conversion failed when converting the nvarchar value '239.6' to data type int.
The query works if I compare CustNum with a different value, but it fails when I try CustNum IN (2).
How can I fix this?
You have a varchar column named CustNum. The varchar values in this column may contain only digits, but that doesn't make them numbers! Then you compare this text column with the integer value 2. Again, the integer value 2 is not the same as the text value '2'. It's also not the same as the floating point value 2.0. These are all different, they have different types, and SQL Server must resolve any such differences before it can compare values.
Based on type precedence rules SQL Server determines it needs to convert the text in the column to the integer, instead of vice versa. Once this determination is made for the query, if you have any data in the text column that is not integer-compatible, the query is going to fail.
It's important to understand this conversion happens separately from the conditional check in the WHERE clause, and is a prerequisite for that check. It's not enough to expect the WHERE condition to evaluate to FALSE for rows that do not convert. This is true even if you don't need the row, because SQL Server can't know you don't need that row until after it attempts the conversion!
In this case, we have the value 293.6. This value may be numeric, but it is not an integer. Nor is it convertible to integer. Therefore the query fails.
In addition to (eventually!) failing the query, this is absolutely awful for performance. SQL Server has to do this conversion for every row in the table... even rows you don't need. This is because SQL Server doesn't know which rows will match the WHERE clause until after it checks the conditional expression, and it needs to do this conversion in order to make that check. Worse still, the new converted value no longer matches your indexes, so any indexes you might have become worthless for this query. That cuts to the core of database performance.
If you don't like it, define your data types better, or trying comparing the string with another string:
SELECT SerialNumber
FROM [ETEL-PRDSQL].[ERP10DBLIVE].[ERP].[SerialNo]
WHERE CustNum IN ('2');
The query might also run if you did this:
SELECT SerialNumber
FROM [ETEL-PRDSQL].[ERP10DBLIVE].[ERP].[SerialNo]
WHERE CustNum IN (2.0);
Now the type precedence rules will convert your text to a floating point type, and it's possible that will succeed if the rest of the values in the table are compatible. It's also possible this is closer to what you intend... but again, the performance here will be much worse.

Why is SQL converting Varchar value to Integer in a varchar column?

I am receiving an error
Conversion failed when converting the varchar value to data type int
while trying to insert data from one table into another. Both have the same table structure (table being inserted is an exact copy of the one used in the Select) and data types on the columns are the same.
INSERT INTO PS_PSOPRDEFN_BA
SELECT *
FROM PSOPRDEFN
Error:
Conversion failed when converting the varchar value '11000_600' to data type int.
The column this is inserting with this value is a varchar(30) in both tables, so I don't know why SQL is trying to convert it to int. Any ideas are appreciated.
When doing inserts, always include the columns:
INSERT INTO PS_PSOPRDEFN_BA ( . . . ) -- column list here
SELECT . . . -- column list here
FROM PSOPRDEFN;
You have a value which is a string which is being assigned to an integer column, and the value cannot be converted.
When doing an insert, the columns are aligned by order in the column list, not by name. So, merely having the same name doesn't mean that the code will work. The tables have to have exactly the same columns defined in the same order with compatible types for your code to work.

PostgreSQL - casting varchar to int?

I'm trying to execute the following query:
DELETE FROM table_name WHERE ID>9;
But i can't since the 'ID' field is of type 'varchar'.
How can i cast it to 'int' and have it act properly? (deleting all rows with ID greater than 9 rather than converting it to a numeric varchar value)
I don't know why you would store a numeric value as a string. You might want something more like:
DELETE FROM table_name
WHERE regexp_matches(ID, '^[1-9][0-9]');
This will delete from the table any id that starts with two digits, where the first is not 0. If you attempt a conversion, then you might get a conversion error if not all ids are numbers. This will also work for long numbers that would overflow an int (although numeric would fix that problem).
EDIT:
For a general solution, I think I would write it as:
where (case when regexp_matches(id, '^[0-9]+$')
then id::numeric
end) > 70000
The case should prevent any error on non-numeric ids.

Convert alphanumeric varchar to int

I am using this script
select * from mxiv_sentries where
attrname='mskeyvalue' and mskey in
(select mskey from mxiv_sentries where attrname='MXREF_MX_PRIVILEGE' and searchvalue='672081'
and mskey not in
(select Default_login from mxman_rt_u.VPN))
these are two different tables where Default_login is alphanumeric and varchar and mskey is number in INT. So when i execute this script i end up with error:
Msg 245, Level 16, State 1, Line 1 Conversion failed when converting
the varchar value 'A15271' to data type int.
Can you please suggest how to get proper results
Thanks
You obviously have a value of 'A15271' in the Default_Login field in the VPN table.
If this is a varchar, you need to either
(preferably) NOT COMPARE IT TO NUMERICS since, you know, they are different data types and not equivalent
or (less preferably) CAST the int as a Varchar before the comparison. This will have some overhead and will likely make your indexes moot, but in a query like this with a structure like this indexes may not even exist.

Arithmetic overflow error converting expression to data type datetime

I am using SQL query to pull some student records from SQL database. I am getting the error Arithmetic overflow error converting expression to data type datetime. It looks like there is a column for student number which is char(15) type and throwing this error every time I put a letter in front of the student number (we have students with this case).
This works fine
Select * from StudentDataTable where StudentNumber = '123456789'
This throws the error
Select * from StudentDataTable where StudentNumber = 'A12345678'
Any help would be appreciated.
SQL is converting the student number to an integer in the background, so your first examples works, but your second one won't. Check what data-type student number is, it should be numeric type, INT, BIGINT etc..
Also, you should be querying without the quotes for the student number, saves SQL converting
Select * from StudentDataTable where StudentNumber = 123456789