• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    PostGreSql 判断字符串中是否有中文的案例

    我就废话不多说了,大家还是直接看代码吧~

    实例

    imos=# select 'hello' ~ '[\u2e80-\ua4cf]|[\uf900-\ufaff]|[\ufe30-\ufe4f]';
     ?column?
    ----------
     f
    (1 row)
    imos=#
    imos=# select 'hello中国' ~ '[\u2e80-\ua4cf]|[\uf900-\ufaff]|[\ufe30-\ufe4f]';
     ?column?
    ----------
     t
    (1 row)
    

    补充:PostgreSQL 判断字符串包含的几种方法

    判断字符串包含的几种方法:

    1. position(substring in string):

    postgres=# select position('aa' in 'abcd');
     position 
    ----------
     0
    (1 row)
    postgres=# select position('ab' in 'abcd');
     position 
    ----------
     1
    (1 row)
    postgres=# select position('ab' in 'abcdab');
     position 
    ----------
     1
    (1 row)
    

    可以看出,如果包含目标字符串,会返回目标字符串笫一次出现的位置,可以根据返回值是否大于0来判断是否包含目标字符串。

    2. strpos(string, substring):

    该函数的作用是声明子串的位置。

    postgres=# select strpos('abcd','aa');
     strpos 
    --------
     0
    (1 row)
    postgres=# select strpos('abcd','ab');
     strpos 
    --------
     1
    (1 row)
    postgres=# select strpos('abcdab','ab');
     strpos 
    --------
     1
    (1 row)
    

    作用与position函数一致。

    3. 使用正则表达式:

    postgres=# select 'abcd' ~ 'aa';
     ?column? 
    ----------
     f
    (1 row)
    postgres=# select 'abcd' ~ 'ab';
     ?column? 
    ----------
     t
    (1 row)
    postgres=# select 'abcdab' ~ 'ab';
     ?column? 
    ----------
     t
    (1 row)
    

    4. 使用数组的@>操作符(不能准确判断是否包含):

    postgres=# select regexp_split_to_array('abcd','') @> array['b','e'];
     ?column? 
    ----------
     f
    (1 row)
    postgres=# select regexp_split_to_array('abcd','') @> array['a','b'];
     ?column? 
    ----------
     t
    (1 row)
    

    注意下面这些例子:

    postgres=# select regexp_split_to_array('abcd','') @> array['a','a'];
     ?column? 
    ----------
     t
    (1 row)
    postgres=# select regexp_split_to_array('abcd','') @> array['a','c'];
     ?column? 
    ----------
     t
    (1 row)
    postgres=# select regexp_split_to_array('abcd','') @> array['a','c','a','c'];
     ?column? 
    ----------
     t
    (1 row)
    

    可以看出,数组的包含操作符判断的时候不管顺序、重复,只要包含了就返回true,在真正使用的时候注意。

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

    您可能感兴趣的文章:
    • PostgreSQL的中文拼音排序案例
    • 自定义函数实现单词排序并运用于PostgreSQL(实现代码)
    • PostgreSQL将数据加载到buffer cache中操作方法
    • 在PostgreSQL中使用ltree处理层次结构数据的方法
    • postgresql 中的时间处理小技巧(推荐)
    • Postgresql限制用户登录错误次数的实例代码
    • PostgreSQL用户登录失败自动锁定的处理方案
    • postgresql影子用户实践场景分析
    • 如何使用PostgreSQL进行中文全文检索
    上一篇:postgresql 中position函数的性能详解
    下一篇:PostgreSQL物理备份恢复之 pg_rman的用法说明
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

    PostGreSql 判断字符串中是否有中文的案例 PostGreSql,判断,字符串,中,