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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another SQL formatting question

Status
Not open for further replies.

AccessUser22

Technical User
Jan 23, 2003
168
US
I'm trying to display in a field the previous month. Since it's currently May, I want the field to display April. I can get the field to say '4' but can't figure out how to get the month as a char.

Using Datepart
DatePart(Month, Getdate()) - 1 <--Gives me '4'

Using Month
Month(Getdate()) - 1 <-- Gives me '4'

Anyway to get it to say "April"?



 
By using the DATENAME function.

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
Code:
[COLOR=blue]Select[/color] [COLOR=#FF00FF]DateName[/color]([COLOR=#FF00FF]Month[/color], [COLOR=#FF00FF]DateAdd[/color]([COLOR=#FF00FF]Month[/color], -1, [COLOR=#FF00FF]GetDate[/color]()))

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Code:
SELECT DATENAME(month, DATEADD(mm, -1,  GETDATE()))

Hope this helps.

Cheers,
Leigh

"If you had one shot, one opportunity, to seize everything you ever wanted, in one moment, would you capture it, or let it slip" - Eminem

 
Thanks everyone. Still haven't gotten all of the different formatting functions down in SQL yet. This works great though.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top