简体   繁体   English

ssis 使用脚本组件添加数据库的年份日期

[英]ssis adding year to date of database with script component

i have a database with no year, only day and month (example 20-12).我有一个没有年份、只有日期和月份的数据库(例如 20-12)。 because of this, the database adds the year automatically.因此,数据库会自动添加年份。 The problem with this is, that in some cases the year is ahead of time, for example 20-12-2020, while it's actually 2019.这样做的问题是,在某些情况下,这一年会提前,例如 20-12-2020,而实际上是 2019 年。

example of the table:表格示例:

order_number date_of_order (original table) order_number date_of_order(原始表)
1 20-12 1 20-12

how it looks when i add the database in ssms:当我在 ssms 中添加数据库时它的外观:

order_number date_of_order 1 20-12-2020 order_number date_of_order 1 20-12-2020

What i want to do is add the right years to the day and month with scripts component.我想要做的是使用脚本组件将正确的年份添加到日期和月份。 for example if the date is in the past, then make it 2019. If the date is in the future make it 2020.例如,如果日期是过去,则设为 2019。如果日期在未来,则设为 2020。

脚本组件错误

This will give you a path to move forward.这将为您提供前进的道路。 You just need to separate the column into month and day numbers:您只需要将列分成月份和日期:

        //This will split out your date/month
        string OrigCol = "20-12"; //Replace this with Rows.YourColumnName
        string[] col = OrigCol.Split('-');
        int d = Int32.Parse(col[0]);
        int m = Int32.Parse(col[1]);

        DateTime dt = new DateTime(DateTime.Now.Year, m, d);
        if (dt > DateTime.Now)
            dt = dt.AddYears(-1);

        //Set your new column to dt

This code will create a date with current year, and only subtract a year if the result is in the future.此代码将创建一个带有当前年份的日期,如果结果是未来,则仅减去一年。

If your column is already a date (with a 2020 default) you can jump straight into the if.如果您的列已经是日期(默认为 2020),您可以直接跳到 if。

        DateTime dt = Row.YourColumnName;
        if (dt > DateTime.Now)
            dt = dt.AddYears(-1);

        //Set your new column to dt

If you are already starting with a date then you can just use a derived column to do your transformation:如果您已经从日期开始,那么您可以使用派生列进行转换:

Use this as formula similar to the c# above:将此用作类似于上述 c# 的公式:

OrderDate > GETDATE() ? DATEADD("yy",-1,OrderDate) : OrderDate

在此处输入图像描述

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

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