---
title: "Date/time functions: ADDDATE, DATEDIFF, EXTRACT, TRUNC, FROM_UNIXTIME and more"
description: "CoraDB date and time functions: current time, adding intervals, date differences, extracting fields, truncating, Unix time and time zones."
url: "https://www.coradb.ai/manual/sql/function/datetime_fn.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/function/datetime_fn.md"
product: "CoraDB"
version: "1.0"
keywords: "coradb date functions, adddate, date_add, datediff, extract, add_months, last_day, months_between, trunc date, round date, from_unixtime, unix_timestamp, sysdate, now, timediff, from_tz, new_time, tz_offset"
---

# Date/time functions: ADDDATE, DATEDIFF, EXTRACT, TRUNC, FROM_UNIXTIME and more

> **Time zones in the examples:**
> Functions that use the session time zone (the current date and time, Unix time, and the `...LTZ` types) give different results in different time zones. Their examples show the results for a session whose time zone is UTC, set with `SET TIME ZONE 'UTC';`. Examples that show the current date or time are sample output: your values depend on when you run them.


## ADDDATE, DATE_ADD


```sql
ADDDATE (date, INTERVAL expr unit)
```


```sql
ADDDATE (date, days)
```


```sql
DATE_ADD (date, INTERVAL expr unit)
```

The **ADDDATE** function performs an addition or subtraction operation on a specific **DATE** value; **ADDDATE** and **DATE_ADD** are used interchangeably. The return value is a **DATE** or **DATETIME** type. The **DATETIME** type is returned in the following cases.

- The first argument is a **DATETIME** or **TIMESTAMP** type
- The first argument is a **DATE** type and the unit of **INTERVAL** value specified is less than the unit of day

Therefore, to return value of **DATETIME** type, you should convert the value of first argument by using the **CAST** function. Even though the date resulting from the operation exceeds the last day of the month, the function returns a valid **DATE** value considering the last date of the month.

If every input argument value of date and time is 0, the return value is determined by the **return_null_on_function_errors** system parameter; if it is set to yes, then **NULL** is returned; if it is set to no, an error is returned. The default value is **no**.

