gpt4 book ai didi

sql - 格式化日期中的 If/Then/Else

转载 作者:行者123 更新时间:2023-12-03 23:15:27 25 4
gpt4 key购买 nike

我正在尝试从发票日期中获取会计期间和年份。将 month() 函数与 Case 一起使用,我可以获得时间段。因为第 1 期是在 11 月,所以我需要在这是真的那一年做一个 +1 1

我现在可以将 IF 函数与日期函数一起使用。

我的查询是

Select a.OrderAccount 
,a.InvoiceAccount
,a.InvoiceDate
,year(a.InvoiceDate) as Year
,month(a.InvoiceDate) as Month,
Case month(a.InvoiceDate)
WHEN '11' THEN '1' -- increase year by +1
WHEN '12' THEN '2'-- increase year by +1
WHEN '1' THEN '3'
WHEN '2' THEN '4'
WHEN '3' THEN '5'

如有任何建议,我们将不胜感激。谢谢

最佳答案

使用DATEADD只需在原始日期基础上增加 2 个月:

MONTH(DATEADD(month,2,a.InvoiceDate)) as FiscalMonth,
YEAR(DATEADD(month,2,a.InvoiceDate)) AS FiscalYear,

关于sql - 格式化日期中的 If/Then/Else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45599964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com