Right Justify SQL

Hello,
Can you Please help,
How do I Right Justify the Reference field in the below Select.
RTRIM (SUBSTRING('                              ', 0, 30-LEN(CONVERT(varchar(30), Reference))+1) + CONVERT(varchar(30), Reference)) +


SELECT
CONVERT(varchar(8), OrderDate, 112) +                              
SUBSTRING('       ', 0, 7-LEN(CONVERT(varchar(7), AccountNumber))+1) + CONVERT(varchar(7), AccountNumber) +
RTRIM (SUBSTRING('                              ', 0, 30-LEN(CONVERT(varchar(30), Reference))+1) + CONVERT(varchar(30), Reference)) +
RTRIM (SUBSTRING('          ', 0, 10-LEN(CONVERT(varchar(10), Department))+1) + CONVERT(varchar(10), Department)) +
SUBSTRING('          ', 0, 10-LEN(CONVERT(varchar(10), BaseAmount))+1) + CONVERT(varchar(10), BaseAmount) +
SUBSTRING('      ', 0, 6-LEN(CONVERT(varchar(6), WeightAmount))+1) + CONVERT(varchar(6), WeightAmount)
FROM FinalizedOrders F,
finalizedOrdersInvoices FOI
WHERE  ......................

Any help is appreciated,
Thanks,
W.E.BAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
right('                              '+ Reference, 30)

for reference = "12345678901234567890"    20 characters, it will produce '          12345678901234567890'

10 space + 20 char string.
W.E.BAuthor Commented:
I'm sorry,
I mistyped my aquestion,

I meant Left Justify (Not Right),
sorry about this.

thanks
Scott PletcherSenior DBACommented:
Add an LTRIM() around the final expression you want to justify.
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Mike EghtebasDatabase and Application DeveloperCommented:
perhaps:

left(Reference + '                              ', 30)
PortletPaulEE Topic AdvisorCommented:
SPACE(n) can help make the code easier to read/maintain (IMO)

-- padding after the value (right padding)
LEFT( [Reference] + SPACE(30) , 30 )

-- padding before the value (left padding)
RIGHT(SPACE(30) + [Reference] , 30 )

nb: If the source data in longer than 30 it gets truncated.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
W.E.BAuthor Commented:
Thanks you all.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.