I'm using SQL Server 2008, I'd like to split 50/100 for example and return me 0.05. But it returns me 0, it follows what I tried to do:
You must be aware of the data type of the denominator and divisor. When both are numerical expressions in the domain of integers, the whole division occurs, obtaining an integer quotient. When at least one of them is fractional, the desired split occurs.
-- divisão inteira
SELECT 50 / 100
-- divisão fracionária
PRINT 50.0 / 100
PRINT 50 / 100.0
PRINT 1.0 * 50 / 100
José's answer is correct, but complementing it can also be like this:
declare @numero decimal(10,3) = 50;
set @numero = @numero / 100;
print @numero
Already declaring the variable as decimal, it also works
EDIT:Thisalsoworks:
printconvert(decimal(10,2),50)/100
Firstyouareconvertingthenumber50intodecimal,andthendividingit
But,Iimagine,intermsofperformance,José'sanswerwillbebetter,becauseofthetwoformsI'veshown,onedeclaresavariableandsetsit,andotherwiseusesconvert
,andinresponseJose,thisisalreadykindof"automatic" .. so I guess his form is better at performance.