天天看點

[轉] Oracle 和 MySQL的一些簡單指令對比

Oracle和MySQL的一些簡單指令對比

  SQL> select to_char(sysdate,'yyyy-mm-dd') from dual;

  SQL> select to_char(sysdate,'hh24-mi-ss') from dual;

  mysql> select date_format(now(),'%Y-%m-%d');

  mysql> select time_format(now(),'%H-%i-%S');

  日期函數

  增加一個月:

  SQL> select to_char(add_months(to_date     ('20000101','yyyymmdd'),1),'yyyy-mm-dd') from dual;

  結果:2000-02-01

  SQL> select to_char(add_months(to_date('20000101','yyyymmdd'),5),'yyyy-mm-dd') from dual;

  結果:2000-06-01

  mysql> select date_add('2000-01-01',interval 1 month);

  結果:2000-02-01

  mysql> select date_add('2000-01-01',interval 5 month);

  結果:2000-06-01

  截取字元串:---

  SQL> select substr('abcdefg',1,5) from dual;

  SQL> select substrb('abcdefg',1,5) from dual;

  結果:abcdemysql> select substring('abcdefg',2,3);

  結果:bcd

      mysql> select substring('abcdefg',2);

  結果:bcdefg

  mysql> select substring('abcdefg' from 2);

  結果:bcdefg

  另有SUBSTRING_INDEX(str,delim,count)函數

  傳回從字元串str的第count個出現的分隔符delim之後的子串。

  如果count是正數,傳回最後的分隔符到左邊(從左邊數) 的所有字元。

  如果count是負數,傳回最後的分隔符到右邊的所有字元(從右邊數)。