开发者

T-SQL round to decimal places

开发者 https://www.devze.com 2023-01-06 02:58 出处:网络
How do I round the result of matchpercent to two decimal places (%)? I\'m using the following to return some results:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消