Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to select records based on Date 1

Status
Not open for further replies.

fiat2

Programmer
Nov 26, 2002
104
0
0
US
I'm trying to select records that have workdates greater than 4/1/2009.

workdate is of type Date is stamped with sysdate when inserted so it contains Date & Time

How can I achieve this?

Code:
SELECT * FROM  tb_my_table 
WHERE to_char(workdate, 'MM/DD/YYYY') > ('4/1/2009')
 
Fiat,

Try this:
Code:
SELECT * FROM  tb_my_table 
WHERE workdate > to_date('04/01/2009','mm/dd/yyyy');
Let us know of the outcome.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
“Beware of those that seek to protect you from harm or risk. The cost will be your freedoms and your liberty.”
 
Well, it must be Christmas in April because Santa just brought me a gift. It worked and provided me the results I needed.

Thanks for taking the time.

Enjoy,
 
Bear in mind that that query will include rows where the workdate is 4/1/2009, as [tt]to_date('04/01/2009','mm/dd/yyyy')[/tt] will evaluate to midnight on 1st April and your workdate includes a time component. So any row with a workdate of, say, 1pm on 4/1/2009 will still be included in your report.

If this is a problem for you, there are several ways around it, here's one:
Code:
SELECT * FROM  tb_my_table 
WHERE [red]TRUNC([/red]workdate[red])[/red] > to_date('04/01/2009','mm/dd/yyyy');
here's another
Code:
SELECT * FROM  tb_my_table 
WHERE workdate >[red]=[/red] to_date('04/0[red]2[/red]/2009','mm/dd/yyyy');
The second is probably better, as it will continue to use any index you might have on workdate.

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top