Solved

Ms SQL server 2005 datacasting

Posted on 2011-09-28
2
200 Views
Last Modified: 2012-08-13
What's the difference between a float, decimal, int casting?

If I have a value at '-2.5'
And I pull in from a table via nvarchar, how can I recast to do mathematical calculations on it.
0
Comment
Question by:GlobaLevel
[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
2 Comments
 
LVL 28

Accepted Solution

by:
strickdd earned 500 total points
ID: 36717360
floating point arithmetic is most commonly used for accounting and financial data. Decimal is used when precision isn't quite as important. The integer type is for when you don't want to worry about decimal points at all.

In most cases, a decimal will suffice for "normal" calculations, so you can simply do:

DECLARE @myDecimal As Decimal(18,5)
SET @myDecimal = CAST(@myNVarchar, Decimal(18,5))
0
 
LVL 42

Expert Comment

by:dqmq
ID: 36717565
>floating point arithmetic is most commonly used for accounting and financial data. Decimal is used when precision isn't quite as important.

That's backwards, my friend.


Int's are exact, but do not carry decimal points.

Like int's, decimal's are also exact.  But, they can be more precise to the number of decimal points given in the declaration.  Decimals are excellent for accounting and financial data where decimals are needed and accuracy is critical.

Float's are approximations, meaning, not all numbers can be represented exactly.  They can have high precision (lots of decimal places) using less space than decimals--thus you trade accuracy for space.  Float's are useful for very large or small numbers where exactness is not critical.  
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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