|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 10:07 AM
Points: 59,
Visits: 663
|
|
Hi,
I use a very basic select query
SELECT * FROM Client_Trades.DBO.Trades WHERE MONTH(Trade_Date) = '01'
To extract records from a particular month. Is there any way of me not having to change the value e.g. '01' every month and just having it so it selects from the current month?
Thanks for any help.
EDIT: Trade_Date is a Date datatype.
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, May 06, 2013 1:09 PM
Points: 15,439,
Visits: 9,569
|
|
Try this:
DECLARE @Date DATE = GETDATE();
SELECT * FROM Client_Trades.DBO.Trades WHERE Trade_Date >= DATEADD(MONTH, DATEDIFF(MONTH, 0, @Date), 0) AND Trade_Date < DATEADD(MONTH, DATEDIFF(MONTH, 0, @Date)+1, 0); What that does is calculate the first day of the current month, and the first day of the next month. Then it makes sure the Trade_Date >= first day of current, and < first day of next month.
Does that do what you need?
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 10:07 AM
Points: 59,
Visits: 663
|
|
| This works perfectly. Thank you very much :)
|
|
|
|