使用`split_to_multimap`进行解析,而不抛出异常。

huangapple go评论51阅读模式
英文:

parsing using split_to_multimap without throwing an exception

问题

我想解析分配给变量的值,而不会引发异常:

例如,我想要这个返回3(它的确返回了)

select try_cast(array_join(element_at(split_to_multimap('a=3,b=5', ',', '='), 'a'), '') as int) as a

我希望这个返回NULL(不幸的是它引发了异常)

select try_cast(array_join(element_at(split_to_multimap('a=3Xb=5', ',', '='), 'a'), '') as int) as a
英文:

I want to parse out the values which are assigned to the variables without throwing exception:

e.g I want this to return 3 (which it does)

select try_cast(array_join(element_at(split_to_multimap('a=3,b=5', ',', '='), 'a'), '') as int)  as a

I want this to return NULL (unfortunately it throws an exception)

select try_cast(array_join(element_at(split_to_multimap('a=3Xb=5', ',', '='), 'a'), '') as int)  as a

答案1

得分: 1

你可以使用 try

通过返回 NULL 来评估表达式并处理某些类型的错误。

select try(
    cast(
        array_join(
            element_at(
                split_to_multimap('a=3Xb=5', ',', '='), 'a'), '') as int)) as a
英文:

You can use try:

> Evaluate an expression and handle certain types of errors by returning NULL.

select try(
    cast(
        array_join(
            element_at(
                split_to_multimap('a=3Xb=5', ',', '='), 'a'), '') as int)) as a

huangapple
  • 本文由 发表于 2023年2月10日 10:47:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定