mysql中的类似于oracle的to_date函数
2015-02-25 00:00
323 查看
摘要: mysql中的类似于oracle的to_date函数
mysql日期和字符相互转换方法
date_format(date,'%Y-%m-%d') -------------->oracle中的to_char();
str_to_date(date,'%Y-%m-%d') -------------->oracle中的to_date();
%Y:代表4位的年份
%y:代表2为的年份
%m:代表月, 格式为(01……12)
%c:代表月, 格式为(1……12)
%d:代表月份中的天数,格式为(00……31)
%e:代表月份中的天数, 格式为(0……31)
%H:代表小时,格式为(00……23)
%k:代表 小时,格式为(0……23)
%h: 代表小时,格式为(01……12)
%I: 代表小时,格式为(01……12)
%l :代表小时,格式为(1……12)
%i: 代表分钟, 格式为(00……59)
%r:代表 时间,格式为12 小时(hh:mm:ss [AP]M)
%T:代表 时间,格式为24 小时(hh:mm:ss)
%S:代表 秒,格式为(00……59)
%s:代表 秒,格式为(00……59)
比如有以下表结构:
mysql> desc logistics_express_trajectory;
+-------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| id | varchar(32) | NO | PRI | NULL | |
| longitude | varchar(32) | YES | | NULL | |
| latitude | varchar(32) | YES | | NULL | |
| create_date | datetime | YES | | NULL | |
| modify_date | datetime | YES | | NULL | |
| member_express_id | varchar(32) | YES | MUL | NULL | |
| loctime | varchar(32) | YES | | NULL | |
| radius | varchar(16) | YES | | NULL | |
| speed | varchar(16) | YES | | NULL | |
| orient | varchar(16) | YES | | NULL | |
| location | varchar(256) | YES | | NULL | |
| client_loctime | datetime | YES | | NULL | |
| flag | int(11) | YES | | NULL | |
+-------------------+--------------+------+-----+---------+-------+
其中loctime为字符串格式的时间,格式为yyyy-mm-dd hh24:mi:ss,要把每条记录对应的client_loctime更新为loctime的date格式,可以执行以下语句:
update logistics_express_trajectory set client_loctime = str_to_date(loctime, '%Y-%m-%d %H:%i:%s') where loctime is not null;
注意mysql不会对错误的格式作检验,比如我执行语句(注意红字部分):
update logistics_express_trajectory set client_loctime = str_to_date(loctime, '%Y-%m-$d %H:%i:%s') where loctime is not null;
mysql也会提示执行完成,但更新后client_loctime是空的,这个问题居然搞了我十几分钟,汗!
mysql日期和字符相互转换方法
date_format(date,'%Y-%m-%d') -------------->oracle中的to_char();
str_to_date(date,'%Y-%m-%d') -------------->oracle中的to_date();
%Y:代表4位的年份
%y:代表2为的年份
%m:代表月, 格式为(01……12)
%c:代表月, 格式为(1……12)
%d:代表月份中的天数,格式为(00……31)
%e:代表月份中的天数, 格式为(0……31)
%H:代表小时,格式为(00……23)
%k:代表 小时,格式为(0……23)
%h: 代表小时,格式为(01……12)
%I: 代表小时,格式为(01……12)
%l :代表小时,格式为(1……12)
%i: 代表分钟, 格式为(00……59)
%r:代表 时间,格式为12 小时(hh:mm:ss [AP]M)
%T:代表 时间,格式为24 小时(hh:mm:ss)
%S:代表 秒,格式为(00……59)
%s:代表 秒,格式为(00……59)
比如有以下表结构:
mysql> desc logistics_express_trajectory;
+-------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| id | varchar(32) | NO | PRI | NULL | |
| longitude | varchar(32) | YES | | NULL | |
| latitude | varchar(32) | YES | | NULL | |
| create_date | datetime | YES | | NULL | |
| modify_date | datetime | YES | | NULL | |
| member_express_id | varchar(32) | YES | MUL | NULL | |
| loctime | varchar(32) | YES | | NULL | |
| radius | varchar(16) | YES | | NULL | |
| speed | varchar(16) | YES | | NULL | |
| orient | varchar(16) | YES | | NULL | |
| location | varchar(256) | YES | | NULL | |
| client_loctime | datetime | YES | | NULL | |
| flag | int(11) | YES | | NULL | |
+-------------------+--------------+------+-----+---------+-------+
其中loctime为字符串格式的时间,格式为yyyy-mm-dd hh24:mi:ss,要把每条记录对应的client_loctime更新为loctime的date格式,可以执行以下语句:
update logistics_express_trajectory set client_loctime = str_to_date(loctime, '%Y-%m-%d %H:%i:%s') where loctime is not null;
注意mysql不会对错误的格式作检验,比如我执行语句(注意红字部分):
update logistics_express_trajectory set client_loctime = str_to_date(loctime, '%Y-%m-$d %H:%i:%s') where loctime is not null;
mysql也会提示执行完成,但更新后client_loctime是空的,这个问题居然搞了我十几分钟,汗!
相关文章推荐
- mysql实现类似于oracle的to_char() to_date()
- Connecting to databases like Mysql, SQL Server or Oracle on J2ME devices
- Migration mass DB from Mysql to Oracle (原创)
- oracle 10g to_char函数与to_date函数的用法
- mysql中的日期转换函数(类似oracle中的to_date)
- Oracle的to_date函数(SQL语句往Oracle数据库中插入日期型数据)
- sql server 实现类似oracle to_date函数
- mysql中的日期转换函数(类似oracle中的to_date)
- Migrate Oracle Table to Mysql
- oracle之to_date函数
- oracle、sqlserver、mysql常用函数对比[to_char、to_number、to_date]
- SQL Server中模拟Oracle的to_date函数
- oracle to_date函数
- oracle to_date函数
- mysql中对应oracle中的to_char()和to_number()函数
- Linq to MySql/Oracle/Postgres/Sqlite
- Oracle中的To_date函数
- oracle之to_date函数
- oracle、sqlserver、mysql常用函数对比[to_char、to_number、to_date]
- Oracle的to_date函数