This site uses cookies to deliver our services. By using this site, you acknowledge that you have read and understand our Cookie and Privacy policy. Your use of Kontext website is subject to this policy.

# Teradata Tutorial – DateTime Related Functions and Examples

###### 2821 views 0 comments last modified about 2 years ago Raymond

This post summarize the common functions that are used when dealing with DateTime datatypes in Teradata.

### Converting String to DateTime

#### String to Date

SELECT CAST('2017-10-15' AS DATE FORMAT 'YYYY-MM-DD');
SELECT CAST('2017 10 15' AS DATE FORMAT 'YYYYBMMBDD');
SELECT CAST('2017 Oct 15' AS DATE FORMAT 'YYYYBMMMBDD');
SELECT CAST('15, Oct, 2017' AS DATE FORMAT 'DD,BMMM,BYYYY');
-- default ansi format
SELECT CAST('2017-10-15' AS DATE);
-- use to_date function
SELECT to_date('2017-10-15', 'YYYY-MM-DD');

#### String to Time

-- no seconds
SELECT CAST('16h20m' AS TIME(0) FORMAT 'HHhMIm');
-- with seconds and space
SELECT CAST('16h 20m 32s' AS TIME(0) FORMAT 'HHhBMImBSSs');
-- with miliseconds
SELECT CAST('16:20:32.2456 ' AS TIME(6));
-- With PM or AM
SELECT CAST('PM 04:20:32.245 ' AS TIME FORMAT 'TBHH:MI:SS.S(4)B');
SELECT CAST('AM 04:20:32.245 ' AS TIME FORMAT 'TBHH:MI:SS.S(4)B');

#### String to Timestamp

SELECT CAST('2017-10-15 23:59:59.999999' AS TIMESTAMP(6) WITH TIME ZONE);
-- with time zone Z
SELECT CAST('2017-10-15 23:59:59.999999 +10:00' AS TIMESTAMP(6) WITH TIME ZONE FORMAT 'YYYY-MM-DDBHH:MI:SS.S(6)BZ');
-- time zone in the string
SELECT CAST('2017-10-15 23:59:59 +10:00' AS TIMESTAMP(0) FORMAT 'Y4-MM-DDBHH:MI:SS.S(6)BZ');

#### String to Interval

SELECT CAST('-114-25' AS INTERVAL YEAR(4) TO MONTH);
SELECT CAST('-9999-11' AS INTERVAL YEAR(4) TO MONTH);
SELECT CAST('9999-11' AS INTERVAL YEAR(4) TO MONTH);

### Converting DateTime to String

#### Conversions

-- using cast
SELECT CAST(CURRENT_TIMESTAMP AS VARCHAR(100));
SELECT CAST(CURRENT_DATE AS VARCHAR(100));
-- using functions
SELECT to_char(CURRENT_DATE,'YYYY-MM-DD');
SELECT to_char(CURRENT_TIMESTAMP,'MON-DD-YYYY" "HH:MI:SS" "TZH:TZM');
-- time examples
SELECT CAST(TIME AS VARCHAR(100));
SELECT CAST(CURRENT_TIME AS VARCHAR(100));
SELECT to_char(CURRENT_TIME,'HH:MI:SS" "TZH:TZM');

#### Extract Components

SELECT EXTRACT(YEAR
FROM CURRENT_TIMESTAMP);
SELECT EXTRACT(MONTH
FROM CURRENT_TIMESTAMP);
SELECT EXTRACT(DAY
FROM CURRENT_TIMESTAMP);
SELECT EXTRACT(MINUTE
FROM TIME);
SELECT EXTRACT(HOUR
FROM TIME);
SELECT EXTRACT(SECOND
FROM TIME);

-- extract from interval
SELECT EXTRACT(DAY
FROM CAST('99' AS INTERVAL DAY(2)));

### DateTime Comparison and Operations

#### Add Intervals

-- add 10 year and 1 month to the current timestamp
SELECT CURRENT_TIMESTAMP + CAST('10-01' AS INTERVAL YEAR(2) TO MONTH);
-- minus 10 years from the current date
SELECT DATE + CAST('-10-00' AS INTERVAL YEAR(2) TO MONTH);
SELECT ADD_MONTHS(DATE, -12*10);

#### DateTime Comparison

-- Timestamp minus timestamp get one interval
SELECT CURRENT_TIMESTAMP - ADD_MONTHS(CURRENT_TIMESTAMP, -133) YEAR(4) TO MONTH;
SELECT DATE - (DATE + CAST('99' AS INTERVAL DAY(2))) YEAR(4) TO MONTH;

### Related pages

#### Useful DBC (Data Base Computer) System Views in Teradata

###### 931 views   0 comments last modified about 9 months ago

This page summarize some of the commonly used views in Teradata. Conventions In all the views in the following sections, X views are also available though they only return rows that contain information on objects that the requesting database user owns, created, granted privilige on,...

#### Install Teradata Express 15.0.0.8 by Using VMware Player 6.0 in Windows

###### 13436 views   23 comments last modified about 5 years ago

In this article, I am going to introduce how to install Teradata Express in virtual machines in Windows. Download software 1) Download VMware Player for Windows 32-bit and 64-bit from the following link (version 6.0): ...

#### Connect to Teradata database through Python

###### 3576 views   0 comments last modified about 2 years ago

Teradata published an official Python module which can be used in DevOps projects. More details can be found at the following GitHub site: https://github.com/Teradata/PyTd Install Teradata module ...

#### Teradata Tutorials Summary - October 2017

###### 354 views   0 comments last modified about 2 years ago

This post summarizes the tutorials I created in the past about Teradata. It is trying to help you gain the basic SQL skills with Teradata especially if you are a new beginner or you just move from SQL Server or Oracle. It also includes some guides about setting up your own Teradata server (latest Teradata Express version TDExpress16.10.00.03_Sles11_40GB.7z).

#### Setup Teradata in Microsoft Azure

###### 394 views   0 comments last modified about 2 years ago

This page provides the steps to create a Teradata database in Microsoft cloud computing platform Azure.

#### Teradata SQL Tricks for SQL Server/Oracle Developers

###### 1237 views   0 comments last modified about 2 years ago

For many SQL Server or Oracle developers, you may encounter some inconveniences when writing SQL queries. For example, how to select from dummy table or local defined variables. This page summarize the equivalents in Teradata SQL.

### Add comment

Please login first to add comments. New user?

No comments yet.

## Contacts

• enquiry[at]kontext.tech