0

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?

1
1

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)
    ....
0

If you want to cast an existing column from varchar to int, you have to change the definition of this column, using alter table

For instance :

alter table my_table alter column my_column int null

If you don't want to change your table structure, and just want to cast in a select, cast and convert would do the job

For instance :

select cast(my_column as int)
from my_table
2
  • Hi @irimias i don't want to alter table, i want to change the data type in the Package view level in Sql server. Yes CONVERT function is what i want .Can you please tell me how to use this for my field? Thanks in advance !!! – Siddhartha Saha Feb 16 '17 at 8:49
  • @Siddhartha: if the answer is not relevant, can you please update your question and describe a bit more your issue and what you want to do exactly? ("Package view level"??) – irimias Feb 16 '17 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.