Skip to content
Advertisement

How to add characters in the results up to certain length in SQL

I need to add * symbols to make all records to 15 digits. What is the best way?

Ex:)
From
2345
32456674
543
5
467888

to 
2345***********
32456674*******
543************
5**************
467888*********

Thanks,

Advertisement

Answer

Your other questions are tagged for SQL Server, so I will assume that database.

SQL Server does not support rpad(), but you can use left() and replicate():

select left(concat(col, replicate('*', 15)), 15)

or:

select left(col + replicate('*', 15), 15)

If col is not already a string, then you need to cast it to one.

Some databases have direct support for an rpad() function that does this directly.

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