0

How to add space between two name columns concatenated in SQL ?

SELECT CONCAT(a.first_name, a.last_name), f.title, f.description, f.length
FROM actor a
JOIN film_actor fa
ON a.actor_id = fa.actor_id
JOIN film f
ON f.film_id = fa.film_id

snapshot of result of query

i want to have space between names like "PenelopeGuiness" to "Penelope Guiness"

m.aljundi
  • 1
  • 2
  • 2
    `select CONCAT('adrian', ' ', 'klaver'); adrian klaver`. From docs(where you should have started) [String Functions](https://www.postgresql.org/docs/current/functions-string.html): *concat ( val1 "any" [, val2 "any" [, ...] ] ) → text ... Concatenates the text representations of all the arguments. NULL arguments are ignored.*. – Adrian Klaver Jan 13 '23 at 20:59

3 Answers3

0

I tried to add ' ' inside CONCATE parameters but id did not work! Anyway, I just found below solution and it worked.

SELECT a.first_name || ' ' || a.last_name AS full_name
m.aljundi
  • 1
  • 2
  • `concat(a.first_name, ' ', a.last_name)` will most certainly work –  Jan 13 '23 at 21:35
  • Look at my comment to your question and you will see it does indeed work. If there was an error when you did it then it is due to something else. To your question add what you tried and the complete error message. – Adrian Klaver Jan 13 '23 at 21:46
  • 1
    @m.aljundi Are you perhaps using oracle? Oracle limits `concat` to just two arguments; therefore you need pipes in oracle. If you're using oracle, then you mis-tagged your question. – Isolated Jan 13 '23 at 21:48
  • https://dbfiddle.uk/RSbpNWBN –  Jan 15 '23 at 09:09
0

Each of these concatenates a space character in between:

first_name || ' ' || last_name
concat(first_name, ' ', last_name)
concat_ws(' ', first_name, last_name)

(The first form with basic concatenation operator being very slightly cheaper.)

But each form handles NULL input differently. You typically want "NULL-safe" concatenation with concat_ws() if that can happen. Detailed assessment:

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
-1
SELECT CONCAT(a.first_name, " ", a.last_name), f.title, f.description, f.length
FROM actor a
JOIN film_actor fa
ON a.actor_id = fa.actor_id
JOIN film f
ON f.film_id = fa.film_id
  • Strings need to be enclosed in single quotes in SQL: `" "` needs to b `' '` –  Jan 15 '23 at 09:06