Solved

# Convert from varchar to decimal

Posted on 2003-10-21
1,346 Views

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
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

LVL 4

Accepted Solution

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

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

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

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
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…
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
###### Suggested Courses
Course of the Month4 days, 18 hours left to enroll