Oracle字元和時間比較

2021-10-23 11:41:41 字數 2284 閱讀 5558

資料庫中的字段 2017-07-11 13:37:51  型別是char 或者varchar

要進件與 '20170625' 比較,格式不一致,需要將他轉換成:yyyymmdd 字串

1、先to_dateto_date(create_date,'yyyy-mm-dd,hh24:mi:ss')  格式一定要與create_date一致

2、轉字串 to_char(sysdate ,'yyyymmdd')

例:下面將create_date轉成自己想要的格式

select create_date createdate,

to_char(to_date(create_date,'yyyy-mm-dd,hh24:mi:ss'),'yyyymmdd') date1,

to_char(to_date(create_date,'yyyy-mm-dd,hh24:mi:ss'),'yyyy/mm/dd') date2,

to_char(to_date(create_date,'yyyy-mm-dd,hh24:mi:ss'),'yyyymmddhh24miss') date3

from pfb_payment_order_info;

轉換的格式:

表示year的:y  表示年的最後一位 yy 表示年的最後2位 yyy 表示年的最後3位 yyyy 用4位數表示年

表示month的:mm 用2位數字表示月;mon 用簡寫形式 比如11月或者nov ;month 用全稱 比如11月或者november

表示day的:dd 表示當月第幾天;ddd表示當年第幾天;dy 當周第幾天 簡寫  比如星期五或者fri;day當周第幾天 全寫

比如星期五或者friday。

表示hour的:hh 2位數表示小時 12進製; hh24 2位數表示小時 24小時

表示minute的:mi 2位數表示分鐘

表示second的:ss 2位數表示秒 60進製

表示季度的:q 一位數 表示季度 (1-4)

另外還有ww 用來表示當年第幾周 w用來表示當月第幾周。

24小時制下的時間範圍:00:00:00-23:59:59

12小時制下的時間範圍:1:00:00-12:59:59

比如:select to_char(sysdate,'yy-mm-dd hh24:mi:ss') from dual   //顯示:08-11-07 13:22:42

select to_date('2005-12-25,13:25:59','yyyy-mm-dd,hh24:mi:ss') from dual //顯示:2005-12-25 13:25:59

而如果把上式寫作:select to_date('2005-12-25,13:25:59','yyyy-mm-dd,hh:mi:ss') from dual,則會報錯,因為小時hh是12進製,13為非法輸入,不能匹配。

補充:當前時間減去7分鐘的時間 

select sysdate,sysdate - interval '7' minute from dual 

當前時間減去7小時的時間 

select sysdate - interval '7' hour from dual 

當前時間減去7天的時間 

select sysdate - interval 』7』 day from dual 

當前時間減去7月的時間 

select sysdate,sysdate - interval '7' month from dual 

當前時間減去7年的時間 

select sysdate,sysdate - interval '7' year from dual 

時間間隔乘以乙個數字 

select sysdate,sysdate - 8*interval '7' hour from dual

首先糾正幾個問題:

(1)select * from tb where createtime > '20111109'

如果想要這個條件滿足的話,tb表裡的createtime不能寫成'2011/11/10'這種格式的,只能寫成'20111110',因為'2011/11/10'不大於'20111109',只有'20111110'才大於'20111109'。

(2)比較的時候,是不會自動轉換成date型別的,是挨個字元比較它的acsii碼。

所以說'2011/11/10'不大於'20111109',因為前四個字元都是2011,肯定是相等的;比到第5個字元的時候,用'/'和'1'比較,而select 1 from dual where '/' > '1'是不成立的,說明'/' 小於 '1', 所以'2011/11/10'就小於'20111109'

oracle日期時間比較

select cicai as merchantname,tre.bank code as bankcode,sum tr.trade sum as totalmount,count as count,tr.biz type as biztype from trade relation tre,tr...

Oracle時間比較和2個相關函式

1,比較當前時間與指定時間相差分鐘數 select sysdate,sysdate to date 2011 07 07 12 07 33 yyyy mm dd hh24 mi ss from dual 輸出結果 2011 07 07 12 50 05 0.029537037037037 selec...

mysql時間比較

時間比較 當前時間是否在某個時間段之內 是否在create time 5天之內 select from message detail where unix timestamp now between unix timestamp create time and unix timestamp crea...