运维工程师常用的Mysql语句——1.日期、字符串函数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_23322973/article/details/88183386
  1. 常用日期函数
#时间转字符串
date_format(date, format)
#时间转时间戳(字符串)
unix_timestamp()
#字符串转时间
str_to_date(str, format)
#时间戳(字符串)转时间
from_unixtime(unix_timestamp, format)

1)时间转字符串

select date_format(now(), '%Y-%m-%d');  

2019-03-05

2)时间转时间戳

select unix_timestamp(now()); 

1551769830

3)字符串转时间

select str_to_date('2019-03-05', '%Y-%m-%d %H'); 

2019-03-05 00:00:00

4)字符串转时间戳

select unix_timestamp('2019-03-05'); 

1551715200

5)时间戳转时间

select from_unixtime(1551715200); 

2019-03-05 00:00:00

6)时间戳转字符串

select from_unixtime(1551715200,'%Y-%d');  

2019-05
秒		%S、%s		两位数字形式的秒( 00,01, ..., 59)

分		%I、%i		两位数字形式的分( 00,01, ..., 59)

小时 	%H			24小时制,两位数形式小时(00,01, ...,23)
		%h			12小时制,两位数形式小时(00,01, ...,12)
		%k			24小时制,数形式小时(0,1, ...,23)
		%l			12小时制,数形式小时(0,1, ...,12)
		%T			24小时制,时间形式(HH:mm:ss)
		%r			12小时制,时间形式(hh:mm:ss AM 或 PM)
		%p 			AM上午或PM下午 

周 	 	%W			一周中每一天的名称(Sunday,Monday, ...,Saturday)
		%a			一周中每一天名称的缩写(Sun,Mon, ...,Sat) 
		%w 			以数字形式标识周(0=Sunday,1=Monday, ...,6=Saturday) 
		%U			数字表示周数,星期天为周中第一天
		%u			数字表示周数,星期一为周中第一天
		天			%d 	两位数字表示月中天数(01,02, ...,31)
		%e 			数字表示月中天数(1,2, ...,31)
		%D			英文后缀表示月中天数(1st,2nd,3rd ...) 
		%j			以三位数字表示年中天数(001,002, ...,366) 

月		%M 			英文月名(January,February, ...,December) 
		%b 			英文缩写月名(Jan,Feb, ...,Dec) 
		%m 			两位数字表示月份(01,02, ...,12)
		%c 			数字表示月份(1,2, ...,12) 
		
年		%Y 			四位数字表示的年份(2015,2016...)
		%y 			两位数字表示的年份(15,16...)
  1. 常用字符串函数
    1)CONCAT_WS(separator,str1,str2,…):将多个字符串参数以给定的分隔符
mysql> select concat_ws(';','First name','Second name','Last name');
+-------------------------------------------------------+
| concat_ws(';','First name','Second name','Last name') |
+-------------------------------------------------------+
| First name;Second name;Last name                      |
+-------------------------------------------------------+
**#注意:**如果有任何参数为null,则函数不返回null,而是直接忽略它

mysql> select concat_ws(',','id',null,'name');
+---------------------------------+
| concat_ws(',','id',null,'name') |
+---------------------------------+
| id,name                         |
+---------------------------------+

2)SUBSTR(str,pos[,len]):从源字符串str中的指定位置pos开始取一个字串并返回

猜你喜欢

转载自blog.csdn.net/qq_23322973/article/details/88183386