Mysql 日期-字符串转换。

mysql的字符串和日期类型的转换。
1.now()和curdate()的区别:
now():datetime类型。
mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2013-10-27 21:14:40 |
+---------------------+
1 row in set (0.01 sec)
 curdate():date类型
mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2013-10-27 |
+------------+
1 row in set (0.00 sec)
 2.日期-字符串类型的转换。
     <1>字符串 -> 日期: str_to_date(日期字符串,'模式字符串')
例子:student(id,name,score,birth);
update student set birth=str_to_date('1985-01-04','%Y-%m-%d');
 <2>日期 -> 字符串: date_format(birth,'%Y/%m/%d');
alter table student add column birth_str varchar(20);

update student set birth_str = date_format(birth,'%Y/%m/%d');
 
mysql> select * from student;
+----+--------+-------+------------+------------+
| id | name   | score | birth      | birth_str  |
+----+--------+-------+------------+------------+
|  1 | Aaron  | 78.00 | 1985-01-04 | 1985/01/04 |
 
(关于模式字符串的简单说明)
%Y=============2013
%y=============13
%m=============01
%M=============January
%d=============04
%D=============4th
 
%h:%i:%s=======时:分:秒


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值