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.

info Last modified by Raymond at 3 years ago * This page is subject to Site terms.

More from Kontext

local_offer teradata local_offer python

visibility 150
thumb_up 0
access_time 26 days ago

Pandas is commonly used by Python users to perform data operations. In many scenarios, the results need to be saved to a storage like Teradata. This article shows you how to do that easily using JayDeBeApi or  ...

open_in_new View open_in_new Spark + PySpark

local_offer teradata local_offer SQL

visibility 26
thumb_up 0
access_time 2 months ago

In SQL Server, we can use TRUNCATE statement to clear all the records in a table and it usually performs better compared with DELETE statements as no transaction log for each individual row deletion. The syntax looks like the following: TRUNCATE TABLE { database_name.schema_name.tab...

open_in_new View open_in_new Code snippets

local_offer teradata local_offer python local_offer Java

visibility 106
thumb_up 0
access_time 2 months ago

Python JayDeBeApi module allows you to connect from Python to Teradata databases using Java JDBC drivers. In article Connect to Teradata database through Python , I showed ho...

open_in_new View open_in_new Python Programming

local_offer teradata local_offer SQL

visibility 112
thumb_up 0
access_time 2 months ago

OREPLACE functions in Teradata can be used to replace or remove characters from a string. OREPACE is Teradata's extension to ASNI SQL. The usual REPLACE function is not available. ANSI SQL REPLACE function REPLACE function is commonly implemented in many other SQL databases such as ...

open_in_new View open_in_new Code snippets

info About author

Dark theme mode

Dark theme mode is available on Kontext.

Learn more arrow_forward

Kontext Column

Created for everyone to publish data, programming and cloud related articles. Follow three steps to create your columns.


Learn more arrow_forward