How to convert VARCHAR to Integer in SQL Server with CAST and CONVERT

If a table column is VARCHAR and has all the numeric values in it, it can be converted to Integer using CAST or CONVERT function.

SELECT CAST(VarcharColumnName AS INT) FROM My_Table
SELECT CONVERT(INT, VarcharColumnName ) FROM My_Table

CAST or CONVERT will throw an error when you try to convert column values from alpha-numeric characters to numeric, and it will not retrieve only numbers when column values are converted from alpha-numeric characters to numeric.

If the post helped you, please share it:
Pin It

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">