Home » Questions » Computers [ Ask a new question ]

Merge multiple columns into one via a query in an access database

Merge multiple columns into one via a query in an access database

i need to write a query that combines multiple columns of text.

Asked by: Guest | Views: 422
Total answers/comments: 2
Guest [Entry]

"Maybe this?

SELECT col1 FROM table
UNION
SELECT col2 FROM table
UNION
SELECT col3 FROM table
UNION
SELECT col4 FROM table

UNION only joins distinct values (if you have 4 't' values, it only puts one). UNION ALL will store duplicates."
Guest [Entry]

"SELECT (Column1 + Column2 + Column3 + Column4)
FROM YourTable

The above will concatenate all four columns. If you need a space separator:

SELECT (Column1 + "" "" + Column2 + "" "" + Column3 + "" "" + Column4)
FROM YourTable

Thus the plus sign works as a concatenator. Finally, if you need to name the resulting column:

SELECT (Column1 + Column2 + Column3 + Column4) AS ColumnName
FROM YourTable

One last note, been ages since I last worked on Access, but I reckon you may also be able to concatenate fields with the || operator:

SELECT (Column1||Column2||Column3||Column4)
FROM YourTable

Following on the comments to this answer, different database engines may provide different syntaxes. An annoyance, no doubt:

SELECT Column1 & Column2 & Column3 & Column4
FROM YourTable

Here we replaced '+' with the usual '&' concatenation symbol present in many windows applications.

Cheers"