Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a null value to a Varchar Value

Tags:

sql

sql-server

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..

like image 326
bAN Avatar asked Dec 22 '22 23:12

bAN


2 Answers

Try this:

isnull(c.NAME_CONT +' ', '')+isnull(c.SURNAME_CONT,'')
like image 153
Denis Valeev Avatar answered Jan 06 '23 03:01

Denis Valeev


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, ''), '')
like image 44
onedaywhen Avatar answered Jan 06 '23 03:01

onedaywhen