Solved

SELECT MONTH FROM A GIVEN DATE

Posted on 2004-09-01
15
230 Views
Last Modified: 2008-03-03
Hi,

I am trying to get the month part from a date which I get by using a max fuction.
My date comes from a select statement which goes like this:

select max(Invoice Date)

This returns me  the latest date .(Ex:8/31/2004)

From this date I need to get the month number.

So my query should return 8.

Do I need a sub query for this.Please help as I am a novice to SQL Server.

Thanks,
MARK
0
Comment
Question by:newcom408
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
  • +1
15 Comments
 
LVL 15

Expert Comment

by:jdlambert1
ID: 11959698
SELECT Month(Max(InvoiceDate))
0
 

Author Comment

by:newcom408
ID: 11959714
Hi jdlambert1,

Do i get this value as a number or as a char.
Please let me know what the query would look like for

1)Number format
2)Char Format

Thanks,
MARK
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11959730
1) See previous solution.
2) SELECT MonthName(max([Invoice date])
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11959738
Ignore my last comment.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11959745
It should have been:
1) See previous solution.
2) SELECT DATENAME(month, max([Invoice date])
0
 

Author Comment

by:newcom408
ID: 11959765
Hi guys,
That helps a lot.
Sorry I have one more how would I get the same values for the previous month.
I mean if I have a date like 8/31/2004 and if I need to get the previous month number from this how would I do that.

Thanks,
MARK.
0
 

Author Comment

by:newcom408
ID: 11959773
Hi guys,

I am increasing the point value as I think I might have many questions based on these date functions.

Thanks,
MARK
0
 
LVL 11

Expert Comment

by:ram2098
ID: 11959873
1. SELECT month([invoice date])-1

2. select datename(month,dateadd(mm,-1,[invoice date]))

'-1' in both the queries ..you can change based on the month you want to display. (This is specific to previous month
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11959875
SELECT MONTH(DATEADD(month, -1, max(Invoice Date)))

Or:
SELECT DATENAME(month, DATEADD(month, -1, max(Invoice Date)))

You may want to read up on the date time functions in BOL:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_fa-fz_2c1f.asp
0
 
LVL 11

Expert Comment

by:ram2098
ID: 11959883
Ofcourse, even if you want to make it dynamic, you can pass the value of '-1' as a paramter too..like '0' for current month, '-1' for previous month etc..
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11959902
ram2098,

>>SELECT month([invoice date])-1<<
Just a word of caution: This could be a tad problematic in January
0
 
LVL 11

Expert Comment

by:ram2098
ID: 11959918
acperkins..you are right!! thanks.
0
 

Author Comment

by:newcom408
ID: 11963043
Hi guys,

I understand that this statement gives me the month name
SELECT MonthName(max([Invoice date])

but what if i want the month numbe rin char format

Thanks,
MARK.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 250 total points
ID: 11963707
>>I understand that this statement gives me the month name
SELECT MonthName(max([Invoice date])<<
No it does not.  That was my mistake, which I retracted after the fact.  You really need to check out BOL and do some testing in SQL Query Analyzer.

>>but what if i want the month numbe rin char format<<
SELECT CAST(MONTH(max(Invoice Date)) As char(2))
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11963716
Also, please follow-up on this abandoned question:
http://www.experts-exchange.com/Databases/Oracle/Q_21023541.html
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

690 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