什么是SQL Server GETDATE()函数?

Hey, readers! In our previous article, we had understood the working of SQL Server DATEPART() function. In this article, we will be focusing on SQL Server GETDATE() function.

嘿,读者们! 在上一篇文章中,我们了解了SQL Server DATEPART()函数的工作原理 。 在本文中,我们将重点介绍SQL Server GETDATE()函数



SQL Server GETDATE()函数的工作 (Working of SQL Server GETDATE() function)

SQL GETDATE()function is used to fetch the current date and time from the system.

SQL GETDATE()函数用于从系统获取当前日期和时间

The GETDATE() function renders the below format while representing the current timestamp i.e. the current date and time.

GETDATE()函数在表示当前时间戳(即当前日期和时间)时呈现以下格式。

Having understood the working of SQL GETDATE() function, lets jump into understanding the syntax of the same.

了解了SQL GETDATE()函数的工作原理之后,我们就可以开始理解其语法。



SQL Server GETDATE()函数的语法 (Syntax of SQL Server GETDATE() function)

Unlike other SQL Date functions, the GETDATE() function does not accept any parameter. It does return the current date and time in the below format — YYYY-MM-DD hh:mm:ss.mmm.

与其他SQL Date函数不同,GETDATE()函数不接受任何参数。 它会以以下格式返回当前日期和时间— YYYY-MM-DD hh:mm:ss.mmm

Syntax:

句法:


GETDATE()

Now, let us understand the implementation of this function through various examples in the below section.

现在,让我们通过下一节中的各种示例来了解此功能的实现。



SQL Server GETDATE()函数的示例 (Examples of SQL Server GETDATE() function)

In the below example, we have used SQL SELECT statement along with GETDATE() function to display the current date and time.

在下面的示例中,我们使用了SQL SELECT statement以及GETDATE()函数来显示当前日期和时间。

Example 1:

范例1:


select GETDATE();

Output:

输出:


2020-06-05T15:14:39.967Z

Example 2:

范例2:

In this example, we have used SQL Server DATEPART() function wherein we have passed the interval to be extracted as ‘month‘ from the input date as the current date obtained through GETDATE() function as a parameter to the DATEDIFF() function.

在此示例中,我们使用了SQL Server DATEPART() function其中传递了要从输入日期中提取为“ month ”的间隔作为通过GETDATE() function获得的当前日期,作为DATEDIFF()函数的参数。


SELECT DATEPART(month, GETDATE()) AS Output;

Output:

输出:


6


GETDATE()v / s CURRENT_TIMESTAMP() (GETDATE() v/s CURRENT_TIMESTAMP())

While understanding the SQL Server Date Functions, we may come across the below question.

在理解SQL Server日期函数的同时,我们可能会遇到以下问题。

What’s the difference between GETDATE() function and CURRENT_TIMESTAMP() function, if both the functions return exactly the same value!?

如果两个函数返回的值完全相同,那么GETDATE()函数和CURRENT_TIMESTAMP()函数有什么区别?

Well, both functions have the same implementation. The only basic difference spotted is the origin of the functions i.e. CURRENT_TIMESTAMP() is an ANSI SQL function while GETDATE() is a T-SQL function.

嗯,这两个功能具有相同的实现。 发现的唯一基本区别是函数的来源,即CURRENT_TIMESTAMP()是ANSI SQL函数,而GETDATE()是T-SQL函数。



结论 (Conclusion)

By this, we have come to the end of this topic. Please feel free to comment below if you come across any doubts.

至此,我们到了本主题的结尾。 如果您有任何疑问,请随时在下面发表评论。

For more such posts on similar topics related to SQL Server, please do visit SQL Server JournalDev.

有关与SQL Server相关的类似主题的更多此类帖子,请访问SQL Server JournalDev



参考资料 (References)

翻译自: https://www.journaldev.com/41024/sql-server-getdate

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值