Teradata SQLReference

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

SQL Query --- CLI --- TDP – PDE (

Appendix C: Data Types and Format Symbols


Overview

BYTEINT SMALLINT INTEGER

DECIMAL(n,f) NUMERIC(n,f) BIGINT

FLOAT REAL DOUBLE PRECISION

BYTE(n) VARBYTE(n) BLOB

CHAR(n) VARCHAR(n) LONG VARCHAR

GRAPHIC(n) VARGRAPHIC(n) LONG VARGRAPHIC

DATE TIME(n) TIMESTAMP(n)

TIME(n) WITH TIMEZONE CLOB

TIMESTAMP(n) WITH TIMEZONE

INTERVAL YEAR(n)

INTERVAL MONTH(n)

INTERVAL DAY(n)

INTERVAL HOUR(n)

INTERVAL MINUTE(n)
The following table is a chart of all the ANSI Standard Data Types that Teradata supports.
Data Type Description Data Value Range
INTEGER Signed whole number -2,147,483,648 to 2,147,483,647
SMALLINT Signed smaller whole number -32,768 to 32,767
DECIMAL (X,Y) Signed decimal number 18 digits on Largest value DEC(18,0)
Where: X=1 thru 18, either side of the decimal point Smallest value DEC(18,18)
total number of digits in
the number
And Y=0 thru 18 digits
to the right of the
decimal
NUMERIC (X,Y) Synonym for DECIMAL Same as DECIMAL
Same as DECIMAL
FLOAT Floating Point Format (IEEE) <value>×10307 to <value>×10-308
REAL Stored internally as FLOAT
PRECISION Stored internally as FLOAT
DOUBLE PRECISION Stored internally as FLOAT
CHARACTER (X) Fixed length character string, 1 byte of 1 to 64,000 characters long,
CHAR (X) storage per character, pads to length with space

Where: X=1 thru 64000


VARCHAR (X) Variable length character string, 1 1 to 64,000 characters as a
CHARACTER byte of storage per character, plus 2 maximum. The system only
VARYING (X) bytes to record length of actual data stores the characters presented
to it
CHAR VARYING (X)
Where: X=1 thru 64000
DATE Signed internal representation of Currently to the year 3500 as a
YYYMMDD (YYY represents the positive number and back into
number of years from 1900, i.e. 100 AD years as a negative number.
for Year 2000)
TIME Identifies a field as a TIME value with
Hour, Minutes and Seconds
TIMESTAMP Identifies a field as a TIMESTAMP
value with Year, Month, Day, Hour,
Minute, and Seconds

Teradata also has its own data types that are acknowledged as Teradata Extensions:
Data Type Description Data Value Range
BYTEINT Signed whole number -128 to 127
BYTE (X) Binary 1 to 64,000 bytes
Where: X=1 thru
64000
VARBYTE (X) Variable length binary 1 to 64,000 bytes
Where: X=1 thru
64000
Data Type Description Data Value Range
LONG VARCHAR Variable length string 64,000 characters (maximum data length) The
system only stores the characters provided, not
trailing spaces.)
GRAPHIC (X) Fixed length string of 16-bit 1 to 32,000 KANJI characters
Where: X=1 thru bytes (2 bytes per character)
32000
VARGRAPHIC (X) Variable length string of 16- 1 to 32,000 characters as a maximum. The
Where: X=1 thru bit bytes system only stores characters provided.
32000
Format Symbols
Formatting symbols are very powerful. These symbols allow the user to control the way data can be
inserted into a row and displayed in the result set. It provides for more predictable result sets.
Hint - Be sure to keep in mind that the ODBC will ignore formatting, unless you trick it.
Man To see true formatting, the user should use BTEQ.

Basic Numeric and Character Data Formatting Symbols


Symbol Mask character and how used
X or x Character data. Each X represents one character. Can repeat value- i.e. XXXXX or X(5).
9 Decimal digit. Holds place for numeric digit for a display 0 through 9. All leading zeroes are
shown if the format mask is longer than the data value. Can repeat value- i.e. 99999 or
9(5).
V or v Implied decimal point. Aligns data on a decimal value. Primarily used on imported data
without actual decimal point.
E or e Exponential. Aligns the end of the mantissa and the beginning of the exponent.
G or g Graphic data. Each G represents one logical (double byte- KANJI or Katakana) character.
Can repeat value- i.e. GGGGG or G(5).

Advanced Numeric and Character Formatting Symbols


