![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hello, I have this: declare @myvar as decimal set @myvar=15.84 select cast(@myvar as int) Result: 16 I want to always round down. How can I get a result 15? Thanks for any help in advance! Andreas |
#3
| |||
| |||
|
|
Hello, I have this: declare @myvar as decimal set @myvar=15.84 select cast(@myvar as int) Result: 16 I want to always round down. How can I get a result 15? Thanks for any help in advance! Andreas |
#4
| |||
| |||
|
|
Hello, I have this: declare @myvar as decimal set @myvar=15.84 select cast(@myvar as int) Result: 16 I want to always round down. How can I get a result 15? Thanks for any help in advance! Andreas |
#5
| |||
| |||
|
|
It (CAST) does round down (truncates). Your problem was that you define the decimal as decimal, which is same as decimal(18,0) which means that you will get a round up, at the assignment stage, not the CAST. Anyhow, you can also use FLOOR. See below, should explain it all: declare @myvar1 as decimal, @myvar2 as decimal(5,2) set @myvar1=15.84 set @myvar2=15.84 SELECT @myvar1, @myvar2 select cast(@myvar1 as int), cast(@myvar2 as int) SELECT FLOOR(@myvar1), FLOOR(@myvar2) -- Tibor Karaszi, SQL Server MVP http://www.karaszi.com/sqlserver/default.asp http://sqlblog.com/blogs/tibor_karaszi "Andreas Klemt" <aklemt68 (AT) live (DOT) de> wrote in message news:evc7Xj0eJHA.4868 (AT) TK2MSFTNGP05 (DOT) phx.gbl... Hello, I have this: declare @myvar as decimal set @myvar=15.84 select cast(@myvar as int) Result: 16 I want to always round down. How can I get a result 15? Thanks for any help in advance! Andreas |
#6
| |||||
| |||||
|
|
On Tuesday, January 20, 2009 4:48 PM Andreas Klemt wrote: Hello, I have this: declare @myvar as decimal set @myvar=15.84 select cast(@myvar as int) Result: 16 I want to always round down. How can I get a result 15? Thanks for any help in advance! Andreas |
|
On Tuesday, January 20, 2009 4:57 PM Russell Fields wrote: SELECT FLOOR(@myvar) RLF |
|
On Tuesday, January 20, 2009 4:58 PM Tom Cooper wrote: Always give a precision and scale when declaring decimal datatypes. If you don't, it defaults to decimal(18,0) which can only hold integers. So when you put 15.84 into a decimal (18,0) it's immediately converted to 16. And, of course, whatever type of rounding you do to 16 will get you 16. But you want the Round() function. See BOL for a description. Something like declare @myvar as decimal (5,2) set @myvar=15.84 select Cast(Round(@myvar, 0, 1) As int) which returns 15 Tom "Andreas Klemt" <aklemt68 (AT) live (DOT) de> wrote in message news:evc7Xj0eJHA.4868 (AT) TK2MSFTNGP05 (DOT) phx.gbl... |
|
On Tuesday, January 20, 2009 5:05 PM Tibor Karaszi wrote: It (CAST) does round down (truncates). Your problem was that you define the decimal as decimal, which is same as decimal(18,0) which means that you will get a round up, at the assignment stage, not the CAST. Anyhow, you can also use FLOOR. See below, should explain it all: declare @myvar1 as decimal, @myvar2 as decimal(5,2) set @myvar1=15.84 set @myvar2=15.84 SELECT @myvar1, @myvar2 select cast(@myvar1 as int), cast(@myvar2 as int) SELECT FLOOR(@myvar1), FLOOR(@myvar2) -- Tibor Karaszi, SQL Server MVP http://www.karaszi.com/sqlserver/default.asp http://sqlblog.com/blogs/tibor_karaszi "Andreas Klemt" <aklemt68 (AT) live (DOT) de> wrote in message news:evc7Xj0eJHA.4868 (AT) TK2MSFTNGP05 (DOT) phx.gbl... |
|
On Tuesday, January 20, 2009 6:43 PM Andreas Klemt wrote: Thanks a lot! That helped me. Andreas |
![]() |
| Thread Tools | |
| Display Modes | |
| |