简体   繁体   English

如何使用 Azure SQL 获取上个月?

[英]How to get previous month with Azure SQL?

I'm starting on azure SQL and I want to convert a string into a date and then retrieve the two months before that date.我从 azure SQL 开始,我想将字符串转换为日期,然后检索该日期之前的两个月。

I tried to use DATEADD(month,-2,sysdatetime()) to get the previous two months but dateadd doesn't work.我尝试使用 DATEADD(month,-2,sysdatetime()) 来获取前两个月,但 dateadd 不起作用。 "This function is neither a registered temporary function nor a permanent function registered in the database" “这个 function 既不是注册的临时 function 也不是注册在数据库中的永久 function”

Thank you for your help谢谢您的帮助

The DATEADD() function is supported in Azure SQL database: Azure SQL 数据库支持DATEADD() function: 在此处输入图像描述

I tested it in my Azure SQL database and it works well:我在我的 Azure SQL 数据库中对其进行了测试,它运行良好:

DECLARE @datetime2 datetime2 = sysdatetime();    
SELECT DATEADD(month,-2,@datetime2) as month

在此处输入图像描述

The point is what's the date string look like or where you call the function.关键是日期字符串是什么样的,或者您在哪里调用 function。 Maybe you can share us your whole SQL statements也许您可以与我们分享您的整个 SQL 声明

Here's the official example:这是官方的例子:

DECLARE @datetime2 datetime2 = '2007-01-01 13:10:10.1111111';
SELECT 'month',DATEADD(month,1,@datetime2)    

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM