Teradata SQL Tricks for SQL Server/Oracle Developers

264 views 0 comments last modified about 8 months ago Raymond

teradata SQL

In this page

Context

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.

Select from Dummy

In Teradata, the syntax is very similar to SQL Server when selecting data from dummies.

SQL Server T-SQL or Teradata

SELECT CURRENT_TIMESTAMP

Oracle PL/SQL

SELECT CURRENT_TIMESTAMP from dual

One Limitation in Teradata

You cannot select without an object name when it is placed in union.

For example, the following SQL will encounter one error:

SELECT CURRENT_TIMESTAMP AS mytime
UNION
SELECT CURRENT_TIMESTAMP + CAST('10-01' AS INTERVAL YEAR(2) TO MONTH);

Executed as Single statement.  Failed [3888 : 42000] A SELECT for a UNION,INTERSECT or MINUS must reference a table.

One trick it to use one dummy table:

WITH dummy(num) AS
     (
     SELECT 1 AS num)
     SELECT CURRENT_TIMESTAMP AS mytime
         FROM dummy
     UNION
     SELECT CURRENT_TIMESTAMP + CAST('10-01' AS INTERVAL YEAR(2) TO MONTH)
         FROM dummy;

Local Variables in Direct SQL Statements

In Teradata, you can only define local variables in macros, procedures and functions. In SQL Server or Oracle you can define it very easily.

Oracle PL/SQL

DECLARE id NUMBER := 1000;

SQL Server T-SQL

DECLARE @id integer = 1000;

Teradata

As we did for UINON example, we can use the same trick for local variables. You can extend the queries to joins and etc.

WITH variables(months) AS
     (
     SELECT 10 AS months)
     SELECT ADD_MONTHS(CURRENT_TIMESTAMP, v.months)
         FROM variables AS v;

Or the simplest way is to use literal values directly in your query instead of using ‘variables’.

SELECT ADD_MONTHS(CURRENT_TIMESTAMP, 10)

Summary

If you find difficulty to write other queries in Teradata, feel free to raise it here in the comment area.

Related pages

Useful DBC (Data Base Computer) System Views in Teradata

8 views   0 comments last modified about 3 days 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,...

View detail

Install Teradata Express 15.0.0.8 by Using VMware Player 6.0 in Windows

11421 views   23 comments last modified about 4 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): ...

View detail

Connect to Teradata database through Python

543 views   0 comments last modified about 5 months 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 ...

View detail

Teradata Tutorials Summary - October 2017

152 views   0 comments last modified about 8 months 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).

View detail

Add comment

Comments (0)

No comments yet.