函数:单独的功能,不牵扯到面向对象;
方法:一般面向对象,通过对象调用类中的方法
字符串函数
1、CONCAT(S1,S2…SN)
select CONCAT('A','B','C') AS RESULT
注:任何字符串与null连接,结果都是null
但是在dm中测试,结果如下:
select CONCAT('A','B',null) AS RESULT
2、insert(str,x,y,instr)
select insert('abcd1234',5,2,'**')
3、字符串大小写转换:lower(str) upper(str)
select lower('ABcd')
select upper('ABcd')
4、left(str,x) right(str,x)
select left('ABcd',2)
select right('ABcd',3)
如果长度超出了字符串长度,例如:
select right('ABcd',8)
结果如下图所示:
注:如果第二个参数为null,则返回null
select right('ABcd',null)
5、lpad(str,n,pad) rpad(str,n,pad)
select lpad('ABcde',8,'*')
select rpad('AB',5,'cdefgh')
如果小于长度:
select rpad('ABcde',4,'*')
6、去除空格ltrim(str) rtrim(str)
select ltrim(' abcd 2')
select rtrim(' abcd ')
trim可以同时去除左右的空格
select trim(' ab cd ')
7、repeat(str,n)
select repeat('ab',3)
8、replace
select replace('ab','a','A')
9、substring
select substring('abcdef',3,2)
数值函数
abs(x)
ceil(x)
select ceil(2.1)
floor(x)
mod(x,y)
rand()返回0-1之间的随机数
select rand()
如果想返回0-10之间的随机数,使用如下语句:
select floor(rand()*10)
日期和时间函数
返回当前日期curdate()—年月日
select curdate()
返回当前时间curtime()—时分秒
select curtime()
返回当前日期和时间 now()
select now()
返回当前时间戳 unix_timestamp
dm中是CURRENT_TIMESTAMP()
select CURRENT_TIMESTAMP()
将时间戳转为日期 from_unixtime(unixtime)
返回所给时间是一年中的第几周 week(date)
返回所给时间是哪一年 year(date)
返回当前时间的小时 hour(time)
返回当前时间的分钟 minute(time)
按字符串格式化日期date值 date_formate(date,fmt)
date_add(date,interval,exper type)
select DATE_ADD(NOW(),INTERVAL 3 YEAR)
两个日期之间的相差天数datediff(date1,date2)
流程函数
if(value,t,f)
select if((2>3),'false','true')
ifnull(value1,value2)
case when then end
select case when 2>3 then 'false' else 'false' end
其他常用函数
返回当前数据库database()
返回当前数据库版本version()
返回当前登录用户user()
对str进行加密password(str)
返回str的ND5值MD5(str)