Sql regex_substr提取字符串中的度量单位

Sql regex_substr提取字符串中的度量单位,sql,regex,amazon-redshift,Sql,Regex,Amazon Redshift,我不熟悉regexp,试图在一个字符串块中提取度量单位。字符串的示例包括: 产品名称3.5G 3.5g产品名称 产品名称3.5克 产品名称3500MG 我怎样才能使用redshift中的regex_substr函数从上述字符串中提取3.5G。当前正在使用case样式 when regexp_substr(trim(upper(productname)), '3.5G') = '3.5G' then '3.5G' when regexp_substr(trim(upper(productna

我不熟悉regexp,试图在一个字符串块中提取度量单位。字符串的示例包括:

  • 产品名称3.5G
  • 3.5g产品名称
  • 产品名称3.5克
  • 产品名称3500MG
我怎样才能使用redshift中的regex_substr函数从上述字符串中提取3.5G。当前正在使用case样式

when regexp_substr(trim(upper(productname)), '3.5G') = '3.5G' then '3.5G'

when regexp_substr(trim(upper(productname)), ' .5G') = ' .5G' then '.5G'

when regexp_substr(trim(upper(productname)), ' 1/8TH') = ' 1/8TH' then '3.5G'
when regexp_substr(trim(upper(productname)), ' 1/4') = ' 1/4' then '7G'
when regexp_substr(trim(upper(productname)), ' 1G') = ' 1G' then '1G'
when regexp_substr(trim(upper(productname)), ' 2G') = ' 2G' then '1G'
when regexp_substr(trim(upper(productname)), ' 1.75G') = ' 1.75G' then '1.75G'
when regexp_substr(trim(upper(productname)), ' 7G') = ' 7G' then '7G'

when regexp_substr(trim(upper(productname)), ' 1/2 ') = ' 1/2 ' and producttype = 'FLOWER' then '14G'
when regexp_substr(trim(upper(productname)), ' 14G') = ' 14G' then '14G'

when regexp_substr(trim(upper(productname)), ' 3.5 GRAM') = ' 3.5 GRAM' then '3.5G'
when regexp_substr(trim(upper(productname)), ' EIGHTH') = ' EIGHTH' then '3.5G'
when regexp_substr(trim(upper(productname)), ' 1 GRAM') = ' 1 GRAM' then '1G'
when regexp_substr(trim(upper(productname)), ' 1.75 GRAM') = ' 1.75 GRAM' then '1.75G'
when regexp_substr(trim(upper(productname)), ' 7 GRAM') = ' 7 GRAM' then '7G'
when regexp_substr(trim(upper(productname)), '14 GRAM') = '14 GRAM' then '14G'

when regexp_substr(trim(upper(productname)), ' 5 MILLIGRAM') = ' 5 MILLIGRAM' then '5MG'
when regexp_substr(trim(upper(productname)), ' 5MG') = ' 5MG' then '5MG'
when regexp_substr(trim(upper(productname)), ' 10MG') = ' 10MG' then '10MG'
when regexp_substr(trim(upper(productname)), ' 25MG') = ' 25MG' then '25MG'

一种方法是
regexp\u replace()

您还可以使用:

regexp_replace(str, '(^.*[^.0-9]|^)([\.0-9]+) ?[gG].*$', '\2')

您从
'PRODUCT NAME 14 GRAM'
中提取了什么?红移还是Postgres?虽然它们有一些古老的根源,但它们现在使用红移是非常不同的数据库产品。14GRAM是一个不好的例子,但还有其他计量单位类型,如ML,mg感谢您的快速回答,使用rexexp_替换上述结果:另外,如果有其他计量单位,如(ML,mg等),我如何向regex模式添加更多变化?@user1615573。这就回答了你的问题。如果你有一个新问题,你应该以新问题的形式提出,并附上适当的样本数据、期望的结果和解释。
regexp_replace(str, '(^.*[^.0-9]|^)([\.0-9]+) ?[gG].*$', '\2')