Skip to content
Advertisement

How can i merge two columns of a table in sql as a sentence?

i am a beginner in database programming. I am doing my homework, and i get a task about sql queries. My task is to merge 3 columns(first name, last name, sex) of a table(person) in a sentence.

for example:

"John Anderson is a male."

"Julia Smith is a female."

I wrote the following query:

SELECT first_name, last_name, ||' is a '|| sex ||'.'||    
FROM person    
LIMIT 10

and I get this error:

ERROR: Operator does not exist: text ||

LINE 1:select first_name, last_name, ||' is a '|| sex ||'.'||
                                                           ^ 
HINT: No operator matches the specified name and argument type. You may have to add explicit type conversions.

If anyone can offer me an idea or solution, it would be great for me.

Advertisement

Answer

You have an error in the way you have written your SELECT expression, it should be

SELECT first_name || ' ' || last_name || ' is a ' || sex || '.'    
FROM person    
LIMIT 10

Demo on dbfiddle

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement