Solved

convert string to integer

Posted on 2013-05-23
3
318 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
  • 2
3 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 150 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 65

Accepted Solution

by:
Jim Horn earned 150 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 65

Expert Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now