dbTalk Databases Forums  

Format Date in GroupBy query

microsoft.public.sqlserver.mseq microsoft.public.sqlserver.mseq


Discuss Format Date in GroupBy query in the microsoft.public.sqlserver.mseq forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
J
 
Posts: n/a

Default Format Date in GroupBy query - 04-14-2005 , 06:13 PM






What is the syntax for a query that will give me the Count of [PartID]
shipped each month. I think I need to do a Count on PK_ID and Group By the
DateShipped but how do I format the date. I tried using DateName but I can
only get it to work if I split out the Year and month. Thanks for the help.
The raw data looks like
PK_ID PartID DateShipped
1 KLP098A 2005-04-11 10:59:05
2 PLL907C 2005-04-12 10:43:03
3 LPK761F 2005-05-15 10:23:07

And I want the output to be
Month PartsShipped
Mar-2005 2
May-2005 1


Reply With Quote
  #2  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Format Date in GroupBy query - 04-15-2005 , 02:02 PM






On Thu, 14 Apr 2005 16:13:02 -0700, J wrote:

Quote:
What is the syntax for a query that will give me the Count of [PartID]
shipped each month. I think I need to do a Count on PK_ID and Group By the
DateShipped but how do I format the date. I tried using DateName but I can
only get it to work if I split out the Year and month. Thanks for the help.
The raw data looks like
PK_ID PartID DateShipped
1 KLP098A 2005-04-11 10:59:05
2 PLL907C 2005-04-12 10:43:03
3 LPK761F 2005-05-15 10:23:07

And I want the output to be
Month PartsShipped
Mar-2005 2
May-2005 1
Hi J,

With no DDL ans sample data to go on (see www.aspfaq.com/5006), this is
of course just guesswork - but you might want to try:

SELECT SUBSTRING(CONVERT(varchar(11), DateShipped, 106), 4, 8) AS
Month,
COUNT(*) AS PartsShipped
FROM YourTable
GROUP BY SUBSTRING(CONVERT(varchar(11), DateShipped, 106), 4, 8)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.