• PostgreSQL自学笔记:6 PostgreSQL函数


    6 PostgreSQL函数

    6.2 数学函数

    • abs(x) 绝对值

    • pi() 圆周率π
      select abs(-3),pi();

      • cookie:
        MySQL中的pi()默认值3.141593,
        PostgreSQL中的pi()默认值3.14159265358979
        e值默认值位数不同,一般浮点数未设置保留
        位数显示会不同,MySQL保留6位
    • sqrt(x) 平方根

    • mod(x,y) x被y除后的余数
      select sqrt(4),mod(9.0,4);

    • ceil(x) ceiling(x) 返回不小于x的最小整数值

    • floor(x) 返回不大于x的最大整数
      select ceil(3.3),floor(3.3);

    • round(x) 返回最接近x的整数,即四舍五入

    • round(x,y) 返回最接近x的数,其值保留小数点后y位
      select round(5.5),round(5.5,2);

    • sign(x) 正 零 负 => 1 0 -1
      select sign(pi()),sign(0),sign(-3);

    • pow(x,y) power(x,y) 返回x的y次方

    • exp(x) e的x次方
      select pow(2,3),exp(1);

    • log(x) 返回x相对于10的对数
      select log(10),log(3);

    • log(x,y) 返回x相对于y的对数

      • cookie:
        MySQL中log(x)返回的是x相对于e的对数
    • radians(x) 将角度转换为弧度

    • degrees(x) 将弧度转换为角度
      select radians(45),degrees(0.5*pi());

    • sin(x) x的正弦,其中x为弧度值

    • asin(x) x的反正弦,x值在-1到1范围之内,否则报错

    • cos(x) x的余弦,其中x为弧度值

    • acos(x) x的反余弦,x值在-1到1范围之内,否则报错

    • tan(x) x的正切,其中x为弧度值

    • atan(x) x的反正切,其中x为弧度值

    • cot(x) x的余切
      select sin(1),round(sin(pi()));

    6.3 字符串函数

    • char_length(str)
      返回str所包含的字符个数
      select char_length('good');

    • concat(s1,s2,...)
      返回结果为连接参数产生的字符串
      select concat('|**',now(),'**|');

    • concat_ws(x,s1,s2,...)
      返回*为分隔符连接后面参数的字符串
      select concat_ws('*','1st','null','3rd');

    • left(s,n)
      返回字符串s开始最左边n个字符

    • right(s,n)
      返回字符串s最右边n个字符
      select left('123456',2),right('123456',2);

    • lpad(s1,len,s2)
      返回字符串s1,其左边由字符串s2填充,填充长度为len

    • rpad(s1,len,s2)
      返回字符串s1,其左边由字符串s2填充,填充长度为len
      select lpad('123',4,'456'),rpad('123',2,'456');

    • ltrim(s)
      返回字符串s,字符串左侧空格被删除

    • rtrim(s)
      返回字符串s,字符串右侧空格被删除

    • trim(s)
      返回字符串s,字符串两侧空格被删除

    • trim(s1 from s)
      返回字符串s,字符串两侧s1被删除
      select trim('a' from 'aa11aa');

    • repeat(s,n)
      返回一个重复n次s组成的字符串,n<=0则为空字符串,s或n
      为null则返回null
      select repeat('|*|',3);

    • replace(s,s1,s2)
      返回s字符串,s其中s1被s2替换
      select replace('aabbcc','b','d');

    • substring(s,n,len)
      从字符串s返回一个长度为len,起始于位置n
      select substring('123456',4,4);

      • cookie:
        PostgreSQL10 不支持n为负数,即倒数位置
    • position(str1 in str)
      返回字符串srl1在字符串str中的开始位置
      select position('ball' in 'football');

    • reverse(s)
      将字符串s反转返回
      select reverse('football');

    6.4 日期时间函数

    • current_date
      将当前时间按 YYYU-MM-DD 格式返回
      select current_date;

    • current_time
      将当前时间按 HH:MM:SS 格式返回
      select current_time;

    • curent_timestamplocaltimestamp ow()
      返回当前时间和日期

    • extract(type from date)
      从日期中提取一部分,而不是执行日期运算
      select extract(day from now()); //日
      select extract(month from now());//月
      select extract(year from now()); //年
      select extract(doy from now());//一年的第几天
      select extract(dow from now()); //星期几
      select extract(quarter from now()); //季度

      • cookie:
        此函数和MySQL中的extract略有不同

    日期和时间的运算操作
    select date '2019-1-17'-'1993-12-22'

    6.5 条件判断函数

    • case expr when v1 then r1 [when v2 r2] [else rm] end;
      如果expr等于某个vn,就返回对应位置的rn的值,
      如果expr与所有的值都不相等,就返回rm

      select case 2 when 1  then 'one'
          when 2 then 'two' else 'more' end; 
      
    • case when v1 then r1 [when v2 then r2] [else rm] end;
      如果vn的某个值为true,就返回对应位置的rn的值,
      如果都不为true,就返回rm

    6.6 系统信息函数

    • version()
      获取PostgreSQL版本号
      select version();

    • user current_user
      返回当前被PostgreSQL服务器验证的用户名
      select user,current_user;

      • cookie:
        在MySQL中version()同样获取mysql版本号,
        没有usercurrent_user函数

    6.7 加密和解密函数

    • md5(str)
      为字符串算出一个MD5 128比特检查和
      select md5('mypwd');

    • encode(str,pswd_str)
      使用pswd_str作为加密编码,加密str
      常见加密编码base64hexescape

    • decode(str,pswd_str)
      使用pswd_str作为密码,解密str
      select encode ('secret','hex');
      select decode ('736563726574','hex');

      • cookie:
        MySQL中没有encodedecode函数

    6.8 改变数据类型

    • cast(x,as type) 将x转换成type类型

      select cast(100 as char(5));

  • 相关阅读:
    Selenium实战(七)——自动发送邮件
    Selenium实战(六)——数据驱动应用
    Selenium实战(四)——unittest单元测试3(测试用例的执行顺序)
    Selenium实战(四)——unittest单元测试2(断言方法+discover()多测试用例的执行)
    运维工具
    实用的shell脚本面试题和答案
    mssql2000 数据库一致性错误修复
    c#基础
    任务表 步骤表
    BPM事件
  • 原文地址:https://www.cnblogs.com/wangbaby/p/10289553.html
Copyright © 2020-2023  润新知