sql date 函数_什么是SQL DATE()函数?

sql date 函数

Hey, folks! In this article, we will be focusing on SQL DATE() function in detail. So, let us start learning!.

嘿伙计! 在本文中,我们将重点关注SQL DATE()函数 。 因此,让我们开始学习!。



什么是SQL DATE()函数? (What is the SQL DATE() function?)

SQL DATE() function enables us to access the date value from the particular historical or current timestamp values.

SQL DATE() function使我们能够从特定的历史或当前时间戳值访问日期值。

Let us consider an example to understand the same.

让我们考虑一个例子来理解它。

Consider a small scale company, wherein a record of the attendance of the employees is being maintained in the form of a database. If the supervisor wishes to check for the days on which a particular employee is absent, the developer can run the DATE() function on the attendance database and help the supervisor with the date values of the same.

考虑一个小型公司,其中以数据库的形式维护员工出勤的记录。 如果主管希望检查特定员工缺席的日期,则开发人员可以在出勤数据库上运行DATE()函数,并为主管提供日期信息。

So, we can say that the DATE() function helps us extract the date values from the provided datetime expression.

因此,可以说DATE()函数有助于我们从提供的datetime表达式中提取日期值。

Let us now understand the structure of DATE() function.

现在让我们了解DATE()函数的结构。



DATE()函数的语法 (Syntax of DATE() function)

The Date() function returns the date extracted from the passed datetime expression to it.

Date()函数返回从传递的datetime表达式中提取的日期。


DATE(datetime expression)

The function returns NULL, if the passed argument is not a date or datetime expression.

如果传递的参数不是日期或日期时间表达式,则该函数返回NULL。

Having understood the working of DATE() function, let us now implement the same through some examples.

了解了DATE()函数的工作原理后,现在让我们通过一些示例来实现相同的功能。



通过示例实现DATE()函数 (Implementing DATE() function through examples)

In the below example, we have passed a datetime expression. The DATE() function extracts the date value from the expression.

在下面的示例中,我们传递了一个datetime表达式。 DATE()函数从表达式中提取日期值。


SELECT DATE("2020-06-07 11:43:21");

Output:

输出:


2020-06-06T22:00:00.000Z

In this example, we have violated the standard format of the datetime expression and thus the function returns NULL.

在此示例中,我们违反了datetime表达式的标准格式,因此该函数返回NULL。


SELECT DATE(" DATE: 2020-06-07 11:43:21");

Output:

输出:


NULL

In this function, we can use the SQL NOW() function to fetch the current datetime expression from the system. Further, we have applied DATE() function to extract the date part from it.

在此函数中,我们可以使用SQL NOW() function从系统中获取当前的日期时间表达式。 此外,我们已应用DATE()函数从中提取日期部分。


SELECT DATE(NOW());

Output:

输出:


2020-07-12T22:00:00.000Z

Now, we have created a table ‘Info’ using SQL Create command and have inserted a record in the table using Insert command.

现在,我们使用SQL Create命令创建了一个表'Info',并使用Insert命令在该表中插入了一条记录。


create table Info(id integer, Name varchar(200), Attendance DATE);
insert into Info(id, Name,Attendance) values(1, "Pune", '2020-03-03 12:32:23');
select * from Info;

Output:

输出:

SQL DATE() Function--Table Info
SQL DATE() Function–Table Info SQL DATE()函数-表信息

From the above data, we use the DATE() function to extract the date part from the datetime expression present in the table column.

从以上数据中,我们使用DATE()函数从表列中存在的datetime表达式中提取日期部分。


Select Name, DATE(Attendance) from Info;

Output:

输出:

SQL DATE() Function Example
SQL DATE() Function Example SQL DATE()函数示例


结论 (Conclusion)

By this, we have come to the end of this topic. Feel free to comment below in case you come across any questions. Till then, Happy Learning!

至此,我们到了本主题的结尾。 如果您遇到任何问题,请在下面发表评论。 到那时,祝您学习愉快!



参考资料 (References)

翻译自: https://www.journaldev.com/42411/sql-date-function

sql date 函数

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQL中,date_diff和date_sub函数都是用来处理日期的函数date_diff函数用于计算两个日期之间的差异,返回的是日期之间的天数差。可以通过指定日期的格式,从完整的日期和时间到仅日期,来计算日期之间的差异。例如,使用date_diff函数计算两个日期之间的天数差可以使用以下语法: datediff('2020-04-18 00:00:00', '2019-11-21 00:00:00'); 这将返回两个日期之间的天数差。 而date_sub函数用于从指定日期中减去指定的时间间隔。可以使用date_sub函数来获取某个日期的前一个月的第一天。例如,使用date_sub函数获取当前日期的前一个月的第一天可以使用以下语法: select date_sub(current_date, dayofmonth(current_date)-1); 这将返回当前日期的前一个月的第一天。 另外,如果要获取下一个月的第一天,可以使用add_months函数结合date_sub函数。以下是一个示例: select add_months(date_sub(current_date, dayofmonth(current_date)-1), 1); 这将返回下一个月的第一天。 总结来说,date_diff函数用于计算日期之间的差异,而date_sub函数用于从指定日期中减去指定的时间间隔。两个函数都在SQL中用于处理日期。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Hive的时间函数](https://blog.csdn.net/zhang5324496/article/details/121407420)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值