getdate函数

There are 61 Date Functions defined in MySQL. Don’t worry, we won’t review them all here. This guide will give you an introduction to some of the common ones, and enough exposure for you to comfortably to explore on your own.

MySQL中定义了61种日期函数。 不用担心,我们不会在这里全部审查。 本指南将向您介绍一些常见的知识,并为您提供足够的机会让您轻松地自行探索。

We will cover:

我们将介绍:

  • Getting the current date获取当前日期
  • Date Math日期数学
  • Dates in a where or having clause在where或having子句中的日期

获取当前日期 (Getting the current date)

Getting the date from the system can be very handy for processing data using SQL.

从系统获取日期对于使用SQL处理数据非常方便。

-- current date
select now(), sysdate(), current_date(), current_time(), -- date and time from the system on execution
dayofyear(now()) as NumDaysSoFarThisYr,
EXTRACT(YEAR FROM now()) as theYearPart,
EXTRACT(YEAR_MONTH FROM now()) as theYrMonPart,
date_format(now(), '%W %M %Y') as oneOfManyFormats;
;

In SQL query, we see the following:

在SQL查询中,我们看到以下内容:

  • The first two columns in the result are two ways to get the same information: the date on the system at the time the SQL is executed.结果的前两列是获得相同信息的两种方法:执行SQL时系统上的日期。
  • The next two columns slice out just the Date and Time parts of the system date.接下来的两列仅切出系统日期的日期和时间部分。
  • The next one presents the “day number” of the system date in this year. You’ll notice that this is one day more than the math shown in the next example.下一个表示今年系统日期的“天数”。 您会注意到,这比下一个示例中显示的数学要多一天。
  • The next two extract just the year and then both the year and month接下来的两个仅提取年份,然后提取年份和月份
  • Last, but not least, there is a single example of one of the many ways to format this dates.最后但并非最不重要的是,存在一个格式化此日期的多种方法之一的单个示例。

You can also use GETDATE() to get the current date.

您也可以使用GETDATE()获取当前日期。

日期数学 (Date Math)

select now(), current_date(),
datediff(now(),'2017-01-01') as daysThisYear,
subdate(current_date(), interval 150 day) as '150DaysAgo',
adddate(now(), interval 7 day) as dateInA_Week -- date in a week
;

Here we see:

在这里我们看到:

  • The first two columns are just the system date and time for reference.前两列只是系统日期和时间供参考。
  • The second column is the date difference (datediff) between the first of January 2017 and the system date.第二列是2017年1月1日与系统日期之间的日期差(datediff)。
  • The last two columns are examples of subtracting and adding dates.最后两列是减去和添加日期的示例。

在where或having子句中 (In a where or having clause)

Here are two examples of using date math in a where clause:

这是在where子句中使用日期数学的两个示例:

select * from student; - to show the current data being used for the example
select * from student where recordCreated < '2017-01-01';
select * from student where recordCreated < subdate(current_date(), interval 225 day);

Regarding the HAVING part: Keep in mind, most of the WHERE clause logic will also work in the HAVING clause of a GROUP BY. The difference between the two is that the WHERE clause runs against the full data, and the HAVING runs against the data aggregated by the GROUP BY clause.

关于HAVING部分:请记住,大多数WHERE子句逻辑也可以在GROUP BY的HAVING子句中使用。 两者之间的区别在于WHERE子句针对完整数据运行,而HAVING子句针对GROUP BY子句聚合的数据运行。

As with all of these things there is MUCH MORE to them than what’s in this introductory guide. I hope this at least gives you enough to get started. Please see the manual for your database manager and have fun trying different options yourself.

与所有这些内容一样,它们比本入门指南中的内容要多得多。 我希望这至少能给您足够的入门。 请参阅数据库管理员的手册,并尝试自己尝试其他选项,这很有趣。

翻译自: https://www.freecodecamp.org/news/sql-date-functions-getdate/

getdate函数

