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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Date Functions for Oracle

Status
Not open for further replies.

jlindahl

Programmer
Sep 23, 2003
46
US
My ASP page needs to query the oracle database looking for a date that meets a particular month and year. I can't seem to use the MONTH nor YEAR functions in SQL. Is there a way around this?
 
Have you tried:
Code:
select *
from   mytable
where  to_char(mydate,'MMYYYY') = '012004';

[sup]Beware of false knowledge; it is more dangerous than ignorance.[/sup][sup] ~George Bernard Shaw[/sup]
Consultant/Custom Forms & PL/SQL - Oracle 8.1.7 - Windows 2000
 
That won't work because the oracle field is a date datatype. Since I couldn't get other functions to work, i managed with this:
Code:
dateSelected = request.form("month") & "/01/" & request.form("year")
dim sql, RS
sql = "select * from oracle.table " &_
	"where (TRUNC(lastuserdate) >= To_Date('" & dateSelected & "','MM/DD/YYYY') " &_
	&quot;AND TRUNC(lastuserdate) <= Last_Day(To_Date('&quot; & dateSelected & &quot;','MM/DD/YYYY'))) &quot;

Thanks for your post though.
 
Hey man, the responce above to your question is correct.
After creating dinamically the SQL put a MsgBox sql to see what is inside the sql. By the way, I've found many samples in different books whith dinamy sql, but I see nothing more stupid than dinamyc SQL. What kind of API are you using to access Oracle?


Ion Filipski
1c.bmp
 
by the way, if you use OO4O, I advise you to use this syntax:

select * from oracle.table
where
lastuserdate
between
To_Date:)dateSelected, 'MM/DD/YYYY')
AND
Last_Day(To_Date:)dateSelected, 'MM/DD/YYYY'))

And bind the parameter dateSelected to a string.

Ion Filipski
1c.bmp
 
Try Standard SQL:
EXTRACT(YEAR FROM dateselected)
EXTRACT(MONTH FROM dateselected)

IIRC it's included since Oracle 8i

Dieter
 
wow, lots of responses. that's great! so BJCooperIT's solution will work, even if the oracle datatype is a date with the date and time stamp?

Ion Filipski, I am using OO4O. What syntax are you talking about, the BETWEEN? I think when I use BETWEEN, I'm not getting the last date included. I think something was going on, so I decided to use the <= & >= method.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top