Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Databases Reporting with SQL Working with Text Creating Excerpts From Text

Concatenate empty data

Hi,

How do I write my query if I'm using concatenation with two columns, say for instance, first_name and last_name, and there are empty cells for some last_name?

Thanks

Are you talking about empty or null? If null the default behavior is to return null when concatenating values (depending on the data engine and settings used).

I just tried in the playground and found this to be the case. Open the playground for the video and run the following

SELECT typeof(first_name || last_name) AS myname FROM customers WHERE last_name IS NULL;
UPDATE customers SET last_name = '' WHERE last_name IS NULL; 
SELECT first_name || last_name AS myname FROM customers WHERE last_name = '';

The first select returns null for the concatenated fields

The update statement updates null last_names to empty

The second select returns the first name and an empty string for the concatenated fields

1 Answer

They'll just be empty unless you want to filter or replace the empty value somehow

Hi Thanks for your response. When I tried it came back with both empty, even though there was a first name.