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
๐ŸŒ
SQLServerCentral
sqlservercentral.com โ€บ forums โ€บ topic โ€บ convert-varchar-to-numeric
Convert varchar to numeric โ€“ SQLServerCentral Forums
September 22, 2007 - You can always convert a varchar field to a decimal field.I have tested this by creating a Test Table with a decimal field with length=5,precision=9,scale=3.
Discussions

Converting varchar to numeric type in SQL Server - Stack Overflow
I have a column in a table with a varchar datatype. It has 15 digits after the decimal point. Now I am having a hard time converting it to a numeric format.. float, double etc. Does anyone have any More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to convert data type from varchar to number in SQL Server? - Database Administrators Stack Exchange
I want to convert a field currently in varchar data type to number data type in the Views of SQL Server say (dbo.CUSTOMER.STORE_NBR) from Varchar to number. How to do that ? Will CONVERT function w... More on dba.stackexchange.com
๐ŸŒ dba.stackexchange.com
February 16, 2017
How Do I Convert VARCHAR to NUMERIC
Msg 8114, Level 16, State 5, Line 110 Error converting data type varchar to numeric. Thank you for your anticipated quick response. ... An Azure relational database service. ... A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented ... More on learn.microsoft.com
๐ŸŒ learn.microsoft.com
2
0
February 24, 2023
sql - How to convert Varchar column to Numeric - Stack Overflow
I have a requirement to move varchar column data to Numeric but with two conditions. All the alphanumeric value should migrate as null All the decimal values should go as it is. I wrote the conditi... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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).

๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ answers โ€บ questions โ€บ 1184096 โ€บ how-do-i-convert-varchar-to-numeric
How Do I Convert VARCHAR to NUMERIC - Microsoft Q&A
February 24, 2023 - DECLARE @Category VARCHAR (3) SET @Category='001' SELECT CASE WHEN Category=@Category THEN CONCAT(TotalCommission , '*disc') ELSE CAST(TotalCommission AS VARCHAR(15)) END AS TotalCommission FROM GenReport ยท 1 comment Show comments for this answer Report a concern ... Thank you Naomi for your assistance. Your code also worked but I preferred using CONCAT() because CONCAT() function automatically does the data type conversion while CAST() is standard ANSI very similar to CONVERT() which is specific to T-SQL.
๐ŸŒ
W3Docs
w3docs.com โ€บ php
Converting Varchar Value to Integer/Decimal Value in SQL Server
In SQL Server, you can convert a varchar value to an integer or decimal value using the CAST or CONVERT function. To convert a varchar value to an integer, you can use the CAST function like this: ... Please note that if the varchar value cannot ...
Find elsewhere
Top answer
1 of 6
4

There is by default function in SQL Server ISNUMERIC() so, first of all Check your data value by that function,

Select ISNUMERIC(DATA)

Whole query is written as below,

SELECT CASE WHEN ISNUMERIC(data)=1 THEN CAST(data as decimal(18,2))
            ELSE NULL END as tData FROM DataTable 

As per your question,first we have to convert with numeric with using case,which satisfies your first condition,another thing if the value is String than convert as NULL. In Above query both the condition has been taken care.

EDIT : If you are using SQL SERVER 2012 or higher version then use TRY_PARSE(), then there will be no need to worry about using CASE too...

I have tried this,

SELECT TRY_PARSE('63.36' as decimal(18,2)) got result 63.36

and

SELECT TRY_PARSE('.' as decimal(18,2)) got result NULL
2 of 6
2

I think that this fits your spec. It is quite verbose, but hopefully it breaks down the conditions sufficiently that it's clearly doing the correct thing or, if it isn't, that it's easy enough to modify:

declare @t table (data varchar(30))

insert into @t(data) values
('1234'),
('1.23'),
('abc'),
('.abc'),
('+6000'),
('1.2.3')

select
    CASE WHEN
        Possible = 1 AND
            (DecCheck = 0 OR
            SingleDec = 1
        ) THEN
            CONVERT(decimal(12,3),data)
        END
from
    @t t
        cross apply
            (select
                --Only contains the correct characters
                CASE WHEN not t.data like '%[^0-9.]%' THEN 1 ELSE 0 END as Possible,
                --Contains a decimal point? (Needs more checks)
                CASE WHEN CHARINDEX('.',t.data) > 0 THEN 1 ELSE 0 END as DecCheck,
                CHARINDEX('.',t.data) as FirstDec --Where the first decimal point is
            ) p
        cross apply
            (select
                CASE WHEN DecCheck = 1 THEN
                    --Only contains one decimal point
                    CASE WHEN LEN(data) = FirstDec + CHARINDEX('.',REVERSE(data)) - 1
                        THEN 1
                    ELSE 0 END
                ELSE 0 END as SingleDec
            ) d

Results:

data                           
------------------------------ ---------------------------------------
1234                           1234.000
1.23                           1.230
abc                            NULL
.abc                           NULL
+6000                          NULL
1.2.3                          NULL

I.e. one additional check you may want to use is that a decimal cannot be the first or last character in the string. That is easy enough to do by adding those additional checks into the first CASE for the SingleDec column.

๐ŸŒ
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 โ€บ 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 ...
๐ŸŒ
Quora
quora.com โ€บ How-do-I-convert-Varchar-to-Int-in-SQL-server
How to convert Varchar to Int in SQL server - Quora
This will return 1 if the VARCHAR value is numeric and 0 if not. ... PRINT 'string ' + @string1 + ' is a number. You can use CAST to convert to INT'; ... PRINT 'string ' + @string2 + ' is a number. You can use CAST to convert to INT'; ... Conversion failed when converting the varchar value '1234567a' to data type int. ... Itโ€™s clean and without runtime errors. ... How do you resolve an error converting varchar value to data type int (SQL, SQL server, development)?
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.

๐ŸŒ
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.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ sql-convert-varchar-to-numeric
SQL Convert Varchar to Numeric โ€“ Linux Hint
Some database engines may implement the procedures differently. For example, in SQL Server, the safe_cast() function is renamed to try_cast or try_convert. In this tutorial, we discussed how to perform type-casting in Standard SQL. For example, we discussed converting varchar to int64, numeric, bignumeric, and float64.
๐ŸŒ
Experts Exchange
experts-exchange.com โ€บ questions โ€บ 28153242 โ€บ Convert-VarChar-to-Numeric-in-WHERE-Clause.html
Solved: Convert VarChar to Numeric in WHERE Clause | Experts Exchange
June 10, 2013 - BAKADY's second SQL statement above handles that scenario by forcing a return of zero. You can utilize that approach to find out which record(s) are causing you difficulty: SELECT * FROM Accounts WHERE ISNUMERIC(AgencyDefinField) = 0 ... It's better from a performance perspective to filter the opposite way i.e. amend the criteria to suit that data. This way indexes can be used by the query. e.g. SELECT * FROM Accounts WHERE AgencyDefinField = convert(varchar,4765765) -- or nvarchar if the field is only supposed to carry strings of digits then there may be data you need to correct, however I'd still suggest filtering by conversion of criteria to suit the field's data type.
๐ŸŒ
Reddit
reddit.com โ€บ r/sql โ€บ convert varchar to int ?
r/SQL on Reddit: Convert VARCHAR to INT ?
March 15, 2022 -

Is there any method to convert VARCHAR to INTEGER?

CAST and CONVERT failed because we have alphanumeric values in the data.

Trying to find if there is a way to convert VARCHAR to INT without losing alphanumeric characters

Example data looks like 56H543G in varchar data type, when converted to integer I would like to see 56H543G ....is this possible

๐ŸŒ
Microsoft Community Hub
techcommunity.microsoft.com โ€บ microsoft community hub โ€บ communities โ€บ products โ€บ sql server โ€บ sql server
Converting a varchar field to Numeric | Microsoft Community Hub
July 5, 2022 - I thought I could use cast or convert like the Val function in Access but instead of ignoring the alpha it errors. What I want is to convert the field to a numeric and exclude any record where that field has a length of 4.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ answers โ€บ questions โ€บ 228262 โ€บ cant-fix-error-converting-data-type-varchar-to-num
cant fix Error converting data type varchar to numeric. - Microsoft Q&A
January 12, 2021 - You have a varchar column, so use varchar literals. Converting to any numeric value runs the risk of error (as you found) as well as incorrect results. The string '00001' is not logically the same string as '1' but convert them to numbers and ...