Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a datetime from parts in Oracle/PLSQL?

I have a date (4/30/2012), an hour (4) and a minute (45). I need to convert that to an Oracle datetime value for comparison in a PLSQL/Oracle SELECT statement. Ideally, there would be a function like this (C# since I know it a little better):

var date = "4/30/2012";
var hour = "4";
var minute = "45";
DateTime myDateTime = convertDateTime(date, hour, minute);

I have a table with three columns to make up the date and time of an event. I have another table that has items that may or may not match based on their start and end dates. In pseudo SQL, here's what I'm trying to do:

SELECT 
    G.Name, 
    (SELECT MAX(Cost) 
        FROM CrappyTable AS C 
        WHERE G.StartTime < convertDateTime(C.Date, C.Hour, C.Minute) 
        AND G.EndTime > convertDateTime(C.Date, C.Hour, C.Minute)) as Cost
FROM GoodTable as G
WHERE G.Price < 100.00;

OR "Select name and max cost (from reported cost during a time range) where the price is less than $100."

Is there an existing function that might do something like the above convertDateTime function?

like image 310
Byron Sommardahl Avatar asked Dec 15 '22 21:12

Byron Sommardahl


2 Answers

Concatenate your date and time fields and pass them to TO_DATE (assumes 24 hour clock time):

TO_DATE(date||' '||hour||':'||minute,'MM/DD/YYYY HH24:MI')

This will convert your date/time values to an Oracle DATE type.

like image 55
DCookie Avatar answered Dec 29 '22 00:12

DCookie


Or:

TO_DATE(date, 'MM/DD/YYYY') + hour/24 + minute/1440
like image 21
Lee Avatar answered Dec 28 '22 22:12

Lee