You could try updating the table to get rid of these characters:

UPDATE dbo.[audit]
  SET UserID = REPLACE(UserID, CHAR(0), '')
  WHERE CHARINDEX(CHAR(0), UserID) > 0;

But then you'll also need to fix whatever is putting this bad data into the table in the first place. In the meantime perhaps try:

SELECT CONVERT(INT, REPLACE(UserID, CHAR(0), ''))
  FROM dbo.[audit];

But that is not a long term solution. Fix the data (and the data type while you're at it). If you can't fix the data type immediately, then you can quickly find the culprit by adding a check constraint:

ALTER TABLE dbo.[audit]
  ADD CONSTRAINT do_not_allow_stupid_data
  CHECK (CHARINDEX(CHAR(0), UserID) = 0);

EDIT

Ok, so that is definitely a 4-digit integer followed by six instances of CHAR(0). And the workaround I posted definitely works for me:

DECLARE @foo TABLE(UserID VARCHAR(32));
INSERT @foo SELECT 0x31353831000000000000;

-- this succeeds:
SELECT CONVERT(INT, REPLACE(UserID, CHAR(0), '')) FROM @foo;

-- this fails:
SELECT CONVERT(INT, UserID) FROM @foo;

Please confirm that this code on its own (well, the first SELECT, anyway) works for you. If it does then the error you are getting is from a different non-numeric character in a different row (and if it doesn't then perhaps you have a build where a particular bug hasn't been fixed). To try and narrow it down you can take random values from the following query and then loop through the characters:

SELECT UserID, CONVERT(VARBINARY(32), UserID)
  FROM dbo.[audit]
  WHERE UserID LIKE '%[^0-9]%';

So take a random row, and then paste the output into a query like this:

DECLARE @x VARCHAR(32), @i INT;
SET @x = CONVERT(VARCHAR(32), 0x...); -- paste the value here
SET @i = 1;
WHILE @i <= LEN(@x)
BEGIN
  PRINT RTRIM(@i) + ' = ' + RTRIM(ASCII(SUBSTRING(@x, @i, 1)))
  SET @i = @i + 1;
END

This may take some trial and error before you encounter a row that fails for some other reason than CHAR(0) - since you can't really filter out the rows that contain CHAR(0) because they could contain CHAR(0) and CHAR(something else). For all we know you have values in the table like:

SELECT '15' + CHAR(9) + '23' + CHAR(0);

...which also can't be converted to an integer, whether you've replaced CHAR(0) or not.

I know you don't want to hear it, but I am really glad this is painful for people, because now they have more war stories to push back when people make very poor decisions about data types.

Answer from Aaron Bertrand on Stack Overflow
Top answer
1 of 5
27

You could try updating the table to get rid of these characters:

UPDATE dbo.[audit]
  SET UserID = REPLACE(UserID, CHAR(0), '')
  WHERE CHARINDEX(CHAR(0), UserID) > 0;

But then you'll also need to fix whatever is putting this bad data into the table in the first place. In the meantime perhaps try:

SELECT CONVERT(INT, REPLACE(UserID, CHAR(0), ''))
  FROM dbo.[audit];

But that is not a long term solution. Fix the data (and the data type while you're at it). If you can't fix the data type immediately, then you can quickly find the culprit by adding a check constraint:

ALTER TABLE dbo.[audit]
  ADD CONSTRAINT do_not_allow_stupid_data
  CHECK (CHARINDEX(CHAR(0), UserID) = 0);

EDIT

Ok, so that is definitely a 4-digit integer followed by six instances of CHAR(0). And the workaround I posted definitely works for me:

DECLARE @foo TABLE(UserID VARCHAR(32));
INSERT @foo SELECT 0x31353831000000000000;

-- this succeeds:
SELECT CONVERT(INT, REPLACE(UserID, CHAR(0), '')) FROM @foo;

-- this fails:
SELECT CONVERT(INT, UserID) FROM @foo;

Please confirm that this code on its own (well, the first SELECT, anyway) works for you. If it does then the error you are getting is from a different non-numeric character in a different row (and if it doesn't then perhaps you have a build where a particular bug hasn't been fixed). To try and narrow it down you can take random values from the following query and then loop through the characters:

SELECT UserID, CONVERT(VARBINARY(32), UserID)
  FROM dbo.[audit]
  WHERE UserID LIKE '%[^0-9]%';

So take a random row, and then paste the output into a query like this:

DECLARE @x VARCHAR(32), @i INT;
SET @x = CONVERT(VARCHAR(32), 0x...); -- paste the value here
SET @i = 1;
WHILE @i <= LEN(@x)
BEGIN
  PRINT RTRIM(@i) + ' = ' + RTRIM(ASCII(SUBSTRING(@x, @i, 1)))
  SET @i = @i + 1;
END

This may take some trial and error before you encounter a row that fails for some other reason than CHAR(0) - since you can't really filter out the rows that contain CHAR(0) because they could contain CHAR(0) and CHAR(something else). For all we know you have values in the table like:

SELECT '15' + CHAR(9) + '23' + CHAR(0);

...which also can't be converted to an integer, whether you've replaced CHAR(0) or not.

I know you don't want to hear it, but I am really glad this is painful for people, because now they have more war stories to push back when people make very poor decisions about data types.

2 of 5
24

This question has got 91,000 views so perhaps many people are looking for a more generic solution to the issue in the title "error converting varchar to INT"

If you are on SQL Server 2012+ one way of handling this invalid data is to use TRY_CAST

SELECT TRY_CAST (userID AS INT)
FROM   audit 

On previous versions you could use

SELECT CASE
         WHEN ISNUMERIC(RTRIM(userID) + '.0e0') = 1
              AND LEN(userID) <= 11
           THEN CAST(userID AS INT)
       END
FROM   audit 

Both return NULL if the value cannot be cast.

In the specific case that you have in your question with known bad values I would use the following however.

CAST(REPLACE(userID COLLATE Latin1_General_Bin, CHAR(0),'') AS INT)

Trying to replace the null character is often problematic except if using a binary collation.

Discussions

sql server - Cast varchar into int, converting any value under 0 to 0 - Database Administrators Stack Exchange
DECLARE @BadData VARCHAR(100) = '-5' DECLARE @TinyInt TINYINT SET @TinyInt = CASE WHEN cast(@BadData AS INT) < 0 THEN 0 ELSE @BadData END SELECT @TinyInt ... Naturally, the result of the CAST still has to be <= 255 or it won't fit into a TinyInt (you'll get an arithmetic overflow) ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. 4 How to export a single huge table from SQL Server... More on dba.stackexchange.com
๐ŸŒ dba.stackexchange.com
September 21, 2016
Best practice : converting INT to VARCHAR
What are your opinions on converting INT (and indeed anything else like GUIDs) to VARCHAR? For INTs I see the following used SELECT MyINT, ']'+CAST(MyINT as varchar(20))+'[' AS [Cast], ']'+CONVERT(varchar(20),MyINT)+'[' AS [Convert()], ']'+STR(MyINT,20)+'[' AS [Str()], ']'+LTRIM(STR(MyINT,20))+'[' ... More on forums.sqlteam.com
๐ŸŒ forums.sqlteam.com
0
0
August 6, 2015
convert varchar to int โ€“ SQLServerCentral Forums
convert varchar to int Forum โ€“ Learn more on SQLServerCentral More on sqlservercentral.com
๐ŸŒ sqlservercentral.com
September 22, 2007
Convert VARCHAR to INT ?
You canโ€™t convert letters into int bruh More on reddit.com
๐ŸŒ r/SQL
11
0
March 15, 2022
๐ŸŒ
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
๐ŸŒ
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: ...
๐ŸŒ
Quora
quora.com โ€บ How-do-I-convert-Varchar-to-Int-in-SQL-server
How to convert Varchar to Int in SQL server - Quora
So you can use CAST and CONVERT to change the VARCHAR in INT. But make sure the values in the columns are not alphanumerics. ... You are a coder if you think at row level, a DBA if you think at column level!!
๐ŸŒ
T-SQL Tutorial
tsql.info โ€บ sql โ€บ how-to-convert-varchar-to-int.php
How to convert Varchar to INT in sql - T-SQL Tutorial
To convert a Varchar to INT uses sql conversion functions like cast or convert. CAST ( expression AS datatype [ ( length ) ] ) CONVERT ( datatype [ ( length ) ] , expression [ , style ] )
๐ŸŒ
SQLTeam
forums.sqlteam.com โ€บ transact-sql
Best practice : converting INT to VARCHAR - Transact-SQL - SQLTeam.com Forums
August 6, 2015 - For INTs I see the following used SELECT MyINT, ']'+CAST(MyINT as varchar(20))+'[' AS [Cast], ']'+CONVERT(varchar(20),MyINT)+'[' AS [Convert()], ']'+STR(MyINT,20)+'[' AS [Str()], ']'+LTRIM(STR(MyINT,20))+'[' ...
Find elsewhere
๐ŸŒ
W3Docs
w3docs.com โ€บ php
Converting Varchar Value to Integer/Decimal Value in SQL Server
To convert a varchar value to an integer, you can use the CAST function like this: ... Please note that if the varchar value cannot be converted to the specified data type, the CAST or CONVERT function will return an error.
๐ŸŒ
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 - SELECT CAST('77788' AS INT); SELECT CAST(CAST ('888.67' AS NUMERIC) AS INT). You'd need to determine exactly what character was in the original string. Hopefully you have a copy of the original somewhere.
๐ŸŒ
SQLServerCentral
sqlservercentral.com โ€บ forums โ€บ topic โ€บ convert-varchar-to-int-2
convert varchar to int โ€“ SQLServerCentral Forums
September 22, 2007 - In case anyone else reads this, the reason two conversions are necessary is because the decimal point causes the following error when trying to convert a VARCHAR representation of a number with a decimal point in it, directly to INT. ... Server: Msg 245, Level 16, State 1, Line 1 Syntax error converting the varchar value '27.49' to a column of data type int.
๐ŸŒ
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 ...
๐ŸŒ
DevArt
blog.devart.com โ€บ home โ€บ products โ€บ sql server tools โ€บ sql server cast function with examples for data type conversion
SQL CAST Function in SQL Server โ€“ Syntax, Examples, and Best Practices
October 17, 2025 - With this test table in place, ... and why CAST() is essential. Here is an example of an unexpected string concatenation issue. ... Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value 'Value: ' to data type int. Problem: SQL Server tries to convert the string โ€˜Value: โ€˜ into an integer instead of treating 50 as a ...
๐ŸŒ
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 Learn
learn.microsoft.com โ€บ en-us โ€บ sql โ€บ t-sql โ€บ functions โ€บ cast-and-convert-transact-sql
CAST and CONVERT (Transact-SQL) - SQL Server | Microsoft Learn
AS Result ยท In this case, the SELECT statement will throw the following error: Msg 245, Level 16, State 1, Line 3 Conversion failed when converting the varchar value ' is not a string.' to data type int. In order to evaluate the expression @notastring + ' is not a string.', SQL Server needs to ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ sql โ€บ convert-int-to-varchar-sql
Convert INT to VARCHAR SQL - GeeksforGeeks
July 23, 2025 - The employee_id column is converted to VARCHAR using the CAST method. The AS VARCHAR(10) indicates that a maximum of 10 characters may be included in the resulting VARCHAR column. We then use the CONCAT function to concatenate the string 'Employee ID: ' with the converted employee_id column. An alias for the concatenated column in the query result is employee_id_as_varchar. employee_id column, originally of type INT, is now a VARCHAR ... In SQL Server...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ cast-a-function-in-sql-convert-char-to-int-sql-server-example
Cast a Function in SQL โ€“ Convert Char to Int SQL Server Example
February 8, 2022 - Remember, store_id is an INTEGER, whereas postal_code is VARCHAR. To convert postal_code into an integer, we can use CAST like this: -- convert char to int -- generate a new id by adding store id and postal code select store_id, postal_code, store_id + cast(postal_code AS INTEGER) AS [StoreID-Postalcode] from store_locations
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ sql โ€บ t-sql โ€บ data-types โ€บ data-type-conversion-database-engine
Data type conversion (Database Engine) - SQL Server | Microsoft Learn
SQL Server automatically converts the data from one data type to another. For example, when a smallint is compared to an int, the smallint is implicitly converted to int before the comparison proceeds. GETDATE() implicitly converts to date style 0. SYSDATETIME() implicitly converts to date style 21. Explicit conversions use the CAST or CONVERT functions.
๐ŸŒ
TechOnTheNet
techonthenet.com โ€บ sql_server โ€บ functions โ€บ cast.php
SQL Server: CAST Function
Let's look at some SQL Server CAST function examples and explore how to use the CAST function in SQL Server (Transact-SQL). ... SELECT CAST(14.85 AS int); Result: 14 (result is truncated) SELECT CAST(14.85 AS float); Result: 14.85 (result is not truncated) SELECT CAST(15.6 AS varchar); Result: '15.6' SELECT CAST(15.6 AS varchar(4)); Result: '15.6' SELECT CAST('15.6' AS float); Result: 15.6 SELECT CAST('2014-05-02' AS datetime); Result: '2014-05-02 00:00:00.000'