Marshal
Marshal

Reputation: 6671

SQL Gives Erroreneous Result

I was just going through a question

So I tried to calculate it myself and check the result.. But the result always I get is a rounded value. Why ??

Actually I should get 0.1428......

enter image description here

@Everyone who answered: Thank you everyone, as per SO rules I can accept only one answer, so accepting the one who was earliest.

Upvotes: 2

Views: 94

Answers (5)

Morphed
Morphed

Reputation: 3619

SELECT 2.0/14

Need to decimal place to "trigger" the floating point

Upvotes: 2

dcp
dcp

Reputation: 55467

Try this:

declare @a float
set @a = 2.0/14.0
select a=@a

Upvotes: 2

Oded
Oded

Reputation: 499382

You are doing integer division.

At least one of the operands needs to be a float/decimal in order for the whole expression to evaluate to one of those types.

Upvotes: 2

Mark Byers
Mark Byers

Reputation: 839234

The result of the integer division 2/14 is always 0.

Try 2.0/14.0 instead.

Upvotes: 2

juergen d
juergen d

Reputation: 204924

try that

select 2.0 / 14

If you use integers the result will be integer too. integers are not floating point numbers

Upvotes: 3

Related Questions