Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL round to decimal places

How do I round the result of matchpercent to two decimal places (%)? I'm using the following to return some results:

DECLARE @topRank int
set @topRank=(SELECT MAX(RANK) FROM
FREETEXTTABLE(titles, notes, 'recipe cuisine', 1))
SELECT 
    ftt.RANK, 
    (CAST(ftt.RANK as DECIMAL)/@topRank) as matchpercent, --Round this
    titles.title_id, 
    titles.title
FROM Titles
INNER JOIN 
FREETEXTTABLE(titles, notes, 'recipe cuisine') as ftt
ON
ftt.[KEY]=titles.title_id
ORDER BY ftt.RANK DESC
like image 432
madlan Avatar asked Jul 06 '10 22:07

madlan


1 Answers

CAST/CONVERT the result:

CAST((CAST(ftt.RANK as DECIMAL)/@topRank) AS DECIMAL(n,2)) as matchpercent,

...where n is a number large enough not to truncate left of the decimal point. That is to say, if you use "123.456", you need to use DECIMAL(7,2) because the total length is 7 digits.

like image 64
OMG Ponies Avatar answered Nov 03 '22 12:11

OMG Ponies