简体   繁体   English

如何识别小数点后只有零的记录?

[英]How to identify records with only Zeros after decimal point?

Please refer to the screenshot: 请参考屏幕截图:

SS

How would you write a case statement to identify records with only zeros after the decimal point. 您将如何编写case语句来识别小数点后只有零的记录。

This is what the structure of my select case statement looks like: 这是我的select case语句的结构如下所示:

SELECT CASE WHEN (ONLY ZEROS AFTER 
DECIMAL POINT)
THEN CAST(VALUE AS int) FROM MyTable

I'm keeping is as varchar(25) only because there are two more fields that I need to combine with the above numbers. 我之所以保留varchar(25)只是因为还有两个字段需要与上述数字组合。

一种方法是:

(case when value = floor(value) then cast(value as int) end)

You could do something similar to Gordon's answer but this takes into account that you are storing the number as varchar(25) 您可以执行类似于Gordon答案的操作,但是要考虑到您将数字存储为varchar(25)

declare @mytable table (value varchar(25) not null)
insert into @mytable (value) values ('1.31'),('5.00'),('2.500'),('6.00'),('8.0000'),('1.0'),('3.0000000')

select coalesce(cast((case when DecimalValue = floor(DecimalValue) then cast(DecimalValue as int) end) as varchar(25)), VarcharValue)
from (select cast(value as decimal(18,9)) as DecimalValue, value as VarcharValue from @mytable) as tmp

http://rextester.com/QRRW4456 http://rextester.com/QRRW4456

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM