【问题标题】:How to get the date of the beginning of the month in SSIS Script Task?如何在 SSIS 脚本任务中获取月初的日期?
【发布时间】:2017-03-18 13:13:21
【问题描述】:

我正在使用 SSIS,并且我有一个脚本任务,如果今天的日期是月初,我想更改变量的值。

所以我想将布尔变量 startDate 的值更改为 TRUE,如果它是任务运行的月初,否则为 FALSE。

基本上我缺少此 SQL 语句的 SSIS 脚本版本:

SELECT DATEADD(month, DATEDIFF(month, 0, @mydate), 0) AS StartOfMonth

到目前为止,这是我的脚本:

        public void Main()
    {
        if (DateTime.Today = ?  )
        {
            Dts.Variables["User::startDate"].Value = True;
        }
            Dts.Variables["User::startDate"].Value = False;

        Dts.TaskResult = (int)ScriptResults.Success;
    }
}

回答:

    public void Main()
{

DateTime value = new DateTime(DateTime.Today.Year,DateTime.Today.Month,1);

if (DateTime.Today == value )
{
    Dts.Variables["User::startDate"].Value = bool.Parse("True");
}
    Dts.Variables["User::startDate"].Value = bool.Parse("False");

Dts.TaskResult = (int)ScriptResults.Success;
}
}

【问题讨论】:

    标签: sql ssis bids


    【解决方案1】:

    只需使用:

    public void Main()
    {
    
      DateTime value = new DateTime(DateTime.Today.Year,DateTime.Today.Month,1);
    
        if (DateTime.Today == value )
        {
            Dts.Variables["User::startDate"].Value = bool.Parse("True");
        }
            Dts.Variables["User::startDate"].Value = bool.Parse("False");
    
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    }
    

    【讨论】:

    • 感谢您的回复。我收到一个错误,变量的数据类型是布尔值,但传入的值是字符串。删除引号并没有解决这个问题。
    • 尝试将其作为位 0 或 1 传递
    • 记住变量名是区分大小写的
    • 我尝试了这两种方法并得到了这个错误:分配给变量“User::startDate”的值的类型与当前的变量类型不同。变量在执行期间可能不会改变类型
    • 尝试 (bool)"True" 或 (bool)1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2020-11-16
    相关资源
    最近更新 更多