Date Functions, How to convert 'datetime' field to text decription('December 2012') in SQl Server Developer 14?

I've looked thru the 'Date and Time Functions' in SSMS under the 'Functions'/System Functions but I don't have any documentation.

I am pulling data from a file that I want to sort and group on the Year and Month of the Payment Date.  The payment date is defined at 'datetime'.  Payments from the same year and month should be grouped and summarized into a 'Collection Period' line on the report.

I was able to used the Year function to extract the 4 digit year (for example:(2002)  and I was able to use the 'Month' function to pull the month (6) but I can't make the leap to what the user wants.  On the generated report the user wants to see the 'Collection Period' in text (December 2012, November 2012 etc..).  They also want the collection period sorted in ascending order.  December 2012 will be followed by January 2013 etc..

Any payments for dates 12/1/12 thru 12/31/12 would be grouped, summarized and show on the report as December 2012.

 Any November payment dates dates would be shown as November 2012 etc..

Is there any way within SQL to convert from the original 'PaymentDate', datetime field (12/2/12, 12/3/12, 12/15/12, 12/22/12) to the corresponding text description (December 2012)?
LVL 1
mlcktmguyAsked:
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.

NorieAnalyst Assistant Commented:
You could use FORMAT.

FORMAT(PaymentDate, 'MMMM yyyy')
1

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
Manoj SinghSoftware EngineerCommented:
Have you tried above
0
PortletPaulEE Topic AdvisorCommented:
T-SQL documentation  used to be called "Books On Line" (BOL)

As mentioned above from SQL Sever 2012 the easiest way is to use the FORMAT() function

If you are on SQL 2008 you can use DATENAME()
e.g.
select
       format(getdate(),'MMMM yy')
     , datename(mm,getdate()) + ' ' + right(datename(yy,getdate()),2)

Open in new window

0
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Mark WillsTopic AdvisorCommented:
Concur with FORMAT

Actually wrote an Article on it : https://www.experts-exchange.com/articles/20059/T-SQL-formatting-Dates-and-Times-using-FORMAT.html

But you have a number of options :

select format(getdate(),'MMMM yyyy')

select datename(month,getdate()) + ' ' + datename(year,getdate())

-- or a calendar table (where a lot of places build their own)

Select mname+' '+yname from MyCalendar where cdate = cast(getdate() as date) 

-- BUT
-- for reporting, you need to use components separately because FORMAT essentially becomes string - nvarchar

select format(reporting_period,'MMMM yyyy') as RP
from ( select getdate() as Reporting_Period 
       union all 
       select getdate() - 33 ) as report_data
order by year(reporting_period),month(reporting_period)

Open in new window

0
Mark WillsTopic AdvisorCommented:
Oh, and that Calendar Table, Jim Horn wrote an Article about building and using : https://www.experts-exchange.com/articles/12267/SQL-Server-Calendar-Table.html
0
mlcktmguyAuthor Commented:
Thanks you all, great information
0
Ramesh D JaiswalCommented:
Excellent answer Norie.
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
SQL

From novice to tech pro — start learning today.