SQL convert date style formatting

I want to get a string in the format of "YYYYMMM" from date().  So far I have it down to Convert(nvarchar(10),date(),112) as [YYYYMMM].  I don't write much sql and need to add it to a stored procedure and don't want to break it.  Thanks for any help.
gseales12Asked:
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.

Lee SavidgeCommented:
Close enough. Are you just trying to get the current date in that format? If so use getdate()

declare @myDate nvarchar(8)

select @myDate = convert(nvarchar(8),getdate(),112)

Open in new window


Replace @myDate with whatever you want to call the variable.
0
PortletPaulEE Topic AdvisorCommented:
convert(nvarchar(10),getdate(),112) returns a date in the format YYYYMMDD (8 characters)

see: http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/A_12315-SQL-Server-Date-Styles-formats-using-CONVERT.html

------

but you are asking for YYYYMMM (7 characters)
not sure if this is exactly what you meant, but YYYYMMM would be 'unusual'

e.g. 18th October 2013 would become: 2013OCT

for this I would try:

select datename(year, getdate() ) + upper( left( datename(month, getdate() ) ,3) )

or if a field, not getdate()

select datename(year, [date_field] ) + upper( left( datename(month, [date_field] ) ,3) )

return type from datename() is nvarchar
see: http://msdn.microsoft.com/en-us/library/ms174395.aspx
table of examples: http://www.dailyfreecode.com/Code/datename-function-270.aspx

-------
note also if using SQL Server 2012 there is the format() function which makes life easier with dates.
0
gseales12Author Commented:
Just to clarify I am looking for it to be "2013OCT".  I am not using it as a date but as a batch number.
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

PortletPaulEE Topic AdvisorCommented:
that would be:

select datename(year, getdate() ) + upper( left( datename(month, getdate() ) ,3) )
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
gseales12Author Commented:
Thank you
0
PortletPaulEE Topic AdvisorCommented:
no problem, thanks for the question, cheers. Paul
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.