The REPEAT Function

Moderator: NorbertKrupa

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

The REPEAT Function

Post by JimKnicely » Mon Dec 17, 2012 5:05 pm

Hi,

The REPEAT function returns a new value that repeats a given value a given number of times. The general syntax of the function is:
  • REPEAT (string, count)
EXAMPLE:

Code: Select all

dbadmin=> SELECT REPEAT('Vertica is cool! ', 3)     "The Truth",
dbadmin->        'Five' || REPEAT('.', 5) || 'Dots' "Dippin' Dots";
                      The Truth                      | Dippin' Dots
-----------------------------------------------------+---------------
Vertica is cool! Vertica is cool! Vertica is cool!  | Five.....Dots
(1 row)
The REPEAT function can also be used to annoy people with the age old “Pete and Repeat” joke:

Code: Select all

dbadmin=> SELECT REPEAT('Pete and Repeat were in a boat. Pete fell off. Who was left? Repeat... ', 10) "Annoying Joke";
Annoying Joke 
------------------------------------------------------------------------------------------------------------------------
Pete and Repeat were in a boat. Pete fell off. Who was left? Repeat... Pete and Repeat were in a boat. Pete fell off. Who was left? Repeat... Pete and Repeat were in a boat. Pete fell off. Who was left? Repeat... Pete and Repeat were in a boat. Pete fell off. Who was left? Repeat... Pete and Repeat were in a boat. Pete fell off. Who was left? Repeat... Pete and Repeat were in a boat. Pete fell off. Who was left? Repeat... Pete and Repeat were in a boat. Pete fell off. Who was left? Repeat... Pete and Repeat were in a boat. Pete fell off. Who was left? Repeat... Pete and Repeat were in a boat. Pete fell off. Who was left? Repeat... Pete and Repeat were in a boat. Pete fell off. Who was left? Repeat...
(1 row)
NOTE: The value returned from the REPEAT function has a maximum size of 65,000 bytes (i.e. characters).

Code: Select all

dbadmin=> SELECT LENGTH(REPEAT('A', 65000)) "65,000",
dbadmin->        LENGTH(REPEAT('A', 65001)) "Still 65,000";
65,000 | Still 65,000
--------+--------------
  65000 |        65000
(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 REPEAT Function

Post by Julie » Mon Dec 17, 2012 9:08 pm

Ha! I like the example with the Pete and Repeat joke my stupid uncle used to play on me!
Thanks,
Juliette

Post Reply

Return to “Vertica Tips, Lessons and Examples”