Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将CamelCase转换为snake_case_Sql_Postgresql - Fatal编程技术网

Sql 将CamelCase转换为snake_case

Sql 将CamelCase转换为snake_case,sql,postgresql,Sql,Postgresql,需要以下查询的结果 select regexp_replace('StackOverflow', 'something', 'something') 作为 以下正则表达式在每个大写字母前添加下划线: regexp_replace(name, '([A-Z])','_\1', 'g')) 由于这会导致在开头出现下划线,因此需要使用trim() 以下查询: with names (name) as ( values ('StackOverflow'), ('Foo'),

需要以下查询的结果

select regexp_replace('StackOverflow', 'something', 'something')
作为


以下正则表达式在每个大写字母前添加下划线:

regexp_replace(name, '([A-Z])','_\1', 'g'))
由于这会导致在开头出现下划线,因此需要使用
trim()

以下查询:

with names (name) as (
  values ('StackOverflow'), 
         ('Foo'), 
         ('FooBar'), 
         ('foobar'), 
         ('StackOverflowCom')
)
select name, trim(both '_' from lower(regexp_replace(name, '([A-Z])','_\1', 'g'))) as new_name
from names;
返回:

name |新名称
-----------------+-------------------
堆栈溢出|堆栈溢出
福|福
福巴|福巴
foobar | foobar
堆栈溢出com |堆栈溢出com

我想你想要什么

lower(
  regexp_replace(
    replace(column_name, ' ', '_'),
    '([[:lower:]])([[:upper:]])',
    '\1_\2',
    'g'
  )
)
我们在这里测试它

WITH t (name) as (
  VALUES ('StackOverflow'), 
    ('Foo'), 
    ('FooBar'), 
    ('foobar'), 
    ('StackOverflowCom'),
    ('BLEHHHHokBaz')  -- doesn't go funky
)
SELECT name, lower(
  regexp_replace(
    replace(name, ' ', '_'),
    '([[:lower:]])([[:upper:]])',
    '\1_\2',
    'g'
  )
) AS new_name
FROM t;
这比使用“无”名称的@a_horse_有一个优势,即它可以处理连续的大写字母,并且可以解决空格问题

也看,


抱歉,champ,但由于以下字符串StackOverflowCom出现stack\u overflowcom预期的stack\u overflow\u Com很抱歉提及,但对于出现的每个大写字符,都应将其替换为尾随小写的“\u”,因此您所需的输出应为:\u stack\u overflow是否正确?您尝试了什么?你犯了哪些错误。请投入更多的时间来寻找答案,我们不是来做你的工作,而是来帮助你,如果你陷入困境
lower(
  regexp_replace(
    replace(column_name, ' ', '_'),
    '([[:lower:]])([[:upper:]])',
    '\1_\2',
    'g'
  )
)
WITH t (name) as (
  VALUES ('StackOverflow'), 
    ('Foo'), 
    ('FooBar'), 
    ('foobar'), 
    ('StackOverflowCom'),
    ('BLEHHHHokBaz')  -- doesn't go funky
)
SELECT name, lower(
  regexp_replace(
    replace(name, ' ', '_'),
    '([[:lower:]])([[:upper:]])',
    '\1_\2',
    'g'
  )
) AS new_name
FROM t;