SQL Server,MySQL,Oracle,PostgreSQL中常用函数用法(1)日
练习使用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>
- MySQL
curdate()函数(返回当前的日期)
now()函数 (返回当前的日期和时间)
<span>SELECT CURDATE() </span>
- Oracle
sysdate (返回服务器时间)
<span>SELECT SYSDATE FROM dual; </span>
- PostgreSQL
current_date (返回当天的日期)
current_timestamp (返回日期和时间)
now() (返回当前的日期和时间,等效于 current_timestamp)
<span>SELECT CURRENT_DATE; </span>
【注意】
- 以上的函数都是不需要参数的。
-
在Oracle中current_date和sysdate都是现实当前时间,结果基本相同,但是也有区别:
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
* datepart()函数可以方便的取到日期中的各个部分,如日期:2012-12-05 15:15:36.513
yy 取年:2012 mm 取月:12 dd 取月中的天:5 dy 取年中的天:340 wk 取年中的周:50 dw 取周中的天:4 取年中的季度:4 hh 取小时:15 mi 取分钟:15 ss 取秒:36
- 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')

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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 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 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.

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.

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 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.

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