May 12, 2015 at 5:00 am
Hi,
Can anyone please advice on how to convert a date to the format as Month name and year(2 digits).
For e.g.- Jan 14, Feb 14......Mar 15.
Thanks.
May 12, 2015 at 5:37 am
BrainDonor (5/12/2015)
SELECT LEFT(DATENAME(MONTH,GETDATE()),3) + ' ' + RIGHT('00' + CAST(YEAR(GETDATE()) AS VARCHAR),2)
Thanks very much it worked perfectly !
Just one more thing if you can help me with, I need to sort the same now in order of calendar months as below (which needs to be further displayed as bar graphs):
Apr 14
May 14
Jun 14
July 14
Aug 14
Sep 14
Oct 14
Nov 14
Dec 14
Jan 15
Feb 15
Mar 15
Apr 15
Could you please advice on how to achieve this ?
Thanks again.
May 12, 2015 at 5:52 am
You already have your date column that you're formatting for display, so just sort by the date column itself and not the output.
May 12, 2015 at 7:28 am
I would suggest putting that type of formatting in your bar graph instead of in your sql. Formatting in the front end is best so you can maintain your dates so that doing things like sorting is easy.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 12, 2015 at 10:14 am
I agree with Sean, formatting in the front end will help you with order and more format functions.
However, here's a shorter way to do it.
SELECT STUFF( CONVERT( char(11), GETDATE(), 0), 5, 5, '')
,STUFF( CONVERT( char(11), GETDATE()), 5, 5, '')
May 12, 2015 at 6:36 pm
There is a way to seemingly sort formatted month and year combinations. Please see the following article and apply your favorite format.
http://www.sqlservercentral.com/articles/T-SQL/71511/
I second what the others have stated, though. If there's a GUI or reporting tool (including spreadsheets) involved, formatting dates and times in T-SQL is one of the worst things you can do because it overrides the local date and time format settings.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply