Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Getting Month out of date

Author  Topic 

JJ297
Aged Yak Warrior

940 Posts

Posted - 2009-12-18 : 10:15:45
How can I change this procedure to only get the month name out of field:

SELECT CONVERT(VARCHAR(20), DowrDate, 7) AS [Month]
FROM TSRPTotals
GROUP BY CONVERT(VARCHAR(20), Dowrdate,7)
ORDER BY Month DESC

This gives me

Month
Oct 30, 09
Nov 27, 09

I would like:
October or Oct
November or Nov

DP978
Constraint Violating Yak Guru

269 Posts

Posted - 2009-12-18 : 10:18:31
If all your Months are represented by 3 char. abbreviations then simply put - Left(CONVERT(VARCHAR(20), DowrDate, 7),3)


If DowrDate is a DateTime field then make a case statement -

Case Month(DowrDate)
When 1 then 'January'
When 2 then 'February'
....
....
When 12 then 'December'
End As Month_Name
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-18 : 10:33:21
quote:
Originally posted by DP978

If all your Months are represented by 3 char. abbreviations then simply put - Left(CONVERT(VARCHAR(20), DowrDate, 7),3)


If DowrDate is a DateTime field then make a case statement -

Case Month(DowrDate)
When 1 then 'January'
When 2 then 'February'
....
....
When 12 then 'December'
End As Month_Name


You dont need CASE

SELECT datename(month,date_col) from your_table


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-12-18 : 10:34:08
quote:
Originally posted by DP978

If all your Months are represented by 3 char. abbreviations then simply put - Left(CONVERT(VARCHAR(20), DowrDate, 7),3)


If DowrDate is a DateTime field then make a case statement -

Case Month(DowrDate)
When 1 then 'January'
When 2 then 'February'
....
....
When 12 then 'December'
End As Month_Name



Why you use case statement..

what's wrong with this..


SELECT DATENAME(MONTH,PARAMETERNAME OR COLUMNNAME)

For Example
SELECT DATENAME(MONTH,GETDATE())



-------------------------
R...
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2009-12-18 : 10:51:28
Thanks for your help I used this:

SELECT datename(month,DowrDate) AS [Month]
FROM TSRPTotals
GROUP BY datename(month,DowrDate)
ORDER BY Month DESC
Go to Top of Page

DP978
Constraint Violating Yak Guru

269 Posts

Posted - 2009-12-18 : 11:37:44
quote:
Originally posted by rajdaksha

quote:
Originally posted by DP978

If all your Months are represented by 3 char. abbreviations then simply put - Left(CONVERT(VARCHAR(20), DowrDate, 7),3)


If DowrDate is a DateTime field then make a case statement -

Case Month(DowrDate)
When 1 then 'January'
When 2 then 'February'
....
....
When 12 then 'December'
End As Month_Name



Why you use case statement..

what's wrong with this..


SELECT DATENAME(MONTH,PARAMETERNAME OR COLUMNNAME)

For Example
SELECT DATENAME(MONTH,GETDATE())



-------------------------
R...




Yes sorry I realize this now, my apoligies! I forgot about that function :( As I try and answer these I learn more as well, just takes time to be as good as all of you ;-)
Go to Top of Page
   

- Advertisement -