Solved

convert a varchar to a number in ms sql

Posted on 2007-04-03
3
2,809 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connect to an SQL server ActiveX 10 29
SQL Server 2008 R2, need a pivot/cross tab query... 4 29
Database Integrity 1 50
MS SQL Server select from Sub Table 14 26
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

828 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