Table of Contents
日期操作
1. 获取当前日期和时间
2. 操作时间的获取子域。比如:年、月、日、小时等等。
3. 两个时间的间隔
4. 日期格式化(Mon,DD,YYYY;mm/dd/yy;dd/mm/yy;等等)
Home Database Mysql Tutorial SQL Server,MySQL,Oracle,PostgreSQL中常用函数用法(1)日

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

Jun 07, 2016 pm 03:40 PM
mysql oracle server sql

练习使用Hibernate没有用MySQL数据库,而是用了前不久接触的PostgreSQL,由于不同的数据对于相同的操作有各自的函数,MySQL的date_format(),在PostgreSQL中是没有的,google一番发现原来是要用to_char()。 搜索到一篇英文文章《Executing Common SQL Coding

         练习使用Hibernate没有用MySQL数据库,而是用了前不久接触的PostgreSQL,由于不同的数据对于相同的操作有各自的函数,MySQL的date_format(),在PostgreSQL中是没有的,google一番发现原来是要用to_char()。

        搜索到一篇英文文章《Executing Common  SQL Coding Tasks Using Function Calls》(常用SQL函数调用的区别),其中列出了如:MS SQL Server、MySQL、Oracle、PostgreSQL,这几个常用数据库中的常用函数。

        边翻译加自己的补充,分成几个文章共享给大家。方便以后的使用。

日期操作

 

1. 获取当前日期和时间

  • SQL Server

getdate()函数   (返回当前的日期和时间)

<span>SELECT GETDATE()
GO</span>
Copy after login


  •  MySQL

curdate()函数(返回当前的日期)

now()函数 (返回当前的日期和时间)

<span>SELECT CURDATE()

</span>
Copy after login
  • Oracle

sysdate (返回服务器时间)

<span>SELECT SYSDATE 
FROM dual;
</span>
Copy after login
  • PostgreSQL

current_date (返回当天的日期)

current_timestamp  (返回日期和时间)

now()  (返回当前的日期和时间,等效于 current_timestamp)

<span>SELECT CURRENT_DATE;
</span>
Copy after login


【注意】

  1. 以上的函数都是不需要参数的。
  2. Oraclecurrent_datesysdate都是现实当前时间,结果基本相同,但是也有区别:
      a.  urrent_date返回的是当前会话时间,sysdate
    返回的是服务器时间。
     b. current_date有时候比sysdate
    快一秒,这可能是四舍五入的结果。
     c. 如果修改当前会话的时区,比如将中国的时区为东八区,修改为东九区,则current_date显示的时间为东九区时间,根据东加西减的原则,current_date应该比sysdate快一小时.


2. 操作时间的获取子域。比如:年、月、日、小时等等。

  • SQL Server:datepart(datepart,date)
SELECT DATEPART(dw, GETDATE())
GO
Copy after login

*  datepart()函数可以方便的取到日期中的各个部分,如日期:2012-12-05 151536.513


yy 取年:2012
mm 取月:12
dd 取月中的天:5
dy 取年中的天:340
wk 取年中的周:50
dw 取周中的天:4
qq 取年中的季度:4
hh 取小时:15
mi 取分钟:15
ss 取秒:36


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

  • Oracle:to_char(date,'格式')

SELECT TO_CHAR(SYSDATE, 'Day') 
FROM dual;
Copy after login

  • PostgreSQL:date_part(text,timestamp)

SELECT DATE_PART('dow', date 'now');
Copy after login


3. 两个时间的间隔

  • SQLServer

SELECT DATADIFF(dd,'1/1/01',GETDATE())
GO
Copy after login

  • MySQL

SELECT FROM_DAYS(TO_DAYS(CURDATE()) - TO_DAYS('2012-12-05'));
Copy after login

  • Oracle

SELECT TO_DATE('25-Nov-2000','dd-mon-yyyy') - TO_DATE('25-Aug-1969','dd-mon-yyyy')
FROM dual;
Copy after login

  • PostgreSQL

SELECT AGE(CURRENT_DATE,'25-Aug-1969');
Copy after login


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

  • SQL Server

SELECT CONVERT(VARCHAR(11),GETDATE(),102)
GO
Copy after login

  • MySQL

SELECT DATE_FORMAT("2001-11-25","%M %E,%Y");
Copy after login

  • Oracle

ELECT TO_CHAR(SYSDATE,'dd-Mon-yyyy hh:mi,ss PM')
FROM dual;
Copy after login

  • PostgreSQL

SELECT TO_CHAR(timestamp(CURRENT_DATE),'dd-Mon-yyyy hh:mi:ss PM')
Copy after login



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1267
29
C# Tutorial
1239
24
MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MongoDB vs. Oracle: Choosing the Right Database for Your Needs MongoDB vs. Oracle: Choosing the Right Database for Your Needs Apr 22, 2025 am 12:10 AM

MongoDB is suitable for unstructured data and high scalability requirements, while Oracle is suitable for scenarios that require strict data consistency. 1.MongoDB flexibly stores data in different structures, suitable for social media and the Internet of Things. 2. Oracle structured data model ensures data integrity and is suitable for financial transactions. 3.MongoDB scales horizontally through shards, and Oracle scales vertically through RAC. 4.MongoDB has low maintenance costs, while Oracle has high maintenance costs but is fully supported.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

See all articles