简体   繁体   中英

Sql date and time comparison from dual table

I have two columns state_date and state_time

I want to pull records from database using columns state_date and state_time.State_date and state_time are should less than or equals to below sql.Below sql giving date and time.But i have individual columns one is having date and other is having time.SO first i want to compare date and after that time.How can i modify below query to get date and time seperately with milli seconds.

select decode(TRIM(to_char(SYSDATE,'Day')),'Monday','3','1') from dual

Sample Data:

order table:

order id   order name  order date
1          pizza       20-feb-2012
2          burger      17-feb-2012

order_state table

order id     order_state     order_date                order_time
1             initiated      20-feb-2012               12:29:11:203
1             processed      21-feb-2012               12:29:12:112
1             cancelled      21-feb-2012               12:29:11:311

So here i want to fetch the latest record ie cancelled based on order_date and order_time ,order_id and today date.if i query today at 12:29:10:311 or 12:29:11:310.i want to fetch the cancelled record.

Thanks,

Chaitu

Okay, I think I've got you. You want to do the following?

select <columns>
  from my_table
 where state_date <= <some date>
   and state_time <= <some time>

It's fairly unusual to care about the milliseconds but if you do then you should use systimestamp .

Judging by the fact you've split date and time, these are characters, So, I'm going to have to guess the format masks . If they're wrong the link should guide you on what to do. It's not wise, by the way, to split a date in this way. You could create a column using the timestamp data-type in your table, which would make your problem extremely simple.

So, I don't know why you've chosen the 'Day' format for your query but going with that <some date> becomes to_char(sysdate, 'DAY') .


From your comment below <some date> would be to_char(sysdate, 'DD-MON-YY')


<some time> would be to_char(systimestamp,'HH24:MI:SS:FF3') , which would give you the timestamp to the millisecond, though the datatype can go to the micro-second.

It seems a little strange to me but your query would then become:

select <columns>
  from my_table
 where state_date <= to_char(sysdate, 'DD-MON-YY')
   and state_time <= to_char(systimestamp,'HH24:MI:SS:FF3')

It would be more normal, if storing a date as a string, to store it in the format yyyymmdd so at least you can guarantee it's in order. If you've done something like this then just change the format mask. If you haven't then these queries aren't going to work as intended.


Personally, if you have to store data this way and assuming state_date is stored as, say, dd-mon-yy , ie including year, month AND day, and state_time is stored as indicated above, then I would do something like this:

select <columns>
  from my_table
 where to_timestamp(state_date || state_time, 'DD-MON-YYHH24:MI:SS:FF3')
        <= systimestamp

It makes it a lot more obvious what is going on and there is no ambiguity on what < means in this situation as a date will always be less than a future date, which doesn't necessarily hold true for strings.

I hope this makes sense.

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