SQL - CURRENT_TIMEZONE_ID() 函数



SQL 的CURRENT_TIMEZONE_ID()函数是 SQL 中的一个内置函数,用于检索服务器或实例观察到的当前时区的 ID。

CURRENT_TIMEZONE_ID() 函数返回的 ID 在不同的数据库管理系统中可能有所不同。它可能是时区名称或缩写。例如,在Oracle中,此函数可能返回“America/New_York”表示美国东部时间,而在IBM Db2中,它可能返回“EST5EDT”表示相同的时区。

注意 - 在 SQL 中,时区始终设置为 UTC,CURRENT_TIMEZONE_ID 将返回 UTC 时区的 ID。

语法

以下是 SQL CURRENT_TIMEZONE_ID()函数的语法:

CURRENT_TIMEZONE_ID()

参数

此函数不接受任何参数。

示例

以下示例演示了 SQL 中 SQL CURRENT_TIMEZONE_ID() 函数的用法:

SQL> SELECT CURRENT_TIMEZONE_ID() AS CURRENT_TIMEZONE_ID;

输出

当我们执行上述查询时,输出如下所示:

+----------------------+	
| CURRENT_TIMEZONE_ID  |
+----------------------+
| India Standard Time  |
+----------------------+

示例

我们可以使用以下查询检索数据库服务器上的所有时区 ID:

SQL> SELECT * FROM sys.time_zone_info;

输出

如果我们执行上述查询,则结果如下所示:

+-----------------------------------+--------------------+------------------+
| name                              | current_utc_offset | is_currently_dst | 
+-----------------------------------+--------------------+------------------+
| Dateline Standard Time            | -12:00             | 0                |              
| Dateline Standard Time            | -11:00             | 0                |
| UTC-11                            | -10:00             | 0                |
| Aleutian Standard Time            | -10:00             | 0                |
| Hawaiian Standard Time            | -09:30             | 0                |
| Marquesas Standard Time           | -09:00             | 0                |
| Alaskan Standard Time             | -09:00             | 0                |
| UTC-09                            | -08:00             | 0                |
| Pacific Standard Time (Mexico)    | -08:00             | 0                |
| UTC-08                            | -08:00             | 0                |
| Pacific Standard Time             | -07:00             | 0                |
| US Mountain Standard Time         | -07:00             | 0                |
| Mountain Standard Time (Mexico)   | -07:00             | 0                |
| Mountain Standard Time            | -07:00             | 0                |
| Yukon Standard Time               | -06:00             | 0                |
| Central America Standard Time     | -06:00             | 0                |
| Central Standard Time             | -05:00             | 0                |
| Easter Island Standard Time       | -06:00             | 1                |
| Central Standard Time (Mexico)    | -06:00             | 0                |
| Canada Central Standard Time      | -05:00             | 0                |
| SA Pacific Standard Time          | -05:00             | 0                |
| Eastern Standard Time (Mexico)    | -05:00             | 0                |
| Eastern Standard Time             | -05:00             | 0                |
| Haiti Standard Time               | -05:00             | 0                |
| Cuba Standard Time                | -05:00             | 0                |
| US Eastern Standard Time          | -05:00             | 0                |
| Turks And Caicos Standard Time    | -03:00             | 0                |
| Paraguay Standard Time            | -04:00             | 1                |
| Atlantic Standard Time            | -04:00             | 0                |
| Venezuela Standard Time           | -04:00             | 0                |
| Central Brazilian Standard Time   | -04:00             | 0                |
| SA Western Standard Time          | -03:00             | 0                |
| Pacific SA Standard Time          | -03:30             | 1                |
| Newfoundland Standard Time        | -03:00             | 0                |
| Tocantins Standard Time           | -03:00             | 0                |
| E. South America Standard Time    | -03:00             | 0                |
| SA Eastern Standard Time          | -03:00             | 0                |
| Argentina Standard Time           | -03:00             | 0                |
| Greenland Standard Time           | -03:00             | 0                |
| Montevideo Standard Time          | -03:00             | 0                |
| Magallanes Standard Time          | -03:00             | 0                |
| Saint Pierre Standard Time        | -03:00             | 0                |
| Bahia Standard Time               | -02:00             | 0                |
| UTC-02                            | -02:00             | 0                |
| Mid-Atlantic Standard Time        | -01:00             | 0                |
| Azores Standard Time              | -01:00             | 0                |
| Cape Verde Standard Time          | +00:00             | 0                |
| UTC                               | +00:00             | 0                |
| GMT Standard Time                 | +00:00             | 0                |
| Greenwich Standard Time           | +00:00             | 0                |
| Sao Tome Standard Time            | +01:00             | 0                |
| Morocco Standard Time             | +01:00             | 1                |
| W. Europe Standard Time           | +01:00             | 0                |
| Central Europe Standard Time      | +01:00             | 0                |
| Romance Standard Time             | +01:00             | 0                |
| Central European Standard Time    | +01:00             | 0                |
| W. Central Africa Standard Time   | +02:00             | 0                |
| GTB Standard Time                 | +02:00             | 0                |
| Middle East Standard Time         | +02:00             | 0                |
| Egypt Standard Time               | +02:00             | 0                |
| E. Europe Standard Time           | +02:00             | 0                |
| Syria Standard Time               | +02:00             | 0                |
| West Bank Standard Time           | +02:00             | 0                |
| South Africa Standard Time        | +02:00             | 0                |
| FLE Standard Time                 | +02:00             | 0                |
| Israel Standard Time              | +02:00             | 0                |
| South Sudan Standard Time         | +02:00             | 0                |
| Kaliningrad Standard Time         | +02:00             | 0                |
| Sudan Standard Time               | +02:00             | 0                |
| Libya Standard Time               | +02:00             | 0                |
| Namibia Standard Time             | +03:00             | 0                |
| Jordan Standard Time              | +03:00             | 0                |
| Arabic Standard Time              | +03:00             | 0                |
| Turkey Standard Time              | +03:00             | 0                |
| Arab Standard Time                | +03:00             | 0                |
| Belarus Standard Time             | +03:00             | 0                |
| Russian Standard Time             | +03:00             | 0                |
| E. Africa Standard Time           | +03:00             | 0                |
| Volgograd Standard Time           | +03:30             | 0                |
| Iran Standard Time                | +04:00             | 0                |
| Arabian Standard Time             | +04:00             | 0                |
| Astrakhan Standard Time           | +04:00             | 0                |
| Azerbaijan Standard Time          | +04:00             | 0                |
| Russia Time Zone 3                | +04:00             | 0                |
| Mauritius Standard Time           | +04:00             | 0                |
| Saratov Standard Time             | +04:00             | 0                |
| Georgian Standard Time            | +04:00             | 0                |
| Caucasus Standard Time            | +04:30             | 0                |
| Afghanistan Standard Time         | +05:00             | 0                |
| West Asia Standard Time           | +05:00             | 0                |
| Ekaterinburg Standard Time        | +05:00             | 0                |
| Pakistan Standard Time            | +05:00             | 0                |
| Qyzylorda Standard Time           | +05:30             | 0                |
| India Standard Time               | +05:30             | 0                |
| Sri Lanka Standard Time           | +05:45             | 0                |
| Nepal Standard Time               | +06:00             | 0                |
| Central Asia Standard Time        | +06:00             | 0                |
| Bangladesh Standard Time          | +06:00             | 0                |
| Omsk Standard Time                | +06:30             | 0                |
| Myanmar Standard Time             | +07:00             | 0                |
| SE Asia Standard Time             | +07:00             | 0                |
| Altai Standard Time               | +07:00             | 0                |
| W. Mongolia Standard Time         | +07:00             | 0                |
| North Asia Standard Time          | +07:00             | 0                |
| N. Central Asia Standard Time     | +07:00             | 0                |
| Tomsk Standard Time               | +08:00             | 0                |
| China Standard Time               | +08:00             | 0                |
| North Asia East Standard Time     | +08:00             | 0                |
| Singapore Standard Time           | +08:00             | 0                |
| W. Australia Standard Time        | +08:00             | 0                |
| Taipei Standard Time              | +08:00             | 0                |
| Ulaanbaatar Standard Time         | +08:45             | 0                |
| Aus Central W. Standard Time      | +09:00             | 0                |
| Transbaikal Standard Time         | +09:00             | 0                |
| Tokyo Standard Time               | +09:00             | 0                |
| North Korea Standard Time         | +09:00             | 0                |
| Korea Standard Time               | +09:00             | 0                |
| Yakutsk Standard Time             | +10:30             | 0                |
| Cen. Australia Standard Time      | +09:30             | 1                |
| AUS Central Standard Time         | +10:00             | 0                |
| E. Australia Standard Time        | +11:00             | 0                |
| AUS Eastern Standard Time         | +10:00             | 1                |
| West Pacific Standard Time        | +11:00             | 0                |
| Tasmania Standard Time            | +10:00             | 1                |
| Vladivostok Standard Time         | +11:00             | 0                |
| Lord Howe Standard Time           | +11:00             | 1                |
| Bougainville Standard Time        | +11:00             | 0                |
| Russia Time Zone 10               | +11:00             | 0                |
| Magadan Standard Time             | +12:00             | 0                |
| Norfolk Standard Time             | +11:00             | 1                |
| Sakhalin Standard Time            | +11:00             | 0                |
| Central Pacific Standard Time     | +12:00             | 0                |
| Russia Time Zone 11               | +13:00             | 0                |
| New Zealand Standard Time         | +12:00             | 1                |
| UTC+12                            | +12:00             | 0                |
| Fiji Standard Time                | +12:00             | 0                |
| Kamchatka Standard Time           | +13:45             | 0                |
| Chatham Islands Standard Time     | +13:00             | 1                |
| UTC+13                            | +13:00             | 0                |
| Tonga Standard Time               | +14:00             | 0                |
| Samoa Standard Time               | +14:00             | 1                |
| Line Islands Standard Time        | +14:00             | 0                |  
+-----------------------------------+--------------------+------------------+

