Wednesday, March 21, 2012

SQL SERVER – Convert Text to Numbers (Integer) – CAST and CONVERT

Using  CAST
SELECT CAST(YourVarcharColumn AS INT) FROM YourTable

Using  CONVERT
SELECT CONVERT(INT, YourVarcharColumn) FROM YourTable

Here  YourVarcharColumn is of SQL type Varchar in table YourTable.

Exceptions:
Exception happens when using  CAST or CONVERT is applied on alpha-numeric and casted/converted to numeric.




No comments:

Post a Comment