替换柱雪花(SQL)中的值

发布于 2025-02-09 22:49:54 字数 160 浏览 0 评论 0原文

我使用以下代码从字符串中提取数字:

regexp_substr(data, '\\d+\.\\d+') AS Age

其中值为0(在字符串中),我得到了一个空值。是否有任何方法可以在更广泛的查询中纠正此问题,因此所有空的null被0替换为0?

I'm extracting a number from a string using the following code:

regexp_substr(data, '\\d+\.\\d+') AS Age

Where the value is 0 (within the string), I'm getting a null value. Is there any way to correct this within the wider query, so all the nulls are replaced with 0s?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜司空 2025-02-16 22:49:54

如果您共享了示例数据,我可能会有所帮助,但不确定下面是否有帮助:

select 
    regexp_substr(column1, '\\d+(\.?\\d+)?')
from values 
    ('test0me'),
    ('234.234'),
    ('test my age 25.')
;

+------------------------------------------+
| REGEXP_SUBSTR(COLUMN1, '\\D+(\.?\\D+)?') |
|------------------------------------------|
| 0                                        |
| 234.234                                  |
| 25                                       |
+------------------------------------------+

与@Kurt建议的内容非常相似,但没有领先的“?:”。

I could be helpful if you have shared your sample data, but not sure if below helps:

select 
    regexp_substr(column1, '\\d+(\.?\\d+)?')
from values 
    ('test0me'),
    ('234.234'),
    ('test my age 25.')
;

+------------------------------------------+
| REGEXP_SUBSTR(COLUMN1, '\\D+(\.?\\D+)?') |
|------------------------------------------|
| 0                                        |
| 234.234                                  |
| 25                                       |
+------------------------------------------+

Pretty similar to what @kurt suggested, but without the leading "?:".

堇年纸鸢 2025-02-16 22:49:54

注意\ d+。\ d+表示“一个矿石更多数字,后跟任何字符,然后是一个或多个数字。此模式至少需要3个字符才能匹配,中间甚至不必是

数字 但是,如果您还需要捕获十进制值,则不匹配此模式,您将需要更复杂的东西来处理数字,然后是可选的小数点和更多数字:

\ d+(?:\。\。\。\ d+) ?

此模式将匹配0、0.0、42、98.6,等等。

Note \d+.\d+ means "one ore more digits followed by any character followed by one or more digits. This pattern requires at least 3 characters to match, and the middle one doesn't even have to be a digit.

0 does not match that pattern. To match 0 or 42 or 1000000, i.e. any integer that is merely a string of digits, you only need \d+

10.5 does not match this pattern, however, so if you need to also capture decimal values, you will need something more complex that handles digits followed by an optional decimal point and more digits:

\d+(?:\.\d+)?

This pattern will match 0, 0.0, 42, 98.6, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文