Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

How sql server convert varchar to int?

Asked by Delbert Rauch Apr 24, 2021 17.0K views 3 answers
Share

About this question

I came across an issue today, and while I was able to fix it, I am unsure what caused the issue to happen. I have a column that is Varchar(20). It is used to store just year data (2010, 2011, etc.) it is being converted to an Int data type, but when the command below is ran, an error code happens.ALTER TABLE mytable ALTER COLUMN1 int NULL The error displayed is: Conversion failed when converting the varchar value '2010.' to data type int.There are 3 rows in the table with the value of 2010 in Column1, but NO rows with '2010.' (period and 10 spaces) as a value. I ran select len(Column1) from MyTable where Column1 like ' 10%' I got 2 rows with a length of 4, and 1 with a length of 5. After updating the row with 5 length to have 2010 as the value, the table could be altered. I am curious where the period and spaces came from here. Also curious how 1 character length increase results in a period and 10 spaces. I am thinking something along the lines of a carriage return or line feed, but I can't find any data to back that up, its just a hunch. How sql server convert varchar to int?



Your answer

3 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Feb 3, 2025

In SQL Server, you can convert a VARCHAR value to an INT using CAST(), CONVERT(), or TRY_CONVERT() functions. Here’s how:

1. Using CAST() (Standard SQL)

  SELECT CAST('123' AS INT) AS ConvertedValue;

  • Converts '123' to 123 as an integer.
  • If the value is non-numeric, it throws an error.

2. Using CONVERT() (SQL Server Specific)

  SELECT CONVERT(INT, '123') AS ConvertedValue;

  • Works similarly to CAST().
  • Allows specifying different data types, e.g., CONVERT(FLOAT, '123.45').

3. Using TRY_CONVERT() (Safe Conversion)

SELECT TRY_CONVERT(INT, '123') AS ConvertedValue;
SELECT TRY_CONVERT(INT, 'ABC') AS ConvertedValue; -- Returns NULL

  • Returns NULL if conversion fails, preventing errors.
  • Useful for handling invalid numeric values safely.

4. Handling Non-Numeric Values

If your column contains mixed data, filter numeric values before conversion:

SELECT CAST(Value AS INT) 
FROM TableName 
WHERE ISNUMERIC(Value) = 1;

OR

Using TRY_CAST() to avoid errors:

  SELECT TRY_CAST(Value AS INT) FROM TableName;

Best Practices

✔ Use TRY_CONVERT() or TRY_CAST() for safe conversions.

✔ Ensure data contains only numeric values before conversion.

✔ Handle NULL or invalid values properly to avoid runtime errors.

Would you like an example with real-world data?

Was this helpful?

Ranjana Admin JanBask Expert

Answered on Apr 25, 2024

In SQL Server, you can convert a varchar (or any other compatible data type) to an int using the CAST or CONVERT functions. Here's how you can do it:

  SELECT CAST('123' AS INT) AS ConvertedValue;

Using CONVERT:

SELECT CONVERT(INT, '123') AS ConvertedValue;

Both methods will convert the string '123' to an integer value of 123. Make sure that the varchar value you are converting can be safely cast to an int. If the varchar value contains non-numeric characters, or if it exceeds the range of valid integer values, the conversion will fail, and you may encounter errors.




Was this helpful?

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.