PostgreSQL的时间/日期函数使用
PostgreSQL的时间/日期函数使用 PostgreSQL的常用时间函数使用整理如下: 一、获取系统时间函数 1.1 获取当前完整时间 select now(); david=# select now(); now ------------------------------- 2013-04-12 15:39:40.399711+08 (1 row) david=# current_ti
PostgreSQL的时间/日期函数使用
PostgreSQL的常用时间函数使用整理如下:
一、获取系统时间函数
1.1 获取当前完整时间
select now();
david=# select now();
now
-------------------------------
2013-04-12 15:39:40.399711+08
(1 row)
david=#
current_timestamp 同 now() 函数等效。
david=# select current_timestamp;
now
-------------------------------
2013-04-12 15:40:22.398709+08
(1 row)
david=#
1.2 获取当前日期
select current_date;
david=# select current_date;
date
------------
2013-04-12
(1 row)
david=#
1.3 获取当前时间
select current_time;
david=# select current_time;
timetz
--------------------
15:43:31.101726+08
(1 row)
david=#
二、时间的计算
david=# select now();
now
-------------------------------
2013-04-12 15:47:13.244721+08
(1 row)
david=#
2.1 两年后
david=# select now() + interval '2 years';
?column?
-------------------------------
2015-04-12 15:49:03.168851+08
(1 row)
david=# select now() + interval '2 year';
?column?
-------------------------------
2015-04-12 15:49:12.378727+08
(1 row)
david=# select now() + interval '2 y';
?column?
------------------------------
2015-04-12 15:49:25.46986+08
(1 row)
david=# select now() + interval '2 Y';
?column?
-------------------------------
2015-04-12 15:49:28.410853+08
(1 row)
david=# select now() + interval '2Y';
?column?
-------------------------------
2015-04-12 15:49:31.122831+08
(1 row)
david=#
2.2 一个月后
david=# select now() + interval '1 month';
?column?
------------------------------
2013-05-12 15:51:22.24373+08
(1 row)
david=# select now() + interval 'one month';
ERROR: invalid input syntax for type interval: "one month"
LINE 1: select now() + interval 'one month';
^
david=#
2.3 三周前
david=# select now() - interval '3 week';
?column?
-------------------------------
2013-03-22 16:00:04.203735+08
(1 row)
david=#
2.4 十分钟后
david=# select now() + '10 min';
?column?
-------------------------------
2013-04-12 16:12:47.445744+08
(1 row)
david=#
说明:
interval 可以不写,其值可以是:
Abbreviation Meaning
Y Years
M Months (in the date part)
W Weeks
D Days
H Hours
M Minutes (in the time part)
S Seconds
2.5 计算两个时间差
使用 age(timestamp, timestamp)
david=# select age(now(), timestamp '1989-02-05');
age
----------------------------------------
24 years 2 mons 7 days 17:05:49.119848
(1 row)
david=#
david=# select age(timestamp '2007-09-15');
age
------------------------
5 years 6 mons 27 days
(1 row)
david=#
三、时间字段的截取
在开发过程中,经常要取日期的年,月,日,小时等值,PostgreSQL 提供一个非常便利的EXTRACT函数。
EXTRACT(field FROM source)
field 表示取的时间对象,source 表示取的日期来源,类型为 timestamp、time 或 interval。
3.1 取年份
david=# select extract(year from now());
date_part
-----------
2013
(1 row)
david=#
3.2 取月份
david=# select extract(month from now());
date_part
-----------
4
(1 row)
david=#
david=# select extract(day from timestamp '2013-04-13');
date_part
-----------
13
(1 row)
david=#
david=# SELECT EXTRACT(DAY FROM INTERVAL '40 days 1 minute');
date_part
-----------
40
(1 row)
david=#
3.3 查看今天是一年中的第几天
david=# select extract(doy from now());
date_part
-----------
102
(1 row)
david=#
3.4 查看现在距1970-01-01 00:00:00 UTC 的秒数
david=# select extract(epoch from now());
date_part
------------------
1365755907.94474
(1 row)
david=#
3.5 把epoch 值转换回时间戳
david=# SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 1369755555 * INTERVAL '1 second';
?column?
------------------------
2013-05-28 23:39:15+08
(1 row)
david=#
以上是基本的PG时间/日期函数使用,可满足一般的开发运维应用。

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

AI Hentai Generator
Generate AI Hentai for free.

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

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.
