简体   繁体   中英

AM/PM query and comparison

can anyone help me with this problem? I have a form which allows user to select time. The time format on the form is 7:00am, 8:00pm, 9:00pm for example, while the time format in Oracle database is varchar2 7:20, 8:30, 9:20 varchar2 am, pm, pm. I am trying to use request.getParemeter in jsp, and I am wondering how I can compare the time. Let's say if I want to give the user the option of choosing a class before 8:00pm? How do I generate the query and how can I compare the times to tell whether is before 8:00pm or after 8:00 pm?

Any help would be appreciated.

Here is how to do it in SQL - hopefully you can integrate it into JSP.

SQL Fiddle

Oracle 11g R2 Schema Setup :

CREATE TABLE CLASSES (name, hour, meridian) AS
          SELECT 'Minions for Dummies',            '07:20', 'AM' FROM DUAL
UNION ALL SELECT 'Evil Genius 101',                '08:30', 'PM' FROM DUAL
UNION ALL SELECT 'Superhero and Their Weaknesses', '09:20', 'PM' FROM DUAL;

Query 1 :

SELECT name, hour, meridian
FROM   CLASSES
WHERE  TO_DATE( hour || ' ' || meridian, 'HH:MI AM' )
         <= TO_DATE( '09:00 PM', 'HH:MI AM' )

Results :

|                NAME |  HOUR | MERIDIAN |
|---------------------|-------|----------|
| Minions for Dummies | 07:20 |       AM |
|     Evil Genius 101 | 08:30 |       PM |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM