HiveBrain v1.2.0
Get Started
← Back to all entries
gotchasqlModerate

Why does SELECT 1/2 return 0?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
returnselectwhydoes

Problem

As the title says. I even tried SELECT CONVERT(NUMERIC, 1/2) which also returned 0.

I am using SQL Server 2008.

Solution

First, you are executing the numbers as integers but second, you also have not defined the precision and scale of the numeric datatype.

Try this:

SELECT CONVERT(NUMERIC(5, 2), 1.0/2.0)


or even

SELECT CONVERT(NUMERIC(5, 2), 1/2.0)

Code Snippets

SELECT CONVERT(NUMERIC(5, 2), 1.0/2.0)
SELECT CONVERT(NUMERIC(5, 2), 1/2.0)

Context

StackExchange Database Administrators Q#21650, answer score: 14

Revisions (0)

No revisions yet.