Solved

Convert from varchar to decimal

Posted on 2003-10-21
3
1,329 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
3 Comments
 
LVL 4

Accepted Solution

by:
SurferJoe 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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

832 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