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)

PR 66

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




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