sybase: left pad with whitespace

Below is a quick & dirty function to left-pad an integer (or anything you want if you adapt the function) with a blank space (or any other character).

Solution is based on Sybase native ‘replicate’ and ‘len’ functions.

declare @id int, @prefix char(2), @padding_width int
set @padding_width = 10.
set @id = 66.
set @prefix = 'PR'

select @prefix || replicate(' ', @padding_width - len(convert(varchar(10), @id))) || convert(varchar(10), @id)
go

————
PR 66

1 record(s) selected [Fetch MetaData: 0/ms] [Fetch Data: 0/ms]

sources

 

 

Post a Comment

Your email is never published nor shared. You're allow to say what you want...

Powered by sweet Captcha