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
select first and last day in a month
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
-
- 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
You can use
SELECT Max(DATA1) AS MaxDate FROM MyTable GROUP BY DATA1-Day(DATA1)+1
where MyTable is the name of the table.
SELECT Max(DATA1) AS MaxDate FROM MyTable GROUP BY DATA1-Day(DATA1)+1
where MyTable is the name of the table.
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
Re: selct first and last day in a month
ok work perfect!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.
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.
-
- Administrator
- Posts: 78586
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
Re: selct first and last day in a month
Myvar=02/01/2014HansV wrote:Sorry, I don't understand what you want.
Extract From The List of dates in Field The max date based month in myvar in My case 31/01/2014.
-
- 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
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
Hans
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
Re: selct first and last day in a month
WORK!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") & "#"
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.
-
- Administrator
- Posts: 78586
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- 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
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:
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.
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") & "#"
Best wishes,
Hans
Hans