Solved

Pulling Month from mixed date format

Posted on 2008-06-24
6
420 Views
Last Modified: 2012-06-21
In my database there are dates which are stored in two different formats (within the same field): MM/DD/YYYY and DD-MMM-YYYY.

I want to extract purely the months from these fields and aggregate based on the consolidated month label.

So, if these are the dates that I have:

03/20/2007 ---> Mar
04/27/2007 ---> Apr
06/03/2007 ---> Jun
1-APR-2007 ---> Apr
11-FEB-2007 ---> Feb
11-MAR-2007 ---> Mar
14-JAN-2007 ----> Jan

0
Comment
Question by:jimbofish8
6 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21856802
try this:

select datename(m, cast(yourfieldname as datetime))
from yourtablename
0
 
LVL 16

Expert Comment

by:SQL_SERVER_DBA
ID: 21856838
SELECT count(month(columnname)) MonthCount, DATENAME(month, columnname) MonthName FROM Tablename
GROUP BY columnname
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21856847
sorry, forgot the aggregate request:

select datename(m, cast(yourfieldname as datetime)), count(*)
from yourtablename
group by datename(m, cast(yourfieldname as datetime))
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:jimbofish8
ID: 21857217
Nice solution. However, i need it in the shortened month form. (ie Mar  instead of March)
0
 
LVL 8

Accepted Solution

by:
CoyotesIT earned 250 total points
ID: 21857517
I would just add then,

select
substring(datename(m, cast(emp_dob as datetime)),1,3) [BirthMonth]
, count(emp_dob)
from employee
group by substring(datename(m, cast(emp_dob as datetime)),1,3)

Tested this on one of my DB's

Good luck!
0
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 250 total points
ID: 21857634
select left(datename(m, cast(yourfieldname as datetime)), 3), count(*)
from yourtablename
group by datename(m, cast(yourfieldname as datetime))
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Pivot with row total 5 26
sql, case when & top 1 14 26
SSMS Opening Mode 9 18
Accessing variables in MySQL query 4 26
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question