views:

66

answers:

5

Hi!

I have a table in Access with string columns and a date column. I want to get all the rows from the table when the date is lower than 22.10.2010, except this month. So, i need the rows from 30.09.2010 to ...

I tied something, but I figured out it's not right:

SELECT name FROM table WHERE YEAR(date)<=2010 AND MONTH(date)<10

But this solution it's not good, and i have no other idea. Could you help me with a general solution, pls? Thanks

+1  A: 

Access?

String together the year, month, and "1" (to get the first day of the month of date) and convert that to a Date.

SELECT  *
FROM    MyTable
WHERE   dateColumn 
           < CDate(CStr(YEAR(date)) + "-" + CStr(MONTH(date)) + "-1")

SQL

Subtract the DAY (minus one) from the date in question from the date to get the first of the month. Then return all rows less than this value.

DECLARE @date DATE
SET @date = GETDATE()DECLARE

SELECT  *
FROM    MyTable
WHERE   dateColumn 
           < DATEADD(DAY, -( DATEPART(DAY, @date) - 1 ), @date)
Brad
That will not work in MS Access SQL, I am afraid.
Remou
I'm not in sql server, Iso I tried this: SELECT * FROM MyTableWHERE dateColumn < (DATEADD(DAY(d), -( DATEPART(DAY(d), d) - 1 ), d)), where d is 22-10-2010, and it's telling me: "This expression is typed incorrectly, or is too complex to be evaluated"
DaJackal
@DaJackal, this may not be of much help since I don't have Access readily available, but see if you can get it to work for you.
Brad
Thank you very much. Have a nice day
DaJackal
+1  A: 

The zeroth day of a month is the last day of the previous month:

DateSerial(Year(Date()),Month(Date()),0)

Therefore:

 SELECT [name] FROM [table] 
 WHERE [date]<=DateSerial(Year(Date()),Month(Date()),0)
Remou
This is a very good solution, and elegant too, thank you very much, it's working, but I have to try mangokun's solution too, it seems stunningly simple ad I think it could work too. Thanks again!
DaJackal
Just bear in mind that mangokun's solution is not sargable: http://en.wikipedia.org/wiki/Sargable
Remou
Thank you very much for your help, have a nice day!
DaJackal
+1  A: 

SELECT name FROM table WHERE ( YEAR(date)<2010 ) OR ( YEAR(date)=2010 AND MONTH(date)<10 )

mangokun
Thank you for your solution, have a nice day
DaJackal
That can't use the indexes, so will perform very poorly on a data set of any size.
David-W-Fenton
A: 

Hi its simple, you subtract the number of days in the month so far and use that date for the comparison

Select myName FROM myTable Where myTable.myDate <=DateAdd("d",-Day(Now()),Now());

The above query will give you all the records till the end of the last day of the last month.

Cheers.

CodeCanvas
Is there some affirmative reason why you're using Now() instead of Date()? Nobody mentioned anything other than dates, and Now() returns a date and time component, and won't necessarily give you the right results.
David-W-Fenton
You are correct David, Date() would work just fine.
CodeCanvas
A: 
SELECT you_col
  FROM YourTable
 WHERE your_date 
          < DATEADD('M', 
              DATEDIFF('M', #1990-01-01 00:00:00#, NOW()
          ), #1990-01-01 00:00:00#);
onedaywhen