Skip to content
Advertisement

Print rows of stars using PostgreSQL?

I am trying to replicate the following code in SQL Server to PostgreSQL:

declare @row int = 1
while (@row < 6)
begin
    print replicate('* ', @row)
    set @row = @row + 1
end;

Required output

* 
* * 
* * * 
* * * * 
* * * * *

Advertisement

Answer

You can use generate_series()

select rpad('*', i, '*')
from generate_series(1,5) as g(i)

Online example

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement