【问题标题】:Need regex for SQL to extract substring from a long stringSQL需要正则表达式才能从长字符串中提取子字符串
【发布时间】:2023-01-10 01:06:40
【问题描述】:

我的数据库中有以下字符串 (SnowFlake)

:display_redemption_mark: 0
:redeemable_properties: '1205,1206,1207,1208,1209,1210,1211,1212,1213,1214,1215,1216,1217,1218,1225,1227,1230,1231,1232,1239,1242,1244,1249,1250,1254,1281,1282,1283,1284,1285,1286,1287,1288,1289,1290,1291,1292,1293,1294,1296,1297,1240,1247,1255'
:redemption_meta_keys: ''
:max_redemptions_in_transaction: 0
:allow_multiple_redemption_on_item: false
:allow_qualifying_items_reused: false

我想提取 redeemable_properties 的值,在上面的例子中

1205,1206,1207,1208,1209,1210,1211,1212,1213,1214,1215,1216,1217,1218,1225,1227,1230,1231,1232,1239,1242,1244,1249,1250,1254,1281,1282,1283,1284,1285,1286,1287,1288,1289,1290,1291,1292,1293,1294,1296,1297,1240,1247,1255

我想要排除单引号和双引号因为整个值是一个字符串并存储在表的一列中。我正在创建一个包含提取值的新列。

以下是 redeemable_properties 可能具有的值

:redeemable_properties: 1,2,3,early_access,donatable

:redeemable_properties: 21+ Age

:redeemable_properties: 10c/gal Fuel Reward,Bag of Ice,Breakfast Taco,Cake Cone,RaceTrac
  Chips,RaceTrac Water,Regular Size Chocolate Candy Bar,Roller Grill,Fresh Pastry,Frito
  Lay Chips,Gatorade,Breakfast Sandwich,Soda,King Size Chocolate Candy Bar,Monster
  Energy Drink,Pizza Slice,Fresh Sandwich,Pouch Candy,Jumbo Sunflower Seeds,RaceTrac
  Gummies,Whole Pizza,3 Piece Chicken Tenders,Red Bull,Kind Bar,$4 off,$1 off,Loyalty
  Coupon,King Tier Coupon,PrinceTierCoupon,SultanTierCoupon,_Employee

:redeemable_properties: Online_Orders_Only,In-Store_Orders_Only,All_Orders

:redeemable_properties: 58011,58021,58031,58041,54610,Merchandize,53082,53086,55784,56101,51704,51703,51702,51602,53100,53700,53701,53702,53300,53703,53704,54310,54710,54711,
54712,54713,54311,54611,54100,54901,54312,54313,54101,54714,54900,54715,54716,54314,54717,14721,14722,14723,14724,14725,54612,54315,55701,55705,55704,56301,56302,56702,56200,56700,56701,56201,56600,56601,56300,51706,  51707,  51708,  51603,  51630,  51709,  51710,  51711,  51301,  51712,51300,52301,12820,12810,52700,12710,52701,12711,12720,12790,52702,51707,52201,52302,52303,52200,56703,
  53380, 53280, 53381, 53382, 53383,54200,54201,54701,55110, 55120, 55610

:redeemable_properties: Bag of Ice,
Breakfast Taco,Cake Cone,RaceTrac Chips,RaceTrac
  Water,Regular Size Chocolate Candy Bar,Roller Grill,Fresh Pastry,Frito Lay Chips,Gatorade,Breakfast
  Sandwich,Soda,King Size 

【问题讨论】:

    标签: sql snowflake-cloud-data-platform mysql-regexp


    【解决方案1】:

    你能试试这个吗?

    select trim( regexp_substr( your_column, '.*:redeemable_properties:([^:]*):',1,1,'e' ), '''" ' ) from your_table;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-25
      • 2010-10-14
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多