您的当前位置:首页正文

SQLServer,MySQL,Oracle,PostgreSQL中常用函数用法(1)日

2020-11-09 来源:汇意旅游网


  • MySQL:dayofmonth(date)返回对应的工作日名称
  • SELECT DAYNAME('1998-02-03');
    ->'周四'
    

  • Oracle:to_char(date,'格式')
  • SELECT TO_CHAR(SYSDATE, 'Day') 
    FROM dual;
    

  • PostgreSQL:date_part(text,timestamp)
  • SELECT DATE_PART('dow', date 'now');
    


    3. 两个时间的间隔

  • SQLServer
  • SELECT DATADIFF(dd,'1/1/01',GETDATE())
    GO

  • MySQL
  • SELECT FROM_DAYS(TO_DAYS(CURDATE()) - TO_DAYS('2012-12-05'));

  • Oracle
  • SELECT TO_DATE('25-Nov-2000','dd-mon-yyyy') - TO_DATE('25-Aug-1969','dd-mon-yyyy')
    FROM dual;

  • PostgreSQL
  • SELECT AGE(CURRENT_DATE,'25-Aug-1969');
    


    4. 日期格式化(Mon,DD,YYYY;mm/dd/yy;dd/mm/yy;等等)

  • SQL Server
  • SELECT CONVERT(VARCHAR(11),GETDATE(),102)
    GO

  • MySQL
  • SELECT DATE_FORMAT("2001-11-25","%M %E,%Y");

  • Oracle
  • ELECT TO_CHAR(SYSDATE,'dd-Mon-yyyy hh:mi,ss PM')
    FROM dual;

  • PostgreSQL
  • SELECT TO_CHAR(timestamp(CURRENT_DATE),'dd-Mon-yyyy hh:mi:ss PM')
    



    显示全文