首页 > 数据库 >SQL 时间函数

SQL 时间函数

时间:2023-04-12 16:11:06浏览次数:36  
标签:12 函数 31 01 2020 SQL date 时间 select

转载自:SQL千字总结:如何更好的操练你手上的时间数据

1. 认识时间格式

非标准时间格式:20200101可转换
可识别时间格式:2020-12-12、2020-12-12 12:12:12、1577836800
说下时间戳:1577836800,表示1970年1月1日开始过去了多少秒

2. 时间格式转换

2.1 unix_timestamp 和from_unixtime

select '20200131' as `1`,
      unix_timestamp('20200131','yyyyMMdd') as `2`,
      from_unixtime(unix_timestamp('20200131','yyyyMMdd'),'yyyy-MM-dd') as `3`

select '2020-01-31' as `1`,
      unix_timestamp('2020-01-31','yyyy-MM-dd') as `2`,
      from_unixtime(unix_timestamp('2020-01-31','yyyy-MM-dd'),'yyyyMMdd') as `3`

2.2 2020-01-31 12:12:12 to 2020-01-31

select to_date('2020-01-31 12:12:12','yyyy-MM-dd')

2.3 2020-01-31 12:12:12 to 2020-01

select date_format(date '2020-01-31 12:12:12', "yyyy-LL")

2.4 2020-01-31 12:12:12 to 01 OR 1

select date_format(date '2020-01-31 12:12:12', "LL")
# 'M' or 'L': Month number in a year starting from 1

select date_format(date '2020-01-31 12:12:12', "L")

3. 时间计算

计算天数之差:
datediff(endDate, startDate) - Returns the number of days from startDate to endDate
计算月数之差:

months_between(timestamp1, timestamp2[, roundOff]) - If timestamp1 is later than timestamp2, then the result is positive. If timestamp1 and timestamp2 are on the same day of month, or both are the last day of month, time of day will be ignored. Otherwise, the difference is calculated based on 31 days per month, and rounded to 8 digits unless roundOff=false.
如果日期是同一天或者都处于月末,那么按照整月算,否则月 按照31天一个月来计算。

SELECT months_between('2023-02-28 10:30:00', '2023-03-31')


计算秒数之差:

select unix_timestamp('2020-01-31 12:21:12')-unix_timestamp('2020-01-31 12:12:12')


几个月前,几个月后:

select add_months('2020-01-31',1)

几天前:

select date_add('2020-02-29',7)

标签:12,函数,31,01,2020,SQL,date,时间,select
From: https://www.cnblogs.com/tian1022/p/17310151.html

相关文章

  • 再次思考一下Mysql的ACID
    ACID是事务的特性:一致性、原子性、隔离性、持久性。(记不住的话,一原隔持-依然搁置)事务本身来讲,是具有强隔离性的。即一个事务想访问另外一个事务正在访问的数据,需要排队。这种强隔离性,导致并发情况下的访问性能受限。但是有的时候,隔离性弱一点,可以带来的并发访问性能的提升,这个正......
  • SQL concat_ws, collect_set, 和explode合并使用
    1.背景有一个这样的数据集:字段和字段的值是两列目的是将这个数据转换成规整的一个特征是一列的数据:2.做法第一步:先造出列selectucid,CASEWHENtype='性别'THENlabelend`性别`,CASEWHENtype='产品'THENlabelend`产品`,CASEWHENtype='还款表现'THEN......
  • SQL Server 配置管理器打不开提示错误
    ---------------------------SQLServer配置管理器---------------------------无法连接到WMI提供程序。您没有权限或者该服务器无法访问。请注意,您只能使用SQLServer配置管理器来管理SQLServer2005和更高版本的服务器。无效类[0x80041010]解决“无法连接到WMI提供......
  • mysql——date_format(),str_to_date()函数
    date_format():类似python中的strftime: 将给定格式的日期时间对象转换为字符串。日期时间对象=>字符串,控制输出格式selectdate_format(datetime的字段,‘%Y-%m-%d’)括号中前面是你要格式化的字段,后面是具体要格式化成什么样式。 str_to_date():类似python中的strptime:将字......
  • keycloak~时间不正确的问题
    首先我们应该知道,写到数据库里的时间,主要和你的mysql时区system_time_zone有关,而把mysql里的数据取出来,以json形式响应到浏览器上,这个时间会经过反序列化的过程,这时时间和注解@JsonFormat(pattern="yyyy-MM-ddHH:mm:ss",timezone="GMT+8")有关。和我们相关的几个时区UTC:......
  • MySQL安装与配置
    一,安装MySQL(绿色解压版)打开下载链接,选择版本5.7.24下载自己电脑对应的位数MySQL::DownloadMySQLCommunityServer(ArchivedVersions)---MySQL::下载MySQLCommunityServer(存档版本) 二,解压MySQL下载的文件到D盘 三,环境变量打开资源管理器右击此电脑点击......
  • js-函数记忆
    函数记忆:指将上次的(计算结果)缓存起来,当下次调用时,如果遇到相同的(参数),就直接返回(缓存中的数据)。实现原理:将参数和对应的结果保存在对象中,再次调用时,判断对象key是否存在,存在返回缓存的值。functionmemorize(){constcache={};returnfunction(){constkey=A......
  • MySQL 中的 distinct 和 group by 哪个效率更高?
     1、distinct用法 语法:SELECTDISTINCTcolumnsFROMtable_nameWHEREwhere_conditions;举例:   多列去重:distinct多列的去重,则是根据指定的去重的列信息来进行,即只有所有指定的列信息都相同,才会被认为是重复的信息。    2、groupby用法  语法:SE......
  • mysql连接数查看
    12showvariableslike '%max_connection%';查看最大连接数set globalmax_connections=1000;       重新设置最大连接数1234567891011mysql>showstatuslike  'Threads%';+-------------------+-------+|Variable_name  ......
  • 【230412-1】已知:f(2x+1)=4x^2-6x+5 求:f(3) 三种方法做一初中函数题
    ......