2

Dataset is following:

FirstName          LastName       city      street        housenum
john            silver            london   ukitgam         780/19
gret            garbo             berlin   akstrass         102
le              chen             berlin    oppenhaim        NULL
daniel          defo             rome      corso vinchi     25
maggi           forth           london     bolken str      NULL
voich           lutz            paris      pinchi platz     NULL
anna           poperplatz        milan     via domani       15/4

write following query:

SELECT Trim(a.FirstName) & ' ' & Trim(a.LastName) AS employee_name,
a.city, a.street + ' ' + a.housenum AS address
FROM Employees AS a 

The result will be as this:

employee_name        city          address
john silver         london       ukitgam 780/19
gret garbo          berlin       akstrass 102
le chen             berlin       NULL
daniel defo         rome        corso vinchi 25
maggi forth         london       NULL
voich lutz          paris        NULL
anna poperplatz     milan      via domani 15/4

but I want this:

employee_name           city                  address
john silver            london               ukitgam 780/19
gret garbo             berlin               akstrass 102
le chen                berlin               oppenhaim
daniel defo            rome                corso vinchi 25
maggi forth            london              bolken str
voich lutz             paris               pinchi platz
anna poperplatz        milan             via domani 15/4

please help me.

0

6 Answers 6

5

Just use ISNULL() function for housenum column.

SELECT Trim(a.FirstName) & ' ' & Trim(a.LastName) AS employee_name,
a.city, a.street + ' ' + ISNULL(a.housenum,'') AS address
FROM Employees AS a 

You are getting address as NULL when housenum column value is NULL because NULL concatenated with anything gives NULL as final result.

As has been posted in other answers , you can also use COALESCE() to handle NULL values.

COALESCE is in the SQL '92 standard and supported by more different databases. On other hand ISNULL() is provided in SQL Server only as so would not be much portable.

Sign up to request clarification or add additional context in comments.

Comments

3

You can use COALESCE to handle the nulls:

SELECT Trim(a.FirstName) & ' ' & Trim(a.LastName) AS employee_name,
       Coa.city, a.street + Coalesce(' ' + a.housenum,'')  AS address
FROM Employees AS a 

Note that I apply the coalesce to ' ' + a.housenum so in case housenum is null you won't get a trailing space.

Comments

2

try like this

  SELECT concat_ws(a.FirstName,a.LastName) AS employee_name,
 concat_ws(a.city, a.street,a.housenum) AS address
 FROM Employees AS a

as it ignores null . You will get the result you expected

Comments

1
SELECT Trim(a.FirstName) & ' ' & Trim(a.LastName) AS employee_name,
a.city, a.street & (' ' +a.housenum) AS address
FROM Employees AS a

Comments

1

You can use CONCAT(col1,col2) function instead of using + as follows:

SELECT Trim(a.FirstName) & ' ' & Trim(a.LastName) AS employee_name,
a.city, CONCAT(a.street,a.housenum) AS address
FROM Employees AS a 

1 Comment

You can ask me here only if you have any more queries.
0

just replace

housenum

with

ISNULL ( housenum , '' )

The result would be the housenum or an empty string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.