SQL using If Not Null on a Concatenation

后端 未结 8 1696
孤独总比滥情好
孤独总比滥情好 2021-01-01 15:22

If I have the table

SELECT (Firstname || \'-\' || Middlename || \'-\' || Surname)  AS example_column
FROM example_table

This will

相关标签:
8条回答
  • 2021-01-01 15:39
    • The NULLIF expression reduces blank Middlename to NULL
    • Concatenating '-' with a NULL will always return NULL
    • TheVALUE expression replaces NULLs with an empty string

    _

    SELECT Firstname || VALUE( '-' || NULLIF('Middlename',''),'') || '-' || Surname'  
           AS example_column
    FROM example_table
    
    0 讨论(0)
  • 2021-01-01 15:40

    If you use Postgres, concat_ws() is what you are looking for:

    SELECT concat_ws('-', Firstname, Middlename, Surname)  AS example_column
    FROM example_table
    

    SQLFiddle: http://sqlfiddle.com/#!15/9eecb7db59d16c80417c72d1e1f4fbf1/8812

    To treat empty strings or strings that only contain spaces like NULL use nullif():

     SELECT concat_ws('-', Firstname, nullif(trim(Middlename), ''), Surname)  AS example_column
     FROM example_table
    
    0 讨论(0)
  • 2021-01-01 15:44

    One solution could be using case statement

    select case Middlename is not null then (Firstname || '-' || Middlename || '-' || Surname) 
        else (Firstname || '-' || Surname) end AS example_column
    from ....
    
    0 讨论(0)
  • 2021-01-01 15:50

    You could use REPLACE (if Oracle)

    SELECT
       REPLACE(Firstname || '-' || Middlename || '-' || Surname,'--','-') 
       AS example_column
    FROM example_table;
    

    Warning: I've assumed there is no valid name with - as first or last character.


    For downvoter

    OP clearly said that:

    SELECT (Firstname || '-' || Middlename || '-' || Surname)  AS example_column
    FROM example_table
    

    This will display Firstname-Middlename-Surname e.g.

    John--Smith

    So:

    1. Middlename is blank: '' this solution works in SQLite/PostgreSQL/Oracle
    2. Middlename is NULL and OP probably uses Oracle

    Although Oracle treats zero-length character strings as nulls, concatenating a zero-length character string with another operand always results in the other operand, so null can result only from the concatenation of two null strings.

    || concatentaion operator:

     -- PostgreSQL/SQLite
     SELECT 'sth' || NULL
     -- NULL
    
    
     -- Oracle
     SELECT 'sth' || NULL
     -- sth
    
    0 讨论(0)
  • 2021-01-01 15:51

    This approach works:

    select first_name || coalesce('-' || middle_name, '') || '-' || last_name 
    from t;
    

    Output:

    |        ?column? |
    |-----------------|
    |      john-smith |
    | jane-anne-smith |
    

    UPDATE

    Live code: http://sqlfiddle.com/#!15/d5a1f/1

    Just as my inkling, someone will give a scenario that is not in the question. So to make it work with empty middle name. Just add a nullif for empty string:

    select first_name || coalesce('-' || nullif(middle_name,'') , '') || '-' || last_name 
    from t;
    

    Output:

    |        ?column? |
    |-----------------|
    |      john-smith |
    |      obi-kinobi |
    | jane-anne-smith |
    
    0 讨论(0)
  • 2021-01-01 15:51

    You can use CASE statement

    select Firstname 
          || case when Middlename <> '' Then '-'||Middlename  else '' END 
          || case when Surname<> '' Then '-'||Surname else '' END
    

    As per your sample data I have check for empty string. To check NULL use Middlename IS NOT NULL instead of Middlename <> ''

    0 讨论(0)
提交回复
热议问题