• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

convert string to integer

I have a column that is nvarchar(50) called Number1.  How do I convert the string to a number?

select * from table where Number1 > 0
0
al4629740
Asked:
al4629740
  • 2
2 Solutions
 
Aneesh RetnakaranDatabase AdministratorCommented:
Your query should work as 0 is an integer, the data from 'Number1' will be converted to integer

or else if you want explicit conversion

select * from table where cast(Number1 as bigint )  > 0
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
-- Here's how you select it as an integer..
select CAST(Number1 as int)  
from table

Depending on your situation you'll want to validate whether Number1 really is an integer, either because it has decimal values (12.3) or it's a string ('banana')

-- This will weed out all of the non-numeric values.
select CAST(Number1 as int)  
from table
where ISNUMERIC(Number1) = 1


-- Then you'll have to decide if you want to weed out the decimal values, or round them.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the split.  Good luck with your project.  -Jim
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now