使用sql将列中的值从pascalcase更改为case

h9a6wy2h  于 2021-08-01  发布在  Java
关注(0)|答案(1)|浏览(332)

我有一张雪花table,看起来像这样:

  1. --------------------
  2. | fieldname |
  3. --------------------
  4. |thisIsTestOne |
  5. |thisIsTestTwo |
  6. |this_test |
  7. --------------------

我需要将列中的pascalcase值转换为大小写。注意:如果它们是pascalcase,我只想把它们转换成snake\u case。输出应该是这样的;

  1. -------------------- ---------------------
  2. | fieldname | newfieldname |
  3. -------------------- ---------------------
  4. |thisIsTestOne |this_is_test_one |
  5. |thisIsTestTwo |this_is_test_two |
  6. |this_test |this_test |
  7. -------------------- ---------------------
brvekthn

brvekthn1#

你应该可以使用 REGEXP_REPLACE 在小写字符和大写字符之间插入下划线,然后 LOWER 转换成小写,即。

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

相关问题