简体   繁体   English

如何从 Db2 中的日期列生成 FY

[英]How to Generate FY from Date Column In Db2

I have one Column In my Db2 table in which I have data in 'YYYYMMDD' format.我的 Db2 表中有一个列,其中有“YYYYMMDD”格式的数据。 I need to Create Financial Year from this column,so If month is in Jan,Feb or March then in Fy I need to generate as Prev year '-' current year else current year '-' next Year.我需要从这个列创建财政年度,所以如果月份是在 1 月、2 月或 3 月,那么在 Fy 我需要生成为 Prev year '-' current year else current year '-' next year。

In My source Table I have MTH_YR column as '20210101'.I am Able to generate date from this column as 2021-01-01 now from this column i need to genarate Fy as 2020-2021 I am writing below Query在我的源表中,我的 MTH_YR 列为“20210101”。我能够从该列生成日期为 2021-01-01 现在从该列中我需要将 Fy 生成为 2020-2021 我在查询下面写

    select substring(MTH_YR, 1, 4) || '-' || substring(MTH_YR, 5, 2) || '-' || 
    substring(MTH_YR, 7, 2)  as date_format,
    CASE WHEN month(to_date(date_format)) IN (01,02,03) THEN 
    CONCAT(CONCAT(year(to_date(date_format))-1,'-'),substr(year(to_date(date_format)),3,4)) 
    ELSE 
    CONCAT(CONCAT(year(to_date(date_format)),'-'),SUBSTR(year(to_date(date_format))+1,3,4))
    end as FY from schemaname.Tablename;

But Getting Below Error.但低于错误。

    "FY" is not valid in the context where it is used.. SQLCODE=-206, SQLSTATE=42703, DRIVER=4.24.92

Can someone Please Guide me How should I Implement the same.有人可以指导我我应该如何实施。

If you stored your date as a date , you would simply subtract 3 months and take the year:如果您将日期存储为date ,您只需减去 3 个月并取年:

select year(add_months(date_col, -3)) as fiscal_year

You can make this a string representation if you prefer:如果您愿意,可以将其设为字符串表示形式:

select year(add_months(date_col, -3)) || '-' || (year(add_months(date_col, -3)) + 1)

If your date is not stored as a date, you should fix the data .如果您的日期未存储为日期,则应修复数据 But absent that, you can convert it in the expression.但如果没有,您可以在表达式中转换它。

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

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