sql常用函数

查看字符的ascii码值ascii(str),str是空串时返回0

select ascii('a');

查看ascii码值对应的字符char(数字)

select char(97);

拼接字符串concat(str1,str2...)

select concat(12,34,'ab');

包含字符个数length(str)

select length('abc');

截取字符串

left(str,len)返回字符串str的左端len个字符

select left('abc123',2);

right(str,len)返回字符串str的右端len个字符

select right('abc123',2);

substring(str,pos,len)返回字符串str的位置pos起len个字符

select substring('abc123',2,3);

去除空格

ltrim(str)返回删除了左空格的字符串str

select ltrim('  abcd')

rtrim(str)返回删除了右空格的字符串str

select ltrim('abcd  ')

trim([方向 remstr from str)返回从某侧删除remstr后的字符串str,方向词包括both、leading、trailing,表示两侧、左、右

select trim('  bar  ');

select trim(leading 'x' FROM 'xxxbarxxx');

select trim(both 'x' FROM 'xxxbarxxx');

select trim(trailing 'x' FROM 'xxxbarxxx');

返回由n个空格字符组成的一个字符串space(n)

select space(10);

替换字符串replace(str,from_str,to_str)

select replace('abc123','123','def');

大小写转换,函数如下

lower(str)

select lower('aBcD');

upper(str)

select upper('aBcD');



数学函数

求绝对值abs(n)

select abs(-32);

求m除以n的余数mod(m,n),同运算符%

select mod(10,3);

select 10%3;

地板floor(n),表示不大于n的最大整数

select floor(2.3);

天花板ceiling(n),表示不小于n的最大整数

select ceiling(2.3);

求四舍五入值round(n,d),n表示原数,d表示小数位置,默认为0

select round(1.6);

求x的y次幂pow(x,y)

select pow(2,3);

获取圆周率PI()

select PI();

随机数rand(),值为0-1.0的浮点数

select rand();

还有其它很多三角函数,使用时可以查询文档



日期时间函数

获取子值,语法如下

year(date)返回date的年份(范围在1000到9999)

select year('2016-12-21');

month(date)返回date中的月份数值

select month('2016-12-21');

day(date)返回date中的日期数值

select day('2016-12-21');

hour(time)返回time的小时数(范围是0到23)

select hour('12:50:30');

minute(time)返回time的分钟数(范围是0到59)

select minute('12:50:30');

second(time)返回time的秒数(范围是0到59)

select second('12:50:30');



日期计算,使用+-运算符,数字后面的关键字为year、month、day、hour、minute、second

select '2016-12-21'+interval 1 day;

日期格式化date_format(date,format),format参数可用的值如下

获取年%Y,返回4位的整数

获取年%y,返回2位的整数

获取月%m,值为1-12的整数

获取日%d,返回整数

获取时%H,值为0-23的整数

获取时%h,值为1-12的整数

获取分%i,值为0-59的整数

获取秒%s,值为0-59的整数

日期-->字符串

select date_format('2019-3-12','%Y年%m月%d日');

字符串-->日期

select str_to_date('2019年3月12日','%Y年%m月%d日')

当前日期current_date()

select current_date();

当前时间current_time()

select current_time();

当前日期时间now()

select now();







import datetime

import time

# 日期时间字符串

st = "2017-11-23 16:10:10"

# 当前日期时间

dt = datetime.datetime.now()

# 1.把datetime转成字符串

def datetime_toString(dt):

print("1.把datetime转成字符串: ", dt.strftime("%Y-%m-%d %H:%M:%S"))

# 2.把字符串转成datetime

def string_toDatetime(st):

print("2.把字符串转成datetime: ", datetime.datetime.strptime(st, "%Y-%m-%d %H:%M:%S"))

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、整理今天所学的知识点 、、、、、、、、、、、、、、、、、、、、、、、、、、 2、内置函数 1、字符串 1、le...
    世界如此美妙_32e2阅读 447评论 0 0
  • 前言 最先接触编程的知识是在大学里面,大学里面学了一些基础的知识,c语言,java语言,单片机的汇编语言等;大学毕...
    oceanfive阅读 3,124评论 0 7
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,487评论 0 13
  • MYSQL 基础知识 1 MySQL数据库概要 2 简单MySQL环境 3 数据的存储和获取 4 MySQL基本操...
    Kingtester阅读 7,838评论 5 116
  • 我们每个人都深知,好的习惯对一个人是多么的重要。但真的要养成好的习惯确实比较困难。但既然出发点是好的,那就心...
    哈哈的幸福生活阅读 912评论 0 2