sql - 检索零和字符之间的数字
问题描述
我有一个需要清理的数字列表,而我使用的代码似乎不起作用。
数字预操作示例: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
任何帮助表示赞赏!
解决方案
不完全确定您的数字结构是什么,但我假设您列表中只有最后一个字符不是数字,它只能是“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 非常接近。
推荐阅读
- angular5 - angular5中带有文本框的下拉菜单
- php - 在图像标签正则表达式中替换 .PNG 扩展名后的数据
- routing - 如何在 Jsprit 中使用成本矩阵解决提货和送货问题
- tensorflow - 如何使用文件夹中已经存在的 eventFiles 在 tensorboard 上查看图表?
- javascript - return 语句不适用于 setTimeout()
- javascript - 在Javascript中调用自定义库函数中的函数
- apache-kafka - 在运行时在 apache kafka flink 中添加新规则
- sql - 查询以查找 SQL 查询中上一行的更改
- azure - 我们可以在生成 OAuth 令牌时使用范围参数来限制 Azure API 管理中操作级别的访问吗
- php - 带有 MySQL 的高级网店过滤