Solved

Convert from varchar to decimal

Posted on 2003-10-21
3
1,353 Views
Last Modified: 2012-05-04

hi all

i have a field which is stored as a varchar(30). I need it as varchar as the value can be characters or numbers etc.

in some case i  need to add two of these fields when the text value are deciamls e.g. '.75' to do this i need to change them to decimals first. i have tried Cast('0.75' as DECIMAL(28,9)) but it gives me 1.00000000. it seems to be rounding to the nearest whole number. can anyone help?
0
Comment
Question by:Cause
[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 4

Accepted Solution

by:
Greg Rowland earned 100 total points
ID: 9591768
declare @aNumber DECIMAL(28,9)

set @aNumber =  Cast('0.759' as DECIMAL(28,9))

SELECT @aNumber
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9591795
What SurferJoe posted should work.  You shouldn't issue Cast('0.75' as DECIMAL(28,9)) and get 1.000000 unless there is some other conversion going on.  Post your SELECT statement, and we can help determine that.

But from what you've mentioned, I think you need a more robust solution.  For example, if you have 2 fields in a table, and you want to return the sum of those fields if they are numeric, then try something like this:

SELECT Value = CASE
            WHEN ISNUMERIC(Field1) = 1 THEN CAST(Field1 AS DECIMAL(28,9))
            ELSE 0 END
            +
            CASE WHEN ISNUMERIC(Field2) = 1 THEN CAST(Field2 AS DECIMAL(28,9))
            ELSE 0 END
FROM Table

(Where Field1 and Field2 are your VARCHAR fields and Table is your table)
0
 

Author Comment

by:Cause
ID: 9591868

Thanks SurferJoe that worked!

Thanks as well  Dexstar! i had implemented the other answer before i saw urs :-( as you poined out another field in the statment had an incorrect conversion!

Thanks again
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

615 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