【问题标题】:How to get the numeric value of the below column in SQL?如何在 SQL 中获取以下列的数值?
【发布时间】:2020-08-07 21:27:05
【问题描述】:

我正在尝试从下面的sdescription 列中获取值71,其值为:

'€22.95 FUP BB charge for Black Waste: Total Weight: 71Kg'

这是我使用的代码:

Select
  th.sDescription,
  right(sDescription,6) as [column1]
from TransactionHistory th
join #2050FUPBB t on t.cust = th.scustomerid
where dttimestamp between '31-July 2020 00:00' and '31-Jul-2020 23:29'
and sdescription like '%€22.95 FUP BB charge for Black Waste:%'

但我只得到结果:': 71Kg'

如果可能的话,请你帮我获取价值'71'

【问题讨论】:

  • 你得到了正确的 6 个字符,但你想要的字符串只有 2 个字符长。
  • 您使用的是哪个数据库?请将您使用的数据库作为标签添加到问题中。
  • left(right(sDescription,4),2)
  • 重量可以有多大?如果它是 12234 Kg - 那么 6 个字符是不够的。单位总是公斤吗?如果重量超过 999 个单位,该值是否会使用分组数字进行格式化,例如 12,345?请提供更多详细信息。
  • 简单的REPLACE(your_string, 'KG', '')会发生什么@???

标签: sql sql-server select


【解决方案1】:
Select
  th.sDescription,
  SUBSTRING(sDescription, CHARINDEX(':', sDescription)+1, 2)
from TransactionHistory th
join #2050FUPBB t on t.cust = th.scustomerid
where dttimestamp between '31-July 2020 00:00' and '31-Jul-2020 23:29'
and sdescription like '%€22.95 FUP BB charge for Black Waste:%'

【讨论】:

  • 结肠呢?
  • @Abion47 如果权重是更长的位数怎么办?
  • 是的,单位等..但回答了所提出的问题..
  • @Bohemian 那么这个解决方案通常需要大修,但至少需要编辑才能工作。
  • @randy 当我使用您的答案时,我收到以下错误“rtrim 函数需要 1 个参数”
【解决方案2】:

我想你想要

replace(right(txt,len(txt)-charindex('Weight:',txt)-7),'Kg','')

这应该可以处理任何长度的重量

DEMO

【讨论】:

    【解决方案3】:

    使用替换删除不需要的文本

    Select
      th.sDescription,
      Replace(Replace(right(sDescription,6),'kg',''),':','') as [column1]
    from TransactionHistory th
    join #2050FUPBB t on t.cust = th.scustomerid
    where dttimestamp between '31-July 2020 00:00' and '31-Jul-2020 23:29'
    and sdescription like '%€22.95 FUP BB charge for Black Waste:%'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-22
      • 2012-01-21
      相关资源
      最近更新 更多