SQL date (Dates) 2019-10-17 22:17:26
When we deal with the date, it is probably the most difficult task to ensure that the date format is inserted, and format of the date column in the database matches.
If the date is part saved, the query will not be much problem. But if it comes to part time, the situation is a bit complicated.
Here we take a look at the built-in date processing function
SQL Server Date Functions
Here are the most important built-in date functions in SQL Server:
1, GETDATE () returns the current date and time
grammar:
GETDATE()
The following is a SELECT statement:
SELECT GETDATE() AS DateTime
The results are as follows:
NOTE: The above time to the millisecond.
2, DATEPART () Returns the separate portions of date / time
grammar:
DATEPART(datepart,date)
date parameter is valid date expression. datepart parameter may be a value of the following:
datepart | abbreviation |
year | yy,yyyy |
Quarter | qq,q |
month | mm,m |
On the day | house, the |
day | dd,d |
week | wk,ww |
week | dw,w |
hour | hh |
minute | me n |
second | ss,s |
millisecond | ms |
Microsecond | mcs |
Ns | ns |
Example:
select datepart(yy,getdate());
3, DATEADD () to add or subtract the date specified time interval
grammar:
DATEADD(datepart,number,date)
date parameter is valid date expression. number is the number of intervals you want to add; for a future date, the number is a positive number, for the last time, the number is negative.
Datepart parameter values consistent with the parameters listed above, and I will not copy the
Example:
select DATEADD(day,30,GETDATE());
result:
4, DATEDIFF () Returns the time between two dates
grammar:
DATEDIFF(datepart,startdate,enddate)
startdate argument is legitimate and enddate date expression. datepart parameters listed above like
examples:
select DATEDIFF(day,'2019-10-17','2020-1-24');
result:
5, CONVART () displays the date / time data in different formats
grammar:
CONVERT(data_type(length),expression,style)
value | description |
data_type(length) | Predetermined target data type (with optional length). |
express | Prescribed values need to be converted. |
style | Predetermined date / time of the output format |
The following table shows the style values datetime or smalldatetime conversion to character data:
Value (Century yy) | Value (Century yyyy) | input Output | standard |
- | 0 or 100 | mon dd yyyy hh:miAM(or PM) | Default |
1 | 101 | mm/dd/yy | USA |
2 | 102 | yy.mm.dd | ANSI |
3 | 103 | dd/mm/yy | British/French |
4 | 104 | dd.mm.yy | German |
5 | 105 | dd-mm-yy | ltalian |
6 | 106 | dd mon yy | |
7 | 107 | Mon dd,yy | |
8 | 108 | hh:mm:ss | |
- | 9 or 109 | mon dd yyyy hh:mi:ss:mmmAM(or PM) | Default+millisec |
10 | 110 | mm-dd-yy | USA |
11 | 111 | yy/mm/dd | Japan |
12 | 112 | yymmdd | ISO |
- | 13 or 113 | dd mon yyyy hh:mi:ss:mmm(24h) | |
14 | 114 | hh:mi:ss:mmm(24h) | |
- | 20 or 120 | yyyy-mm-dd hh:mi:ss(24h) | |
- | 21 or 121 | yyyy-mm-dd hh:mi:ss.mmm(24h) | |
- | 126 | yyyy-mm-ddThh:mi:ss.mmm(no spaces) | ISO8601 |
- | 130 | dd mon yyyy hh:mi:ss:mmmAM | Hijiri |
- | 131 | dd/mm/yy hh:mi:ss:mmmAM | Hijiri |
Example:
The following script uses the CONVERT () function to display different formats. Use GETDATE () function to get the current date / time
CONVERT(VARCHAR(19),GETDATE()) CONVERT(VARCHAR(10),GETDATE(),10) CONVERT(VARCHAR(10),GETDATE(),110) CONVERT(VARCHAR(11),GETDATE(),6) CONVERT(VARCHAR(11),GETDATE(),106) CONVERT(VARCHAR(24),GETDATE(),113)
The results are as follows:
Nov 04 2011 11 : 45 PM 11 - 04 - 11 11 - 04 - 2011 04 Nov 11 04 Nov 2011 04 Nov 2011 11 : 45 : 34 : 243
PostgreSQL time / date functions and operators
In certain postgre in time, the operator can perform basic number plus + subtraction - addition multiplication * / operation
Date / Time Operators
Operators | example | result |
+ | select date '2019-10-18' +integer '7' | date '2019-10-25' |
+ | select date '2019-10-18' + interval '1 hour' | timestamp '2019-10-18 01:00:00' |
+ | select date '2019-10-18' + time '03:00' | timestamp '2019-10-18 03:00:00' |
+ | select interval '1 day' + interval '1 hour' | interval '1 day 01:00:00' |
+ | select timestamp '2019-10-18 01:00' + interval '23 hours' | timestamp '2019-10-19 00:00:00' |
+ | select time '01:00' + interval '3 hours' | time '04:00:00' |
- | - interval '23 hours' | interval -23:00:00 |
- | select date '2019-10-18' - date '2019-10-15' | intrger 3(days) |
- | select date '2019-10-18' - integer '7' | date '2019-10-11' |
- | date '2001-09-28' - interval '1 hour' | timestamp '2019-10-17 23:00:00' |
- | time '05:00' - time '03:00' | interval '02:00:00' |
- | time '05:00' - interval '2 hours' | time '03:00:00' |
- | select timestamp '2019-10-18 23:00' - interval '23 hours' | timestamp '2019-10-18 00:00:00' |
- | select interval '1 day' - interval '1 hour' | interval '1 day -01:00:00' |
- | timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00' | interval '2 days 15:00:00' |
* | 900 * interval '1 second' | interval '00:15:00' |
* | select 21 * interval '1 day' | interval '21 days' |
* | select double precision '3.5' * interval '1 hour' | interval '03:30:00' |
/ | interval '1 hour' / double precision '1.5' | interval '00:40:00' |
Date / Time Functions
function | Return Type | description | example | result |
age(timestamp, timestamp) | interval | After subtracting the parameter "symbol" of the results, using the year and month, not just the use of space | select age(timestamp '2019-10-18', timestamp '1998-07-31') | "21 years 2 mons 18 days" |
age(timestamp) | interval | 从current_date 减去参数后的结果(在午夜) |
select age(timestamp '1998-07-31') | "21 years 2 mons 18 days" |
clock_timestamp() | timestamp with time zone | 实时时钟的当前时间戳(在语句执行时变化) | select clock_timestamp() | "2019-10-18 21:47:03.880667+08" |
current_date | date | 当前的日期; | select current_date | "2019-10-18" |
current_time | time with time zone | 当前的时间; | select current_time | "21:50:35.168911+08:00" |
current_timestamp | timestamp with time zone | 当前事务开始时的时间戳; | select current_timestamp | "2019-10-18 21:49:17.912353+08" |
date_part(text, timestamp) | double precision | 获取子域(等效于extract ); |
date_part('hour', timestamp '2001-02-16 20:38:40') | 22 |
date_part(text, interval) | double precision | 获取子域(等效于extract ); |
date_part('month', interval '2 years 3 months') | 3 |
date_trunc(text, timestamp) | timestamp | 截断成指定的精度; | select date_trunc('hour', timestamp '2019-10-18 22:55:40') | "2019-10-18 22:00:00" |
date_trunc(text, interval) | interval | 截取指定的精度, | date_trunc('hour', interval '2 days 3 hours 40 minutes') | "2 days 03:00:00" |
extract (field from timestamp) |
double precision | 获取子域; | select extract(hour from timestamp '2019-10-18 22:58:40') | "22" |
extract (field from interval) |
double precision | 获取子域; | select extract(month from interval '2 years 3 months') | "3" |
isfinite(date) | boolean | 测试是否为有穷日期(不是 +/-无穷) | select isfinite(date '2019-10-18') | true |
isfinite(timestamp) | boolean | 测试是否为有穷时间戳(不是 +/-无穷) | select isfinite(timestamp '2001-02-16 21:28:30') | true |
isfinite(interval) | boolean | 测试是否为有穷时间间隔 | select isfinite(interval '4 hours') | true |
justify_days(interval) | interval | 按照每月 30 天调整时间间隔 | select justify_days(interval '35 days') | "1 mon 5 days" |
justify_hours(interval) | interval | 按照每天 24 小时调整时间间隔 | select justify_hours(interval '27 hours') | "1 day 03:00:00" |
justify_interval(interval) | interval | 使用justify_days 和justify_hours 调整时间间隔的同时进行正负号调整 |
select justify_interval(interval '1 mon -1 hour') | "29 days 23:00:00" |
localtime | time | 当日时间; | select localtime | "22:29:47.906458" |
localtimestamp | timestamp | 当前事务开始时的时间戳; | select localtimestamp | "2019-10-18 22:30:51.573436" |
make_date(year int, month int, day int) | date | 为年、月和日字段创建日期 | make_date(2013, 7, 15) | 2013-07-15 |
now() | timestamp with time zone | 当前事务开始时的时间戳; | select now() | "2019-10-18 22:33:19.559465+08" |
statement_timestamp() | timestamp with time zone |
实时时钟的当前时间戳; | statement_timestamp() | "2019-10-18 22:34:50.077578+08" |
timeofday() | text | 与clock_timestamp 相同,但结果是一个text 字符串; |
select timeofday() | "Fri Oct 18 22:35:41.196317 2019 HKT" |
transaction_timestamp() | timestamp with time zone | 当前事务开始时的时间戳; | select transaction_timestamp() | "2019-10-18 22:36:42.310184+08" |