Convert a string to int using sql query

Viewed 990418

How to convert a string to integer using SQL query on SQL Server 2005?

4 Answers

You could use CAST or CONVERT:

SELECT CAST(MyVarcharCol AS INT) FROM Table

SELECT CONVERT(INT, MyVarcharCol) FROM Table

Starting with SQL Server 2012, you could use TRY_PARSE or TRY_CONVERT.

SELECT TRY_PARSE(MyVarcharCol as int)

SELECT TRY_CONVERT(int, MyVarcharCol)

Try this one, it worked for me in Athena:

cast(MyVarcharCol as integer)
Related