If the calculated value is between '0000-00-00 00:00:00' and '0001-01-01 00:00:00', a value having 0 for all arguments is returned in **DATE** or **DATETIME** type. Note that operation in JDBC program is determined by the configuration of zeroDateTimeBehavior, connection URL property. For more information about JDBC connection URL, please see [Configuration connection](https://www.coradb.ai/manual/api/jdbc.html#configuration-connection).

**Parameters**

- `date`: It is a **DATE**, **TIMETIME**, or **TIMESTAMP** expression that represents the start date. If an invalid **DATE** value such as '2006-07-00' is specified, an error is returned.
- `expr`: It represents the interval value to be added to the start date. If a negative number is specified next to the **INTERVAL** keyword, the interval value is subtracted from the start date.
- `unit`: It represents the unit of the interval value specified in the *expr* expression. See the following table to specify the format for the interpretation of the interval value. If the value of *expr* unit is less than the number requested in the *unit*, it is specified from the smallest unit. For example, if it is HOUR_SECOND, three values such as 'HOURS:MINUTES:SECONDS' are required. In the case, if only two values such as "1:1" are given, it is regarded as 'MINUTES:SECONDS'.

**Return type:** **DATE** or **DATETIME**

**Format of expr for unit**

| unit value         | expr format                               | Example                                                      |
|--------------------|-------------------------------------------|--------------------------------------------------------------|
| MILLISECOND        | MILLISECONDS                              | ADDDATE(SYSDATE, INTERVAL 123 MILLISECOND)                   |
| SECOND             | SECONDS                                   | ADDDATE(SYSDATE, INTERVAL 123 SECOND)                        |
| MINUTE             | MINUTES                                   | ADDDATE(SYSDATE, INTERVAL 123 MINUTE)                        |
| HOUR               | HOURS                                     | ADDDATE(SYSDATE, INTERVAL 123 HOUR)                          |
| DAY                | DAYS                                      | ADDDATE(SYSDATE, INTERVAL 123 DAY)                           |
| WEEK               | WEEKS                                     | ADDDATE(SYSDATE, INTERVAL 123 WEEK)                          |
| MONTH              | MONTHS                                    | ADDDATE(SYSDATE, INTERVAL 12 MONTH)                          |
| QUARTER            | QUARTERS                                  | ADDDATE(SYSDATE, INTERVAL 12 QUARTER)                        |
| YEAR               | YEARS                                     | ADDDATE(SYSDATE, INTERVAL 12 YEAR)                           |
| SECOND_MILLISECOND | 'SECONDS.MILLISECONDS'                    | ADDDATE(SYSDATE, INTERVAL '12.123' SECOND_MILLISECOND)       |
| MINUTE_MILLISECOND | 'MINUTES:SECONDS.MILLISECONDS'            | ADDDATE(SYSDATE, INTERVAL '12:12.123' MINUTE_MILLISECOND)    |
| MINUTE_SECOND      | 'MINUTES:SECONDS'                         | ADDDATE(SYSDATE, INTERVAL '12:12' MINUTE_SECOND)             |
| HOUR_MILLISECOND   | 'HOURS:MINUTES:SECONDS.MILLISECONDS'      | ADDDATE(SYSDATE, INTERVAL '12:12:12.123' HOUR_MILLISECOND)   |
| HOUR_SECOND        | 'HOURS:MINUTES:SECONDS'                   | ADDDATE(SYSDATE, INTERVAL '12:12:12' HOUR_SECOND)            |
| HOUR_MINUTE        | 'HOURS:MINUTES'                           | ADDDATE(SYSDATE, INTERVAL '12:12' HOUR_MINUTE)               |
| DAY_MILLISECOND    | 'DAYS HOURS:MINUTES:SECONDS.MILLISECONDS' | ADDDATE(SYSDATE, INTERVAL '12 12:12:12.123' DAY_MILLISECOND) |
| DAY_SECOND         | 'DAYS HOURS:MINUTES:SECONDS'              | ADDDATE(SYSDATE, INTERVAL '12 12:12:12' DAY_SECOND)          |
| DAY_MINUTE         | 'DAYS HOURS:MINUTES'                      | ADDDATE(SYSDATE, INTERVAL '12 12:12' DAY_MINUTE)             |
| DAY_HOUR           | 'DAYS HOURS'                              | ADDDATE(SYSDATE, INTERVAL '12 12' DAY_HOUR)                  |
| YEAR_MONTH         | 'YEARS-MONTHS'                            | ADDDATE(SYSDATE, INTERVAL '12-13' YEAR_MONTH)                |

The examples use order 0, placed on 2021-11-07:

```sql
SELECT order_date, ADDDATE(order_date, INTERVAL 24 HOUR), ADDDATE(order_date, 1), ADDDATE(order_date, -1) FROM orders WHERE id = 0;
```

| order_date | ADDDATE(order_date, INTERVAL 24 HOUR) | ADDDATE(order_date, 1) | ADDDATE(order_date, -1) |
|---|---|---|---|
| 2021-11-07 | 2021-11-08 00:00:00.000 | 2021-11-08 | 2021-11-06 |

Adding an **INTERVAL** to a **DATE** returns a **DATETIME**; adding a number of days returns a **DATE**. A negative number subtracts days.

When the expression gives fewer fields than the unit, the missing leading fields are 0: `'1:20'` with **HOUR_SECOND** means 0 hours, 1 minute and 20 seconds.

```sql
SELECT ADDDATE(DATETIME '2021-11-07 18:18:24.149', INTERVAL '1:20' HOUR_SECOND);
```

| ADDDATE(DATETIME '2021-11-07 18:18:24.149', INTERVAL '1:20' HOUR_SECOND) |
|---|
| 2021-11-07 18:19:44.149 |

```sql
SELECT ADDDATE('0000-00-00', 1 );
```

```text
ERROR: Attempt to use 'zero date'.
```

```sql
SELECT ADDDATE('0001-01-01 00:00:00', -1);
```

```text
ERROR: Conversion error in date format.
```

## ADDTIME

```sql
ADDTIME(expr1, expr2)
```

The **ADDTIME** function adds or subtracts a value of specific time. The first argument is **DATE**, **DATETIME**, **TIMESTAMP**, or **TIME** type and the second argument is **TIME**, **DATETIME**, or **TIMESTAMP** type. Time should be included in the second argument, and the date of the second argument is ignored. The return type for each argument type is follows:

| First argument type | Second argument type                     | Return type | Note                                                     |
|---------------------|------------------------------------------|-------------|----------------------------------------------------------|
| TIME                | TIME, DATETIME, TIMESTAMP                | TIME        | The result value must be equal to or less than 24 hours. |
| DATE                | TIME, DATETIME, TIMESTAMP                | DATETIME    |                                                          |
| DATETIME            | TIME, DATETIME, TIMESTAMP                | DATETIME    |                                                          |
| date/time string    | TIME, DATETIME, TIMESTAMP or time string | VARCHAR     | The result string includes time.                         |

**Parameters**

- `expr1`: **DATE**, **DATETIME**, **TIME** or **TIMESTAMP** type
- `expr2`: **DATETIME**, **TIMESTAMP**, **TIME** type or date/time string

```sql
SELECT ADDTIME(datetime'2007-12-31 23:59:59', time'1:1:2');
```

| ADDTIME(datetime'2007-12-31 23:59:59', time'1:1:2') |
|---|
| 2008-01-01 01:01:01.000 |

```sql
SELECT ADDTIME(time'01:00:00', time'02:00:01');
```

| ADDTIME(time'01:00:00', time'02:00:01') |
|---|
| 03:00:01 |

## ADD_MONTHS

```sql
ADD_MONTHS ( date_argument , month )
```

The **ADD_MONTHS** function adds a *month* value to the expression *date_argument* of **DATE** type, and it returns a **DATE** type value. If the day (*dd*) of the value specified as an argument exists within the month of the result value of the operation, it returns the given day (*dd*); otherwise returns the last day of the given month (*dd*). If the result value of the operation exceeds the expression range of the **DATE** type, it returns an error.

**Parameters**

- `date_argument`: Specifies an expression of **DATE** type. To specify a **TIMESTAMP** or **DATETIME** value, an explicit casting to **DATE** type is required. If the value is **NULL**, **NULL** is returned.
- `month`: Specifies the number of the months to be added to the *date_argument*. Both positive and negative values can be specified. If the given value is not an integer type, conversion to an integer type by an implicit casting (rounding to the first place after the decimal point) is performed. If the value is **NULL**, **NULL** is returned.

```sql
--it returns DATE type value by adding month to the first argument
SELECT ADD_MONTHS(DATE '2008-12-25', 5), ADD_MONTHS(DATE '2008-12-25', -5);
```

| ADD_MONTHS(DATE '2008-12-25', 5) | ADD_MONTHS(DATE '2008-12-25', -5) |
|---|---|
| 2009-05-25 | 2008-07-25 |

```sql
SELECT ADD_MONTHS(DATE '2008-12-31', 5.5), ADD_MONTHS(DATE '2008-12-31', -5.5);
```

| ADD_MONTHS(DATE '2008-12-31', 5.5) | ADD_MONTHS(DATE '2008-12-31', -5.5) |
|---|---|
| 2009-06-30 | 2008-06-30 |

```sql
SELECT order_date, ADD_MONTHS(order_date, 5), LAST_DAY(order_date) FROM orders WHERE id = 0;
```

| order_date | ADD_MONTHS(order_date, 5) | LAST_DAY(order_date) |
|---|---|---|
| 2021-11-07 | 2022-04-07 | 2021-11-30 |

The following are examples of using timezone type values. For timezone related description, see [Date/time types with timezone](https://www.coradb.ai/manual/sql/datatype_datetime.html#datetime-types-with-timezone).

```sql
SELECT ADD_MONTHS (datetimeltz'2001-10-11 10:11:12', 1);
```

| ADD_MONTHS (datetimeltz'2001-10-11 10:11:12', 1) |
|---|
| 2001-11-11 |

```sql
SELECT ADD_MONTHS (datetimetz'2001-10-11 10:11:12 Europe/Paris', 1);
```

| ADD_MONTHS (datetimetz'2001-10-11 10:11:12 Europe/Paris', 1) |
|---|
| 2001-11-11 |

```sql
SELECT ADD_MONTHS (timestampltz'2001-10-11 10:11:12', 1);
```

| ADD_MONTHS (timestampltz'2001-10-11 10:11:12', 1) |
|---|
| 2001-11-11 |

```sql
SELECT ADD_MONTHS (timestamptz'2001-10-11 10:11:12 Europe/Paris', 1);
```

| ADD_MONTHS (timestamptz'2001-10-11 10:11:12 Europe/Paris', 1) |
|---|
| 2001-11-11 |

## CURDATE, CURRENT_DATE


```sql
CURDATE ()
```


```sql
CURRENT_DATE ()
```


```sql
CURRENT_DATE
```

**CURDATE** (), **CURRENT_DATE** and **CURRENT_DATE** () are used interchangeably and they return the current date of session as the **DATE** type (*MM*/*DD*/*YYYY* or *YYYY*-*MM*-*DD*). The unit is day. When the time zone of the current session is same as that of server, these functions are same as `SYS_DATE`, `SYSDATE`. Please refer `SYS_DATE`, `SYSDATE` and the following examples to find a difference and [DBTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#dbtimezone), [SESSIONTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#sessiontimezone) for details of the functions.

If input every argument value of year, month, and day is 0, the return value is determined by the **return_null_on_function_errors** system parameter; if it is set to yes, then **NULL** is returned; if it is set to no, an error is returned. The default value is **no**.

**Return type:** DATE

```sql
SELECT DBTIMEZONE(), SESSIONTIMEZONE();

  dbtimezone            sessiontimezone     
============================================
  'Asia/Seoul'          'Asia/Seoul'        

-- it returns the current date in DATE type

SELECT CURDATE(), CURRENT_DATE(), CURRENT_DATE, SYS_DATE, SYSDATE;

   CURRENT_DATE    CURRENT_DATE    CURRENT_DATE    SYS_DATE    SYS_DATE 
========================================================================
  02/05/2016      02/05/2016      02/05/2016      02/05/2016  02/05/2016
```

```sql
-- it returns the date 60 days added to the current date

SELECT CURDATE()+60;

   CURRENT_DATE +60
===================
  04/05/2016    
```

```sql
-- change session time from 'Asia/Seoul' to 'America/Los_Angeles'

SET TIME ZONE 'America/Los_Angeles';

SELECT DBTIMEZONE(), SESSIONTIMEZONE();

  dbtimezone            sessiontimezone     
============================================
  'Asia/Seoul'          'America/Los_Angeles'

-- Note that CURDATE() and SYS_DATE returns different results

SELECT CURDATE(), CURRENT_DATE(), CURRENT_DATE, SYS_DATE, SYSDATE;

   CURRENT_DATE    CURRENT_DATE    CURRENT_DATE    SYS_DATE    SYS_DATE 
========================================================================
  02/04/2016      02/04/2016      02/04/2016      02/05/2016  02/05/2016
```

## CURRENT_DATETIME, NOW


```sql
CURRENT_DATETIME ()
```


```sql
CURRENT_DATETIME
```


```sql
NOW ()
```

**CURRENT_DATETIME**, **CURRENT_DATETIME** () and **NOW** () are used interchangeably, and they return the current date and time of session in **DATETIME** type. The unit is millisecond. When the time zone of the current session is same as that of server, these functions are same as `SYS_DATETIME`, `SYSDATETIME`. Please also refer `SYS_DATETIME`, `SYSDATETIME` to find a difference and [DBTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#dbtimezone), [SESSIONTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#sessiontimezone) for details of the functions.

**Return type:** DATETIME

```sql
SELECT DBTIMEZONE(), SESSIONTIMEZONE();

  dbtimezone            sessiontimezone     
============================================
  'Asia/Seoul'          'Asia/Seoul'        

-- it returns the current date and time in DATETIME type

SELECT NOW(), SYS_DATETIME;                                                                                                                                                

   CURRENT_DATETIME               SYS_DATETIME                
==============================================================
  04:05:09.292 PM 02/05/2016     04:05:09.292 PM 02/05/2016   
```

```sql
-- it returns the timestamp value 1 hour added to the current sys_datetime value

SELECT TO_CHAR(SYSDATETIME+3600*1000, 'YYYY-MM-DD HH24:MI');

   to_char( SYS_DATETIME +3600*1000, 'YYYY-MM-DD HH24:MI')
======================
  '2016-02-05 17:05'  
```

```sql
-- change session time from 'Asia/Seoul' to 'America/Los_Angeles'

set time zone 'America/Los_Angeles';

SELECT DBTIMEZONE(), SESSIONTIMEZONE();

  dbtimezone            sessiontimezone     
============================================
  'Asia/Seoul'          'America/Los_Angeles'

-- Note that NOW() and SYS_DATETIME return different results

SELECT NOW(), SYS_DATETIME;

   CURRENT_DATETIME               SYS_DATETIME                
==============================================================
  11:08:57.041 PM 02/04/2016     04:08:57.041 PM 02/05/2016   
```

## CURTIME, CURRENT_TIME


```sql
CURTIME ()
```


```sql
CURRENT_TIME
```


```sql
CURRENT_TIME ()
```

**CURTIME** (), **CURRENT_TIME** and **CURRENT_TIME** () are used interchangeably and they return the current time of session as **TIME** type (*HH*:*MI*:*SS*). The unit is second. When the time zone of the current session is same as that of server, these functions are same as `SYS_TIME`, `SYSTIME`. Please also refer `SYS_TIME`, `SYSTIME` to find a difference and [DBTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#dbtimezone), [SESSIONTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#sessiontimezone) for details of the functions.

**Return type:** TIME

```sql
SELECT DBTIMEZONE(), SESSIONTIMEZONE();

  dbtimezone            sessiontimezone     
============================================
  'Asia/Seoul'          'Asia/Seoul'        

-- it returns the current time in TIME type

SELECT CURTIME(), CURRENT_TIME(), CURRENT_TIME, SYS_TIME, SYSTIME;

   CURRENT_TIME    CURRENT_TIME    CURRENT_TIME    SYS_TIME     SYS_TIME  
==========================================================================
  04:22:54 PM     04:22:54 PM     04:22:54 PM     04:22:54 PM  04:22:54 PM
```

```sql
-- change session time from 'Asia/Seoul' to 'America/Los_Angeles'

SET TIME ZONE 'AMERICA/LOS_ANGELES';

SELECT DBTIMEZONE(), SESSIONTIMEZONE();

  dbtimezone            sessiontimezone     
============================================
  'Asia/Seoul'          'America/Los_Angeles'

-- Note that CURTIME() and SYS_TIME return different results

SELECT CURTIME(), CURRENT_TIME(), CURRENT_TIME, SYS_TIME, SYSTIME;

   CURRENT_TIME    CURRENT_TIME    CURRENT_TIME    SYS_TIME     SYS_TIME  
==========================================================================
  11:23:16 PM     11:23:16 PM     11:23:16 PM     04:23:16 PM  04:23:16 PM
```

## CURRENT_TIMESTAMP, LOCALTIME, LOCALTIMESTAMP


```sql
CURRENT_TIMESTAMP
```


```sql
CURRENT_TIMESTAMP ()
```


```sql
LOCALTIME
```


```sql
LOCALTIME ()
```


```sql
LOCALTIMESTAMP
```


```sql
LOCALTIMESTAMP ()
```

**CURRENT_TIMESTAMP**, **CURRENT_TIMESTAMP** (), **LOCALTIME**, **LOCALTIME** (), **LOCALTIMESTAMP** and **LOCALTIMESTAMP** () are used interchangeably and they return the current date and time of session as **TIMESTAMP** type. The unit is second. When the time zone of the current session is same as that of server, these functions are same as `SYS_TIMESTAMP`, `SYSTIMESTAMP`. Please also refer `SYS_TIMESTAMP`, `SYSTIMESTAMP` to find a difference and [DBTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#dbtimezone), [SESSIONTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#sessiontimezone) for details of the functions.

**Return type:** TIMESTAMP

```sql
SELECT DBTIMEZONE(), SESSIONTIMEZONE();

  dbtimezone            sessiontimezone     
============================================
  'Asia/Seoul'          'Asia/Seoul'        

-- it returns the current date and time in TIMESTAMP type of session and server timezones.

SELECT LOCALTIME, SYS_TIMESTAMP;

   CURRENT_TIMESTAMP          SYS_TIMESTAMP           
======================================================
  04:34:16 PM 02/05/2016     04:34:16 PM 02/05/2016   
```

```sql
-- change session time from 'Asia/Seoul' to 'America/Los_Angeles'

SET TIME ZONE 'America/Los_Angeles';

SELECT DBTIMEZONE(), SESSIONTIMEZONE();                                                                                                                                    

  dbtimezone            sessiontimezone     
============================================
  'Asia/Seoul'          'America/Los_Angeles'

-- Note that LOCALTIME() and SYS_TIMESTAMP return different results

SELECT LOCALTIME, SYS_TIMESTAMP;                                                                                                                                           

   CURRENT_TIMESTAMP          SYS_TIMESTAMP           
======================================================
  11:34:37 PM 02/04/2016     04:34:37 PM 02/05/2016   
```

## DATE

```sql
DATE (date)
```

The **DATE** function extracts the date part from specified argument, and returns it as '*MM*/*DD*/*YYYY*' format string. Arguments that can be specified are **DATE**, **TIMESTAMP** and **DATETIME** types. The return value is a **VARCHAR** type.

0 is not allowed in the argument value corresponding to year, month, and day; however, if 0 is inputted in every argument value corresponding to date and time, string where 0 is specified for year, month, and day is returned.

**Parameters**

- `date`: **DATE**, **TIMESTAMP** or **DATETIME** can be specified.

**Return type:** STRING

```sql
SELECT DATE('2010-02-27 15:10:23');
```

| DATE('2010-02-27 15:10:23') |
|---|
| 2010-02-27 |

```sql
SELECT DATE(TIMESTAMP '2021-11-07 10:00:00');
```

| DATE(TIMESTAMP '2021-11-07 10:00:00') |
|---|
| 11/07/2021 |

**DATE** returns a string in the format `MM/DD/YYYY`.

```sql
SELECT DATE('0000-00-00 00:00:00');
```

| DATE('0000-00-00 00:00:00') |
|---|
| 0000-00-00 |

## DATEDIFF

```sql
DATEDIFF (date1, date2)
```

The **DATEDIFF** function returns the difference between two arguments as an integer representing the number of days. Arguments that can be specified are **DATE**, **TIMESTAMP** and **DATETIME** types and its return value is only **INTEGER** type.

If every input argument value of date and time is 0, the return value is determined by the **return_null_on_function_errors** system parameter; if it is set to yes, then **NULL** is returned; if it is set to no, an error is returned. The default value is **no**.

**Parameters**

- `date1,date2`: Specifies the types that include date (**DATE**, **TIMESTAMP** or **DATETIME**) type or string that represents the value of corresponding type. If invalid string is specified, an error is returned.

**Return type:** INT

```sql
SELECT DATEDIFF('2010-2-28 23:59:59','2010-03-02');
```

| DATEDIFF('2010-2-28 23:59:59','2010-03-02') |
|---|
| -2 |

```sql
SELECT DATEDIFF('0000-00-00 00:00:00', '2010-2-28 23:59:59');
```

```text
ERROR: Attempt to use 'zero date'.
```

The following are examples of using timezone type values. For timezone related description, see [Date/time types with timezone](https://www.coradb.ai/manual/sql/datatype_datetime.html#datetime-types-with-timezone).

```sql
SELECT IF(DATEDIFF('2002-03-03 12:00:00 AM','1990-01-01 11:59:59 PM') = DATEDIFF(timestampltz'2002-03-03 12:00:00 AM',timestampltz'1990-01-01 11:59:59 PM'),'ok','nok');
```

| IF(DATEDIFF('2002-03-03 12:00:00 AM','1990-01-01 11:59:59 PM') = DATEDIFF(timestampltz'2002-03-03 12:00:00 AM',timestampltz'1990-01-01 11:59:59 PM'),'ok','nok') |
|---|
| ok |

## DATE_SUB, SUBDATE


```sql
DATE_SUB (date, INTERVAL expr unit)
```


```sql
SUBDATE(date, INTERVAL expr unit)
```


```sql
SUBDATE(date, days)
```

The functions **DATE_SUB** and **SUBDATE** () are used interchangeably and they perform an addition or subtraction operation on a specific **DATE** value. The value is returned in **DATE** or **DATETIME** type. If the date resulting from the operation exceeds the last day of the month, the function returns a valid **DATE** value considering the last date of the month.

If every input argument value of date and time is 0, the return value is determined by the **return_null_on_function_errors** system parameter; if it is set to yes, then **NULL** is returned; if it is set to no, an error is returned. The default value is **no**.

If the calculated value is between '0000-00-00 00:00:00' and '0001-01-01 00:00:00', a value having 0 for all arguments is returned in **DATE** or **DATETIME** type. Note that operation in JDBC program is determined by the configuration of zeroDateTimeBehavior, connection URL property (see [Configuration connection](https://www.coradb.ai/manual/api/jdbc.html#configuration-connection) for details).

**Parameters**

- `date`: It is a **DATE** or **TIMESTAMP** expression that represents the start date. If an invalid **DATE** value such as '2006-07-00' is specified, **NULL** is returned.
- `expr`: It represents the interval value to be subtracted from the start date. If a negative number is specified next to the **INTERVAL** keyword, the interval value is added to the start date.
- `unit`: It represents the unit of the interval value specified in the *exp* expression. To check the expr argument for the unit value, see the table of [ADDDATE](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-adddate).

**Return type:** DATE or DATETIME

```sql
SELECT order_date, SUBDATE(order_date, 1), DATEDIFF(shipped_date, order_date) AS days_to_ship FROM orders WHERE id = 0;
```

| order_date | SUBDATE(order_date, 1) | days_to_ship |
|---|---|---|
| 2021-11-07 | 2021-11-06 | 2 |

```sql
SELECT SUBDATE('0000-00-00 00:00:00', -50);
```

```text
ERROR: Attempt to use 'zero date'.
```

```sql
SELECT SUBDATE('0001-01-01 00:00:00', 10);
```

```text
ERROR: Conversion error in date format.
```

## DAY, DAYOFMONTH


```sql
DAY (date)
```


```sql
DAYOFMONTH (date)
```

The function **DAY** or **DAYOFMONTH** returns day in the range of 1 to 31 from the specified parameter. You can specify the **DATE**, **TIMESTAMP** or **DATETIME** type; the value is returned in **INTEGER** type.

0 is not allowed in the argument value corresponding to year, month, and day; however, if 0 is inputted in every argument value corresponding to year, month, and day, 0 is returned as an exception.

**Parameters**

- `date`: Date

**Return type:** INT

```sql
SELECT DAYOFMONTH('2010-09-09');
```

| DAYOFMONTH('2010-09-09') |
|---|
| 9 |

```sql
SELECT DAY('2010-09-09 19:49:29');
```

| DAY('2010-09-09 19:49:29') |
|---|
| 9 |

```sql
SELECT DAYOFMONTH('0000-00-00 00:00:00');
```

| DAYOFMONTH('0000-00-00 00:00:00') |
|---|
| 0 |

## DAYOFWEEK

```sql
DAYOFWEEK (date)
```

The **DAYOFWEEK** function returns a day in the range of 1 to 7 (1: Sunday, 2: Monday, ..., 7: Saturday) from the specified parameters. The day index is same as the ODBC standards. You can specify the **DATE**, **TIMESTAMP** or **DATETIME** type; the value is returned in **INTEGER** type.

If every input argument value of year, month, and day is 0, the return value is determined by the **return_null_on_function_errors** system parameter; if it is set to yes, then **NULL** is returned; if it is set to no, an error is returned. The default value is **no**.

**Parameters**

- `date`: Date

**Return type:** INT

```sql
SELECT DAYOFWEEK('2010-09-09');
```

| DAYOFWEEK('2010-09-09') |
|---|
| 5 |

```sql
SELECT DAYOFWEEK('2010-09-09 19:49:29');
```

| DAYOFWEEK('2010-09-09 19:49:29') |
|---|
| 5 |

```sql
SELECT DAYOFWEEK('0000-00-00');
```

```text
ERROR: Attempt to use 'zero date'.
```

## DAYOFYEAR

```sql
DAYOFYEAR (date)
```

The **DAYOFYEAR** function returns the day of a year in the range of 1 to 366. You can specify the **DATE**, **TIMESTAMP** or **DATETIME** types; the value is returned in **INTEGER** type.

If every input argument value of year, month, and day is 0, the return value is determined by the **return_null_on_function_errors** system parameter; if it is set to yes, then **NULL** is returned; if it is set to no, an error is returned. The default value is **no**.

**Parameters**

- `date`: Date

**Return type:** INT

```sql
SELECT DAYOFYEAR('2010-09-09');
```

| DAYOFYEAR('2010-09-09') |
|---|
| 252 |

```sql
SELECT DAYOFYEAR('2010-09-09 19:49:29');
```

| DAYOFYEAR('2010-09-09 19:49:29') |
|---|
| 252 |

```sql
SELECT DAYOFYEAR('0000-00-00');
```

```text
ERROR: Attempt to use 'zero date'.
```

## EXTRACT

```sql
EXTRACT ( field FROM date-time_argument )
```

The **EXTRACT** operator extracts the values from *date-time_argument* and then converts the value type into **INTEGER**.

0 is not allowed in the input argument value corresponding to year, month, and day; however, if 0 is inputted in every argument value corresponding to date and time, 0 is returned as an exception.

**Parameters**

- `field`: Specifies a value to be extracted from date-time expression.
- `date-time_argument`: An expression that returns a value of date-time. This expression must be one of **TIME**, **DATE**, **TIMESTAMP**, or **DATETIME** types. If the value is **NULL**, **NULL** is returned.

**Return type:** INT

```sql
SELECT EXTRACT(MONTH FROM DATETIME '2008-12-25 10:30:20.123' );
```

```text
12
```

```sql
SELECT EXTRACT(HOUR FROM DATETIME '2008-12-25 10:30:20.123' );
```

```text
10
```

```sql
SELECT EXTRACT(MILLISECOND FROM DATETIME '2008-12-25 10:30:20.123' );
```

```text
123
```

```sql
SELECT EXTRACT(MONTH FROM '0000-00-00 00:00:00');
```

```text
0
```

The following are examples of using timezone type values. For timezone related description, see [Date/time types with timezone](https://www.coradb.ai/manual/sql/datatype_datetime.html#datetime-types-with-timezone).

```sql
SELECT EXTRACT (MONTH FROM datetimetz'10/15/1986 5:45:15.135 am Europe/Bucharest');
```

```text
10
```

```sql
SELECT EXTRACT (MONTH FROM datetimeltz'10/15/1986 5:45:15.135 am Europe/Bucharest');
```

```text
10
```

```sql
SELECT EXTRACT (MONTH FROM timestampltz'10/15/1986 5:45:15 am Europe/Bucharest');
```

```text
10
```

```sql
SELECT EXTRACT (MONTH FROM timestamptz'10/15/1986 5:45:15 am Europe/Bucharest');
```

```text
10
```

## FROM_DAYS

```sql
FROM_DAYS (N)
```

The **FROM_DAYS** function returns a date value in **DATE** type if **INTEGER** type is inputted as an argument.

It is not recommended to use the **FROM_DAYS** function for dates prior to the year 1582 because the function does not take dates prior to the introduction of the Gregorian Calendar into account.

If a value in the range of 0 to 3,652,424 can be inputted as an argument. If a value in the range of 0 to 365 is inputted, 0 is returned. 3,652,424, which is the maximum value, means the last day of year 9999.

**Parameters**

- `N`: Integer in the range of 0 to 3,652,424

**Return type:** DATE

```sql
SELECT FROM_DAYS(719528);
```

| FROM_DAYS(719528) |
|---|
| 1970-01-01 |

```sql
SELECT FROM_DAYS('366');
```

| FROM_DAYS('366') |
|---|
| 0001-01-03 |

```sql
SELECT FROM_DAYS(3652424);
```

| FROM_DAYS(3652424) |
|---|
| 9999-12-31 |

```sql
SELECT FROM_DAYS(0);
```

| FROM_DAYS(0) |
|---|
| 0000-00-00 |

## FROM_TZ

```sql
FROM_TZ(datetime, timezone_string)
```

Converts date/time type without timezone as date/time type with timezone by adding timezone to DATETIME typed value. Input value's type is DATETIME, and the result value's type is DATETIMETZ.

**Parameters**

- `datetime`: DATETIME
- `timezone_string`: String representing a timezone name or and offset '+05:00', 'Asia/Seoul'.

**Return type:** DATETIMETZ

```sql
SELECT FROM_TZ(datetime '10/10/2014 00:00:00 AM', 'Europe/Vienna');
```

| FROM_TZ(datetime '10/10/2014 00:00:00 AM', 'Europe/Vienna') |
|---|
| 2014-10-10 00:00:00.000 Europe/Vienna CEST |

```sql
SELECT FROM_TZ(datetime '10/10/2014 23:59:59 PM', '+03:25:25');
```

| FROM_TZ(datetime '10/10/2014 23:59:59 PM', '+03:25:25') |
|---|
| 2014-10-10 23:59:59.000 +03:25:25 |

For timezone related description, see [Date/time types with timezone](https://www.coradb.ai/manual/sql/datatype_datetime.html#datetime-types-with-timezone).

> **See also:**
> [DBTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#dbtimezone), [SESSIONTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#sessiontimezone), [NEW_TIME](https://www.coradb.ai/manual/sql/function/datetime_fn.html#new_time), [TZ_OFFSET](https://www.coradb.ai/manual/sql/function/datetime_fn.html#tz_offset)


## FROM_UNIXTIME

```sql
FROM_UNIXTIME ( unix_timestamp[, format] )
```

The **FROM_UNIXTIME** function returns the string of the specified format in **VARCHAR** type if the argument *format* is specified; if the argument *format* is omitted, it returns a value of **TIMESTAMP** type. Specify the argument *unix_timestamp* as an **INTEGER** type that corresponds to the UNIX timestamp. The returned value is displayed in the current time zone.

It displays the result according to the format that you specified, and the date/time format, *format* follows the Date/Time Format 2 table of [DATE_FORMAT](https://www.coradb.ai/manual/sql/function/typecast_fn.html#date_format).

The relation is not one of one-to-one correspondence between **TIMESTAMP** and UNIX timestamp so if you use [UNIX_TIMESTAMP](https://www.coradb.ai/manual/sql/function/datetime_fn.html#unix_timestamp) or **FROM_UNIXTIME** function, partial value could be lost. For details, see [UNIX_TIMESTAMP](https://www.coradb.ai/manual/sql/function/datetime_fn.html#unix_timestamp).

0 is not allowed in the argument value corresponding to year, month, and day; however, if 0 is inputted in every argument value corresponding to date and time, string where 0 is specified for every date and time value is returned. Note that operation in JDBC program is determined by the configuration of zeroDateTimeBehavior, connection URL property (see [Configuration connection](https://www.coradb.ai/manual/api/jdbc.html#configuration-connection) for details).

**Parameters**

- `unix_timestamp`: Positive integer
- `format`: Time format. Follows the date/time format of the [DATE_FORMAT](https://www.coradb.ai/manual/sql/function/typecast_fn.html#date_format).

**Return type:** STRING, INT

```sql
SELECT FROM_UNIXTIME(1234567890);
```

| FROM_UNIXTIME(1234567890) |
|---|
| 2009-02-13 23:31:30 |

```sql
SELECT FROM_UNIXTIME('1000000000');
```

| FROM_UNIXTIME('1000000000') |
|---|
| 2001-09-09 01:46:40 |

```sql
SELECT FROM_UNIXTIME(1234567890,'%M %Y %W');
```

| FROM_UNIXTIME(1234567890,'%M %Y %W') |
|---|
| February 2009 Friday |

```sql
SELECT FROM_UNIXTIME('1234567890','%M %Y %W');
```

| FROM_UNIXTIME('1234567890','%M %Y %W') |
|---|
| February 2009 Friday |

```sql
SELECT FROM_UNIXTIME(0);
```

| FROM_UNIXTIME(0) |
|---|
| 0000-00-00 00:00:00 |

## HOUR

```sql
HOUR (time)
```

The **HOUR** function extracts the hour from the specified parameter and then returns the value in integer. The type **TIME**, **TIMESTAMP** or **DATETIME** can be specified and a value is returned in the **INTEGER** type.

**Parameters**

- `time`: Time

**Return type:** INT

```sql
SELECT HOUR('12:34:56');
```

| HOUR('12:34:56') |
|---|
| 12 |

```sql
SELECT HOUR('2010-01-01 12:34:56');
```

| HOUR('2010-01-01 12:34:56') |
|---|
| 12 |

```sql
SELECT HOUR(datetime'2010-01-01 12:34:56');
```

| HOUR(datetime'2010-01-01 12:34:56') |
|---|
| 12 |

## LAST_DAY

```sql
LAST_DAY ( date_argument )
```

The **LAST_DAY** function returns the last day of the given month as **DATE** type.

If every input argument value of year, month, and day is 0, the return value is determined by the **return_null_on_function_errors** system parameter; if it is set to yes, then **NULL** is returned; if it is set to no, an error is returned. The default value is **no**.

**Parameters**

- `date_argument`: Specifies an expression of **DATE** type. To specify a **TIMESTAMP** or **DATETIME** value, explicit casting to **DATE** is required. If the value is **NULL**, **NULL** is returned.

**Return type:** DATE

```sql
--it returns last day of the month in DATE type
SELECT LAST_DAY(DATE '1980-02-01'), LAST_DAY(DATE '2010-02-01');
```

| LAST_DAY(DATE '1980-02-01') | LAST_DAY(DATE '2010-02-01') |
|---|---|
| 1980-02-29 | 2010-02-28 |

```sql
SELECT LAST_DAY('0000-00-00');
```

```text
ERROR: Attempt to use 'zero date'.
```

## MAKEDATE

```sql
MAKEDATE (year, dayofyear)
```

The **MAKEDATE** function returns a date from the specified parameter. You can specify an **INTEGER** type corresponding to the day of the year in the range of 1 to 9999 as an argument; the value in the range of 1/1/1 to 12/31/9999 is returned in **DATE** type. If the day of the year has passed the corresponding year, it will become the next year. For example, MAKEDATE(1999, 366) will return 2000-01-01. However, if you input a value in the range of 0 to 69 as the year, it will be processed as the year 2000-2069, if it is a value in the range of 70 to 99, it will be processed as the year 1970-1999.

If every value specified in *year* and *dayofyear* is 0, the return value is determined by the **return_null_on_function_errors** system parameter; if it is set to yes, then **NULL** is returned; if it is set to no, an error is returned. The default value is **no**.

**Parameters**

- `year`: Year in the range of 1 to 9999
- `dayofyear`: If you input a value in the range of 0 to 99 in the argument, it is handled as an exception; *dayofyear* must be equal to or less than 3,615,902 and the return value of MAKEDATE(100, 3615902) is 9999/12/31.

**Return type:** DATE

```sql
SELECT MAKEDATE(2010,277);
```

| MAKEDATE(2010,277) |
|---|
| 2010-10-04 |

```sql
SELECT MAKEDATE(10,277);
```

| MAKEDATE(10,277) |
|---|
| 2010-10-04 |

```sql
SELECT MAKEDATE(70,277);
```

| MAKEDATE(70,277) |
|---|
| 1970-10-04 |

```sql
SELECT MAKEDATE(100,3615902);
```

| MAKEDATE(100,3615902) |
|---|
| 9999-12-31 |

```sql
SELECT MAKEDATE(9999,365);
```

| MAKEDATE(9999,365) |
|---|
| 9999-12-31 |

```sql
SELECT MAKEDATE(0,0);
```

```text
ERROR: Conversion error in date format.
```

## MAKETIME

```sql
MAKETIME(hour, min, sec)
```

The **MAKETIME** function returns the hour from specified argument in the AM/PM format. You can specify the **INTEGER** types corresponding hours, minutes and seconds as arguments; the value is returned in **DATETIME**.

**Parameters**

- `hour`: An integer representing the hours in the range of 0 to 23
- `min`: An integer representing the minutes in the range of 0 to 59
- `sec`: An integer representing the minutes in the range of 0 to 59

**Return type:** DATETIME

```sql
SELECT MAKETIME(13,34,4);
```

| MAKETIME(13,34,4) |
|---|
| 13:34:04 |

```sql
SELECT MAKETIME('1','34','4');
```

| MAKETIME('1','34','4') |
|---|
| 01:34:04 |

```sql
SELECT MAKETIME(24,0,0);
```

```text
ERROR: Conversion error in time format.
```

## MINUTE

```sql
MINUTE (time)
```

The **MINUTE** function returns the minutes in the range of 0 to 59 from specified argument. You can specify the **TIME** , **TIMESTAMP** or **DATETIME** type; the value is returned in **INTEGER** type.

**Parameters**

- `time`: Time

**Return type:** INT

```sql
SELECT MINUTE('12:34:56');
```

| MINUTE('12:34:56') |
|---|
| 34 |

```sql
SELECT MINUTE('2010-01-01 12:34:56');
```

| MINUTE('2010-01-01 12:34:56') |
|---|
| 34 |

```sql
SELECT MINUTE('2010-01-01 12:34:56.7890');
```

| MINUTE('2010-01-01 12:34:56.7890') |
|---|
| 34 |

## MONTH

```sql
MONTH (date)
```

The **MONTH** function returns the month in the range of 1 to 12 from specified argument. You can specify the **DATE**, **TIMESTAMP** or **DATETIME** type; the value is returned in **INTEGER** type.

0 is not allowed in the argument value corresponding to year, month, and day; however, if 0 is inputted in every argument value corresponding to date, 0 is returned as an exception.

**Parameters**

- `date`: Date

**Return type:** INT

```sql
SELECT MONTH('2010-01-02');
```

| MONTH('2010-01-02') |
|---|
| 1 |

```sql
SELECT MONTH('2010-01-02 12:34:56');
```

| MONTH('2010-01-02 12:34:56') |
|---|
| 1 |

```sql
SELECT MONTH('2010-01-02 12:34:56.7890');
```

| MONTH('2010-01-02 12:34:56.7890') |
|---|
| 1 |

```sql
SELECT MONTH('0000-00-00');
```

| MONTH('0000-00-00') |
|---|
| 0 |

## MONTHS_BETWEEN

```sql
MONTHS_BETWEEN (date_argument, date_argument)
```

The **MONTHS_BETWEEN** function returns the difference between the given **DATE** value. The return value is **DOUBLE** type. An integer value is returned if the two dates specified as arguments are identical or are the last day of the given month; otherwise, a value obtained by dividing the day difference by 31 is returned.

**Parameters**

- `date_argument`: Specifies an expression of **DATE** type. **TIMESTAMP** or **DATETIME** value also can be used. If the value is **NULL**, **NULL** is returned.

**Return type:** DOUBLE

```sql
--it returns the negative months when the first argument is the previous date
SELECT MONTHS_BETWEEN(DATE '2008-12-31', DATE '2010-6-30');
```

| MONTHS_BETWEEN(DATE '2008-12-31', DATE '2010-6-30') |
|---|
| -18 |

```sql
--it returns integer values when each date is the last date of the month
SELECT MONTHS_BETWEEN(DATE '2010-6-30', DATE '2008-12-31');
```

| MONTHS_BETWEEN(DATE '2010-6-30', DATE '2008-12-31') |
|---|
| 18 |

```sql
SELECT order_date, MONTHS_BETWEEN(order_date, DATE '2021-01-01') FROM orders WHERE id = 0;
```

| order_date | MONTHS_BETWEEN(order_date, DATE '2021-01-01') |
|---|---|
| 2021-11-07 | 10.193548387096774 |

The following are examples of using timezone type values. For timezone related description, see [Date/time types with timezone](https://www.coradb.ai/manual/sql/datatype_datetime.html#datetime-types-with-timezone).

```sql
SELECT MONTHS_BETWEEN(datetimetz'2001-10-11 10:11:12 +02:00', datetimetz'2001-05-11 10:11:12 +03:00');
```

| MONTHS_BETWEEN(datetimetz'2001-10-11 10:11:12 +02:00', datetimetz'2001-05-11 10:11:12 +03:00') |
|---|
| 5 |

## NEW_TIME

```sql
NEW_TIME(src_datetime, src_timezone, dst_timezone)
```

Moves a date value from a timezone to the other timezone. The type of *src_datetime* is DATETIME or TIME, and the return value's type is the same with the *src_datetime*'s value.

**Parameters**

- `src_datetime`: input value of DATETIME or TIME
- `src_timezone`: a region name of a source timezone
- `dst_timezion`: a region name of a target timezone

**Return type:** the same type with a *src_datetime*s type

```sql
SELECT NEW_TIME(datetime '10/10/2014 10:10:10 AM', 'Europe/Vienna', 'Europe/Bucharest');
```

| NEW_TIME(datetime '10/10/2014 10:10:10 AM', 'Europe/Vienna', 'Europe/Bucharest') |
|---|
| 2014-10-10 11:10:10.000 |

TIME type only accept an offset timezone as an input argument; a region name is not allowed.

```sql
SELECT NEW_TIME(time '10:10:10 PM', '+03:00', '+10:00');
```

| NEW_TIME(time '10:10:10 PM', '+03:00', '+10:00') |
|---|
| 05:10:10 |

To see the timezone related description, see [Date/time types with timezone](https://www.coradb.ai/manual/sql/datatype_datetime.html#datetime-types-with-timezone).

> **See also:**
> [DBTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#dbtimezone), [SESSIONTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#sessiontimezone), [FROM_TZ](https://www.coradb.ai/manual/sql/function/datetime_fn.html#from_tz), [TZ_OFFSET](https://www.coradb.ai/manual/sql/function/datetime_fn.html#tz_offset)


## QUARTER

```sql
QUARTER (date)
```

The **QUARTER** function returns the quarter in the range of 1 to 4 from specified argument. You can specify the **DATE**, **TIMESTAMP** or **DATETIME** type; the value is returned in **INTEGER** type.

**Parameters**

- `date`: Date

**Return type:** INT

```sql
SELECT QUARTER('2010-05-05');
```

| QUARTER('2010-05-05') |
|---|
| 2 |

```sql
SELECT QUARTER('2010-05-05 12:34:56');
```

| QUARTER('2010-05-05 12:34:56') |
|---|
| 2 |

```sql
SELECT QUARTER('2010-05-05 12:34:56.7890');
```

| QUARTER('2010-05-05 12:34:56.7890') |
|---|
| 2 |

The following are examples of using timezone type values. For timezone related description, see [Date/time types with timezone](https://www.coradb.ai/manual/sql/datatype_datetime.html#datetime-types-with-timezone).

```sql
SELECT QUARTER('2008-04-01 01:02:03 Asia/Seoul');
```

| QUARTER('2008-04-01 01:02:03 Asia/Seoul') |
|---|
| 2 |

```sql
SELECT QUARTER(datetimetz'2003-12-31 01:02:03.1234 Europe/Paris');
```

| QUARTER(datetimetz'2003-12-31 01:02:03.1234 Europe/Paris') |
|---|
| 4 |

## ROUND

```sql
ROUND(date, fmt)
```

This function rounds date to the unit specified by the format string, *fmt*. It returns a value of DATE type.

The format and its unit and the return value are as follows:

| Format          | Unit    | Return value                                                                          |
|-----------------|---------|---------------------------------------------------------------------------------------|
| 'yyyy' or 'yy'  | year    | A value rounded to year                                                               |
| 'mm' or 'month' | month   | A value rounded to month                                                              |
| 'q'             | quarter | A value rounded to quarter, one of 1/1, 4/1, 7/1, 10/1                                |
| 'day'           | week    | A value rounded to week, this Sunday of *date* week or the next Sunday of *date* week |
| 'dd'            | day     | A value rounded to day                                                                |
| 'hh'            | hour    | A value rounded to hour                                                               |
| 'mi'            | minute  | A value rounded to minute                                                             |
| 'ss'            | second  | A value rounded to second                                                             |

**Parameters**

- `date`: The value of **DATE**, **TIMESTAMP** or **DATETIME**
- `fmt`: Specifies the format for the truncating unit. If omitted, "dd" is default.

**Return type:** DATE

```sql
SELECT ROUND(date'2012-10-26', 'yyyy');
```

| ROUND(date'2012-10-26', 'yyyy') |
|---|
| 2013-01-01 |

```sql
SELECT ROUND(timestamp'2012-10-26 12:10:10', 'mm');
```

| ROUND(timestamp'2012-10-26 12:10:10', 'mm') |
|---|
| 2012-11-01 |

```sql
SELECT ROUND(datetime'2012-12-26 12:10:10', 'dd');
```

| ROUND(datetime'2012-12-26 12:10:10', 'dd') |
|---|
| 2012-12-27 |

```sql
SELECT ROUND(datetime'2012-12-26 12:10:10', 'day');
```

| ROUND(datetime'2012-12-26 12:10:10', 'day') |
|---|
| 2012-12-30 |

```sql
SELECT ROUND(datetime'2012-08-26 12:10:10', 'q');
```

| ROUND(datetime'2012-08-26 12:10:10', 'q') |
|---|
| 2012-10-01 |

```sql
SELECT TRUNC(datetime'2012-08-26 12:10:10', 'q');
```

| TRUNC(datetime'2012-08-26 12:10:10', 'q') |
|---|
| 2012-07-01 |

```sql
SELECT ROUND(datetime'2012-02-28 23:10:00', 'hh');
```

| ROUND(datetime'2012-02-28 23:10:00', 'hh') |
|---|
| 2012-02-28 |

```sql
SELECT ROUND(datetime'2012-02-28 23:58:59', 'hh');
```

| ROUND(datetime'2012-02-28 23:58:59', 'hh') |
|---|
| 2012-02-29 |

```sql
SELECT ROUND(datetime'2012-02-28 23:59:59', 'mi');
```

| ROUND(datetime'2012-02-28 23:59:59', 'mi') |
|---|
| 2012-02-29 |

```sql
SELECT ROUND(datetime'2012-02-28 23:59:59.500', 'ss');
```

| ROUND(datetime'2012-02-28 23:59:59.500', 'ss') |
|---|
| 2012-02-29 |

In order to truncate date instead of rounding, please see [TRUNC(date, fmt)](https://www.coradb.ai/manual/sql/function/datetime_fn.html#trunc).

## SEC_TO_TIME

```sql
SEC_TO_TIME (second)
```

The **SEC_TO_TIME** function returns the time including hours, minutes and seconds from specified argument. You can specify the **INTEGER** type in the range of 0 to 86,399; the value is returned in **TIME** type.

**Parameters**

- `second`: Seconds in the range of 0 to 86,399

**Return type:** TIME

```sql
SELECT SEC_TO_TIME(82800);
```

| SEC_TO_TIME(82800) |
|---|
| 23:00:00 |

```sql
SELECT SEC_TO_TIME('82800.3');
```

| SEC_TO_TIME('82800.3') |
|---|
| 23:00:00 |

```sql
SELECT SEC_TO_TIME(86399);
```

| SEC_TO_TIME(86399) |
|---|
| 23:59:59 |

## SECOND

```sql
SECOND (time)
```

The **SECOND** function returns the seconds in the range of 0 to 59 from specified argument. You can specify the **TIME**, **TIMESTAMP** or **DATETIME**; the value is returned in **INTEGER** type.

**Parameters**

- `time`: Time

**Return type:** INT

```sql
SELECT SECOND('12:34:56');
```

| SECOND('12:34:56') |
|---|
| 56 |

```sql
SELECT SECOND('2010-01-01 12:34:56');
```

| SECOND('2010-01-01 12:34:56') |
|---|
| 56 |

```sql
SELECT SECOND('2010-01-01 12:34:56.7890');
```

| SECOND('2010-01-01 12:34:56.7890') |
|---|
| 56 |

## SYS_DATE, SYSDATE

```sql
SYS_DATE
```

```sql
SYSDATE
```

**SYS_DATE** and **SYSDATE** are used interchangeably and they return the current date of server as the **DATE** type (*MM*/*DD*/*YYYY* or *YYYY*-*MM*-*DD*). The unit is day. When the time zone of the current session is same as that of server, these functions are same as [CURDATE](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-curdate), [CURRENT_DATE](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-current_date) and `CURRENT_DATE`. Please also refer [CURDATE](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-curdate), [CURRENT_DATE](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-current_date) to find a difference and [DBTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#dbtimezone), [SESSIONTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#sessiontimezone) for details of the functions.

If input every argument value of year, month, and day is 0, the return value is determined by the **return_null_on_function_errors** system parameter; if it is set to yes, then **NULL** is returned; if it is set to no, an error is returned. The default value is **no**.

**Return type:** DATE

```sql
SELECT DBTIMEZONE(), SESSIONTIMEZONE();

  dbtimezone            sessiontimezone     
============================================
  'Asia/Seoul'          'Asia/Seoul'        

-- it returns the current date in DATE type

SELECT CURDATE(), CURRENT_DATE(), CURRENT_DATE, SYS_DATE, SYSDATE;

   CURRENT_DATE    CURRENT_DATE    CURRENT_DATE    SYS_DATE    SYS_DATE 
========================================================================
  02/05/2016      02/05/2016      02/05/2016      02/05/2016  02/05/2016
```

```sql
-- it returns the date 60 days added to the current date

SELECT CURDATE()+60;

   CURRENT_DATE +60
===================
  04/05/2016    
```

```sql
-- change session time from 'Asia/Seoul' to 'America/Los_Angeles'

SET TIME ZONE 'America/Los_Angeles';

SELECT DBTIMEZONE(), SESSIONTIMEZONE();

  dbtimezone            sessiontimezone     
============================================
  'Asia/Seoul'          'America/Los_Angeles'

-- Note that CURDATE() and SYS_DATE returns different results

SELECT CURDATE(), CURRENT_DATE(), CURRENT_DATE, SYS_DATE, SYSDATE;

   CURRENT_DATE    CURRENT_DATE    CURRENT_DATE    SYS_DATE    SYS_DATE 
========================================================================
  02/04/2016      02/04/2016      02/04/2016      02/05/2016  02/05/2016
```

## SYS_DATETIME, SYSDATETIME

```sql
SYS_DATETIME
```

```sql
SYSDATETIME
```

**SYS_DATETIME** and **SYSDATETIME** are used interchangeably, and they return the current date and time of server in **DATETIME** type. The unit is millisecond. When the time zone of the current session is same as that of server, these functions are same as [CURRENT_DATETIME](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-current_datetime), `CURRENT_DATETIME`, [NOW](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-now). Please also refer [CURRENT_DATETIME](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-current_datetime), [NOW](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-now) to find a difference and [DBTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#dbtimezone), [SESSIONTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#sessiontimezone) for details of the functions.

**Return type:** DATETIME

```sql
SELECT DBTIMEZONE(), SESSIONTIMEZONE();

  dbtimezone            sessiontimezone     
============================================
  'Asia/Seoul'          'Asia/Seoul'        

-- it returns the current date and time in DATETIME type

SELECT NOW(), SYS_DATETIME;                                                                                                                                                

   CURRENT_DATETIME               SYS_DATETIME                
==============================================================
  04:05:09.292 PM 02/05/2016     04:05:09.292 PM 02/05/2016   
```

```sql
-- it returns the timestamp value 1 hour added to the current sys_datetime value

SELECT TO_CHAR(SYSDATETIME+3600*1000, 'YYYY-MM-DD HH24:MI');

   to_char( SYS_DATETIME +3600*1000, 'YYYY-MM-DD HH24:MI')
======================
  '2016-02-05 17:05'  
```

```sql
-- change session time from 'Asia/Seoul' to 'America/Los_Angeles'

SET TIME ZONE 'America/Los_Angeles';

SELECT DBTIMEZONE(), SESSIONTIMEZONE();

  dbtimezone            sessiontimezone     
============================================
  'Asia/Seoul'          'America/Los_Angeles'

-- Note that NOW() and SYS_DATETIME return different results

SELECT NOW(), SYS_DATETIME;

   CURRENT_DATETIME               SYS_DATETIME                
==============================================================
  11:08:57.041 PM 02/04/2016     04:08:57.041 PM 02/05/2016   
```

## SYS_TIME, SYSTIME

```sql
SYS_TIME
```

```sql
SYSTIME
```

**SYS_TIME** and **SYSTIME** are used interchangeably and they return the current time of server as **TIME** type (*HH*:*MI*:*SS*). The unit is second. When the time zone of the current session is same as that of server, these functions are same as [CURTIME](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-curtime), `CURRENT_TIME`, [CURRENT_TIME](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-current_time). Please also refer [CURTIME](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-curtime), [CURRENT_TIME](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-current_time) to find a difference and [DBTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#dbtimezone), [SESSIONTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#sessiontimezone) for details of the functions.

**Return type:** TIME

```sql
select dbtimezone(), sessiontimezone();

  dbtimezone            sessiontimezone     
============================================
  'Asia/Seoul'          'Asia/Seoul'        

-- it returns the current time in TIME type

SELECT CURTIME(), CURRENT_TIME(), CURRENT_TIME, SYS_TIME, SYSTIME;

   CURRENT_TIME    CURRENT_TIME    CURRENT_TIME    SYS_TIME     SYS_TIME  
==========================================================================
  04:22:54 PM     04:22:54 PM     04:22:54 PM     04:22:54 PM  04:22:54 PM
```

```sql
-- change session time from 'Asia/Seoul' to 'America/Los_Angeles'

SET TIME ZONE 'America/Los_Angeles';

select dbtimezone(), sessiontimezone();

  dbtimezone            sessiontimezone     
============================================
  'Asia/Seoul'          'America/Los_Angeles'

-- Note that CURTIME() and SYS_TIME return different results

SELECT CURTIME(), CURRENT_TIME(), CURRENT_TIME, SYS_TIME, SYSTIME;

   CURRENT_TIME    CURRENT_TIME    CURRENT_TIME    SYS_TIME     SYS_TIME  
==========================================================================
  11:23:16 PM     11:23:16 PM     11:23:16 PM     04:23:16 PM  04:23:16 PM
```

## SYS_TIMESTAMP, SYSTIMESTAMP

```sql
SYS_TIMESTAMP
```

```sql
SYSTIMESTAMP
```

**SYS_TIMESTAMP** and **SYSTIMESTAMP** are used interchangeably and they return the current date and time of server as **TIMESTAMP** type. The unit is second. When the time zone of the current session is same as that of server, these functions are same as `CURRENT_TIMESTAMP`, [CURRENT_TIMESTAMP](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-current_timestamp), `LOCALTIME`, [LOCALTIME](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-localtime), `LOCALTIMESTAMP`, [LOCALTIMESTAMP](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-localtimestamp). Please also refer `CURRENT_TIMESTAMP`, [CURRENT_TIMESTAMP](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-current_timestamp), `LOCALTIME`, [LOCALTIME](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-localtime), `LOCALTIMESTAMP`, [LOCALTIMESTAMP](https://www.coradb.ai/manual/sql/function/datetime_fn.html#fn-localtimestamp) to find a difference and [DBTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#dbtimezone), [SESSIONTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#sessiontimezone) for details of the functions.

**Return type:** TIMESTAMP

```sql
SELECT DBTIMEZONE(), SESSIONTIMEZONE();

  dbtimezone            sessiontimezone     
============================================
  'Asia/Seoul'          'Asia/Seoul'        

-- it returns the current date and time in TIMESTAMP type of session and server timezones.

SELECT LOCALTIME, SYS_TIMESTAMP;

   CURRENT_TIMESTAMP          SYS_TIMESTAMP           
======================================================
  04:34:16 PM 02/05/2016     04:34:16 PM 02/05/2016   
```

```sql
-- change session time from 'Asia/Seoul' to 'America/Los_Angeles'

SET TIME ZONE 'America/Los_Angeles';

SELECT DBTIMEZONE(), SESSIONTIMEZONE();                                                                                                                                    

  dbtimezone            sessiontimezone     
============================================
  'Asia/Seoul'          'America/Los_Angeles'

-- Note that LOCALTIME() and SYS_TIMESTAMP return different results

SELECT LOCALTIME, SYS_TIMESTAMP;                                                                                                                                           

   CURRENT_TIMESTAMP          SYS_TIMESTAMP           
======================================================
  11:34:37 PM 02/04/2016     04:34:37 PM 02/05/2016   
```

## TIME

```sql
TIME (time)
```

The **TIME** function extracts the time part from specified argument and returns the **VARCHAR** type string in the 'HH:MI:SS' format. You can specify the **TIME**, **TIMESTAMP** and **DATETIME** types.

**Parameters**

- `time`: Time

**Return type:** STRING

```sql
SELECT TIME('12:34:56');
```

| TIME('12:34:56') |
|---|
| 12:34:56 |

```sql
SELECT TIME('2010-01-01 12:34:56');
```

| TIME('2010-01-01 12:34:56') |
|---|
| 12:34:56 |

```sql
SELECT TIME(datetime'2010-01-01 12:34:56');
```

| TIME(datetime'2010-01-01 12:34:56') |
|---|
| 12:34:56 |

The following are examples of using timezone type values. For timezone related description, see [Date/time types with timezone](https://www.coradb.ai/manual/sql/datatype_datetime.html#datetime-types-with-timezone).

```sql
SELECT TIME(datetimeltz'2000-12-31 17:34:23.1234 -05:00');
```

| TIME(datetimeltz'2000-12-31 17:34:23.1234 -05:00') |
|---|
| 22:34:23.123 |

```sql
SELECT TIME(datetimetz'2000-12-31 17:34:23.1234 -05:00');
```

| TIME(datetimetz'2000-12-31 17:34:23.1234 -05:00') |
|---|
| 17:34:23.123 |

## TIME_TO_SEC

```sql
TIME_TO_SEC (time)
```

The **TIME_TO_SEC** function returns the seconds in the range of 0 to 86,399 from specified argument. You can specify the **TIME**, **TIMESTAMP** or **DATETIME** type; the value is returned in **INTEGER** type.

**Parameters**

- `time`: Time

**Return type:** INT

```sql
SELECT TIME_TO_SEC('23:00:00');
```

| TIME_TO_SEC('23:00:00') |
|---|
| 82800 |

```sql
SELECT TIME_TO_SEC('2010-10-04 23:00:00');
```

| TIME_TO_SEC('2010-10-04 23:00:00') |
|---|
| 82800 |

```sql
SELECT TIME_TO_SEC('2010-10-04 23:00:00.1234');
```

| TIME_TO_SEC('2010-10-04 23:00:00.1234') |
|---|
| 82800 |

The following are examples of using timezone type values. For timezone related description, see [Date/time types with timezone](https://www.coradb.ai/manual/sql/datatype_datetime.html#datetime-types-with-timezone).

## TIMEDIFF

```sql
TIMEDIFF (expr1, expr2)
```

The **TIMEDIFF** function returns the time difference between the two specified time arguments. You can enter a date/time type, the **TIME**, **DATE**, **TIMESTAMP** or **DATETIME** type and the data types of the two arguments must be identical. The **TIME** will be returned and the time difference between the two arguments must be in the range of 00:00:00 -23:59:59. If it exceeds the range, an error will be returned.

**Parameters**

- `expr1, expr2`: Time. The data types of the two arguments must be identical.

**Return type:** TIME

```sql
SELECT TIMEDIFF(time '17:18:19', time '12:05:52');
```

| TIMEDIFF(time '17:18:19', time '12:05:52') |
|---|
| 05:12:27 |

```sql
SELECT TIMEDIFF('17:18:19','12:05:52');
```

| TIMEDIFF('17:18:19','12:05:52') |
|---|
| 05:12:27 |

```sql
SELECT TIMEDIFF('2010-01-01 06:53:45', '2010-01-01 03:04:05');
```

| TIMEDIFF('2010-01-01 06:53:45', '2010-01-01 03:04:05') |
|---|
| 03:49:40 |

The following are examples of using timezone type values. For timezone related description, see [Date/time types with timezone](https://www.coradb.ai/manual/sql/datatype_datetime.html#datetime-types-with-timezone).

```sql
SELECT TIMEDIFF (datetimeltz'2013-10-28 03:11:12 AM Asia/Seoul', datetimeltz'2013-10-27 04:11:12 AM Asia/Seoul');
```

| TIMEDIFF (datetimeltz'2013-10-28 03:11:12 AM Asia/Seoul', datetimeltz'2013-10-27 04:11:12 AM Asia/Seoul') |
|---|
| 23:00:00 |

## TIMESTAMP

```sql
TIMESTAMP (date [,time])
```

The **TIMESTAMP** function converts a **DATE** or **TIMESTAMP** type expression to **DATETIME** type.

If the **DATE** format string ('*YYYY-MM-DD*' or '*MM/DD/YYYY*') or **TIMESTAMP** format string ('*YYYY-MM-DD HH:MI:SS*' or '*HH:MI:SS MM/DD/ YYYY*') is specified as the first argument, the function returns it as **DATETIME**.

If the **TIME** format string ('*HH:MI:SS*.*FF*') is specified as the second, the function adds it to the first argument and returns the result as a **DATETIME** type. If the second argument is not specified, **12:00:00.000 AM** is specified by default.

**Parameters**

- `date`: The format strings can be specified as follows: '*YYYY*-*MM*-*DD*', '*MM*/*DD*/*YYYY*', '*YYYY*-*MM*-*DD* *HH*:*MI*:*SS*.*FF*', '*HH*:*MI*:*SS*.*FF* *MM*/*DD*/*YYYY*'.
- `time`: The format string can be specified as follows: '*HH*:*MI*:*SS*\[.*FF*\]'.

**Return type:** DATETIME

```sql
SELECT TIMESTAMP('2009-12-31'), TIMESTAMP('2009-12-31','12:00:00');
```

| TIMESTAMP('2009-12-31') | TIMESTAMP('2009-12-31','12:00:00') |
|---|---|
| 2009-12-31 00:00:00.000 | 2009-12-31 12:00:00.000 |

```sql
SELECT TIMESTAMP('2010-12-31 12:00:00','12:00:00');
```

| TIMESTAMP('2010-12-31 12:00:00','12:00:00') |
|---|
| 2011-01-01 00:00:00.000 |

```sql
SELECT TIMESTAMP('13:10:30 12/25/2008');
```

| TIMESTAMP('13:10:30 12/25/2008') |
|---|
| 2008-12-25 13:10:30.000 |

The following are examples of using timezone type values. For timezone related description, see [Date/time types with timezone](https://www.coradb.ai/manual/sql/datatype_datetime.html#datetime-types-with-timezone).

```sql
SELECT TIMESTAMP(datetimetz'2010-12-31 12:00:00 America/New_York', '03:00');
```

| TIMESTAMP(datetimetz'2010-12-31 12:00:00 America/New_York', '03:00') |
|---|
| 2010-12-31 15:00:00.000 |

```sql
SET TIME ZONE 'UTC';
SELECT TIMESTAMP(datetimeltz'2010-12-31 12:00:00 America/New_York', '03:00');
```

```text
08:00:00.000 PM 12/31/2010
```

## TO_DAYS

```sql
TO_DAYS (date)
```

The **TO_DAYS** function returns the number of days after year 0 in the rage of 366 to 3652424 from specified argument. You can specify **DATE** type; the value is returned in **INTEGER** type.

It is not recommended to use the **TO_DAYS** function for dates prior to the year 1582, as the function does not take dates prior to the introduction of the Gregorian Calendar into account.

**Parameters**

- `date`: Date

**Return type:** INT

```sql
SELECT TO_DAYS('2010-10-04');
```

| TO_DAYS('2010-10-04') |
|---|
| 734414 |

```sql
SELECT TO_DAYS('2010-10-04 12:34:56');
```

| TO_DAYS('2010-10-04 12:34:56') |
|---|
| 734414 |

```sql
SELECT TO_DAYS('2010-10-04 12:34:56.7890');
```

| TO_DAYS('2010-10-04 12:34:56.7890') |
|---|
| 734414 |

```sql
SELECT TO_DAYS('1-1-1');
```

| TO_DAYS('1-1-1') |
|---|
| 366 |

```sql
SELECT TO_DAYS('9999-12-31');
```

| TO_DAYS('9999-12-31') |
|---|
| 3652424 |

## TRUNC

```sql
TRUNC( date[, fmt] )
```

This function truncates date to the unit specified by the format string, *fmt*. It returns a value of DATE type.

The format and its unit and the return value are as follows:

| Format          | Unit    | Return value                                                        |
|-----------------|---------|---------------------------------------------------------------------|
| 'yyyy' or 'yy'  | year    | The same year with Jan. 1st                                         |
| 'mm' or 'month' | month   | The same month with 1st                                             |
| 'q'             | quarter | The same quarter with one of Jan. 1st, Apr. 1st, Jul. 1st, Oct. 1st |
| 'day'           | week    | Sunday of the same week(starting date of the week including *date*) |
| 'dd'            | day     | The same date with *date*                                           |

**Parameters**

- `date`: The value of **DATE**, **TIMESTAMP** or **DATETIME**
- `fmt`: Specifies the format for the truncating unit. If omitted, "dd" is default.

**Return type:** DATE

```sql
SELECT TRUNC(date'2012-12-26', 'yyyy');
```

| TRUNC(date'2012-12-26', 'yyyy') |
|---|
| 2012-01-01 |

```sql
SELECT TRUNC(timestamp'2012-12-26 12:10:10', 'mm');
```

| TRUNC(timestamp'2012-12-26 12:10:10', 'mm') |
|---|
| 2012-12-01 |

```sql
SELECT TRUNC(datetime'2012-12-26 12:10:10', 'q');
```

| TRUNC(datetime'2012-12-26 12:10:10', 'q') |
|---|
| 2012-10-01 |

```sql
SELECT TRUNC(datetime'2012-12-26 12:10:10', 'dd');
```

| TRUNC(datetime'2012-12-26 12:10:10', 'dd') |
|---|
| 2012-12-26 |

```sql
// It returns the date of Sunday of the week which includes date'2012-12-26'
SELECT TRUNC(datetime'2012-12-26 12:10:10', 'day');
```

```text
12/23/2012
```

In order to round date instead of truncation, please see [ROUND(date, fmt)](https://www.coradb.ai/manual/sql/function/datetime_fn.html#round).

## TZ_OFFSET

```sql
TZ_OFFSET(timezone_string)
```

This returns a timezone offset from a timezone offset or timezone region name (e.g. '-05:00', or 'Europe/Vienna').

**Parameters**

- `timezone_string`: timezone offset of timezone region name.

**Return type:** STRING

```sql
SELECT TZ_OFFSET('+05:00');
```

```text
'+05:00'
```

```sql
SELECT TZ_OFFSET('Asia/Seoul');
```

```text
'+09:00'
```

For timezone related description, see [Date/time types with timezone](https://www.coradb.ai/manual/sql/datatype_datetime.html#datetime-types-with-timezone).

> **See also:**
> [DBTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#dbtimezone), [SESSIONTIMEZONE](https://www.coradb.ai/manual/sql/function/information_fn.html#sessiontimezone), [FROM_TZ](https://www.coradb.ai/manual/sql/function/datetime_fn.html#from_tz), [NEW_TIME](https://www.coradb.ai/manual/sql/function/datetime_fn.html#new_time)


## UNIX_TIMESTAMP

```sql
UNIX_TIMESTAMP ( [date] )
```

The argument of the **UNIX_TIMESTAMP** function can be omitted. If it is omitted, the function returns the interval between '1970-01-01 00:00:00' UTC and the current system date/time in seconds as **INTEGER** type. If the date argument is specified, the function returns the interval between '1970-01-01 00:00:00' UTC and the specified date/time in seconds.

0 is not allowed in the argument value corresponding to year, month, and day; however, if 0 is given in every argument value corresponding to date and time, 0 is returned as an exception.

Argument of DATETIME type is considered in session timezone.

**Parameters**

- `date`: **DATE** type, **TIMESTAMP** type, **TIMESTAMPTZ** type, **TIMESTAMPLTZ** type, **DATETIME** type, **DATETIMETZ** type, **DATETIMELTZ** type, **DATE** format string ('*YYYY*-*MM*-*DD*' or '*MM*/*DD*/*YYYY*'), **TIMESTAMP** format string ('*YYYY*-*MM*-*DD* *HH*:*MI*:*SS*', '*HH*:*MI*:*SS* *MM*/*DD*/*YYYY*') or '*YYYYMMDD*' format string can be specified.

**Return type:** INT

```sql
SELECT UNIX_TIMESTAMP('1970-01-02'), UNIX_TIMESTAMP();
```

```text
unix_timestamp('1970-01-02')   unix_timestamp()
==================================================
                       54000         1270196737
```

```sql
SELECT UNIX_TIMESTAMP ('0000-00-00 00:00:00');
```

| UNIX_TIMESTAMP ('0000-00-00 00:00:00') |
|---|
| 0 |

```sql
-- when used without argument, it returns the exact value at the moment of execution of each occurrence
SELECT  UNIX_TIMESTAMP(), SLEEP(1), UNIX_TIMESTAMP();

  unix_timestamp()     sleep(1)   unix_timestamp()
===================================================
        1454661297            0         1454661298                                        
```

## UTC_DATE

```sql
UTC_DATE ()
```

The **UTC_DATE** function returns the UTC date in 'YYYY-MM-DD' format.

**Return type:** STRING

```sql
SELECT UTC_DATE();
```

```text
utc_date()
==============
01/12/2011
```

## UTC_TIME

```sql
UTC_TIME ()
```

The **UTC_TIME** function returns the UTC time in 'HH:MI:SS' format.

**Return type:** STRING

```sql
SELECT UTC_TIME();
```

```text
utc_time()
==============
10:35:52 AM
```

## WEEK

```sql
WEEK (date[, mode])
```

The **WEEK** function returns the week in the range of 0 to 53 from specified argument. You can specify the **DATE**, **TIMESTAMP** or **DATETIME** type; the value is returned in **INTEGER** type.

**Parameters**

- `date`: Date
- `mode`: Value in the range of 0 to 7

**Return type:** INT

You can omit the second argument, *mode* and must input a value in the range of 0 to 7. You can set that a week starts from Sunday or Monday and the range of the return value is from 0 to 53 or 1 to 53 with this value. If you omit the *mode*, the system parameter, **default_week_format** value(default: 0) will be used. The *mode* value means as follows:

| mode | Start day of the week | Range | The first week of the year                                        |
|------|-----------------------|-------|-------------------------------------------------------------------|
| 0    | Sunday                | 0~53  | The first week that Sunday is included in the year                |
| 1    | Monday                | 0~53  | The first week that more than three days are included in the year |
| 2    | Sunday                | 1~53  | The first week in the year that includes a Sunday                 |
| 3    | Monday                | 1~53  | The first week in the year that includes more than three days     |
| 4    | Sunday                | 0~53  | The first week in the year that includes more than three days     |
| 5    | Monday                | 0~53  | The first week in the year that includes Monday                   |
| 6    | Sunday                | 1~53  | The first week in the year that includes more than three days     |
| 7    | Monday                | 1~53  | The first week in the year that includes Monday                   |

If the *mode* value is one of 0, 1, 4 or 5, and the date corresponds to the last week of the previous year, the **WEEK** function will return 0. The purpose is to see what nth of the year the week is so it returns 0 for the 52th week of the year 1999.

```sql
SELECT YEAR('2000-01-01'), WEEK('2000-01-01',0);
```

| YEAR('2000-01-01') | WEEK('2000-01-01',0) |
|---|---|
| 2000 | 0 |

To see what n-th the week is based on the year including the start day of the week, use 0, 2, 5 or 7 as the *mode* value.

```sql
SELECT WEEK('2000-01-01',2);
```

| WEEK('2000-01-01',2) |
|---|
| 52 |

```sql
SELECT WEEK('2010-04-05');
```

| WEEK('2010-04-05') |
|---|
| 14 |

```sql
SELECT WEEK('2010-04-05 12:34:56',2);
```

| WEEK('2010-04-05 12:34:56',2) |
|---|
| 14 |

```sql
SELECT WEEK('2010-04-05 12:34:56.7890',4);
```

| WEEK('2010-04-05 12:34:56.7890',4) |
|---|
| 14 |

## WEEKDAY

```sql
WEEKDAY (date)
```

The **WEEKDAY** function returns the day of week in the range of 0 to 6 (0: Monday, 1: Tuesday, ..., 6: Sunday) from the specified parameter. You can specify **DATE**, **TIMESTAMP**, **DATETIME** types as parameters and an **INTEGER** type will be returned.

**Parameters**

- `date`: Date

**Return type:** INT

```sql
SELECT WEEKDAY('2010-09-09');
```

| WEEKDAY('2010-09-09') |
|---|
| 3 |

```sql
SELECT WEEKDAY('2010-09-09 13:16:00');
```

| WEEKDAY('2010-09-09 13:16:00') |
|---|
| 3 |

## YEAR

```sql
YEAR (date)
```

The **YEAR** function returns the year in the range of 1 to 9,999 from the specified parameter. You can specify **DATE**, **TIMESTAMP** or **DATETIME** type; the value is returned in **INTEGER** type.

**Parameters**

- `date`: Date

**Return type:** INT

```sql
SELECT YEAR('2010-10-04');
```

| YEAR('2010-10-04') |
|---|
| 2010 |

```sql
SELECT YEAR('2010-10-04 12:34:56');
```

| YEAR('2010-10-04 12:34:56') |
|---|
| 2010 |

```sql
SELECT YEAR('2010-10-04 12:34:56.7890');
```

| YEAR('2010-10-04 12:34:56.7890') |
|---|
| 2010 |