Symbol Mask character and how used
$ Fixed or floating dollar sign. Inserts a $ or leaves spaces and moves (floats) over to the
first character of a currency value. With the proper keyboard, additional currency signs are
available: Cent, Pound and Yen.
, Comma. Inserted where appears in format mask. Used primarily to make large numbers
easier to read.
. Period. Primary use to align decimal point position. Also used for: dates and comma in
some currencies.
- Dash character. Inserted where appears in format mask. Used primarily for dates and
negative numeric values. Also used for: phone numbers, zip codes, and social security
(USA).
/ Slash character. Inserted where appears in format mask. Used primarily for dates.
% Percent character. Inserted where appears in format mask. Used primarily for display of
percentage - i.e. 99% vs. .99
Z or z Zero-suppressed decimal digit. Holds place for numeric digit displays 1 through 9 and 0,
when significant. All leading zeroes (insignificant) are shown as space since their presence
does not change the value of the number being displayed.
Symbol Mask character and how used
B or b Blank data. Insert a space where appears in format mask.
SELECT 'ABCDE' (FORMAT 'XxX') AS Fmt_Shorter
,2014859999 (FORMAT '999-999-9999') AS Fmt_Phone
,1021.53 (FORMAT 'ZZZZZZ9.9999') AS Z_press
,991001 (FORMAT '$$$$,$$$.99') AS Fmt_Pay ;

In addition to have the ability to format Numeric and Character Data Types, a user may also format Dates.

Date Formatting Symbols


Symbol Mask character and how used (not case specific)
M or m Month. Allows month to be displayed any where in the date display. When 'MM' is
specified, the numeric (01-12) value is available. When 'MMM' is specified, the three
character (JAN-DEC) value is available.
D or d Day. Allows day to be displayed any where in the date display. When 'DD' is specified, the
numeric (01-31) value is available. When 'DDD' is specified, the three-digit day of the year
(001-366) value is available.
Y or y Year. Allows day to be displayed any where in the date display. The normal 'YY' has been
used for many years for the 20th century with the 19YY assumed. However, since we have
moved into the 21st century, it is recommended that the 'YYYY' be used.
SELECT
991001(date) (FORMAT 'Yyddd') AS Fmt_Julian
,991001(date) (FORMAT 'YYYY-mm-dd') AS Fmt_ANSI
,9991001(date) (FORMAT 'yy/mm/dd') AS Fmt_Teradata ;

Help, Show, Explain, User Information Functions:

Help: Gives the description.

Show: Will show the internal sql command (Like sp_helptext in SQL Server.)

Explain: It will show the set of steps executed inside for a command.

Ex: EXPLAIN
SELECT last_name, first_name
FROM Employee_Table
WHERE Salary > 45000;

User Information Functions


Default Database Command

Moves the scope of the user from one database to <Database Name>. This command can eliminate the
need to qualify a table in a certain database.

Database <Database Name>;


DATABASE SQL_CLASS;

Account

Returns the account number for the current user you are logged in as.
Select ACCOUNT;

SELECT Database Command

This command displays the database that the user is currently located.
Select DATABASE;

SESSION

Retrieves the session number about the current user.


Select SESSION;

Data Conversions

CAST
1. select cast(eno as char(10)) from empmast

2. SELECT Student_id (CHAR(10))


FROM Student_Table;
TITLE:

SELECT eno (TITLE 'Emplyee Name') FROM empmast;

CASESPECIFIC:

SELECT Customer_Name, Customer_Number


FROM Customer_Table
WHERE Customer_Name (CASESPECIFIC) = 'ACE Consulting'

NOT CASESPECIFIC:

SELECT Customer_Name, Customer_Number


FROM Customer_Table
WHERE Customer_Name (NOT CASESPECIFIC) = 'ACE Consulting';

LOWER and UPPER

SELECT LOWER (Course_Name)


FROM Course_Table
WHERE LOWER (Course_Name) = 'database administration';

Aggregation Functions

select dept,count(basic) count_basic,sum(basic) sum_basic,avg(basic) Avg_Basic


from empmast
group by dept
order by dept desc
having Avg_basic > 50000

“Having” is used with derived fields “where” is used with the existing db filed.

Sub Query…:
select deptno,count(sal) count_basic,sum(sal) sum_basic,avg(sal) Avg_Basic
from Memp
where deptno in (select distinct deptno from Mdept)
group by deptno;

ANY:

SELECT Customer_Name, Phone_Number FROM Customer_Table


WHERE Customer_Number = ANY
(SELECT Customer_Number from Order_Table);

EXISTS:

SELECT Customer_Name
FROM Customer_Table AS CUST
WHERE EXISTS
(SELECT * FROM Order_Table AS OT
WHERE CUST.Customer_Number =
OT.Customer_Number) ;

NOT EXISTS:

SELECT Course_Name
FROM Course_Table AS Course
WHERE NOT EXISTS
(SELECT * FROM Student_Course_Table AS STU
WHERE COURSE.Course_Id = STU.Course_Id) ;

Correlated Subquery:

SELECT Last_Name, First_Name, Dept_No, Salary


FROM Employee_Table as emp
WHERE Salary =
(SELECT MAX(Salary) FROM Employee_Table as emt
WHERE emp.Dept_No = emt.Dept_No);

JOINS

select e.empno,e.ename,e.job,e.deptno,d.loc
from Memp e,Mdept d
where e.deptno=d.deptno

You might also like