如何将字符串与空值连接起来

how to concatenate strings with nulls

我们如何连接可能包含空值的字符串?

我收到以下错误:

...尝试执行时:

@blocks_merged = 
SELECT
AN_52_ENC_CSN_ID AS an_53_enc_csn_id,
string.IsNullOrEmpty(ARTERIAL_LINE_BLOCK_START_TIME) ? ("ARTERIAL_LINE_BLOCK_START_TIME: " + ARTERIAL_LINE_BLOCK_START_TIME) :""
+string.IsNullOrEmpty(BLOCK_PERIPHERAL_BLOCK_START_TIME) ? ("BLOCK_PERIPHERAL_BLOCK_START_TIME: " + BLOCK_PERIPHERAL_BLOCK_START_TIME) :""
+string.IsNullOrEmpty(BLOCK_SPINAL_BLOCK_START_TIME) ? ("BLOCK_SPINAL_BLOCK_START_TIME: " + BLOCK_SPINAL_BLOCK_START_TIME) :""
+string.IsNullOrEmpty(CENTRAL_LINE_BLOCK_START_TIME) ? ("CENTRAL_LINE_BLOCK_START_TIME: " + CENTRAL_LINE_BLOCK_START_TIME) :""
+string.IsNullOrEmpty(CSE_BLOCK_START_TIME) ? ("CSE_BLOCK_START_TIME: " + CSE_BLOCK_START_TIME) :""
+string.IsNullOrEmpty(SPINAL_DRAIN_BLOCK_START_TIME) ? ("SPINAL_DRAIN_BLOCK_START_TIME: " + SPINAL_DRAIN_BLOCK_START_TIME) :""
+string.IsNullOrEmpty(TEE_BLOCK_START_TIME) ? ("TEE_BLOCK_START_TIME: " + TEE_BLOCK_START_TIME) :""
+string.IsNullOrEmpty(TRANSVENOUS_PACING_WIRE_BLOCK_START_TIME) ? ("TRANSVENOUS_PACING_WIRE_BLOCK_START_TIME: " + TRANSVENOUS_PACING_WIRE_BLOCK_START_TIME) :"" AS BLOCK_START,

string.IsNullOrEmpty(ARTERIAL_LINE_BLOCK_END_TIME) ? ("ARTERIAL_LINE_BLOCK_END_TIME: " + ARTERIAL_LINE_BLOCK_END_TIME) :""
+string.IsNullOrEmpty(BLOCK_PERIPHERAL_BLOCK_END_TIME) ? ("BLOCK_PERIPHERAL_BLOCK_END_TIME: " + BLOCK_PERIPHERAL_BLOCK_END_TIME) :""
+string.IsNullOrEmpty(BLOCK_SPINAL_BLOCK_END_TIME) ? ("BLOCK_SPINAL_BLOCK_END_TIME: " + BLOCK_SPINAL_BLOCK_END_TIME) :""
+string.IsNullOrEmpty(CENTRAL_LINE_BLOCK_END_TIME) ? ("CENTRAL_LINE_BLOCK_END_TIME: " + CENTRAL_LINE_BLOCK_END_TIME) :""
+string.IsNullOrEmpty(CSE_BLOCK_END_TIME) ? ("CSE_BLOCK_END_TIME: " + CSE_BLOCK_END_TIME) :""
+string.IsNullOrEmpty(SPINAL_DRAIN_BLOCK_END_TIME) ? ("SPINAL_DRAIN_BLOCK_END_TIME: " + SPINAL_DRAIN_BLOCK_END_TIME) :""
+string.IsNullOrEmpty(TEE_BLOCK_END_TIME) ? ("TEE_BLOCK_END_TIME: " + TEE_BLOCK_END_TIME) :""
+string.IsNullOrEmpty(TRANSVENOUS_PACING_WIRE_BLOCK_END_TIME) ? ("TRANSVENOUS_PACING_WIRE_BLOCK_END_TIME: " + TRANSVENOUS_PACING_WIRE_BLOCK_END_TIME) :"" AS BLOCK_END
FROM @blocks;

我们如何连接可能包含空值的字符串?

该代码试图将第一个字符串与您的三元运算符列表的下一个比较连接起来。基本上不是评估三元运算符,而是首先连接它的布尔表达式。

