跳转到内容

SQL 方言参考/函数和表达式/日期和时间函数

来自维基教科书,开放的书,开放的世界

日期和时间函数

[编辑 | 编辑源代码]
另请参见:日期和时间类型

此页面包含比较表,这些表可能很大且很复杂

虽然完全可以在完整状态下查看它们,但建议安装一个比较表扩展,它可以让你选择特定的列进行比较。

SQL 版本 功能 标准
SQL:2011
DB2 Firebird Ingres Linter MSSQL MySQL
Vers. 5.x
MonetDB Oracle
Vers. 11.x
PostgreSQL SQLite Virtuoso
? 当前日期 CURRENT_DATE CURRENT DATE
CURRENT_DATE
CURRENT_DATE DATE('TODAY') SYSDATE, CURRENT_TIMESTAMP, UNIX_TIMESTAMP (也包括时间) CAST(GETDATE() AS DATE)
CONVERT(DATE, GETDATE())
CURDATE()
CURRENT_DATE
CURRENT_DATE()
CURRENT_DATE TRUNC(CURRENT_DATE), TRUNC(SYSDATE) CURRENT_DATE CURRENT_DATE CURDATE
? 当前时间 CURRENT_TIME CURRENT TIME
CURRENT_TIME
CURRENT_TIME TIME(DATE('NOW')) N/A N/A CURTIME, CURRENT_TIME CURRENT_TIME N/A CURRENT_TIME CURRENT_TIME CURTIME
? 当前日期和时间 CURRENT_TIMESTAMP CURRENT TIMESTAMP
CURRENT_TIMESTAMP
CURRENT_TIMESTAMP, 'NOW' DATE('NOW') SYSDATE, CURRENT_TIMESTAMP, UNIX_TIMESTAMP GETDATE(), CURRENT_TIMESTAMP NOW(), CURRENT_TIMESTAMP NOW(), CURRENT_TIMESTAMP CURRENT_DATE, SYSDATE, SYSTIMESTAMP, CURRENT_TIMESTAMP NOW(), CURRENT_TIMESTAMP CURRENT_TIMESTAMP NOW
? 将间隔添加到日期 date + arg date + arg arg1 + arg2 arg1 + arg2
arg1 = date, arg2 = time-interval;
date+'15 day'
arg1 + arg2
ADD_MONTHS
DATEADD DATE_ADD arg1 + interval in days or fractional days arg1 + interval in days or fractional days

ADD_MONTHS()

查看更多

arg1 + arg2 N/A DATEADD
? 从日期中减去间隔 date - arg date - arg arg1 - arg2 arg1 - arg2
arg1 = date, arg2 = time-interval;
date-'3 month'
arg1 - arg2 DATEDIFF DATE_SUB arg1 - interval '1' DAY arg1 - arg2 (sysdate-interval '1' MONTH) arg1 - arg2 N/A DATEDIFF
? 日期差 (date1 - date2) field date1 - date2 arg1 - arg2
DATEDIFF
date1 - date2;
interval( 'day', date1 - date2)
arg1 - arg2 DATEDIFF DATEDIFF CAST(arg1 - arg2 as bigint)/1000 (以秒为单位的差) arg1 - arg2 查看更多 查看更多-2 AGE N/A DATEDIFF
? 月最后一天 ? date + 1 MONTH - DAY(date) DAYS LASTDAYMONTH LAST_DAY LAST_DAY N/A LAST_DAY ? LAST_DAY N/A N/A N/A
? 时区转换 date AT TIME ZONE offset ? N/A ? N/A N/A CONVERT_TZ ? NEW_TIME TIMEZONE N/A TIMEZONE
? 日期后的第一个工作日 ? ? N/A ? NEXT_DAY N/A ? ? NEXT_DAY N/A N/A N/A
? 将日期转换为字符串 CAST (x AS STRING) TO_CHAR(value, format)
VARCHAR_FORMAT(value, format)
CAST(value, datetype)
DATETOSTR
DATE_FORMAT(date, format)
char(date)
TO_CHAR(value, format) DATENAME DATE_FORMAT(value, format) CAST (x AS STRING) TO_CHAR TO_CHAR(value, format) STRFTIME(format, value) CAST
? 将日期转换为数字 N/A INT(date) EXTRACT ? TO_NUMBER(value)
DATESPLIT
DATEPART ? ? TO_NUMBER(TO_CHAR()) DATE_PART N/A CAST
? 将字符串转换为日期 CAST (x AS DATE) DATE(value)
TIMESTAMP(value)
CAST DATE(string) TO_DATE(value, format)
TO_TIMESTAMP(value, format)
CAST ? CAST, TIMESTAMP 'yyyy-mm-dd HH:mm:ss' TO_DATE TO_DATE N/A STRINGDATE
? 从 DATE 或 DATETIME x 中提取年份 EXTRACT(YEAR FROM x) ? EXTRACT(YEAR FROM x) YEAR(x)
DATE_PART(YEAR, x)[1]
? YEAR(x)
DATEPART(year, x)
YEAR(x)
EXTRACT(YEAR FROM x)
EXTRACT(YEAR FROM x) EXTRACT(YEAR FROM x) EXTRACT(YEAR FROM x) strftime('%Y', x) ?
? 从 DATE 或 DATETIME x 中提取月份 EXTRACT(MONTH FROM x) ? EXTRACT(MONTH FROM x) MONTH(x)
DATE_PART(MONTH, x)[1]
? MONTH(x)
DATEPART(month, x)
MONTH(x)
EXTRACT(MONTH FROM x)
EXTRACT(MONTH FROM x) EXTRACT(MONTH FROM x) EXTRACT(MONTH FROM x) strftime('%m', x) ?
? 从 DATE 或 DATETIME x 中提取月份中的日期 EXTRACT(DAY FROM x) ? EXTRACT(DAY FROM x) DAY(x)
DATE_PART(DAY, x)[1]
? DAY(x)
DATEPART(day, x)
DAYOFMONTH(x)
DAY(x)
EXTRACT(DAY FROM x)
EXTRACT(DAY FROM x) EXTRACT(DAY FROM x) EXTRACT(DAY FROM x) strftime('%d', x) ?
? 从 TIME 或 DATETIME x 中提取小时数(0…23) EXTRACT(HOUR FROM x) ? EXTRACT(HOUR FROM x) HOUR(x)
DATE_PART(HOUR, x)[1]
? DATEPART(hour, x) HOUR(x)
EXTRACT(HOUR FROM x)
EXTRACT(HOUR FROM x) EXTRACT(HOUR FROM x) EXTRACT(HOUR FROM x) strftime('%H', x) ?
  1. a b c d 这些函数仅适用于 Ingres 中的ingresdate 数据类型;用ansidate 数据类型表示的普通日期应首先转换为ingresdate
华夏公益教科书