The SPACE Function

Moderator: NorbertKrupa

Post Reply
User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

The SPACE Function

Post by JimKnicely » Wed Dec 19, 2012 8:04 pm

Hi!

The SPACE function inserts blank spaces into a specified location within a character string. The general syntax of the function is:
  • SPACE (count)
Examples:

The SPACE function can be helpful when formatting output:

Code: Select all

dbadmin=> SELECT last_name,
dbadmin->        first_name,
dbadmin->        last_name || ',' || SPACE(1) || first_name AS full_name
dbadmin->   FROM dba
dbadmin->  ORDER
dbadmin->    BY last_name,
dbadmin->       first_name;
last_name | first_name |   full_name
-----------+------------+----------------
Chan      | Peng       | Chan, Peng
Knicely   | Jimmy      | Knicely, Jimmy
Kyte      | Tom        | Kyte, Tom
Light     | Tom        | Light, Tom
(4 rows)

Code: Select all

dbadmin=> SELECT grp,
dbadmin->        level,
dbadmin->        SPACE(level) || name "Org Tree"
dbadmin->   FROM org_chart
dbadmin->  ORDER
dbadmin->     BY grp, level, name;
grp | level |      Org Tree
-----+-------+--------------------
   0 |     0 | Bon Jovi
   1 |     2 |   P. Kelly
   1 |     4 |     B. Kozorra
   1 |     4 |     J. Knicely
   1 |     4 |     J. McCullough
   1 |     4 |     P. Chan
   1 |     4 |     T. Light
   2 |     2 |   D. Jackson
   2 |     4 |     J. Dzurek
   2 |     4 |     J. Watts
   2 |     4 |     S. Yu
   2 |     4 |     T. Kierzkowski
(12 rows)
Unfortunately, the SPACE function can be devastating if used by a significant other:

Code: Select all

dbadmin=> SELECT 'I need some "' || SPACE(5) || '" ... just for a little while, while I sort things out.' "Yikes";
                                   Yikes
---------------------------------------------------------------------------
I need some "     " ... just for a little while, while I sort things out.
(1 row)
Have fun!
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

User avatar
Julie
Master
Master
Posts: 221
Joined: Thu Apr 19, 2012 9:29 pm

Re: The SPACE Function

Post by Julie » Sat Dec 22, 2012 11:09 am

If I don't feel comfortable with a guy then I might need some space. Most likely 'cause he's too klingy. Anyway, I love your examples, Jim! They're always relative :lol:
Thanks,
Juliette

Post Reply

Return to “Vertica Tips, Lessons and Examples”