TSQL - Converting dates

Hi Experts,

I have this:
'ZCNV'+ Cast(DatePart(yy, [transactionDate]) AS nVARCHAR(2)) + Cast(DatePart(mm,[transactionDate]) AS nvarchar(2)) AS BatchID

Open in new window

I get error:
Arithmetic overflow error converting expression to data type nvarchar

I am going to be inserting this into TblBatch.BatchID and BatchID is nvarchar

Thanks for any help.
Amour22015Asked:
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.

Deepak ChauhanSQL Server DBACommented:
Execute now.

'ZCNV'+ Cast(DatePart(yy, [transactionDate]) AS nVARCHAR(4)) + Cast(DatePart(mm,[transactionDate]) AS nvarchar(4)) AS BatchID

Open in new window

0
Amour22015Author Commented:
Ok, that got past the error.

But,

I am now getting:
ZCNV20157

I am looking to get:
ZCNV1507
ZCNVYYMM

Please help and thanks
0
Deepak ChauhanSQL Server DBACommented:
Alright try now.

SELECT  'ZCNV'+ cast((YEAR( [transactionDate] ) % 100) as nvarchar(2)) 
              + CAST(RIGHT('0' + RTRIM(MONTH([transactionDate])), 2) as nvarchar(2))  
              AS BatchID 

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Amour22015Author Commented:
I think I got it:
'ZCNV'+ Cast(RIGHT(Year([transactionDate]),2) AS nVARCHAR(4)) + Cast(DatePart(mm,[transactionDate]) AS nvarchar(4)) AS BatchID

Open in new window

but still need to work on the month.

Thanks
0
sameer2010Commented:
You are selecting 4 bytes of year and coverting it to 2 bytes and hence, the error. Try this:
'ZCNV'+ Cast(DatePart(yy, [transactionDate]) AS nVARCHAR(4)) + Cast(DatePart(mm,[transactionDate]) AS nvarchar(2)) AS BatchID

Open in new window


If you want only two byte year, you can use:
'ZCNV'+ Cast(Right(DatePart(yy, [transactionDate]),2) AS nVARCHAR(2)) + Cast(DatePart(mm,[transactionDate]) AS nvarchar(2)) AS BatchID

Open in new window

0
Deepak ChauhanSQL Server DBACommented:
Ok did you try which i sent you just before your comment.
0
sameer2010Commented:
'ZCNV'+ Cast(Right(DatePart(yy, [transactionDate]),2) AS nVARCHAR(2)) + right('0'+Cast(DatePart(mm,[transactionDate]) AS nvarchar(2)),2) AS BatchID
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
Amour22015Author Commented:
sameer2010

Ok that is what I am looking for.

I was getting the month 07 as 7 and was just trying to figure it out when you gave that answer also.

Thanks
0
Amour22015Author Commented:
Thanks for helping...
0
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.

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.