Converting a varchar value into an int fails when the value includes a decimal point to prevent loss of data.

If you convert to a decimal or float value first, then convert to int, the conversion works.

Either example below will return 7082:

SELECT CONVERT(int, CONVERT(decimal(12,7), '7082.7758172'));
SELECT CAST(CAST('7082.7758172' as float) as int);

Be aware that converting to a float value may result, in rare circumstances, in a loss of precision. I would tend towards using a decimal value, however you'll need to specify precision and scale values that make sense for the varchar data you're converting.

Answer from Hannah Vernon on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ sql โ€บ sql-query-to-convert-varchar-to-int
SQL Query to Convert VARCHAR to INT - GeeksforGeeks
The CAST() function in SQL Server is used to explicitly convert an expression from one data type to another. It's one of the simplest and most straightforward ways to convert a VARCHAR value into an INT.
Published ย  July 23, 2025
Discussions

Convert varchar to numeric โ€“ SQLServerCentral Forums
Error converting data type varchar to numeric. Please correct what I might be doign wrong. ... ---------------------------------------------- Try to learn something about everything and everything about something. - Thomas Henry Huxley ยท :w00t: Posting Best Practices[/url] Numbers / Tally Tables[/url]SQL-4-Life ... from thangela.NpowerNorthern_v1 a INNER ... More on sqlservercentral.com
๐ŸŒ sqlservercentral.com
April 15, 2010
Error converting data type varchar to numeric issue
I tried a lot of things to fix it, and no success. I have a field called MASTER_BOL_NUMBER . According to documentation it is CHAR. I see that inside it has only blanks and numbers When I try to CAST( MASTER_BOL_NUMBER as numeric) I am getting an error โ€œError converting data type varchar ... More on community.spiceworks.com
๐ŸŒ community.spiceworks.com
24
8
August 3, 2022
sql server - Converting varchar to decimal with truncate - Database Administrators Stack Exchange
I have a column called latitude which is currently varchar(20) I want to convert it to DECIMAL(9,6) However the data stored inside the column is greater than 6 decimal points i.e. 48.12345689112345... More on dba.stackexchange.com
๐ŸŒ dba.stackexchange.com
April 12, 2023
Converting a VARCHAR to NUMBER - Oracle Forums
Hello everyone, I wonder if you can help me. I have a VARCHAR column called "COLUMN_X" and the value of this column is: 15.41854 and I'm trying to insert this column into COLUMN_Y of anothe... More on forums.oracle.com
๐ŸŒ forums.oracle.com
February 2, 2012
Top answer
1 of 2
4

You can use the decimal data type and specify the precision to state how many digits are after the decimal point. So you could use decimal(28,20) for example, which would hold 28 digits with 20 of them after the decimal point.

Here's a SQL Fiddle, showing your data in decimal format.

Fiddle sample:

create table Table1(MyValues varchar(100))

insert into Table1(MyValues)
values                    
('-28.851540616246499'),
('-22.857142857142858'),
('-26.923076923076923'),
('76.19047619047619')

So the values are held as varchar in this table, but you can cast it to decimal as long as they are all valid values, like so:

select cast(MyValues as decimal(28,20)) as DecimalValues 
from table1

Your Sample

Looking at your sample update statement, you wouldn't be able to convert the values from varchar to a numeric type and insert them back in to the same column, as the column is of type varchar. You would be better off adding a new column with a numeric data type and updating that.

So if you had 2 columns:

create table Table1(MyValues varchar(100), DecimalValues decimal(28,20))

You could do the below to update the numeric column with the nvarchar values that have been cast to decimal:

update Table1
set DecimalValues = cast(MyValues as decimal(28,20))
2 of 2
1

I think you're trying to actually change the data type of that column?

If that is the case you want to ALTER the table and change the column type over to float, like so:

alter table table1
alter column column1 float

See fiddle: http://sqlfiddle.com/#!6/637e6/1/0

You would use CONVERT if you're changing the text values to numbers for temporary use within a query (not to actually permanently change the data).

๐ŸŒ
MSSQLTips
mssqltips.com โ€บ home โ€บ handling error converting data type varchar to numeric in sql server
Handling error converting data type varchar to numeric in SQL Server
September 3, 2015 - They appear as numerical characters, yet donโ€™t convert. If we copy the values directly and do a direct SELECT CAST(โ€˜1.00000โ€™ AS DECIMAL(22,8)), they convert without error. If they come with extra spaces, no trimming function works to remove the error. We seldom stumble on these types of data, but they can create encumbrances for developers, so itโ€™s good to know a work-around when transforming these VARCHARs into numerical data points.
๐ŸŒ
SQLServerCentral
sqlservercentral.com โ€บ forums โ€บ topic โ€บ convert-varchar-to-numeric-1
Convert varchar to numeric โ€“ SQLServerCentral Forums
April 15, 2010 - INNER JOIN EPPS.dbo.Customers b ON case when IsNumeric(b.MPANCORE) = 1 then b.MPANCORE else -1 end = a.MPAN1 ยท -- Asuming -1 will not be the valid value for a.MPAN1 ยท That might work, but you need to be carfull using SQL's "IS" functions.. DECLARE @Foo VARCHAR(50) ... Thats right. And thanks for pointing. Also, '1d1' is a Numeric Value but generate error on conversion.
๐ŸŒ
W3Schools
w3schools.com โ€บ sql โ€บ func_sqlserver_cast.asp
SQL Server CAST() Function
The CAST() function converts a value (of any type) into a specified datatype. Tip: Also look at the CONVERT() function. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: ...
Find elsewhere
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ sql โ€บ t-sql โ€บ functions โ€บ cast-and-convert-transact-sql
CAST and CONVERT (Transact-SQL) - SQL Server | Microsoft Learn
SELECT CAST(10.6496 AS INT) AS trunc1, CAST(-10.6496 AS INT) AS trunc2, CAST(10.6496 AS NUMERIC) AS round1, CAST(-10.6496 AS NUMERIC) AS round2; Results of the query are shown in the following table: When converting data types where the target data type has fewer decimal places than the source data type, the value is rounded. For example, this conversion returns $10.3497: ... SQL Server returns an error message when converting nonnumeric char, nchar, nvarchar, or varchar data to decimal, float, int, numeric.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-cast-from-varchar-to-int-in-mysql
How to cast from VARCHAR to INT in MySQL?
September 7, 2023 - To cast VARCHAR to INT, we can use the cast() function from MySQL. Here is the syntax of cast() function. For our example, we will create a table with the help of CREATE command.
๐ŸŒ
Oracle
forums.oracle.com โ€บ ords โ€บ apexds โ€บ post โ€บ converting-a-varchar-to-number-9812
Converting a VARCHAR to NUMBER - Oracle Forums
February 2, 2012 - Hello everyone, I wonder if you can help me. I have a VARCHAR column called "COLUMN_X" and the value of this column is: 15.41854 and I'm trying to insert this column into COLUMN_Y of anothe...
Top answer
1 of 4
2

A conversion error will occur at run time when an attempt is made to convert the sales.pid value 'Colgate' value to numeric(9) to evaluate the join criteria. As to whether or not this actually happens depends on the order of evaluation in the execution plan.

Below is the sales table clustered index scan predicate from the Unicode literal execution plan, showing the conversion occurs before the 'number' condition is evaluated:

CONVERT_IMPLICIT(numeric(9,0),[tempdb].[dbo].[sales].[pid],0)=(1.) AND CONVERT_IMPLICIT(nvarchar(10),[tempdb].[dbo].[sales].[type],0)=N'number'  

The plan with the non-Unicode literal has the same shape except the predicate order in the scan operator is reversed:

[tempdb].[dbo].[sales].[type]='number' AND CONVERT_IMPLICIT(numeric(9,0),[tempdb].[dbo].[sales].[pid],0)=(1.)

Although the non-Unicode literal (or parameter) may workaround the problem, the query will still be vulnerable to run time errors. This can be addressed with TRY_CAST, TRY_CONVERT, CASE expression, etc. but it would be better to fix the data model such as to ensure only like or compatible data types are compared.

2 of 4
1

This is because you are using a column that contains text values (pid) to join on a numeric column

INNER JOIN sales ON products.idn = sales.pid 

When you do a join of those 2 tables, SQL needs to compare all the rows from idn to all the rows from pid. In order to compare them, it needs to convert you varchar into numeric.

How can SQL convert 'Colgate' into a number ? this is why it fails.

You should read about database normalization.

Your PID column does not seems good and you would probably need another table with a product ID and a product description (where 'Colgate' will be the description) in order to have it works as you are expecting.

๐ŸŒ
Oracle
forums.oracle.com โ€บ ords โ€บ apexds โ€บ post โ€บ varchar-to-number-conversion-9291
Varchar to Number conversion - Oracle Forums
January 20, 2011 - Hi, Am trying to convert a varchar to number using to_number select (substr(value1,69,10)) from test . this gives 00000000.1 but when I try to convert this to number like select (to_number(substr(...
๐ŸŒ
SQL Team
sqlteam.com โ€บ forums โ€บ topic.asp
Convert Varchar field with spaces to decimal - SQL Server Forums
January 13, 2012 - Microsoft SQL Server articles, forums and blogs for database administrators (DBA) and developers.
๐ŸŒ
JanBask Training
janbasktraining.com โ€บ community โ€บ sql-server โ€บ how-sql-server-convert-varchar-to-int
How sql server convert varchar to int? | JanBask Training Community
April 24, 2021 - SQL server converts varchar to int type with the help of cast and convert functions. The varchar variable must contain numeric characters.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ sql-query-to-convert-varchar-to-int
SQL Query to Convert VARCHAR to INT
October 25, 2024 - Converting VARCHAR to INT is a common task in SQL especially when dealing with databases where numeric data is stored as text. The SQL provides various methods like CONVERT() and TO_NUMBER() for this conversion across the different database ...
๐ŸŒ
W3Schools
w3schools.com โ€บ sql โ€บ โ€บ sql_count.asp
SQL COUNT() Function
String Functions: Asc Chr Concat with & CurDir Format InStr InstrRev LCase Left Len LTrim Mid Replace Right RTrim Space Split Str StrComp StrConv StrReverse Trim UCase Numeric Functions: Abs Atn Avg Cos Count Exp Fix Format Int Max Min Randomize Rnd Round Sgn Sqr Sum Val Date Functions: Date DateAdd DateDiff DatePart DateSerial DateValue Day Format Hour Minute Month MonthName Now Second Time TimeSerial TimeValue Weekday WeekdayName Year Other Functions: CurrentUser Environ IsDate IsNull IsNumeric SQL Quick Ref
๐ŸŒ
SQL Authority
blog.sqlauthority.com โ€บ home โ€บ sql server โ€“ convert text to numbers (integer) โ€“ cast and convert
SQL SERVER - Convert Text to Numbers (Integer) - CAST and CONVERT - SQL Authority with Pinal Dave
March 30, 2019 - SQL SERVER โ€“ FIX : Error : msg 8115, Level 16, State 2, Line 2 โ€“ Arithmetic overflow error converting expression to data type ... I want to convert โ€˜1,2,3,4,5,6,7,8,9,10,11,12โ€™ this string to numeric when i convert it show โ€˜Error converting data type varchar to numericโ€™. plz helpReply ... SELECT CONVERT(INT, CONVERT(CHAR(10), GETDATE(),112 )) SELECT CAST(CONVERT(CHAR(10), GETDATE(), 112) AS INT)Reply