select first and last day in a month

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

select first and last day in a month

Post by sal21 »

In a field DATA1 have a list of days:

01/01/2014
02/01/2014
...
31/01/2014
02/02/2014
03/02/2014
...
15/03/2014
02/03/2014
03/03/2014
15/03/2014

i need to calculate via sql (to use in vb6) max day from each month...

final result:

31/01/2014
03/02/2014
15/03/2014

note:
- i can have duplicates date in filed
- naturally i need also an example to extract the min day :grin:

User avatar
HansV
Administrator
Posts: 78586
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: selct first and last day in a month

Post by HansV »

You can use

SELECT Max(DATA1) AS MaxDate FROM MyTable GROUP BY DATA1-Day(DATA1)+1

where MyTable is the name of the table.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

Re: selct first and last day in a month

Post by sal21 »

HansV wrote:You can use

SELECT Max(DATA1) AS MaxDate FROM MyTable GROUP BY DATA1-Day(DATA1)+1

where MyTable is the name of the table.
ok work perfect!

ops....

but now if i fill my_var (date dimensioned) with a value from list, for example 02/01/2014, how to exract the max day from the dates in field DATA1, in this case 31/01/2014.

Always with a sql, please.

User avatar
HansV
Administrator
Posts: 78586
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: selct first and last day in a month

Post by HansV »

Sorry, I don't understand what you want.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

Re: selct first and last day in a month

Post by sal21 »

HansV wrote:Sorry, I don't understand what you want.
Myvar=02/01/2014
Extract From The List of dates in Field The max date based month in myvar in My case 31/01/2014.

User avatar
HansV
Administrator
Posts: 78586
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: selct first and last day in a month

Post by HansV »

Code: Select all

    strSQL = "SELECT Max(DATA1) AS MaxDate FROM MyTable GROUP BY DATA1-Day(DATA1) " & _
        "HAVING DATA1-Day(DATA1)=#" & Format(MyVar - Day(MyVar), "yyyy-mm-dd") & "#"
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

Re: selct first and last day in a month

Post by sal21 »

HansV wrote:

Code: Select all

    strSQL = "SELECT Max(DATA1) AS MaxDate FROM MyTable GROUP BY DATA1-Day(DATA1) " & _
        "HAVING DATA1-Day(DATA1)=#" & Format(MyVar - Day(MyVar), "yyyy-mm-dd") & "#"
WORK!

But i need to insert a where clausole one different field in wich part of query?
Last edited by sal21 on 15 Mar 2014, 21:17, edited 1 time in total.

User avatar
HansV
Administrator
Posts: 78586
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: selct first and last day in a month

Post by HansV »

What do you mean?
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78586
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: select first and last day in a month

Post by HansV »

Sal, it's better to post a new reply than to edit an existing reply. If a thread has a new reply, I will see it, but not if a reply has been edited.

Perhaps you can use something like this:

Code: Select all

    strSQL = "SELECT Max(DATA1) AS MaxDate FROM MyTable WHERE OtherField='" & _
        OtherVar & "' GROUP BY DATA1-Day(DATA1) " & _
        "HAVING DATA1-Day(DATA1)=#" & Format(MyVar - Day(MyVar), "yyyy-mm-dd") & "#"
where OtherField is the field you want to impose a condition on, and OtherVar is the value you want to restrict it to. In this example, OtherVar is a text field. If it is a number field, omit the ' characters.
Best wishes,
Hans