【GaussDB数据库】字符串函数,数学函数,日期函数,聚合函数,条件判断函数,系统信息函数,加密函数,格式化函数

1 字符串函数

# 字符串函数
# 计算字符串长度: 18, 9一个汉字占三个字节
select length('GaussDB(for MySQL)'), length('数据库');

# 合并字符串函数: GaussDB(for MySQL)数据库
select concat('GaussDB(for MySQL)', '数据库');

# 去掉字符串的空格:GaussDB(for MySQL)
select trim('  GaussDB(for MySQL)  ');

# 全部字母转换为小写:gaussdb(for mysql)
select lower('GaussDB(for MySQL)');

# 全部字母转换为大写:GAUSSDB(FOR MYSQL)
select upper('GaussDB(for MySQL)');

# 从左侧开始截取字符串:Gauss
select left('GaussDB(for MySQL)', 5);

# 从右侧开始截取字符串:(for MySQL)
select right('GaussDB(for MySQL)', 11);

# 字符串替换函数:GaussDB(for NoSQL)
select replace('GaussDB(for MySQL)', 'My', 'No');

# 从指定位置开始截取指定长度字符串:DB
select substring('GaussDB(for MySQL)', 6, 2);

# 字符串反转:)LQSyM rof(BDssuaG
select reverse('GaussDB(for MySQL)');

2 数学函数

# 数学函数
# 求绝对值
select abs(-200);

# 求二次方根
select sqrt(1024);

# 求余数
select mod(2020, 1024);

# 向下取整
select ceil(3.1415926);

# 向上取整
select floor(3.141596);

# 生成0-1之间的随机数
select rand();

# 四舍五入
select round(3306.1415926);

# 参数的符号(-1,0,1)
select sign(-200);

# 求正弦值
select sin(2020);

# 求余弦值
select cos(-2020);

# 求正切值
select tan(2020);

3 日期函数

# 当前系统的日期值:2022-09-11
select current_date(), curdate();

# 当前系统的时间值:17:17:24
select current_time, curtime();

# 返回当前系统的日期和时间值:2022-09-11 17:17:34
select now(), sysdate();

# 获取UNIX时间戳函数
select unix_timestamp();

# 获取指定日期中的月份
select month('2022-09-11 17:17:34');

# 获取指定日期中的年份
select year('2022-09-11 17:17:34');

# 向日期添加指定的时间间隔:2022-09-21 17:17:34
select adddate('2022-09-11 17:17:34', 10);

# 向日期减去指定的时间间隔:2022-09-01 17:17:34
select subdate('2022-09-11 17:17:34', 10);

# 时间加法运算
select addtime('2022-09-11 17:17:34', 3306);

# 时间减法运算
select subtime('2022-09-11 17:17:34', 3306);

# 获取两个日期之间的间隔,参数1 - 参数2 = -30
select datediff('2022-10-01', '2022-10-31');

4 聚合函数

聚合函数的作用是进行统计分析。

# 聚合函数:指定列的最大值,指定列的最小值,统计查询结果的行数,指定列的求和,指定列的平均值
select max(),min(),count(),sum(),avg();

5 条件判断函数

# 条件判断函数
# 判断、流程控制
select if(1 > 0, 'Yes', 'No');

# 判断是否为空
select ifnull(null, 'Yes');

# 搜索语句
select case
           when 1 > 0
               then '1>0'
           when 2 > 0
               then '2>0'
           when 3 > 0
               then '3>0'
           else '4>0' end;

6 系统信息函数

# 系统信息函数
# 返回当前用户
select session_user(), system_user(), current_user();

# 当前数据库名
select database();

# 数据库的版本号
select version();

7 加密函数

# 加密函数
select md5('GaussDB(for MySQL)');

select sha('GaussDB(for MySQL)');

select sha1('GaussDB(for MySQL)');

select password('GaussDB');

8 格式化函数

# 格式化函数
select date_format(now(), '%W, %D %M %Y %r'); # Sunday, 11th September 2022 06:53:05 PM
select date_format(now(), '%Y-%M-%D, %H:%m:%S'); # 2022-September-11th, 18:09:05
select format(3.1415926, 2); # 3.14

猜你喜欢

转载自blog.csdn.net/qq_40507857/article/details/126805113
今日推荐