Bisoux
Bisoux

Reputation: 522

Pad strings by spaces to certain length

Consider the following two strings,

source
-------
'ADAM'  -- 4 chars length
'BOB'   -- 3 chars length

I want to concatenate spaces after the strings where the number of spaces + length of string(n) should not exceed a specific number.

So the output should look something like the below where n = 8 in this case.

result
-----------
'ADAM    '  -- 8 chars length 
'BOB     '  -- 8 chars length

How can I do this in mysql dynamically?

I could check the length of the field and use case statements for each scenario but thats not ideal. I am using mysql 8.0.17.

Upvotes: 0

Views: 751

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271023

You would use the function rpad():

select rpad(name, 8, ' ')

Upvotes: 1

Related Questions