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

此生再无相见时 提交于 2019-12-22 09:36:17

问题


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?


回答1:


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.




回答2:


Or:

TO_DATE(date, 'MM/DD/YYYY') + hour/24 + minute/1440


来源:https://stackoverflow.com/questions/11404064/how-to-create-a-datetime-from-parts-in-oracle-plsql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!