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') a开发者_JAVA百科s ftt
ON
ftt.[KEY]=titles.title_id
ORDER BY ftt.RANK DESC
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.
精彩评论