Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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
如何使用postgresql函数查找文本中的第一个大写字符_Sql_Postgresql - Fatal编程技术网

如何使用postgresql函数查找文本中的第一个大写字符

如何使用postgresql函数查找文本中的第一个大写字符,sql,postgresql,Sql,Postgresql,我有一个Postgres sql,它有两个列,两个列都包含相同的名称数据,其中的部分由字母大小写区分。例如: col1 col2 johnSmith johnSmith 我希望col1只是“john”,col2只是“Smith” 名称部分之间的分隔在第一个大写字母处。尝试以下操作: update mytable set col1 = regexp_replace(col1, '[A-Z].*', 'm'), col2 = regexp_replace(col2, '^[a

我有一个Postgres sql,它有两个列,两个列都包含相同的名称数据,其中的部分由字母大小写区分。例如:

col1         col2
johnSmith    johnSmith
我希望col1只是
“john”
,col2只是
“Smith”

名称部分之间的分隔在第一个大写字母处。

尝试以下操作:

update mytable set
col1 = regexp_replace(col1, '[A-Z].*', 'm'),
col2 = regexp_replace(col2, '^[a-z]*', '')
这两个调用都以要删除的零件为目标,并通过不替换(空白)来完成

第一个正则表达式
[A-Z].
匹配第一个大写字母以及其后的所有内容

第二个正则表达式与字符串开头匹配,添加紧跟其后的所有小写字母。

尝试以下操作:

update mytable set
col1 = regexp_replace(col1, '[A-Z].*', 'm'),
col2 = regexp_replace(col2, '^[a-z]*', '')
这两个调用都以要删除的零件为目标,并通过不替换(空白)来完成

第一个正则表达式
[A-Z].
匹配第一个大写字母以及其后的所有内容


第二个正则表达式匹配字符串的开头,添加紧跟其后的所有小写字母。

使用两个函数,子字符串使用正则表达式查找第一个大写字符,position返回它在字符串中的位置

select position((select substring('my Firts Uppercase', '([A-Z])')) in 'my Firts Uppercase');

使用两个函数,substring使用正则表达式查找第一个大写字符,position返回它在字符串中的位置

select position((select substring('my Firts Uppercase', '([A-Z])')) in 'my Firts Uppercase');

您也可以尝试以下方法:

select t[1] as col1,t[2] as col2 from (
  select regexp_matches(login,'([a-z]*)([A-Z].*)') t from (
        select 'johnSmith'::character varying as login
 ) a 
 ) b

您也可以尝试以下方法:

select t[1] as col1,t[2] as col2 from (
  select regexp_matches(login,'([a-z]*)([A-Z].*)') t from (
        select 'johnSmith'::character varying as login
 ) a 
 ) b

使用
子字符串
和正则表达式。查询:

SELECT substring(a from '^[a-z]*'),  --select lower case letters from the start
       substring(b from '[A-Z].*')   --select Capital starting string from the end...
FROM t1                              --... ending with '.*$' would probably pay off
返回:

john | Smith
然后将其更新回:

UPDATE t1
SET a=substring(a from '^[a-z]*'),
    b=substring(b from '[A-Z].*')
FROM t1

使用
子字符串
和正则表达式。查询:

SELECT substring(a from '^[a-z]*'),  --select lower case letters from the start
       substring(b from '[A-Z].*')   --select Capital starting string from the end...
FROM t1                              --... ending with '.*$' would probably pay off
返回:

john | Smith
然后将其更新回:

UPDATE t1
SET a=substring(a from '^[a-z]*'),
    b=substring(b from '[A-Z].*')
FROM t1

编辑问题并提供示例数据和所需结果。编辑问题并提供示例数据和所需结果。