获取仅包含sql查询数据的指定小数部分

时间:2015-09-01 14:56:50

标签: sql sql-server

select * from payments where amount = 0 order by id desc

我需要获得它只有0.5部分的值。你能告诉我怎么做吗?

e.g。 12,12.5,12.6,14.5

结果数据集可以具有:12.5,14.5仅限值数据

3 个答案:

答案 0 :(得分:2)

将数量截断为整数,从原始值减去并与0.5

进行比较
SELECT *
FROM payments
WHERE amount - CAST(amount AS INT) = 0.5
ORDER BY id DESC

OR:

 SELECT *
 FROM payments
 WHERE amount - FLOOR(amount) = 0.5
 ORDER BY id DESC

黑客方式:

 SELECT *
 FROM payments
 WHERE PARSENAME(amount, 1)= 5
 ORDER BY id DESC

再使用modulo:

SELECT *
FROM payments
WHERE amount % 1 = 0.5
ORDER BY id DESC

答案 1 :(得分:2)

你也可以找到这种方式

declare @temp table (val decimal(18,3))
insert into @temp values (0.5),(10.5),(20.8)

select * from @temp
where (val % 1) = 0.5

答案 2 :(得分:0)

与接受的答案相同的想法,只是另一个想法:

SELECT *
FROM payments
WHERE FLOOR(amount) < CEILING(amount) -- or <>
ORDER BY id DESC

WHERE FLOOR(amount) + 1 = CEILING(amount)