我正在尝试过滤掉一些查询结果,使其仅显示小数点后6位的项目。我不需要将其舍入或在答案中添加0,只需过滤掉5个小数位或以下的任何内容。我当前的查询看起来像这样:(例如,如果项目是199.54215,我不想看到它,但是如果它是145.253146,我需要返回它)
select
TRA_CODPLANTA,
TRA_WO,
TRA_IMASTER,
tra_codtipotransaccion,
tra_Correlativo,
TRA_INGRESOFECHA,
abs(tra_cantidadparcial) as QTY
from mw_tra_transaccion
where FLOOR (Tra_cantidadparcial*100000) !=tra_cantidadparcial*100000
and substring(tra_imaster,1,2) not in ('CP','SG','PI','MR')
and TRA_CODPLANTA not in ('4Q' , '5C' , '5V' , '8H' , '7W' , 'BD', 'DP')
AND tra_INGRESOFECHA > @from_date
and abs(tra_cantidadparcial) > 0.00000
任何帮助将不胜感激!
答案 0 :(得分:2)
这里是ROUND的一个示例,由于它仍然存在于数字领域,因此它似乎是理想的函数。如果您最多保留5个小数位,那么四舍五入到5个小数位将使值保持不变。
create table #test (Tra_cantidadparcial decimal(20,10));
INSERT #test (Tra_cantidadparcial) VALUES (1),(99999.999999), (1.000001), (45.000001), (45.00001);
SELECT * FROM #test WHERE ROUND(Tra_cantidadparcial,5) != Tra_cantidadparcial;
drop table #test
答案 1 :(得分:0)
以下是使用转换为varchar
并使用LEN
-小数点CHARINDEX
的示例,我并不是说这是最好的方法,但是您做了要求提供语法示例,因此您可以:
--Temp Decimal value holding up to 10 decimal places and 10 whole number places
DECLARE @temp DECIMAL(20, 10) = 123.4565432135
--LEN returns an integer number of characters in the converted varchar
--CHARINDEX returns the integer location of the decimal where it is found in the varchar
--IF the number of characters left after subtracting the index of the decimal from the length of the varchar is greater than 5,
--you have more than 6 decimal places
IF LEN(CAST(@temp AS varchar(20))) - CHARINDEX('.', CAST(@temp AS varchar(20)), 0) > 5
SELECT 1
ELSE
SELECT 0
答案 2 :(得分:0)
如果您的数据库值是VARCHAR
并以如下方式存在于数据库中:
100.123456
100.1
100.100
您可以使用通配符LIKE
语句示例来实现此目的
WHERE YOUR_COLUMN_NAME LIKE '%.[0-9][0-9][0-9][0-9][0-9][0-9]%'
这将是包含小数点后跟至少6个数字值的任何内容
答案 3 :(得分:0)
这是一种简写方式。
WHERE (LEN(CONVERT(DOUBLE PRECISION, FieldName % 1)) - 2) >=5
答案 4 :(得分:0)
一种方法是将convert / cast
的列精度降低。这样做会导致自动舍入,但是会显示出它是6位小数还是不是基于最后一位数字。如果转换后的值的最后一位为0,则为false,否则为true。
declare @table table (v decimal(11,10))
insert into @table
values
(1.123456789),
(1.123456),
(1.123),
(1.123405678)
select
v
,cast(v as decimal(11,5)) --here, we are changing the value to have a precision of 5. Notice the rounding.
,right(cast(v as decimal(11,5)),1) --this is taking the last digit. If it's 0, we don't want it
from @table
因此,您的where
子句就是这样。
where right(cast(tra_cantidadparcial as decimal(11,5)),1) > 0