Hello,
How can I get real value by select statement to some simple function as
follows
select 3/8
The result is 0
Can I get the value (with 3 digits ) :
0.375
(or specific digits, i.e = 2 digits)
0.38 (rounded up)
Thanks :)Since you are dividing integers, the result is an integer. You'll need to
specify a decimal operand to get a result with decimals.
> Can I get the value (with 3 digits ) :
> 0.375
SELECT CAST(3 / 8.0 AS decimal(9,3))
> (or specific digits, i.e = 2 digits)
> 0.38 (rounded up)
SELECT CAST(3 / 8.0 AS decimal(9,2))
Hope this helps.
Dan Guzman
SQL Server MVP
"Eitan M" <no_spam_please@.nospam_please.com> wrote in message
news:%230QkxB$oFHA.3756@.TK2MSFTNGP09.phx.gbl...
> Hello,
> How can I get real value by select statement to some simple function as
> follows
> select 3/8
> The result is 0
> Can I get the value (with 3 digits ) :
> 0.375
> (or specific digits, i.e = 2 digits)
> 0.38 (rounded up)
> Thanks :)
>|||This is caused by "integer math" - if you divide two integers, SQL Server
will round down to the nearest integer. Try the following:
SELECT 3.0 / 8
SELECT CONVERT(DECIMAL(5,2), 3.0/8)
http://www.aspfaq.com/2483
"Eitan M" <no_spam_please@.nospam_please.com> wrote in message
news:%230QkxB$oFHA.3756@.TK2MSFTNGP09.phx.gbl...
> Hello,
> How can I get real value by select statement to some simple function as
> follows
> select 3/8
> The result is 0
> Can I get the value (with 3 digits ) :
> 0.375
> (or specific digits, i.e = 2 digits)
> 0.38 (rounded up)
> Thanks :)
>|||Eitan M wrote:
> Hello,
> How can I get real value by select statement to some simple function
> as follows
> select 3/8
> The result is 0
It would not be if you used
select 3./8
> Can I get the value (with 3 digits ) :
> 0.375
select cast(3./8 as decimal(8,3))
> (or specific digits, i.e = 2 digits)
> 0.38 (rounded up)
>
select cast(3./8 as decimal(8,2))
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
No comments:
Post a Comment