T-SQL Equivalent of Oracle’s RPAD()
Ok, well, this might now be the “equivalent”, and I don’t yet have a solution for LPAD without using a Custom Function, but here’s what I did to perform an RPAD for something I was doing.
I had 2 int fields that I needed to concatenate together. The max size on both were 3 digits, so I decided to make my joint combination of the two keys both 3 digits so keep it as a smart key, always knowing the first 3 meant something and the last 3 meant something in the source databases. Again, these were ints, and I need the combination of the two to be ints.
Forgive the formatting, I’m TRYING to make it easier to read:
select
cast(
replace( /*replace spaces with 0s*/
cast([col1] as nchar(3)) /*casting as nchar to provide training spaces*/
, ' '
, '0') +
replace(
cast(isnull(col2)], 0) as nchar(3)) --second field is nullable (left join), make them 0s
, ' '
, '0')
as int) /*casting it back to an int*/
from table
Recent Buzz