Solved

convert a varchar to a number in ms sql

Posted on 2007-04-03
3
2,806 Views
Last Modified: 2013-12-24
Hi, what is the sql statement to convert a varchar to a number.  I'm trying to run a query that grabs all prices that are either less than, more than, or equal to a value but need to convert those values first.  Currently, the prices have commas in them such as 100,000 and I need them to return like 100000.  My database is ms sql server 2000.
0
Comment
Question by:COwebmaster
3 Comments
 
LVL 2

Assisted Solution

by:kathik
kathik earned 150 total points
ID: 18847149
I would do this:    cast(replace([price],',''') as int)

Remove the commas with replace, then use cast to cast the value to an integer.
0
 
LVL 10

Assisted Solution

by:ksaul
ksaul earned 150 total points
ID: 18847177
If the the source column is a varchar with a comma in the text you have to remove the comma before converting it to an int datatype.  

SELECT CAST(REPLACE('100,000',',','') AS int)

You can convert it directly to money datatype:
SELECT CAST('100,000' AS money)

Or to money and then to int
SELECT CAST(CAST('100,000' AS money) AS int)
0
 
LVL 25

Accepted Solution

by:
dgrafx earned 200 total points
ID: 18847919
where column > convert(money,#b#)
in this example - #b# is your value such as 100,000
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

810 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