简体   繁体   English

从存储为字符串的数字中删除所有尾随小数点

[英]Remove all trailing decimal points from a number stored as a string

I have a couple of strings ( nvarchar data type), one is a whole number and one has decimal points trailing.我有几个字符串( nvarchar数据类型),一个是整数,一个是小数点尾随。 My goal is to remove decimals and have all values as a whole number.我的目标是删除小数并将所有值作为一个整数。

I tried the code below but it gives me an error for the value with no decimals.我尝试了下面的代码,但它给了我一个没有小数的值的错误。 Is there a way to accomplish this without a case expression.有没有办法在没有案例表达式的情况下完成此操作。 I'll be using this new column in a join.我将在连接中使用这个新列。

SELECT [SOW]
  --,LEFT([SOW], CHARINDEX('.', [SOW])-1) as 'TestColumn'
FROM [dbo].[t_Schedule_kdm]
WHERE sow  in ('15229.11','11092')

Output:
11092
15229.11

My desired Output:
11092
15229

Just append a dot character so that you'll always find an index:只需附加一个点字符,以便您总能找到索引:

LEFT(SOW, CHARINDEX('.', SOW + '.') - 1)

It's not clear whether you need to cast the result of that expression to an integer value.目前尚不清楚您是否需要将该表达式的结果转换为整数值。

Convert first to the most precision number you could ever have eg decimal(9,2) then convert to an int .首先转换为您可能拥有的最精确的数字,例如decimal(9,2)然后转换为int You can't convert directly from a decimal string to an int.您不能直接从十进制字符串转换为 int。

SELECT [Value]
  , CONVERT(int,CONVERT(decimal(9,2),[Value]))
FROM (
    VALUES ('15229.11'),('11092')
) x ([Value]);

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

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