How to format and concatenate fields in an SQL server view

I am creating a view in an SQL server database and need to combine two fields to create a new field.  The two fields are AccountingPeriod which is a a smallint field which can contain the values 0 through 12 and AccountingYear which is a smallint field containing the four digit year.
I want to use these fields to create a new char(4) field which is the period and the last two digits of the year (ex. AccountingPeriod 3 and AccountingYear 2011 would become "0311")
What is the syntax that I would use to create this field
dsoderstromAsked:
Who is Participating?
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.

Lee SavidgeCommented:
select '0' + right(cast(isnull(AccountingPeriod, 0) as nvarchar(2)), 2) + right(cast(AccountingYear as nvarchar(4)), 2) as periodyear
0
dsoderstromAuthor Commented:
I had to remove the "select" at the beginning of your code and the "as periodyear" at the end in order for my view to work.  So I end up with the following:

'0' + RIGHT (CAST(ISNULL(dbo.FS_GLBatchHeader.AccountingPeriod, 0) AS nvarchar(2)), 2) + RIGHT (CAST(dbo.FS_GLBatchHeader.AccountingYear AS nvarchar(4)), 2)

This works except for when the AccountingPeriod is a 2 charactor month.  For example, if the AccountingPeriod is 12 and the year is 2010, I get "01210".  I need it to be "1210".
 What do I need to change?
0
Lee SavidgeCommented:
Well, yes you would have to remove the select and column alias as the code I gave was an example to get some output.

To fix the issue in my code:

RIGHT ('0' + CAST(ISNULL(dbo.FS_GLBatchHeader.AccountingPeriod, 0) AS nvarchar(2)), 2) + RIGHT (CAST(dbo.FS_GLBatchHeader.AccountingYear AS nvarchar(4)), 2)
0

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

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.