Solved

Convert from varchar to decimal

Posted on 2003-10-21
3
1,317 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now