提取 REGEXP 子字符串 SQL Server [英] Extract REGEXP Substring SQL Server

查看:37
本文介绍了提取 REGEXP 子字符串 SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从这个查询中提取一个特定的数值...

I want to extract a specific number value from this query...

列:名称此示例的四行:

Column: name Four rows for this example:

  • 1/2 Product1 some_description 1200 GR 更多数据...(UND)
  • 1/4 Product2 some_description 2400G 更多数据(KG 更多数据)
  • Product3 some_description 2200 GRS 更多数据...
  • 1/4 Product4 some_description 1800GR 更多数据 UND...

我只想要整数值.我想查询:

I want the integer value only. I want with the query:

  • 1200
  • 2400
  • 2200
  • 1800

模式是:

  • [0-9]{4} G
  • [0-9]{4}GR
  • [0-9]{4} GRS

如何在 SQL 查询中使用此正则表达式来解析属性值?

How can i use this regexp on a SQL Query to parse an attribute value?

<代码>从 myTable 中选择SUBSTRING(name, (PATINDEX('%[0-9]%', [name])),4) 作为比索

这提取了一些值,但顺序不正确...我认为我可以将长度应用于 LEFT 直到整数值,但我不知道如何解决它.

This extract some values, but not in correct order... I think that i can apply LEFT with length until integer value, but i don't know how resolve it.

推荐答案

CONVERT(
INT, 
(REPLACE(
    SUBSTRING(
        nome,
        (
            PATINDEX(
                '%[0-9]%',
                REPLACE(
                    REPLACE(
                        nome,
                        '1/2',
                        ''
                    ),
                    '1/4',
                    ''
                )
            )
        ),
        4
    ),
    'G',
    ''
))) as pesoTotal

这解决了问题,谢谢.

这篇关于提取 REGEXP 子字符串 SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