Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

convert a varchar to a number in ms sql

Posted on 2007-04-03
3
Medium Priority
?
2,816 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
[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
3 Comments
 
LVL 2

Assisted Solution

by:kathik
kathik earned 600 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 600 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 800 total points
ID: 18847919
where column > convert(money,#b#)
in this example - #b# is your value such as 100,000
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

Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

670 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