VladP
VladP

Reputation: 901

How to add leading spaces to output column

Here is my SQL statement:

SELECT col1 AS MYCOL FROM table 1
UNION
SELECT col2 AS MYCOL FROM table 2

I need to add some spaces to col2 of table2 in output results so it looks like a tree:

MYCOL
row 1
row 2
  row 2.1
  row 2.2
row 3
  row 3.1
  row 3.2

note: just ignore rows sort/order.. Tell me how to add spaces..

Thanks

Upvotes: 0

Views: 1317

Answers (2)

PM 77-1
PM 77-1

Reputation: 13334

SQL's job is to produce the required dataset. Beautification should be done at the front-end.

You can create an extra column to indicate the level of a row and use it for the appropriate formatting.

SELECT col1 AS MYCOL, 1 AS LEVEL FROM table 1
UNION
SELECT col2 AS MYCOL, 2 AS LEVEL FROM table 2

Upvotes: 1

SOehl
SOehl

Reputation: 417

You can use the Concat(...) Function:

SELECT col1 AS MYCOL FROM table 1 UNION SELECT CONCAT(" ", col2) AS MYCOL FROM table 2

Upvotes: 1

Related Questions