Solved

SELECT MONTH FROM A GIVEN DATE

Posted on 2004-09-01
15
229 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

696 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