Sql-server – How to convert data type from varchar to number in SQL Server

sql server

I want to convert a field currently in varchar data type to number data type in the Views of SQL Server say (dbo.CUSTOMER.STORE_NBR) from Varchar to number.
How to do that ?
Will CONVERT function will do the necessity?
If yes, can you help us with syntax?

Best Answer

In Sql Server 2012 and up: each of these will return null when the conversion fails instead of an error.

So if you want to change the way a view is returning the value you would use something like:

alter view dbo.myView as 
  select
    ....
    , store_nbr = try_cast(dbo.customer.store_nbr as int)
    ....