getdate函数_SQL日期函数和GETDATE解释为带有语法示例相关推荐

  1. hive 强转为string_Hive的条件函数与日期函数全面汇总解析

    点击上方蓝字  关注我们 在Hive的开窗函数实战的文章中,主要介绍了Hive的分析函数的基本使用.本文是这篇文章的延续,涵盖了Hive所有的条件函数和日期函数,对于每个函数,本文都给出了具体的解释和 ...

  2. Hive常用函数(日期函数,取整函数,字符串操作函数,集合操作函数)

    常用函数 常用日期函数 常用取整函数 常用字符串操作函数 集合操作函数 多维分析 常用日期函数 unix_timestamp:返回当前或指定时间的时间戳 select unix_timestamp() ...

  3. 【函数】Oracle函数系列(2)--数学函数及日期函数

    [函数]Oracle函数系列(2)--数学函数及日期函数 [函数]Oracle函数系列(2)--数学函数及日期函数 1  BLOG文档结构图 2  前言部分 2.1  导读和注意事项 各位技术爱好者, ...

  4. oracle--day2(单值函数(字符函数,日期函数,转换函数,数字函数),日期格式(yyyy,mm等含义),表示一个日期数据的4种方式,多表查询(连接查询(等值连接,不等值连接,外连接,自连接))

    第三章:单值函数     函数分为:       1.单值函数           1.字符函数           2.日期函数           3.转换函数           4.数字函数 ...

  5. oracle中的循环函数,Oracle日期函数和循环总结

    一,日期相关的函数 Select to_char(sysdate,'Q') from dual;--指定日期的季度 Select to_char(sysdate,'MM') from dual;--月 ...

  6. java datediff函数_sql DATEDIFF 函数

    sql  DATEDIFF 函数 今天的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=0 昨天的所有数据:select ...

  7. sql用于字符串的聚合函数_SQL字符串函数用于数据整理(争用)

    sql用于字符串的聚合函数 In this article, you'll learn the tips for getting started using SQL string functions ...

  8. c语言 时间函数,C语言函数—时间日期函数

    时间日期函数 函数库为time.h.dos.h 在时间日期函数里,主要用到的结构有以下几个: 总时间日期贮存结构tm ┌──────────────────────┐ │struct tm │ │{ ...

  9. 减一天 日期函数_excel日期函数:如何计算项目的开始和完成日期

    编按:哈喽,大家好!在上一篇文章中,我们说到了EDATE.DATEDIF.EOMONTH.WEEKDAY等日期函数,相信大家对于excel中的日期计算,已经有了一个大致的了解,今天我们继续上篇内容,为 ...

最新文章

  1. 华为:HarmonyOS 即将开源!
  2. Alexa偷录私密对话并发送,继诡笑之后出现又一神举动
  3. Ubuntu16.04 使用sudo cat EOF 编辑文件,提示Permission denied错误的解决办法
  4. 有了 serverless,前端也可以快速开发一个 Puppeteer 网页截图服务
  5. 从内涵段子到皮皮虾,娱乐App为何不能一鱼两吃?
  6. spring控制事务:声明式事务(XML)事务的传播行为
  7. 【轉】JAVA中isEmpty和null以及的区别
  8. Swift - 获取、改变按钮的标题文本(UIButton点击切换title)
  9. python模块化编程_Python模块化编程
  10. vue.js computedmethod
  11. MyEclipse Web开发教程:XML XML架构(一)
  12. 盈建科弹性板6计算_盈建科(300935):国内建筑结构设计软件行业的领先企业...
  13. 虚拟化发展历程及原理
  14. 为SQL Server 增加链接到SQL Server 的链接服务器
  15. Flask-MDict搭建在线Mdict词典服务
  16. java jbutton数组_java-JButton需要显示图像数组
  17. 百人研发团队的难题:研发管理、绩效考核、组织文化和OKR
  18. build-up to Ajax v,to build up是什么意思
  19. 692. Top K Frequent Words
  20. 中秋节到了我给大家用python做一个月饼

热门文章

  1. java 判断object类型_Java学习-方法与多态的学习心得
  2. scala定义抽象类与抽象字段
  3. 设置RGBColor
  4. 后端返回的数据中换行符 html换行
  5. 【转】解密“设计模式”
  6. 普华永道重磅报告:决定未来的八大核心科技
  7. 虚方法的调用是怎么实现的(单继承VS多继承)
  8. 在linux系统下实现音视频即时通讯的部分代码
  9. 一个苹果证书供多台电脑开发使用——导出p12文件
  10. 旋转卡壳——模板(对踵点)