1.数据修复最先考虑通过db内做修复,实在不行,在考虑外部应用程序通过jdbc修复.
比如一个场景:profile_image_url与enlarge_image_url都是微博用户信息返回的字段.前者是http://tp2.sinaimg.cn/1928431341/50/5621497131/1,后者正常情况是http://tp2.sinaimg.cn/1928431341/180/5621497131/1,此时如果修复后者的数据,只需将/50/替换成/180/,只需通过postgres的字符函数解决。
2.常用函数
2.1常用字符串函数列表
注意,下页的示例中字符串都是可以用表中的字段替代.?测试函数可以类似select"char_length"('string');?*标识不常用,字符串在任何库的函数最主要的不过就是substring,position,length,replace几种,类似于db的CRUD。
函数:string||string
说明:Stringconcatenation字符串连接操作例子:'Post'||'greSQL' = PostgreSQL
函数:string||non-stringornon-string||string说明:Stringconcatenationwithonenon-stringinput字符串与非字符串类型进行连接操作例子:'Value:'||42=Value:42
函数:bit_length(string)说明:Numberofbitsinstring计算字符串的位数例子:bit_length('jose')=32
函数:char_length(string)orcharacter_length(string)说明:Numberofcharactersinstring计算字符串中字符个数例子:char_length('jose')=4
select"char_length"('string'),"length"('string');?res:66
函数:lower(string)说明:Convertstringtolowercase转换字符串为小写例子:select"lower"('ABC')=abc
函数:octet_length(string)说明:Numberofbytesinstring计算字符串的字节数例子:octet_length('jose')=4

又比如要将'http://tp2.sinaimg.cn/1928431341/50/5621497131/1'中的/50/替换成/180/,可以使用的方法:
updatet_sns_member?set?enlarge_image_url=overlay(profile_image_urlplacing'/180/'fromposition('/50/'inprofile_image_url)for4)?whereenlarge_image_url=''
2.不使用替换,substring+position+||去拼新串
updatet_sns_member?set?enlarge_image_url=substring(profile_image_url,0,position('/50/'inprofile_image_url))||'/180/'||substring(profile_image_url,position('/50/'inprofile_image_url)+4,char_length(profile_image_url))whereenlarge_image_url='';
函数:position(substringinstring)说明:Locationofspecifiedsubstring子串在一字符串中的位置例子:position('om'in'Thomas')=3
函数:substring(string[fromint][forint])说明:Extractsubstring截取任意长度的子字符串例子:substring('Thomas'from2for3)=hom
函数:substring(stringfrompattern)说明:ExtractsubstringmatchingPOSIXregularexpression.SeeSection9.7formoreinformationonpatternmatching.利用正则表达式对一字符串进行任意长度的字串的截取例子:substring('Thomas'from'...$')=mas
函数:substring(stringfrompatternforescape)说明:ExtractsubstringmatchingSQLregularexpression.SeeSection9.7formoreinformationonpatternmatching.利于正则表达式对某类字符进行删除,以得到子字符串例子:trim(both'x'from'xTomxx')=Tom
函数:trim([leading|trailing|both][characters]fromstring)说明:Removethelongeststringcontainingonlythecharacters(aspacebydefault)fromthestart/end/bothendsofthestring去除尽可能长开始,结束或者两边的某类字符,默认为去除空白字符,当然可以自己指定,可同时指定多个要删除的字符串例子:trim(both'x'from'xTomxx')=Tom
函数:upper(string)说明:Convertstringtouppercase将字符串转换为大写例子:upper('tom')=TOM
函数:ascii(string)说明:ASCIIcodeofthefirstcharacteroftheargument.ForUTF8returnstheUnicodecodepointofthecharacter.Forothermultibyteencodings.theargumentmustbeastrictlyASCIIcharacter.得到某一个字符的Assii值例子:ascii('x')=120
函数:btrim(stringtext[,characterstext])说明:Removethelongeststringconsistingonlyofcharactersincharacters(aspacebydefault)fromthestartandendofstring去除字符串两边的所有指定的字符,可同时指定多个字符例子:btrim('xyxtrimyyx','xy')=trim
updatepropertysetmemorial_no=btrim(memorial_no,'')wherememorial_nolike'%'
或updatepropertysetmemorial_no=trim(both''?frommemorial_no)wherememorial_nolike'%'
本文来源:国外服务器--函数postgres一(post函数的用法)
本文地址:https://www.idcbaba.com/guowai/5816.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 1919100645@qq.com 举报,一经查实,本站将立刻删除。




