用于CASE的SSIS派生列表达式

时间:2014-07-25 15:36:51

标签: sql tsql sql-server-2012 ssis

我有以下SQL代码,我试图转换为SSIS 2012派生列表达式:

QUATER= 
        CASE
            WHEN DATEPART(M, @BD) BETWEEN 1 AND 3 THEN 'Q1'
            WHEN DATEPART(M, @BD) BETWEEN 4 AND 6 THEN 'Q2'
            ELSE 'OTHER'
        END

My Expression看起来像这样,并且有效:

DATEPART("M", @[User::BD]) == 1 ? "Q1":
(DATEPART("M", @[User::BD]) == 2 ? "Q1":
(DATEPART("M", @[User::BD]) == 3 ? "Q1":
(DATEPART("M", @[User::BD]) == 4 ? "Q2":
(DATEPART("M", @[User::BD]) == 5 ? "Q2":
(DATEPART("M", @[User::BD]) == 6 ? "Q2" : "OTHER"
)))))

有更优雅的方法吗?

1 个答案:

答案 0 :(得分:0)

不是更令人兴奋,但这应该是等价的:

DATEPART("M", @[User::BD]) <= 3 ? "Q1":
(DATEPART("M", @[User::BD]) <= 6 ? "Q2" : "OTHER"
)

同样,您可以使用季度而不是月份:

DATEPART("q", @[User::BD]) <= 2 ? "Q"+DATEPART("q", @[User::BD]) : "OTHER"

无法测试字符串连接,您可能需要将DATEPART()输出转换为文本。