简体   繁体   English

使用 SQL 将列中的值从 PascalCase 更改为 snake_case

[英]Change the values in a column from PascalCase to snake_case using SQL

I have a table in snowflake that looks like this:我有一张雪花表,看起来像这样:

 --------------------
|      fieldname     |
 --------------------
|thisIsTestOne       |
|thisIsTestTwo       |
|this_test           |
 --------------------

I need to convert the PascalCase values in the column to snake_case.我需要将列中的 PascalCase 值转换为 snake_case。 Note: I only want to convert them to snake_case if they are PascalCase.注意:如果它们是 PascalCase,我只想将它们转换为 snake_case。 The output should look like this; output 应该是这样的;

 -------------------- ---------------------
|      fieldname     |    newfieldname     |
 -------------------- ---------------------
|thisIsTestOne       |this_is_test_one     |
|thisIsTestTwo       |this_is_test_two     |
|this_test           |this_test            |
 -------------------- ---------------------

You should be able to use REGEXP_REPLACE to insert an underscore between a lower-case character followed by an upper-case character, and then LOWER to convert to lower case ie您应该能够使用REGEXP_REPLACE在小写字符后跟大写字符之间插入下划线,然后将LOWER转换为小写,即

SELECT LOWER(REGEXP_REPLACE(fieldname, '([a-z])([A-Z])', '\\1_\\2'))
FROM yourtable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM