I have a table where I have to do a SELECT ... BETWEEN start_date AND end_date, as well as insert Date values into the same table.
I read things like:
java.util.Date now = new java.util.Date();
pStmt.setDate( 1, new java.sql.Date( now.getTime() ) );
pStmt.setTimestamp( 2, new java.sql.Timestamp( now.getTime() ) );
pStmt.executeUpdate();
But that kind of code sets or gets the current date, doesn't it? I want to insert and select custom dates myself.
I also read it here at StackOverflow:
SELECT *
FROM myTable
WHERE ( YEAR(myfield) = '2009')
AND ( MONTH(myfield) = '1')
Is it also possible to make the Date fields String, then use StringTokenizer to extract only the day, month or year information and use them in a SELECT similar to this code? (In case my first - and simpler - idea is impossible.)
So until now, there's two possible solutions. I also need to know which one is faster since the database accessed is in a fail-safe / critical system (a bank) with lots of data.
NOW(). If not, tell us what kind of date range you're after.YEAR(myfield)is a function on the field, and mysql won't be able to use an index to get the data quickly.