admin管理员组文章数量:1384613
I'm working on MariaDB 11.7.2. I have a list of columns as source_columns and a target column as target_column, dynamically given. I want to get the first non-NULL and non-empty value from the source columns and update the target column with that value.
Sample data:
source_columns = col1, col2, col3
col1, col2, col3
NULL, 12345, NULL
223, NULL, ""
"", 556, 2234
In the above example, some entries (noted with double quotes) are empty. as in col = "".
The expected output is:
12345
223
556
I have the following query so far:
SET @sql = CONCAT(
'UPDATE `', table_name, '` SET `', target_column, '` = COALESCE(',
REPLACE(source_columns, ',', ', '),
')'
);
This one skips the NULL values but does not skip the empty values in the source columns. I am trying the following but could not make it work.
SET @sql = CONCAT(
'UPDATE `', table_name, '` SET `', target_column, '` = COALESCE(NULLIF(`', REPLACE(source_columns, ',', '`, ""), NULLIF(`'), '`, ""))'
);
Any ideas how to fix it?
Thanks!
I'm working on MariaDB 11.7.2. I have a list of columns as source_columns and a target column as target_column, dynamically given. I want to get the first non-NULL and non-empty value from the source columns and update the target column with that value.
Sample data:
source_columns = col1, col2, col3
col1, col2, col3
NULL, 12345, NULL
223, NULL, ""
"", 556, 2234
In the above example, some entries (noted with double quotes) are empty. as in col = "".
The expected output is:
12345
223
556
I have the following query so far:
SET @sql = CONCAT(
'UPDATE `', table_name, '` SET `', target_column, '` = COALESCE(',
REPLACE(source_columns, ',', ', '),
')'
);
This one skips the NULL values but does not skip the empty values in the source columns. I am trying the following but could not make it work.
SET @sql = CONCAT(
'UPDATE `', table_name, '` SET `', target_column, '` = COALESCE(NULLIF(`', REPLACE(source_columns, ',', '`, ""), NULLIF(`'), '`, ""))'
);
Any ideas how to fix it?
Thanks!
Share Improve this question edited Mar 17 at 20:24 yenren asked Mar 17 at 19:51 yenrenyenren 52210 silver badges22 bronze badges 10 | Show 5 more comments1 Answer
Reset to default 0The whitespace problem
As discussed in the comments notably by @ysth,
your 2nd query is right… except for the sanitizing.
When input with a source_columns
of col1, col2, col3
,
the spaces get included in the resulting query:
UPDATE `t` SET `dest` = COALESCE(NULLIF(`col1`, ""), NULLIF(` col2`, ""), NULLIF(` col3`, ""));
which looks for table named col2
and col3
with an (easy to miss) space in their name.
The solution
Simply replace your raw source_columns
by REPLACE(source_columns, ' ', '')
to purge those dangerous spaces:
SET @sql = CONCAT(
'UPDATE `', table_name, '` SET `', target_column, '` = COALESCE(NULLIF(`', REPLACE(REPLACE(source_columns, ' ', ''), ',', '`, ""), NULLIF(`'), '`, ""))'
);
And it runs marvelously.
本文标签: sqlGetting first nonNULL and nonEMPTY value with COALESCE()Stack Overflow
版权声明:本文标题:sql - Getting first non-NULL and non-EMPTY value with COALESCE() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744537736a2611417.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
COALESCE(NULLIF(col1,''), NULLIF(col2, '') ... )
Assumes you have string (varchar) columns. – Joel Coehoorn Commented Mar 17 at 20:13SET
runs perfectly fine. Could you provide a complete example where it does not work? – Guillaume Outters Commented Mar 17 at 21:02replace(replace(source_columns,' ',''), ',', ...
– ysth Commented Mar 18 at 13:52