Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SELECT MONTH FROM A GIVEN DATE

Posted on 2004-09-01
15
Medium Priority
?
234 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

636 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