Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 CAST and its usage

Author  Topic 

msvdm
Starting Member

17 Posts

Posted - 2011-07-28 : 14:15:56
Thanks in advance

I came across AND P.Price=CAST(ROUND(A.pl_price,4) AS DECIMAL(8,4)).

Can you please explain me the meaning of this query and what it results in?. I like to know about ROUND. I know that it rounds of a value...but I am quite confused.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-07-28 : 17:34:01
As the name suggests, it rounds a number to specified number of decimal digits. This page has a pretty good explanation and some examples: http://msdn.microsoft.com/en-us/library/ms175003.aspx

In this case, I don't know if you need to have the round function at all, because if I remember right, casting to decimal would automatically round it.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-07-28 : 17:47:31
CASTing would change the type, precision and/or scale of a value, while ROUND() will preserve them:
;WITH A(a) AS (SELECT 2.123456)
--;WITH A(A) AS (SELECT 1) -- uncomment these lines to try different data types
--;WITH A(A) AS (SELECT PI()) -- uncomment these lines to try different data types

SELECT a, SQL_VARIANT_PROPERTY(a,'BaseType') Type, SQL_VARIANT_PROPERTY(ROUND(a,4),'BaseType') RoundType,
SQL_VARIANT_PROPERTY(a,'Precision') Prec, SQL_VARIANT_PROPERTY(ROUND(a,4),'Precision') RoundPrec,
SQL_VARIANT_PROPERTY(a,'Scale') Scale, SQL_VARIANT_PROPERTY(ROUND(a,4),'Scale') RoundScale
FROM A
This might not matter for just a single value, but if you do math on it later it should be kept in mind.
Go to Top of Page
   

- Advertisement -