MySql中经常遇到字符串格式时间转换成时间类型的情况:

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 TIME values in 'HH:MM:SS' format (or 'HHH:MM:SS' format for large hours values). TIME values may range from '-838:59:59' to '838:59:59'. The hours part may be so large because the TIME type 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 TIME values 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 D represents 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 HHMMSS format, provided that it makes sense as a time. For example, 101112 is 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 TIME context, such as CURRENT_TIME.

For TIME values 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 TIME column. Without colons, MySQL interprets values using the assumption that the two rightmost digits represent seconds. (MySQL interprets TIME values as elapsed time rather than as time of day.) For example, you might think of '1112' and 1112 as 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 12 are interpreted as '00:00:12'. TIME values 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 TIME range 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 TIME values are converted to '00:00:00'. Note that because '00:00:00' is itself a legal TIME value, 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 字符串时间转换的更多相关文章

  1. JS 时间转换函数 字符串时间转换毫秒(互转)

    字符串转化为日期 let util = function(){ Date.prototype.Format = function(fmt) { var o = { "M+" : t ...

  2. python 爬虫数据处理字符串时间转换格式方法

    startDate = "2018-10-01"endDate = "2018-10-31" ###字符转化为日期startTime = datetime.da ...

  3. mysql 毫秒时间转换

    当在数据库中存储的时间类型为bigint类型时,及时间的毫秒数 java中:  new DATE().gettime();//获取时间的毫秒数 当需要将毫秒数转化为时间的时候 mysql中: FROM ...

  4. mysql 字符串数字转换

    1 方法一:SELECT CAST('123' AS SIGNED); 2 方法二:SELECT CONVERT('123',SIGNED); 3 方法三:select '123'+1

  5. mysql 将时间转换成时间戳

    select UNIX_TIMESTAMP(addtime/*date_column*/) from tablename 输出:1548658912 数据库原格式:2019-01-28 15:01:2 ...

  6. mysql 中 时间和日期函数

    From: http://www.cnblogs.com/redfox241/archive/2009/07/23/1529092.html 一.MySQL 获得当前日期时间 函数 1.1 获得当前日 ...

  7. MySQL 日期、时间转换函数

    MySQL 日期.时间转换函数:date_format(date,format), time_format(time,format) 能够把一个日期/时间转换成各种各样的字符串格式.它是 str_to ...

  8. mysql 日期操作 增减天数、时间转换、时间戳(转)

    转自http://www.cnblogs.com/wenzichiqingwa/archive/2013/03/05/2944485.html http://hi.baidu.com/juntao_l ...

  9. mysql 日期操作 增减天数、时间转换、时间戳(转换)

    http://hi.baidu.com/juntao_li/item/094d78c6ce1aa060f6c95d0b MySQL datediff(date1,date2):两个日期相减 date1 ...

  10. Mysql日期转换函数、时间转换函数

    Mysql日期转换函数.时间转换函数 一.MySQL 获得当前日期时间 函数 1,获得当前日期+时间(date + time)函数:now(): select now(); 结果: :: 2,获得当前 ...

随机推荐

  1. jabc连接数据库

    Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法.JD ...

  2. Fortran笔记 派生类型-整理版

    以下为整理后的笔记,英文原文 Introduction to Modern Fortran for the Earth System Sciences, 英文翻译 https://www.cnblog ...

  3. django查询中values_list(flat=True) 是什么意思?

    1.values() departments = models.Department.objects.filter(dpm_status=1).values('dnp__name') print(de ...

  4. URAL2127 Determinant of a Graph 题解

    这个题真的折磨了我超久的.全网几乎搜不到一个详细的题解,俺来写写吧. 题意:给你一个无自环无重边的连通无向图,求它邻接矩阵的行列式的值. \(n\le 2*10^5,n-1\le m \le n+50 ...

  5. IntelliJ IDEA 生成serialVersionUID

    1.设置Settings-->Editor->Inspections 2.实现Serializble

  6. el-pagination分页-自定义左右箭头样式

    1,官方样式: 查了网上,有人说可以用slot插槽,但是试过之后,因为 不能插入多个 slot(没法定义名字做区分),所以导致左右按钮一样了.. 2,还有种方法: 利用 prev-text  和  n ...

  7. opencv对图片画框写文字

    业务背景:对图片进行画框后调用排序方法按照"从上到下,从左到右"对已经画的框进行排序.排序方法当前有缺陷,修复后需要验证. 目前有如下信息: 原始图片地址 图片对应的每个框的坐标( ...

  8. Cloudflare.com设置域名URL转发

    1.登录Cloudflare.com,将语言设置为简体中文,并选择需要设置URL转发的域名. 2.选择域名,需先对域名进行解析,解析地址随便填写,可以填写CloudFlare官方提供的DNS服务器地址 ...

  9. Python 的入门学习之 Day4~6 ——from”夜曲编程“

    Day 4 time: 2021.8.1. 打卡第四天.今天起得很早(7点多一些),很棒,而梳头发更快些就更好了.我也算渐渐养成了晨间学习的习惯吧,一起床就心心念念着Python课程,这让我感觉到了生 ...

  10. 我的编程之路刷题⑦:Problem 2719.--约瑟夫问题

    2719: 约瑟夫问题   时间限制 : 1.000 sec  内存限制 : 128 MB 题目描述 有M个人,其编号分别为1-M.这M个人按顺序排成一个圈.现在给定一个数N,从第一个人开始依次报数, ...