SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Different results with CAST ( )
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Rock_query
Yak Posting Veteran

52 Posts

Posted - 07/25/2013 :  20:51:37  Show Profile  Reply with Quote
When I test the CAST function on AdventureWorks2012, the CAST function will round down or up, i.e. 2024.994 to 2025 or 5.1865 to 5.

However, when I try the same the CAST function as:

SELECT CAST(2024.994 AS INT) the result I get is now 2024 (and not 2025).

Why is CAST rounding up when I use it on a table in the AdventureWorks2012 database, but when I use CAST on its own, it rounds down?

khtan
In (Som, Ni, Yak)

Singapore
17630 Posts

Posted - 07/25/2013 :  21:39:11  Show Profile  Reply with Quote
According to the BOL http://msdn.microsoft.com/en-us/library/ms187928.aspx
quote:

When you convert data types that differ in decimal places, sometimes the result value is truncated and at other times it is rounded. The following table shows the behavior.

From     To   Behavior
numeric  int  Truncate






KH
Time is always against us

Go to Top of Page

MuMu88
Aged Yak Warrior

547 Posts

Posted - 07/25/2013 :  21:51:22  Show Profile  Reply with Quote
What do you get when you run the following query:


USE AdventureWorks2012;
SELECT TOP 10 LineTotal, CAST(LineTotal AS INT) from Sales.SalesOrderDetail;


Refer to http://msdn.microsoft.com/en-us/library/ms187928.aspx for specifics.



Edited by - MuMu88 on 07/25/2013 21:52:09
Go to Top of Page

Rock_query
Yak Posting Veteran

52 Posts

Posted - 07/26/2013 :  19:22:47  Show Profile  Reply with Quote
quote:
Originally posted by MuMu88

What do you get when you run the following query:


USE AdventureWorks2012;
SELECT TOP 10 LineTotal, CAST(LineTotal AS INT) from Sales.SalesOrderDetail;


Refer to http://msdn.microsoft.com/en-us/library/ms187928.aspx for specifics.






Here is what I get:

2024.994000 is 2024
6074.982000 is 6074
2024.994000 is 2024
2039.994000 is 2039
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17630 Posts

Posted - 07/26/2013 :  19:46:28  Show Profile  Reply with Quote
as expected, it is truncating the value.

quote:
When I test the CAST function on AdventureWorks2012, the CAST function will round down or up, i.e. 2024.994 to 2025 or 5.1865 to 5.

can you post the query that you used ?


KH
Time is always against us

Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22755 Posts

Posted - 07/28/2013 :  10:53:00  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
That is the default behavior of CASTing to INT. When you CAST a value to INT, the decimal part is fully omitted.

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000