Solved

SELECT MONTH FROM A GIVEN DATE

Posted on 2004-09-01
15
227 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
  • 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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

912 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now