示例

假设我们使用 CREATE 语句在 SQL 数据库中创建了一个名为 STUDENTS 的表,如下面的查询所示:

SQL> CREATE TABLE STUDENTS(ID INT NOT NULL, NAME VARCHAR (200) NOT NULL, AGE INT NOT NULL);	

现在,让我们使用 INSERT 语句在 STUDENTS 表中插入一些记录,如下面的查询所示:

SQL> INSERT INTO STUDENTS(ID, NAME, AGE) VALUES(1, 'Dhruv', '20');
INSERT INTO STUDENTS(ID, NAME, AGE) VALUES(2, 'Arjun', '23');
INSERT INTO STUDENTS(ID, NAME, AGE) VALUES(3, 'Dev', '25');
INSERT INTO STUDENTS(ID, NAME, AGE) VALUES(4, 'Riya', '19');
INSERT INTO STUDENTS(ID, NAME, AGE) VALUES(5, 'Aarohi','24');
INSERT INTO STUDENTS(ID, NAME, AGE) VALUES(6, 'Lisa', '20');
INSERT INTO STUDENTS(ID, NAME, AGE) VALUES(7, 'Roy', '24');

我们可以使用以下查询验证表是否已创建:

SQL> SELECT * FROM STUDENTS;

STUDENTS 表已成功在 SQL 数据库中创建。

+----+--------+-----+
| ID | NAME   | AGE |
+----+--------+-----+
| 1  | Dhruv  | 20  |
| 2  | Arjun  | 23  |
| 3  | Dev    | 25  |
| 4  | Riya   | 19  | 
| 5  | Aarohi | 24  |
| 6  | Lisa   | 20  | 
| 7  | Roy    | 24  |
+----+--------+-----+

我们可以使用以下查询检索学生的当前时区 ID:

SQL> SELECT *, CURRENT_TIMEZONE_ID() AS CURRENT_TIMEZONE_IDS FROM STUDENTS;

输出

上述查询的输出如下所示:

+----+--------+-----+----------------------+
| ID | NAME   | AGE | CURRENT_TIMEZONE_IDS |
-----+--------+-----+----------------------+
| 1  | Dhruv  | 20  | India Standard Time  |
| 2  | Arjun  | 23  | India Standard Time  |
| 3  | Dev    | 25  | India Standard Time  |
| 4  | Riya   | 19  | India Standard Time  | 
| 5  | Aarohi | 24  | India Standard Time  |
| 6  | Lisa   | 20  | India Standard Time  |
| 7  | Roy    | 24  | India Standard Time  |
+----+--------+-----+----------------------+
sql-date-functions.htm
广告