79569411

Date: 2025-04-11 17:22:57
Score: 0.5
Natty:
Report link

You'd want to reverse the 'start of line' carrot and the comma though if using this for anything where the first field may be null, otherwise it will return what should be the second field instead of the null first field

select
',UK,244,,Mathews' AS string_key_NULL_1
  ,REGEXP_SUBSTR(string_key_NULL_1, '(,|^)\K([^,]*)(?=,|$)',1,1) --Result: 'UK'--Incorrect
  ,REGEXP_SUBSTR(string_key_NULL_1, '(^|,)\K([^,]*)(?=,|$)',1,1) --Result: Null--Correct Result
,'RES,UK,244,,Mathews' AS string_key_NON_NULL_1
  ,REGEXP_SUBSTR(string_key_NON_NULL_1, '(,|^)\K([^,]*)(?=,|$)',1,1) --Result: 'RES'--Correct Result (Because first field is not null)
  ,REGEXP_SUBSTR(string_key_NON_NULL_1, '(^|,)\K([^,]*)(?=,|$)',1,1) --Result: 'RES'--Correct Result
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Jobastion