4

I have 2 columns with "name" and "surname" I want to return a result with the two concatenated.

but I have a problem, the surname column accept null values and when it's the case the concatenation is null.. I would like in this case just to have the NAME

here is code:

SELECT 
    c.ID_CONT,
    c.ID_TYPE_CONTACT,
    c.ID_PARAM_CENTRE,
    c.FONCTION_CONT,
    c.MEMO_CONT,
    c.VISIBLE_CONT,
    c.NAME_CONT +' '+c.SURNAME_CONT as NAMESURNAME      
FROM dbo.CONTACT c 

It's works when Surname is blank or fulled..

Tx a lot..

3 Answers 3

6

Try this:

isnull(c.NAME_CONT +' ', '')+isnull(c.SURNAME_CONT,'')
Sign up to request clarification or add additional context in comments.

2 Comments

Great answer but it begs the question why in Sql null + string = null?
@Marin because NULL is something unknown; how much money will you have if you got unknown plus 3 bucks? Exactly unknown amount.
3

Consider omitting the space separating character when SURNAME_CONT is the NULL value. Also consider handling when SURNAME_CONT is the empty string. COALESCE is Standard SQL e.g.

c.NAME_CONT + COALESCE(' ' + NULLIF(c.SURNAME_CONT, ''), '')

Comments

2

there's a function ISNULL(expression, replacement_value)

refer to msdn manual

so you could do:

SELECT 
    c.ID_CONT,
    c.ID_TYPE_CONTACT,
    c.ID_PARAM_CENTRE,
    c.FONCTION_CONT,
    c.MEMO_CONT,
    c.VISIBLE_CONT,
    c.NAME_CONT +' '+ISNULL(c.SURNAME_CONT, '') as NAMESURNAME      
FROM dbo.CONTACT c 

2 Comments

If SURNAME_CONT is NULL, then you get NAME_CONT and a space. Do you want to have this trailing space there?
Not really important to have this Space ending the string, he is used to bind a dropdownlist.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.