Convert alphanumeric varchar to int - sql-server-2005

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.

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.

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.

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

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.

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.

Error unable to convert data type nvarchar to float

I have searched both this great forum and googled around but unable to resolve this.
We have two tables (and trust me I have nothing to do with these tables). Both tables have a column called eventId.
However, in one table, data type for eventId is float and in the other table, it is nvarchar.
We are selecting from table1 where eventI is defined as float and saving that Id into table2 where eventId is defined as nvarchar(50).
As a result of descrepancy in data types, we are getting error converting datatype nvarchar to float.
Without fooling around with the database, I would like to cast the eventId to get rid of this error.
Any ideas what I am doing wrong with the code below?
SELECT
CAST(CAST(a.event_id AS NVARCHAR(50)) AS FLOAT) event_id_vre,
The problem is most likely because some of the rows have event_id that is empty. There are two ways to go about solving this:
Convert your float to nvarchar, rather than the other way around - This conversion will always succeed. The only problem here is if the textual representations differ - say, the table with float-as-nvarchar uses fewer decimal digits, or
Add a condition to check for empty IDs before the conversion - This may not work if some of the event IDs are non-empty strings, but they are not float-convertible either (e.g. there's a word in the field instead of a number).
The second solution would look like this:
SELECT
case when a.eventid <> ''
then cast(cast(a.event_id as nvarchar(50)) as float)
ELSE 0.0
END AS event_id_vre,
Convert float to nvarchar instead of nvarchar to float. Of course!