azure-data-lakeu-sql

how to concatenate strings with nulls


How do we concatenate strings that may have nulls?

I'm getting the following error:

enter image description here

...when attempting to execute:

@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;

How do we concatenate strings that may have nulls?


Solution

  • The code is trying to concatenate the first string with the next comparation of your ternary operator list. Basically instead of evaluating the ternary operator, you are concatenating the boolean expression of it first.

    Solution: Add extra parenthesis in your script:

    @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;
    

    Extra parenthesis might seem redudant at times but they help organize your code and make sure the operations are done in the right order.