Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change date to "month year"

Status
Not open for further replies.

Ofina

Technical User
Feb 25, 2004
156
US
I want an SQL function that takes a datetime field and returns "month year".

Example:
04/30/2012

I want to return "April 2012" but still be sortable by date, not alphabetically.
 
the function will only return to you the processed value of a single input. How you sort it will be in your select statement.

You can do this

Create you function to look like this
Code:
select 
case DatePart(Month, @DATE)
when 1 then 'January'
when 2 then 'February'
...
when 6 then 'June'
...
end + ' '  + convert(char(4),datepart(year, @DATE))
then when you call your select statement you will need to include the original date and a call to this function. then you will do the order by

so it would look e like this
Code:
select date, function_return
from table
order by date
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top