首页 > 解决方案 > 检索零和字符之间的数字

问题描述

我有一个需要清理的数字列表,而我使用的代码似乎不起作用。

数字预操作示例:0000000001C 数字应该如何:0.13 数字预操作示例:0000000173A 数字应该如何:17.31

我一直在处理的代码有点令人困惑:

select amount, 
       case when right(amount,1) = 'A' then concat(right(amount, charindex('0', reverse(amount))-1), replace(amount,'A','1'))
            when right(amount,1) = 'C' then concat(right(amount, charindex('3', reverse(amount))-1), replace(amount,'C','3')) end
from db

我能得到的数字是 1C00000000013, 173A00000001731

任何帮助表示赞赏!

标签: sqldata-manipulationdata-cleaning

解决方案


不完全确定您的数字结构是什么,但我假设您列表中只有最后一个字符不是数字,它只能是“A”或“C”

WITH amounts AS(
  SELECT '0000000001C' amount
   UNION
  SELECT '0000000173A' amount
)
SELECT 
  case right(amount,1)
    when 'A' then replace(right(amount, charindex('0', reverse(amount))-1), 'A', '1')
    when 'C' then replace(right(amount, charindex('0', reverse(amount))-1), 'C', '3')
  end AS amount
FROM amounts;

将为您提供“13”和“1731”,您可以根据小数位数将其转换为数字。

您自己与您的 SELECT 非常接近。


推荐阅读