MySql 字符串时间转换
2014-04-09 11:56
260 查看
MySql中经常遇到字符串格式时间转换成时间类型的情况:
另外还有int值和时间变量相互转化的情况:
还可以自定义输出格式:
具体格式符号如下:
以下是官方文档的说明:
11.3.2. The
MySQL retrieves and displays
You can specify
As a string in
As a string with no delimiters in
As a number in
As the result of a function that returns a value that is acceptable in a
For
Be careful about assigning abbreviated values to a
By default, values that lie outside the
example,
SELECT STR_TO_DATE('Jul 20 2013 7:49:14:610AM','%b %d %Y %h:%i:%s:%f%p') from DUAL; -- 执行后得到结果: '2013-07-20 07:49:14.610000'
另外还有int值和时间变量相互转化的情况:
SELECT UNIX_TIMESTAMP(2009-4-5 12:50:58) RROM DUAL; -- 结果为: 1238907058 SELECT FROM_UNIXTIME(1238907058) FROM DUAL; -- 结果为: '2009-4-5 12:50:58'
还可以自定义输出格式:
SELECT FROM_UNIXTIME( 1234567890, '%Y-%m-%d %H:%i:%S' ) FROM DUAL; --结果为: '2009-02-14 07:31:30'
具体格式符号如下:
%W 星期名字(Sunday……Saturday) %D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。) %Y 年, 数字, 4 位 %y 年, 数字, 2 位 %a 缩写的星期名字(Sun……Sat) %d 月份中的天数, 数字(00……31) %e 月份中的天数, 数字(0……31) %m 月, 数字(01……12) %c 月, 数字(1……12) %b 缩写的月份名字(Jan……Dec) %j 一年中的天数(001……366) %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) %f 毫秒(000...999) %p AM或PM %w 一个星期中的天数(0=Sunday ……6=Saturday ) %U 星期(0……52), 这里星期天是星期的第一天 %u 星期(0……52), 这里星期一是星期的第一天 %% 一个文字“%”。
以下是官方文档的说明:
11.3.2. The TIME
Type
MySQL retrieves and displays TIMEvalues in
'HH:MM:SS'format (or
'HHH:MM:SS'format for large hours values).
TIMEvalues may range from
'-838:59:59'to
'838:59:59'. The hours part may be so large because the
TIMEtype can be used not only to represent a time of day (which must be less than 24 hours), but also elapsed time or a time interval between two events (which may be much greater than 24 hours, or even negative).
You can specify
TIMEvalues in a variety of formats:
As a string in
'D HH:MM:SS.fraction'format. You can also use one of the following “relaxed” syntaxes:
'HH:MM:SS.fraction',
'HH:MM:SS',
'HH:MM',
'D HH:MM:SS',
'D HH:MM',
'D HH', or
'SS'. Here
Drepresents days and can have a value from 0 to 34. Note that MySQL does not store the fraction part.
As a string with no delimiters in
'HHMMSS'format, provided that it makes sense as a time. For example,
'101112'is understood as
'10:11:12', but
'109712'is illegal (it has a nonsensical minute part) and becomes
'00:00:00'.
As a number in
HHMMSSformat, provided that it makes sense as a time. For example,
101112is understood as
'10:11:12'. The following alternative formats are also understood:
SS,
MMSS,
HHMMSS,
HHMMSS.fraction. Note that MySQL does not store the fraction part.
As the result of a function that returns a value that is acceptable in a
TIMEcontext, such as
CURRENT_TIME.
For
TIMEvalues specified as strings that include a time part delimiter, it is not necessary to specify two digits for hours, minutes, or seconds values that are less than
10.
'8:3:2'is the same as
'08:03:02'.
Be careful about assigning abbreviated values to a
TIMEcolumn. Without colons, MySQL interprets values using the assumption that the two rightmost digits represent seconds. (MySQL interprets
TIMEvalues as elapsed time rather than as time of day.) For example, you might think of
'1112'and
1112as meaning
'11:12:00'(12 minutes after 11 o'clock), but MySQL interprets them as
'00:11:12'(11 minutes, 12 seconds). Similarly,
'12'and
12are interpreted as
'00:00:12'.
TIMEvalues with colons, by contrast, are always treated as time of the day. That is,
'11:12'mean
'11:12:00', not
'00:11:12'.
By default, values that lie outside the
TIMErange but are otherwise legal are clipped to the closest endpoint of the range. For
example,
'-850:00:00'and
'850:00:00'are converted to
'-838:59:59'and
'838:59:59'. Illegal
TIMEvalues are converted to
'00:00:00'. Note that because
'00:00:00'is itself a legal
TIMEvalue, there is no way to tell, from a value of
'00:00:00'stored in a table, whether the original value was specified as
'00:00:00'or whether it was illegal.
相关文章推荐
- mysql 时间戳转换时间字符串
- mysql 时间与字符串相互转换
- 【MySQL笔记】字符串、时间日期转换
- mysql字符串转换成时间格式
- mysql时间字符串和时间戳转换
- Java、MySql中时间类型与字符串类型的相互转换
- mysql中字符串和时间互相转换的方法(自动转换及DATE_FORMAT函数)
- mysql-字符串与时间戳之间的转换
- Java、MySql中时间类型与字符串类型的相互转换
- mysql的时间和字符串的转换
- mysql 日期 字符串 时间戳 转换
- MySql 字符串时间转换 和 中文乱码
- mysql 日期 字符串 时间戳转换
- mysql中时间与字符串相互转换
- mysql日期/时间转换为字符串
- 在flex中把字符串转换成 日期时间类型datetime
- MySQL 获得当前日期时间(以及时间的转换)
- mysql中int型的数字怎么转换成字符串
- MySQL 获得当前日期时间(以及时间的转换)
- php时间戳与字符串的相互转换