解决方案:在脚本中添加额外的括号:

@blocks_merged =
SELECT
    AN_52_ENC_CSN_ID AS an_53_enc_csn_id,
    (
        (string.IsNullOrEmpty(ARTERIAL_LINE_BLOCK_START_TIME) ? ("ARTERIAL_LINE_BLOCK_START_TIME: " + ARTERIAL_LINE_BLOCK_START_TIME) : "")
        +(string.IsNullOrEmpty(BLOCK_PERIPHERAL_BLOCK_START_TIME) ? ("BLOCK_PERIPHERAL_BLOCK_START_TIME: " + BLOCK_PERIPHERAL_BLOCK_START_TIME) : "")
        +(string.IsNullOrEmpty(BLOCK_SPINAL_BLOCK_START_TIME) ? ("BLOCK_SPINAL_BLOCK_START_TIME: " + BLOCK_SPINAL_BLOCK_START_TIME) : "")
        +(string.IsNullOrEmpty(CENTRAL_LINE_BLOCK_START_TIME) ? ("CENTRAL_LINE_BLOCK_START_TIME: " + CENTRAL_LINE_BLOCK_START_TIME) : "")
        +(string.IsNullOrEmpty(CSE_BLOCK_START_TIME) ? ("CSE_BLOCK_START_TIME: " + CSE_BLOCK_START_TIME) : "")
        +(string.IsNullOrEmpty(SPINAL_DRAIN_BLOCK_START_TIME) ? ("SPINAL_DRAIN_BLOCK_START_TIME: " + SPINAL_DRAIN_BLOCK_START_TIME) : "")
        +(string.IsNullOrEmpty(TEE_BLOCK_START_TIME) ? ("TEE_BLOCK_START_TIME: " + TEE_BLOCK_START_TIME) : "")
        +(string.IsNullOrEmpty(TRANSVENOUS_PACING_WIRE_BLOCK_START_TIME) ? ("TRANSVENOUS_PACING_WIRE_BLOCK_START_TIME: " + TRANSVENOUS_PACING_WIRE_BLOCK_START_TIME) : "")
    ) AS BLOCK_START,
    (
        (string.IsNullOrEmpty(ARTERIAL_LINE_BLOCK_END_TIME) ? ("ARTERIAL_LINE_BLOCK_END_TIME: " + ARTERIAL_LINE_BLOCK_END_TIME) : "")
        +(string.IsNullOrEmpty(BLOCK_PERIPHERAL_BLOCK_END_TIME) ? ("BLOCK_PERIPHERAL_BLOCK_END_TIME: " + BLOCK_PERIPHERAL_BLOCK_END_TIME) : "")
        +(string.IsNullOrEmpty(BLOCK_SPINAL_BLOCK_END_TIME) ? ("BLOCK_SPINAL_BLOCK_END_TIME: " + BLOCK_SPINAL_BLOCK_END_TIME) : "")
        +(string.IsNullOrEmpty(CENTRAL_LINE_BLOCK_END_TIME) ? ("CENTRAL_LINE_BLOCK_END_TIME: " + CENTRAL_LINE_BLOCK_END_TIME) : "")
        +(string.IsNullOrEmpty(CSE_BLOCK_END_TIME) ? ("CSE_BLOCK_END_TIME: " + CSE_BLOCK_END_TIME) : "")
        +(string.IsNullOrEmpty(SPINAL_DRAIN_BLOCK_END_TIME) ? ("SPINAL_DRAIN_BLOCK_END_TIME: " + SPINAL_DRAIN_BLOCK_END_TIME) : "")
        +(string.IsNullOrEmpty(TEE_BLOCK_END_TIME) ? ("TEE_BLOCK_END_TIME: " + TEE_BLOCK_END_TIME) : "")
        +(string.IsNullOrEmpty(TRANSVENOUS_PACING_WIRE_BLOCK_END_TIME) ? ("TRANSVENOUS_PACING_WIRE_BLOCK_END_TIME: " + TRANSVENOUS_PACING_WIRE_BLOCK_END_TIME) : "")
    ) AS BLOCK_END
FROM @blocks;

额外的括号有时可能看起来多余,但它们有助于组织您的代码并确保操作以正确的顺序完成。