开发者

Concatenating strings containing NULL values

开发者 https://www.devze.com 2023-02-18 18:28 出处:网络
I want to concatenate a string I want output like this: NEX-SYM-VIM-CRE If the input is NEX-NULL-开发者_JAVA技巧NULL-VRE, it comes out to be NEX---CRE or ---CRE or NEX---as i have replaced NULL w

I want to concatenate a string

I want output like this:

NEX-SYM-VIM-CRE

If the input is NEX-NULL-开发者_JAVA技巧NULL-VRE, it comes out to be NEX---CRE or ---CRE or NEX--- as i have replaced NULL with -

But concatenation to get a final result like NEX-SYM is not coming


Something like this?

ISNULL(NEX,'-') + '-' + ISNULL(SYM,'-') + '-' + ISNULL(VIM,'-') + '-' + ISNULL(CRE,'-')


Always add a - delimiting character to the right but only when there is a value:

NULLIF(
       COALESCE(NEX + '-', '') 
          + COALESCE(SYM + '-', '') 
          + COALESCE(VIM + '-', '') 
          + COALESCE(CRE + '-', ''), ''
      )

then you always need to trim the last character (will be a - delimiting character) unless the result is NULL.

0

精彩评论

暂无评论...
验证码 换一张
取 消