?
Solved

convert string to integer

Posted on 2013-05-23
3
Medium Priority
?
339 Views
Last Modified: 2013-05-23
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
Comment
Question by:al4629740
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 600 total points
ID: 39191479
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
 
LVL 66

Accepted Solution

by:
Jim Horn earned 600 total points
ID: 39191492
-- 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
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39191542
Thanks for the split.  Good luck with your project.  -Jim
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question