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...


Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/remydamo/websites/qc4blog/www/wp-content/plugins/sweetcaptcha-revolutionary-free-captcha-service/library/sweetcaptcha.php on line 81

Warning: fsockopen() [function.fsockopen]: unable to connect to www.sweetcaptcha.com:80 (php_network_getaddresses: getaddrinfo failed: Name or service not known) in /home/remydamo/websites/qc4blog/www/wp-content/plugins/sweetcaptcha-revolutionary-free-captcha-service/library/sweetcaptcha.php on line 81