SQL - Error converting data type nvarchar to float - sql

I am trying to convert data type of one of my columns (the table was imported from Excel), and then it shows an error
Error converting data type nvarchar to float
Code:
ALTER TABLE [dbo].[games_activity_2020$]
ALTER COLUMN [Version] float
What can I do differently?

probably there might be any character values inserted in the table while importing from excel. You can check those values using the below query
SELECT version
from [dbo].[games_activity_2020$]
where TRY_CONVERT(float, version) IS NULL
you can update those values and try to ALTER the table again

I suggest you run
SELECT * FROM [dbo].[games_activity_2020$]
WHERE TRY_CAST([Version] as FLOAT) IS NULL
and fix up any problematic values

Related

Convert ntext to numeric

One of my column is defined as ntext datatype which is no longer supported by many streams. I'm trying my best to convert that column to numeric or int and every attempt is reaching anywhere.
reg_no #my ntext field | name | country | etc |
I tried to alter the col using the command we all use, but failed
alter table tabl1
alter column [reg_no] numeric(38,0)
Error:
Error converting data type nvarchar to numeric
Any suggestions on fixing this or has anyone come across this in the past, if yes how did you get over it
You should be able to do this in two steps:
alter table tabl1 alter column [reg_no] nvarchar(max);
alter table tabl1 alter column [reg_no] numeric(38,0);
ntext is deprecated and conversion to numeric is not supported, but converting to nvarchar() is supported.
This assumes that the values are compatible with numeric. Otherwise you will get a type conversion error. If this happens, you can get the offending values by using:
select *
from t
where try_convert(numeric(38, 0), try_convert(nvarchar(max), x)) is null
Try,
select convert(int,convcert(varchar(40), reg_no)) as newfieldname from tabl1

Conversion failed when converting the nvarchar value to data type int - Error Message

My Query
Select * from MyTable
The table consists 300k rows.
It runs for 200k+ rows and this error pops up.
How to handle this to get the full data?
Does MyTable have any computed columns?
Table consists of a computed column with the name IsExceeds which is given below for your reference.
This is the computed column formula:
(CONVERT([int],[Pro_PCT])-CONVERT([int],replace([Max_Off],'%','')))
Field Definitions:
[Pro_PCT] [nvarchar](50) NULL,
[Max_Off] [nvarchar](50) NULL,
[IsExceeds] AS (CONVERT([int],[Pro_PCT])-CONVERT([int],replace([Max_Off],'%','')))
Kindly convert in float then convert into int.
declare #n nvarchar(20)
set #n='11.11'
if (isnumeric(#n)=0)
SELECT 0
else
SELECT CAST(CONVERT(float, #n) as int) AS n
Why are you storing amounts as strings? That is the fundamental problem.
So, I would suggest fixing your data. Something like this:
update mytable
set max_off = replace(max_off, '%');
alter table mytable alter pro_pct numeric(10, 4);
alter table mytable alter max_off numeric(10, 4);
(Without sample data or an example of the data I am just guessing on a reasonable type.)
Then, you can define IsExceeds as:
(Pro_PCT - Max_Off)
Voila! No problems.
Based on the formula - either Pro_PCT or Max_Off contains the value 11.11 (well, with an extra % for Max_Off. Perhaps they also contain other values that can't be converted to int.
Here's what you can do to find all the rows that will cause this problem:
Select *
from MyTable
where try_cast(Pro_PCT as int) is null
or try_cast(replace([Max_Off],'%','') as int) is null
After you've found them, you can either fix the values or change the calculation of the computed column to use try_cast or try_convert instead of convert.
Check the bad data with
select * from MyTable where isnumeric( Max_Off ) = 0

varchar to numeric:Error converting data type varchar to numeric

I am trying to convert a column formatted in varchar to decimal(19,12) with the following line of code
ALTER TABLE [tablename]
ALTER COLUMN [columnname][format]
and get the following prompt:
Msg 8114, Level 16, State 5, Line 25
Error converting data type varchar to numeric.
Has worked before like a charm. The issue here seems to be that the values in the column are 19 or so digit numeric values formatted as text.
I tried to create a new column, pasted shortened cell values (used the left() function) into it from the original column but that doesn't seem to do the trick either since the code above ends up occationally with the additional "Arithmetic overflow occurred." message.
When some of the rows have incorrect values, ALTER COLUMN would not work. A typical course of action goes like this:
Add a new column of the desired type
Update the column with values that you would like to keep
Drop the old column
Rename the new column
Step 2 would go like this:
UPDATE MyTable
SET NewColumn =
CASE WHEN ISNUMERIC(OldColumn)=1 AND DATALENGTH(OldColumn) <= 19 THEN
CAST(OldColumn AS decimal(19,12))
ELSE
NULL
END
You could also turn ANSI warnings off with SET ANSI_WARNINGS OFF command, which would let you run ALTER COLUMN ignoring data trunction errors. The drawback of this approach is that potential errors get ignored. On the other hand, when you do conversion explicitly with a CASE expression you have an option to supply an alternative value for the error case (I used NULL above, but you can put any number you want).
Could you try to seperate your problem? This does work on SQL 2012:
set nocount on
if object_id ('tempdb..#t1') is not null drop table #t1
create table #t1 (c1 varchar(100))
insert #t1 values ('1234567.8901234567890')
select * from #t1
alter table #t1
alter column c1 decimal(19,12)
select * from #t1
If you play around a bit with the strings you easily can produce an arimetic overflow error. But 'Error converting data type varchar to numeric' needs character or empty sting.
Maybe you can try with your data?

Error converting data type nvarchar to float - nvarchar to float to int

I don't have that much experience with writing SQL queries and I have hit upon a problem. I have read in a table of data into a temporary table (#Temp_Results) and need to change the format of various columns before moving the data to the end table.
What Im trying to do below is take a column (Oil2) that is an nvarchar and convert it to a tinyint and put the result into a new column (Oil4) then drop Oil2 - I realise I will loose decimal places but thats not a problem. The CASE statement is designed to capture anything that is not a number, seeing as the original datatype is nvarchar there could be anything in there and I'm only interested in the numbers.
However when I run the code I get 'Error converting data type nvarchar to float' pointing towards the 'UPDATE' line of code and I cant figure out how to get round it.
Can any of you guys spot my rookie mistake?
ALTER TABLE tempdb..#Temp_Results /*Add new column with datatype of tinyint*/
ADD Oil4 tinyint
GO
UPDATE tempdb..#Temp_Results
SET tempdb..#Temp_Results.Oil4 = CASE
WHEN ISNUMERIC(tempdb..#Temp_Results.Oil2)=1
THEN CAST(ROUND(CAST(tempdb..#Temp_H_Results.Oil2 as float), 0) AS tinyint)
ELSE NULL
END
ALTER TABLE tempdb..#Temp_H_Results /*Drop redundant column of data in wrong (nvarchar) format*/
DROP COLUMN Oil2
Go
Try casting to Decimal in stead of float
CONVERT(varchar(28), cast(tempdb..#Temp_H_Results.Oil2 as decimal(28,0)))

Datetime colum to float type conversion is getting issue in sql server

I'd like alter a column type from Datetime to float. Hence, I executed the below query but it was getting a issue
alter table dbo.purchasedate
alter column supp_dt float null
Error :: Implicit conversion from data type datetime to float is not allowed. Use the CONVERT function to run this query.
Without knowing your desired output, you could just: Add Float column, populate, drop date column.
To my knowledge you cannot add a CONVERT to an ALTER statement, anyone know otherwise?