Note that there are some explanatory texts on larger screens.

plurals
  1. POsql server query concat string with '-'
    primarykey
    data
    text
    <p>In my SQL query, I'm trying to concatenate two strings in my select clause. Here's the expected results:</p> <pre><code>col A col B Result null null &amp; null '' &amp; null XYZ XYC '' null &amp; '' '' &amp; '' XYZ XYC ABC null ABC ABC '' ABC ABC XYZ ABC-XYC </code></pre> <p>My challenge is this - how do I get the 'dash' to show up for the last scenario and not the others?</p> <p>Here's my attempt:</p> <pre><code>DECLARE @ColA as varchar(10) DECLARE @ColB as varchar(10) set @ColA = null; set @ColB = null; select '&amp;' as 'Expected', COALESCE(NULLIF(COALESCE(@ColA, '') + COALESCE(@ColB, ''), ''), '&amp;') as 'Actual' set @ColA = null; set @ColB = ''; select '&amp;' as 'Expected', COALESCE(NULLIF(COALESCE(@ColA, '') + COALESCE(@ColB, ''), ''), '&amp;') as 'Actual' set @ColA = null; set @ColB = 'XYC'; select 'XYC' as 'Expected', COALESCE(NULLIF(COALESCE(@ColA, '') + COALESCE(@ColB, ''), ''), '&amp;') as 'Actual' set @ColA = ''; set @ColB = null; select '&amp;' as 'Expected', COALESCE(NULLIF(COALESCE(@ColA, '') + COALESCE(@ColB, ''), ''), '&amp;') as 'Actual' set @ColA = ''; set @ColB = ''; select '&amp;' as 'Expected', COALESCE(NULLIF(COALESCE(@ColA, '') + COALESCE(@ColB, ''), ''), '&amp;') as 'Actual' set @ColA = ''; set @ColB = 'XYC'; select 'XYC' as 'Expected', COALESCE(NULLIF(COALESCE(@ColA, '') + COALESCE(@ColB, ''), ''), '&amp;') as 'Actual' set @ColA = 'ABC';set @ColB = null; select 'ABC' as 'Expected', COALESCE(NULLIF(COALESCE(@ColA, '') + COALESCE(@ColB, ''), ''), '&amp;') as 'Actual' set @ColA = 'ABC';set @ColB = ''; select 'ABC' as 'Expected', COALESCE(NULLIF(COALESCE(@ColA, '') + COALESCE(@ColB, ''), ''), '&amp;') as 'Actual' set @ColA = 'ABC';set @ColB = 'XYC'; select 'ABC-XYC' as 'Expected', COALESCE(NULLIF(COALESCE(@ColA, '') + COALESCE(@ColB, ''), ''), '&amp;') as 'Actual' </code></pre> <p>Do you think I have to do one giant case when? I have many columns like this, and that would make it unbearable to read.</p> <p>Thanks!</p> <p>UPDATE: if I use a case when, then my select looks like this, which seems to work, but is going to be a pain.</p> <pre><code>select case when (@ColA is not null and @ColA &lt;&gt; '') and (@ColB is not null and @ColB &lt;&gt; '') then @ColA + '-' + @ColB else COALESCE(NULLIF(COALESCE(@ColA, '') + COALESCE(@ColB, ''), ''), '&amp;') end </code></pre> <p>really hoping someone has some suggestions for improvement!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload