免费建站网站建设/排名优化工具
字符串函数 --拼接字符串组成新的字符串
Select concat(‘A’,’B’);--返回字符串长度
Select length(“CAT”)--返回子字符串在字符串中首次出现的位置,没有返回0
SELECT locate("b","abcd"); --返回2--下例结果为’??hi’ , 在左边填补?直到指定长度
select LPAD('hi',4,'?');--从左边或右边截取指定长度字符串
Select left(“abcdefg”,2) –返回ab--任意位置截取指定长度子串
Select substring(“abcdefg”,2,3) –返回bcd--剔除空格
Select trim(“ abcd ”)--查找替换函数
select REPLACE('www.mysql.com', 'com', 'net') – www.mysql.net--截取第n个符号之前的子串
Select substring_index("www.taobao.com",'.',1);
日期时间函数 --求一周或一个月或一年中的第几天
Select dayofweek(‘2010-12-07’); //输出3
Select dayofmonth(‘2010-12-07’); //输出7
Select dayofyear(‘2010-12-07’); //输出341
--求一年中的第几周
Select week(now())--返回月份
Select month(now())--返回年份
Select year(now())--返回小时
Select hour(now())--返回分钟
Select minute(now())--返回秒
Select second(now())--返回礼拜几的英文
Select dayname(now())--返回月份的英文
Select monthname(now())
--date_add(date,INTERVAL 表达式 type) type可以取year,month,day等等,即往当前日期加多少年(月天)
adddate为该函数别名
select date_add(now(),interval 1 year);--date_sub(date,INTERVAL 表达式 type) type可以取year,month,day等等,即往当前日期减多少年(月天)
subdate为该函数别名
select date_sub(now(),interval 1 year);
-- extract(type FROM date) 从给定日期抽取年份月份日期type=year/month/day
Select extract(year from ‘2010-01-02’) //返回2010
--返回给定日期到目前的天数
Select to_days(now())--相反的函数
Select from_days(0)
--格式化日期
select date_format(now(),'%Y年%m月%d日 %h:%i:%s'); //2010年12月7日 12:01:45