• Post author:
  • Post category:mysql
  • Post comments:0评论

1、使用cast() 和 convert() 函数实现日期格式的转换

  语法格式:
    convert( 表达式,数据类型 )
    cast( 表达式 as 数据类型 )

  可转换的类型有 字符串char、日期date、时间time、日期时间datetime、浮点型decimal、整数signed、无符号整数unsigned。

mysql> select cast(now() as signed);
+-----------------------+
| cast(now() as signed) |
+-----------------------+
|        20201030042922 |
+-----------------------+
1 row in set (0.00 sec)

mysql> select convert(now(),signed);
+-----------------------+
| convert(now(),signed) |
+-----------------------+
|        20201030042933 |
+-----------------------+
1 row in set (0.00 sec)

mysql> select cast(now() as char);
+---------------------+
| cast(now() as char) |
+---------------------+
| 2020-10-30 04:29:44 |
+---------------------+

2、使用date_format()函数实现日期格式的转换

  date_format()函数可以以不同的格式显示日期/时间数据,可以实现日期转换成字符串。
  语法格式:
    date_format(date,format)
  date为合法的日期,format为规定日期/时间的输出格式。
  可以使用的格式有:

格式 描述
%a 缩写星期名
%b 缩写月名
%c 月,数值
%D 带有英文前缀的月中的天
%d 月的天,数值(00-31)
%e 月的天,数值(0-31)
%f 微秒
%H 小时 (00-23)
%h 小时 (01-12)
%I 小时 (01-12)
%i 分钟,数值(00-59)
%j 年的天 (001-366)
%k 小时 (0-23)
%l 小时 (1-12)
%M 月名
%m 月,数值(00-12)
%p AM 或 PM
%r 时间,12-小时(hh:mm:ss AM 或 PM)
%S 秒(00-59)
%s 秒(00-59)
%T 时间, 24-小时 (hh:mm:ss)
%U 周 (00-53) 星期日是一周的第一天
%u 周 (00-53) 星期一是一周的第一天
%V 周 (01-53) 星期日是一周的第一天,与 %X 使用
%v 周 (01-53) 星期一是一周的第一天,与 %x 使用
%W 星期名
%w 周的天 (0=星期日, 6=星期六)
%X 年,其中的星期日是周的第一天,4 位,与 %V 使用
%x 年,其中的星期一是周的第一天,4 位,与 %v 使用
%Y 年,4 位
%y 年,2 位

  示例1:时间类型转换成字符串类型


mysql> select date_format(now(),'%Y-%m-%d');
+-------------------------------+
| date_format(now(),'%Y-%m-%d') |
+-------------------------------+
| 2020-10-30                    |
+-------------------------------+

  示例2:从学生表 ‘student’ 中查询前2人现在的年龄。

mysql> select * from student limit 2;
+------------+-----------+-----+------------+-----------------------+---------+
| Sid        | Sname     | Sex | Brithdate  | Specialty             | AScores |
+------------+-----------+-----+------------+-----------------------+---------+
| 2011216001 | 赵成刚    | 男  | 1992-05-05 | 计算机应用技术        |   405.0 |
| 2011216002 | 李婧      | 女  | 1992-01-06 | 计算机应用技术        |   395.5 |
+------------+-----------+-----+------------+-----------------------+---------+

mysql> select sname,
    -> convert(date_format(now(),'%Y'),signed)-convert(date_format(Brithdate,'%Y'),signed) as '年龄' 
    -> from student 
    -> limit 2;
+-----------+--------+
| sname     | 年龄   |
+-----------+--------+
| 赵成刚    |     28 |
| 李婧      |     28 |
+-----------+--------+

3、使用str_to_date()函数实现字符串类型转换日期类型

  str_to_date()函数可以将时间格式的字符串按照所指定的显示格式(format)转换为不同的时间类型。
  语法格式:
    str_to_date(date,format)

mysql> select str_to_date('2020-10-3','%Y-%m-%d');
+-------------------------------------+
| str_to_date('2020-10-3','%Y-%m-%d') |
+-------------------------------------+
| 2020-10-03                          |
+-------------------------------------+

mysql> select str_to_date('2020-10-3','%Y');
+-------------------------------+
| str_to_date('2020-10-3','%Y') |
+-------------------------------+
| 2020-00-00                    |
+-------------------------------+

mysql> select str_to_date('10:11','%H:%i:%S');
+---------------------------------+
| str_to_date('10:11','%H:%i:%S') |
+---------------------------------+
| 10:11:00                        |
+---------------------------------+

发表评论

验证码: + 75 = 78