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. Allow Cookies and Dismiss

Teradata SQL Tricks for SQL Server/Oracle Developers

726 views 0 comments last modified about 11 months ago Raymond

teradata SQL

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

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

View detail

Install Teradata Express 15.0.0.8 by Using VMware Player 6.0 in Windows

12628 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): ...

View detail

Connect to Teradata database through Python

2023 views   0 comments last modified about 9 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

259 views   0 comments last modified about 11 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

Setup Teradata in Microsoft Azure

249 views   0 comments last modified about 11 months ago

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

View detail

Teradata Tutorial – DateTime Related Functions and Examples

1308 views   0 comments last modified about 11 months ago

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

View detail

Add comment

Please login first to add comments.  Log in New user?  Register

Comments (0)

No comments yet.