w3resource

AdventureWorks Database: Concatenate the columns separated by a unique character

SQL Query - AdventureWorks: Exercise-61 with Solution

61. Write a SQL query that concatenate the columns name, productnumber, colour, and a new line character from the following table, each separated by a specified character.

Sample table: production.product


Click to view Full table

Sample Solution:

SELECT CONCAT_WS( ',', name, productnumber, color,chr(10)) AS DatabaseInfo
FROM production.product;

Sample Output:

databaseinfo                             |
-----------------------------------------+
Adjustable Race,AR-5381,¶                |
Bearing Ball,BA-8327,¶                   |
BB Ball Bearing,BE-2349,¶                |
Headset Ball Bearings,BE-2908,¶          |
Blade,BL-2036,¶                          |
LL Crankarm,CA-5965,Black,¶              |
ML Crankarm,CA-6738,Black,¶              |
HL Crankarm,CA-7457,Black,¶              |
Chainring Bolts,CB-2903,Silver,¶         |
Chainring Nut,CN-6137,Silver,¶           |
Chainring,CR-7833,Black,¶                |
...

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Join the name, color etc.
Next: Return the five leftmost characters of each product name.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

SQL: Tips of the Day

T-SQL Cast versus Convert?

CONVERT is SQL Server specific, CAST is ANSI.

CONVERT is more flexible in that you can format dates etc. Other than that, they are pretty much the same. If you don't care about the extended features, use CAST.

Ref : https://bit.ly/3VKT5M8