Teradata Database SQL Functions, Operators, Expressions, and Predicates Release 13.10 B035-1145-109A September 2010

Teradata sur FNAC.com

ou juste avant la balise de fermeture -->

 

 

ou juste avant la balise de fermeture -->

Voir le pdf

 

 

Voir également d'autres Guide Teradata :

Teradata Database SQL Reference Fundamentals Release V2R6.2 B035-1141-096A September 2006

TERADATA/Teradata-Database-SQL-Release-13-10-Functions-september-2010

Teradata Database SQL Functions, Operators, Expressions, and Predicates Release 13.10 B035-1145-109A September 2010 The product or products described in this book are licensed products of Teradata Corporation or its affiliates. Teradata, BYNET, DBC/1012, DecisionCast, DecisionFlow, DecisionPoint, Eye logo design, InfoWise, Meta Warehouse, MyCommerce, SeeChain, SeeCommerce, SeeRisk, Teradata Decision Experts, Teradata Source Experts, WebAnalyst, and You’ve Never Seen Your Business Like This Before are trademarks or registered trademarks of Teradata Corporation or its affiliates. Adaptec and SCSISelect are trademarks or registered trademarks of Adaptec, Inc. AMD Opteron and Opteron are trademarks of Advanced Micro Devices, Inc. BakBone and NetVault are trademarks or registered trademarks of BakBone Software, Inc. EMC, PowerPath, SRDF, and Symmetrix are registered trademarks of EMC Corporation. GoldenGate is a trademark of GoldenGate Software, Inc. Hewlett-Packard and HP are registered trademarks of Hewlett-Packard Company. Intel, Pentium, and XEON are registered trademarks of Intel Corporation. IBM, CICS, RACF, Tivoli, and z/OS are registered trademarks of International Business Machines Corporation. Linux is a registered trademark of Linus Torvalds. LSI and Engenio are registered trademarks of LSI Corporation. Microsoft, Active Directory, Windows, Windows NT, and Windows Server are registered trademarks of Microsoft Corporation in the United States and other countries. Novell and SUSE are registered trademarks of Novell, Inc., in the United States and other countries. QLogic and SANbox are trademarks or registered trademarks of QLogic Corporation. SAS and SAS/C are trademarks or registered trademarks of SAS Institute Inc. SPARC is a registered trademark of SPARC International, Inc. Sun Microsystems, Solaris, Sun, and Sun Java are trademarks or registered trademarks of Sun Microsystems, Inc., in the United States and other countries. Symantec, NetBackup, and VERITAS are trademarks or registered trademarks of Symantec Corporation or its affiliates in the United States and other countries. Unicode is a collective membership mark and a service mark of Unicode, Inc. UNIX is a registered trademark of The Open Group in the United States and other countries. Other product and company names mentioned herein may be the trademarks of their respective owners. THE INFORMATION CONTAINED IN THIS DOCUMENT IS PROVIDED ON AN “AS-IS” BASIS, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT APPLY TO YOU. IN NO EVENT WILL TERADATA CORPORATION BE LIABLE FOR ANY INDIRECT, DIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS OR LOST SAVINGS, EVEN IF EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. The information contained in this document may contain references or cross-references to features, functions, products, or services that are not announced or available in your country. Such references do not imply that Teradata Corporation intends to announce such features, functions, products, or services in your country. Please consult your local Teradata Corporation representative for those features, functions, products, or services available in your country. Information contained in this document may contain technical inaccuracies or typographical errors. Information may be changed or updated without notice. Teradata Corporation may also make improvements or changes in the products or services described in this information at any time without notice. To maintain the quality of our products and services, we would like your comments on the accuracy, clarity, organization, and value of this document. Please e-mail: teradata-books@lists.teradata.com Any comments or materials (collectively referred to as “Feedback”) sent to Teradata Corporation will be deemed non-confidential. Teradata Corporation will have no obligation of any kind with respect to Feedback and will be free to use, reproduce, disclose, exhibit, display, transform, create derivative works of, and distribute the Feedback and derivative works thereof without limitation on a royalty-free basis. Further, Teradata Corporation will be free to use any ideas, concepts, know-how, or techniques contained in such Feedback for any purpose whatsoever, including developing, manufacturing, or marketing products or services incorporating Feedback. Copyright © 2000 – 2010 by Teradata Corporation. All Rights Reserved. SQL Functions, Operators, Expressions, and Predicates 3 Preface Purpose SQL Functions, Operators, Expressions, and Predicates describes the functions, operators, expressions, and predicates of Teradata SQL. Use this book with the other books in the SQL book set. Audience Application programmers and end users are the principal audience for this manual. System administrators, database administrators, security administrators, Teradata field engineers, and other technical personnel responsible for designing, maintaining, and using Teradata Database might also find this manual to be useful. Supported Software Releases and Operating Systems This book supports Teradata® Database 13.10. Teradata Database 13.10 supports: • Microsoft Windows Server 2003 64-bit • SUSE Linux Enterprise Server 10 Teradata Database client applications can support other operating systems. Prerequisites You should be familiar with basic relational database management technology and SQL. This book is not an SQL primer. If you are not familiar with Teradata Database, read Introduction to Teradata before reading this book. For information about developing applications using embedded SQL, see Teradata Preprocessor2 for Embedded SQL Programmer Guide. Preface Changes to This Book 4 SQL Functions, Operators, Expressions, and Predicates Changes to This Book Release Description Teradata Database 13.10 September 2010 Added clarification that the CAMSET compression function currently can only compress Unicode characters from U+0000 to U+00FF. Teradata Database 13.10 August 2010 Added the following: • Using CASE_N and RANGE_N with CURRENT_DATE or CURRENT_TIMESTAMP in a PPI. • Restrictions when using CASE_N and RANGE_N with Period data types in a PPI. • SQL user-defined function (UDF) expressions. • Using CASE_N and RANGE_N with character data. • New arithmetic functions: CEILING and FLOOR. • New chapter on BYTE/BIT manipulation functions. • New chapter on calendar functions. • New chapter on compression and decompression functions. • New table functions for normalize and sequenced aggregation operations over Period data types. • AT clause extensions used for time zone specification, and using time zone strings and the GetTimeZoneDisplacement UDF to adjust for daylight saving time. • The effect of the DBS Control flag TimeDateWZControl on the built-in functions: CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP, DATE, and TIME. • Window feature support for user-defined aggregate functions. Teradata Database 13.0 April 2009 Added the following: • Clarification that UDT expressions cannot be used as input arguments to UDFs written in Java, and they cannot be used as IN and INOUT parameters of external stored procedures written in Java. • Restriction that the HASH BY or LOCAL ORDER BY clauses cannot be used in derived tables with set operators. • Information about Period data types. • Information about the CURRENT_USER and CURRENT_ROLE built-in functions. • Information about the RESET WHEN clause. • Information about the NEW VARIANT_TYPE expression for constructing dynamic UDTs. • Additional information about implicit DateTime conversions. • Clarification for determining the server character set of the result of a CASE expression. • Information on calculating the interval difference between two DateTime values. • A new chapter about UDF expressions. Preface Additional Information SQL Functions, Operators, Expressions, and Predicates 5 Additional Information To maintain the quality of our products and services, we would like your comments on the accuracy, clarity, organization, and value of this document. Please e-mail: teradatabooks@ lists.teradata.com. URL Description www.info.teradata.com/ Use the Teradata Information Products Publishing Library site to: • View or download a manual: 1 Under Online Publications, select General Search. 2 Enter your search criteria and click Search. • Download a documentation CD-ROM: 1 Under Online Publications, select General Search. 2 In the Title or Keyword field, enter CD-ROM, and click Search. • Order printed manuals: Under Print & CD Publications, select How to Order. www.teradata.com The Teradata home page provides links to numerous sources of information about Teradata. Links include: • Executive reports, case studies of customer experiences with Teradata, and thought leadership • Technical information, solutions, and expert advice • Press releases, mentions and media resources www.teradata.com/t/TEN/ Teradata Customer Education designs, develops and delivers education that builds skills and capabilities for our customers, enabling them to maximize their Teradata investment. www.teradataatyourservice.com Use Teradata @ Your Service to access Orange Books, technical alerts, and knowledge repositories, view and join forums, and download software patches. developer.teradata.com/ Teradata Developer Exchange provides articles on using Teradata products, technical discussion forums, and code downloads. Preface Additional Information 6 SQL Functions, Operators, Expressions, and Predicates SQL Functions, Operators, Expressions, and Predicates 7 Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Audience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Supported Software Releases and Operating Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Changes to This Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 Additional Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 Chapter 1: Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 SQL Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 SQL Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 SQL Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 SQL Predicates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Chapter 2: CASE Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 CASE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Valued CASE Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Searched CASE Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Error Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Rules for the CASE Expression Result Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Format for a CASE Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 CASE and Nulls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 COALESCE Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 NULLIF Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Table of Contents 8 SQL Functions, Operators, Expressions, and Predicates Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions . . . . . . . . . . . . . . . . . . . . . . . . .47 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48 Binary Arithmetic Result Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49 Structure of Arithmetic Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53 Arithmetic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55 ABS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56 CASE_N. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58 CEILING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68 EXP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71 FLOOR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73 LN. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76 LOG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .78 NULLIFZERO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80 RANDOM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83 RANGE_N. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87 SQRT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101 WIDTH_BUCKET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103 ZEROIFNULL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107 Trigonometric Functions (COS, SIN, TAN, ACOS, ASIN, ATAN, ATAN2). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110 DEGREES RADIANS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113 Hyperbolic Functions (COSH, SINH, TANH, ACOSH, ASINH, ATANH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116 Chapter 4: Byte/Bit Manipulation Functions. . . . . . . . . . . . . . . . . . . .119 Bit and Byte Numbering Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .119 Performing Bit-Byte Operations against Arguments with Non-Equal Lengths . . . . . . . . . . .123 BITAND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125 BITNOT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128 BITOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .130 BITXOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133 COUNTSET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136 GETBIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138 ROTATELEFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .140 Table of Contents SQL Functions, Operators, Expressions, and Predicates 9 ROTATERIGHT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 SETBIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 SHIFTLEFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 SHIFTRIGHT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 SUBBITSTR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 TO_BYTE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Chapter 5: Comparison Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Comparison Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Comparison Operators in Logical Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Comparisons That Produce TRUE Results. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Data Type Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Implicit Type Conversion of Comparison Operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Comparison of ANSI DateTime and Interval in USING Clause . . . . . . . . . . . . . . . . . . . . . . 170 Proper Forms of DATE Types in Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Character String Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Comparison of KANJI1 Characters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Comparison Operators and the DEFAULT Function in Predicates . . . . . . . . . . . . . . . . . . . 177 Chapter 6: Set Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Overview of Set Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Rules for Set Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Precedence of Set Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Retaining Duplicate Rows Using the ALL Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Attributes of a Set Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Set Operators With Derived Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Set Operators in Subqueries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Set Operators in INSERT … SELECT Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Set Operators in View Definitions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Queries Connected by Set Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 INTERSECT Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 MINUS/EXCEPT Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 UNION Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Table of Contents 10 SQL Functions, Operators, Expressions, and Predicates Chapter 7: DateTime and Interval Functions and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .209 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .209 ANSI DateTime and Interval Data Type Assignment Rules . . . . . . . . . . . . . . . . . . . . . . . . . . .210 Scalar Operations on ANSI SQL:2008 DateTime and Interval Values. . . . . . . . . . . . . . . . . . .212 ANSI DateTime Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .213 ANSI Interval Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .229 Aggregate Functions and ANSI DateTime and Interval Data Types . . . . . . . . . . . . . . . . . . . .231 Scalar Operations and DateTime Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .232 Teradata Date and Time Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .233 Scalar Operations on Teradata DATE Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .234 ADD_MONTHS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .236 EXTRACT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242 GetTimeZoneDisplacement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .246 Chapter 8: Calendar Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253 day_of_week . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .254 day_of_month . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .256 day_of_year . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .258 day_of_calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260 weekday_of_month . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .262 week_of_month . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264 week_of_year . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .266 week_of_calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .268 month_of_quarter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .270 month_of_year . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .272 month_of_calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .274 quarter_of_year. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .276 quarter_of_calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .278 year_of_calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .280 Table of Contents SQL Functions, Operators, Expressions, and Predicates 11 Chapter 9: Period Functions and Operators. . . . . . . . . . . . . . . . . . . . 283 Period Value Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Comparison of Period Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 BEGIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 CONTAINS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 END . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 IS UNTIL_CHANGED/IS NOT UNTIL_CHANGED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 IS UNTIL_CLOSED/IS NOT UNTIL_CLOSED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 INTERVAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 LAST. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302 MEETS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 NEXT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 OVERLAPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 P_INTERSECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 P_NORMALIZE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 PRECEDES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 PRIOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 LDIFF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 RDIFF. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 SUCCEEDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 TD_NORMALIZE_OVERLAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 TD_NORMALIZE_MEET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 TD_NORMALIZE_OVERLAP_MEET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 TD_SUM_NORMALIZE_OVERLAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 TD_SUM_NORMALIZE_MEET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 TD_SUM_NORMALIZE_OVERLAP_MEET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 TD_SEQUENCED_SUM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 TD_SEQUENCED_AVG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 TD_SEQUENCED_COUNT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 Chapter 10: Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 AVG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 CORR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 Table of Contents 12 SQL Functions, Operators, Expressions, and Predicates COUNT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .356 COVAR_POP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361 COVAR_SAMP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .364 GROUPING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367 KURTOSIS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .370 MAX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .372 MIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .375 REGR_AVGX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .378 REGR_AVGY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .381 REGR_COUNT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .384 REGR_INTERCEPT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .388 REGR_R2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .392 REGR_SLOPE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .396 REGR_SXX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .400 REGR_SXY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .403 REGR_SYY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .406 SKEW. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .409 STDDEV_POP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .412 STDDEV_SAMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .415 SUM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .418 VAR_POP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .421 VAR_SAMP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .424 Chapter 11: Ordered Analytical Functions. . . . . . . . . . . . . . . . . . . . . . .427 Ordered Analytical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .428 Ordered Analytical Functions Benefits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .428 Syntax Alternatives for Ordered Analytical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .429 Window Feature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .430 Applying Windows to Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .437 Characteristics of Ordered Analytical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .439 Nesting Aggregates in Ordered Analytical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .442 GROUP BY Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .443 Using Ordered Analytical Functions Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .446 Window Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .449 CSUM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .467 MAVG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .470 Table of Contents SQL Functions, Operators, Expressions, and Predicates 13 MDIFF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 MLINREG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476 MSUM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479 PERCENT_RANK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481 QUANTILE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 RANK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488 RANK. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491 ROW_NUMBER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494 Chapter 12: String Operator and Functions . . . . . . . . . . . . . . . . . . . . 497 Concatenation Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502 CHAR2HEXINT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511 LOWER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517 POSITION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520 SOUNDEX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523 STRING_CS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527 SUBSTRING/SUBSTR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530 TRANSLATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536 TRANSLATE_CHK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545 TRIM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549 UPPER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553 VARGRAPHIC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556 VARGRAPHIC Function Conversion Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559 Chapter 13: Logical Predicates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569 Logical Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569 ANY/ALL/SOME Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573 BETWEEN/NOT BETWEEN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578 EXISTS/NOT EXISTS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579 IN/NOT IN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585 IS NULL/IS NOT NULL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592 LIKE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594 OVERLAPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604 Table of Contents 14 SQL Functions, Operators, Expressions, and Predicates Logical Operators and Search Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .608 Chapter 14: Attribute Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .613 BYTES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .614 CHARACTER_LENGTH. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .616 CHARACTERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .619 DEFAULT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .621 FORMAT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .625 OCTET_LENGTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .626 TITLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .629 TYPE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .630 Chapter 15: Hash-Related Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .633 Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .633 HASHAMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .634 HASHBAKAMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .637 HASHBUCKET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .640 HASHROW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .643 Chapter 16: Compression/Decompression Functions . . . . . . . .645 CAMSET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .646 CAMSET_L . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .649 DECAMSET. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .652 DECAMSET_L . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .654 LZCOMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .656 LZCOMP_L . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .658 LZDECOMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .660 LZDECOMP_L . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .662 TransUnicodeToUTF8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .664 TransUTF8ToUnicode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .667 Table of Contents SQL Functions, Operators, Expressions, and Predicates 15 Chapter 17: Built-In Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669 ACCOUNT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670 CURRENT_DATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671 CURRENT_ROLE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675 CURRENT_TIME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677 CURRENT_TIMESTAMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681 CURRENT_USER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685 DATABASE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686 DATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687 PROFILE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691 ROLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692 SESSION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695 TEMPORAL_DATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696 TEMPORAL_TIMESTAMP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697 TIME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699 USER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702 Chapter 18: User-Defined Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705 SQL UDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706 Scalar UDF. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711 Aggregate UDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714 Window Aggregate UDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717 Table UDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725 Chapter 19: UDT Expressions and Methods . . . . . . . . . . . . . . . . . . . . 729 UDT Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730 NEW. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734 NEW VARIANT_TYPE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737 Method Invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740 Table of Contents 16 SQL Functions, Operators, Expressions, and Predicates Chapter 20: Data Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .745 Forms of Data Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .745 Implicit Type Conversions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .745 CAST in Explicit Data Type Conversions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .752 Teradata Conversion Syntax in Explicit Data Type Conversions. . . . . . . . . . . . . . . . . . . . . . .755 Data Conversions in Field Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .757 Byte Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .758 Character-to-Character Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .762 Implicit Character-to-Character Translation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .765 Character-to-DATE Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .767 Character-to-INTERVAL Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .773 Character-to-Numeric Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .775 Character-to-Period Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .781 Character-to-TIME Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .784 Character-to-TIMESTAMP Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .790 Character-to-UDT Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .795 Character Data Type Assignment Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .797 DATE-to-Character Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .798 DATE-to-DATE Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .802 DATE-to-Numeric Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .804 DATE-to-Period Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .807 DATE-to-TIMESTAMP Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .809 DATE-to-UDT Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .815 INTERVAL-to-Character Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .817 INTERVAL-to-INTERVAL Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .819 INTERVAL-to-Numeric Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .823 INTERVAL-to-UDT Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .825 Numeric-to-Character Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .827 Numeric-to-DATE Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .832 Numeric-to-INTERVAL Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .835 Numeric-to-Numeric Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .837 Numeric-to-UDT Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .841 Period-to-Character Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .843 Period-to-DATE Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .846 Period-to-Period Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .848 Period-to-TIME Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .853 Period-to-TIMESTAMP Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .855 Table of Contents SQL Functions, Operators, Expressions, and Predicates 17 Signed Zone DECIMAL Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857 TIME-to-Character Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861 TIME-to-Period Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864 TIME-to-TIME Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 866 TIME-to-TIMESTAMP Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874 TIME-to-UDT Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 888 TIMESTAMP-to-Character Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 890 TIMESTAMP-to-DATE Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 894 TIMESTAMP-to-Period Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 905 TIMESTAMP-to-TIME Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 907 TIMESTAMP-to-TIMESTAMP Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915 TIMESTAMP-to-UDT Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 923 UDT-to-Byte Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925 UDT-to-Character Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928 UDT-to-DATE Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 932 UDT-to-INTERVAL Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 935 UDT-to-Numeric Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 938 UDT-to-TIME Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941 UDT-to-TIMESTAMP Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944 UDT-to-UDT Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947 Appendix A: Notation Conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 949 Syntax Diagram Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 949 Character Shorthand Notation Used In This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954 Predicate Calculus Notation Used In This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 959 Table of Contents 18 SQL Functions, Operators, Expressions, and Predicates SQL Functions, Operators, Expressions, and Predicates 19 CHAPTER 1 Introduction This chapter provides a brief introduction and description of the SQL functions, operators, expressions, and predicates described in this book. SQL Functions SQL functions return information about some aspect of the database, depending on the arguments specified at the time the function is invoked. Functions provide a single result by accepting input arguments, and returning an output value. Some SQL functions, referred to as niladic functions, do not have arguments, but do return values. An example of a niladic SQL function is CURRENT_DATE. Types of SQL Functions There are four types of SQL functions: • Scalar • Aggregate • Table • Ordered Analytical Function The following table defines these types. Function Type Definition Scalar The arguments are individual scalar values of either same or mixed type that can have different meanings. The result is a single value or null. Can be used in any SQL statement where an expression can be used. Aggregate The argument is a group of rows. The result is a single value or null. Normally used in the expression list of a SELECT statement and in the summary list of a WITH clause. Chapter 1: Introduction SQL Functions 20 SQL Functions, Operators, Expressions, and Predicates Examples of Functions For examples of table functions, see SQL External Routine Programming. Domain-specific Functions Domain-specific functions are Teradata system functions that are created using a development infrastructure that allows for quick and easy addition of new system functions to the Teradata Database. Domain-specific functions behave and perform in the same manner as native Teradata system functions, except that domain-specific functions follow UDF implicit type conversion rules that are more restrictive than the implicit type conversion rules normally used by Teradata Database. Activating Domain-specific Functions Before you can use the domain-specific functions, you must run the Database Initialization Program (DIP) utility and execute the DIPALL or DIPUDT script. Normally, DIPALL has already been executed as part of system installation. The DIP scripts create a new database named TD_SYSFNLIB. If a database or user with the same name already exists, you must removed it before activating the domain-specific functions. Table The arguments are individual scalar values of either same or mixed type that can have different meanings. The result is a table. Can be used only within the FROM clause of a SELECT statement. Table functions are a form of user-defined functions and are described in SQL External Routine Programming. Ordered Analytical Function The arguments are any normal SQL expression. The result is handled the same as any other SQL expression. It can be a result column or part of a more complex arithmetic expression. Used in operations that require an ordered set of results rows or depend on values in a previous row. See “Ordered Analytical Functions” on page 428. Function Type Definition Function Description SELECT CHARACTER_LENGTH(Details) FROM Orders; Scalar function taking the character or CLOB value in the Details column and returning a numeric value for each row in the Orders table. SELECT AVG(Salary) FROM Employee; Aggregate function returning a single numeric value for the group of numeric values specified by the Salary column in the Employee table. Chapter 1: Introduction SQL Operators SQL Functions, Operators, Expressions, and Predicates 21 Note: The TD_SYSFNLIB database should be used only by the system to support the domainspecific functions. Do not store any database objects in this database. Doing so may interfere with the proper operation of the domain-specific functions. If you perform a BAR operation that involves the TD_SYSFNLIB database or the DBC dictionary tables, you must re-execute the DIPALL or the DIPUDT script to reactivate the domain-specific functions. Invoking Domain-specific Functions You can invoke a domain-specific function using the function name alone. For example, CEILING (arg). You can also qualify the function name by adding the TD_SYSFNLIB database name. For example, you can invoke the CEILING function using the fully qualified syntax, TD_SYSFNLIB.CEILING(arg). Note: If you try to invoke a domain-specific function using the function name alone, but you also have a user-defined function (UDF) with the same name in the current database or in the SYSLIB database, Teradata Database will execute the user-developed UDF instead of the domain-specific function. Therefore, to ensure that you are invoking the domain-specific function, do one of the following: • To invoke a domain-specific function using the function name alone, you must first remove any user-developed functions with the same name from the normal UDF search path. That is, you must remove any existing UDFs with the same name from the current database and from the SYSLIB database. For detailed information, see “Locations Where Teradata Database Looks for Functions” in SQL External Routine Programming. • Use the fully qualified syntax to invoke the domain-specific function. For example, TD_SYSFNLIB.domain_specific_function. In this case, Teradata Database will invoke the domain-specific function instead of the user-developed UDF with the same name. SQL Operators SQL operators are symbols and keywords that perform operations on their arguments. Types of Operators The following types of operators are available in SQL: • Arithmetic operators such as + and - operate on numeric, DateTime, and Interval data types. • The concatenation operator || operates on character and byte types. • Comparison operators such as = and > test the truth of relations between their arguments. (Comparison operators are a type of logical predicate. See also “Types of Logical Predicates” on page 24.) Chapter 1: Introduction SQL Expressions 22 SQL Functions, Operators, Expressions, and Predicates • Set operators, or relational operators, such as INTERSECT and UNION combine result sets from multiple sources into a single result set. SQL Expressions SQL expressions specify a value. They allow you to perform arithmetic and logical operations, and to generate new values or Boolean results from constants and stored values. An expression can consist of any of the following things: • Column name • Constant (also referred to as literal) • Function • USING variable • parameter • parameter marker (question mark (?) placeholder) • Combination of column names, constants, and functions connected by operators Types of Expressions SQL expressions generally fall into the following categories. Type Description Numeric expression Expressions are generally classified by the type of result they produce. For example, a numeric expression consists of a column name, constant, function, or combination of column names, constants, and functions connected by arithmetic operators where the result is a numeric type. String expression DateTime expression Interval expression Period expression Conditional expression An expression that results in a value of TRUE, FALSE, or unknown (NULL). Conditional expressions are also referred to as logical predicates. See “SQL Predicates” on page 23. Chapter 1: Introduction SQL Predicates SQL Functions, Operators, Expressions, and Predicates 23 Examples of Expressions The following are examples of expressions. SQL Predicates SQL predicates, also referred to as conditional expressions, specify a condition of a row or group that has one of three possible states: • TRUE • FALSE • NULL (or unknown) Predicates can appear in the following: CASE expressions CASE expressions consist of a set of WHEN/THEN clauses and an optional ELSE clause. A valued CASE expression tests for the first WHEN expression that is equal to a test expression and returns the value of the matching THEN expression. If no WHEN expression is equal to the test expression, CASE returns the ELSE expression, or, if omitted, NULL. A searched CASE expression tests for the first WHEN expression that evaluates to TRUE and returns the value of the matching THEN expression. If no WHEN expression evaluates to TRUE, CASE returns the ELSE expression, or, if omitted, NULL. Type Description Expression Description 'Test Tech' Character string constant 1024 Numeric constant Employee.FirstName Column name Salary * 12 + 100 Arithmetic expression producing a numeric value INTERVAL '10' MONTH * 4 Interval expression producing an interval value CURRENT_DATE + INTERVAL '2' DAY DateTime expression producing a DATE value CURRENT_TIME - INTERVAL '1' HOUR DateTime expression producing a TIME value 'Last' || ' Order' String expression producing a character string value CASE x WHEN 1 THEN 1001 ELSE 1002 END Valued CASE conditional expression producing a numeric value Chapter 1: Introduction SQL Predicates 24 SQL Functions, Operators, Expressions, and Predicates • WHERE, ON, or HAVING clause to qualify or disqualify rows in a SELECT statement. • WHEN clause search condition of a searched CASE expression • CASE_N function • IF, WHILE, REPEAT, and CASE statements in stored procedures Types of Logical Predicates SQL provides the following logical predicates: • Comparison operators • [NOT] BETWEEN • LIKE • [NOT] IN • [NOT] EXISTS • OVERLAPS • IS [NOT] NULL Logical Operators that Operate on Predicates • NOT • AND • OR Predicate Quantifiers • SOME • ANY • ALL Examples of Predicates Predicate Description SELECT * FROM Employee WHERE Salary < 40000; Predicate in a WHERE clause specifying a condition for selecting rows from the Employee table. SELECT SUM(CASE WHEN part BETWEEN 100 AND 199 THEN 0 ELSE cost END) FROM Orders; Predicate in a CASE expression specifying a condition that determines the value passed to the SUM function for a particular row in the Orders table. SQL Functions, Operators, Expressions, and Predicates 25 CHAPTER 2 CASE Expressions This chapter describes SQL CASE expressions. CASE Purpose Specifies alternate values for a conditional expression or expressions based on equality comparisons and conditions that evaluate to TRUE. ANSI Compliance CASE is ANSI SQL:2008 compliant. Overview CASE provides an efficient and powerful method for application developers to change the representation of data, permitting conversion without requiring host program intervention. For example, you could code employee status as 1 or 2, meaning full-time or part-time, respectively. For efficiency, the system stores the numeric code but prints or displays the appropriate textual description in reports. This storage and conversion is managed by Teradata Database. In addition, CASE permits applications to generate nulls based on information derived from the database, again without host program intervention. Conversely, CASE can be used to convert a null into a value. Two Forms of CASE Expressions CASE expressions are specified in two different forms: Valued and Searched. • Valued CASE is described under “Valued CASE Expression” on page 26. • Searched CASE is described under “Searched CASE Expression” on page 29. CASE Shorthands for Handling Nulls Two shorthand forms of CASE are provided to handle nulls: • COALESCE is described under “COALESCE Expression” on page 42. • NULLIF is described under “NULLIF Expression” on page 44. Chapter 2: CASE Expressions Valued CASE Expression 26 SQL Functions, Operators, Expressions, and Predicates Valued CASE Expression Purpose Evaluates a set of expressions for equality with a test expression and returns as its result the value of the scalar expression defined for the first WHEN clause whose value equals that of the test expression. If no equality is found, then CASE returns the scalar value defined by an optional ELSE clause, or if omitted, NULL. Syntax where: ANSI Compliance Valued CASE is ANSI SQL:2008 compliant. Teradata Database does not enforce the ANSI restriction that value_expression_1 must be a deterministic function. In particular, Teradata Database allows the function RANDOM to be used in value_expression_1. Note that if RANDOM is used, nondeterministic behavior may occur, depending on whether value_expression_1 is recalculated for each comparison to value_expression_n. Syntax element … Specifies … value_expression_1 an expression whose value is tested for equality with value_expression_n. value_expression_n a set of expressions against which the value for value_expression_1 is tested for equality. scalar_expression_n an expression whose value is returned on the first equality comparison of value_expression_1 and value_expression_n. scalar_expression_m an expression whose value is returned if evaluation falls through to the ELSE clause. 1101A012 CASE value_expression_1 END A B ELSE scalar_expression_m A WHEN value_expression_n THEN scalar_expression_n B Chapter 2: CASE Expressions Valued CASE Expression SQL Functions, Operators, Expressions, and Predicates 27 Usage Notes WHEN clauses are processed sequentially. The first WHEN clause value_expression_n that equates to value_expression_1 returns the value of its associated scalar_expression_n as its result. The evaluation process then terminates. If no value_expression_n equals value_expression_1, then scalar_expression_m, the argument of the ELSE clause, is the result. If no ELSE clause is defined, then the result defaults to NULL. The data type of value_expression_1 must be comparable with the data types of all of the value_expression_n values. For information on the result data type of a CASE expression, see “Rules for the CASE Expression Result Type” on page 34. You can use a scalar subquery in the WHEN clause, THEN clause, and ELSE clause of a CASE expression. If you use a non-scalar subquery (a subquery that returns more than one row), a runtime error is returned. Recommendation: Do not use the built-in functions CURRENT_DATE or CURRENT_TIMESTAMP in a CASE expression that is specified in a partitioning expression for a partitioned primary index (PPI). In this case, all rows are scanned during reconciliation. Default Title The default title for a CASE expression appears as: Restrictions on the Data Types in a CASE Expression The following restrictions apply to CLOB, BLOB, and UDT types in a CASE expression: Data Type Restrictions BLOB A BLOB can only appear in value_expression_1, value_expression_n, scalar_expression_m, or scalar_expression_n when it is cast to BYTE or VARBYTE. CLOB A CLOB can only appear in value_expression_1, value_expression_n, scalar_expression_m, or scalar_expression_n when it is cast to CHAR or VARCHAR. UDT Multiple UDTs can appear in a CASE expression only when they are identical types because Teradata Database does not perform implicit type conversion on UDTs in CASE expressions. A workaround for this restriction is to use CREATE CAST to define casts that cast between the UDTs, and then explicitly invoke the CAST function in the CASE expression. For more information on CREATE CAST, see SQL Data Definition Language. Chapter 2: CASE Expressions Valued CASE Expression 28 SQL Functions, Operators, Expressions, and Predicates Related Topics Example 1 The following example uses a Valued CASE expression to calculate the fraction of cost in the total cost of inventory represented by parts of type ‘1’: SELECT SUM(CASE part WHEN '1' THEN cost ELSE 0 END )/SUM(cost) FROM t; Example 2 A CASE expression can be used in place of any value-expression. SELECT * FROM t WHERE x = CASE y WHEN 2 THEN 1001 WHEN 5 THEN 1002 END; Example 3 The following example shows how to combine a CASE expression with a concatenation operator: SELECT prodID, CASE prodSTATUS WHEN 1 THEN 'SENT' ELSE 'BACK ORDER' END || ' STATUS' FROM t1; For additional notes on … See … error conditions “Error Conditions” on page 33. the result data type of a CASE expression “Rules for the CASE Expression Result Type” on page 34. format of the result of a CASE expression “Format for a CASE Expression” on page 39. nulls and CASE expressions “CASE and Nulls” on page 40. Chapter 2: CASE Expressions Searched CASE Expression SQL Functions, Operators, Expressions, and Predicates 29 Searched CASE Expression Purpose Evaluates a search condition and returns one of a WHEN clause-defined set of scalar values when it finds a value that evaluates to TRUE. If no TRUE test is found, then CASE returns the scalar value defined by an ELSE clause, or if omitted, NULL. Syntax where: ANSI Compliance Searched CASE is ANSI SQL:2008 compliant. Usage Notes WHEN clauses are processed sequentially. The first WHEN clause search_condition_n that is TRUE returns the value of its associated scalar_expression_n as its result. The evaluation process then ends. If no search_condition_n is TRUE, then scalar_expression_m, the argument of the ELSE clause, is the result. If no ELSE clause is defined, then the default value for the result is NULL. You can use a scalar subquery in the WHEN clause, THEN clause, and ELSE clause of a CASE expression. If you use a non-scalar subquery (a subquery that returns more than one row), a runtime error is returned. Syntax element … Specifies … search_condition_n a predicate condition to be tested for truth. scalar_expression_n a scalar expression whose value is returned when search_condition_n is the first search condition that evaluates to TRUE. scalar_expression_m a scalar expression whose value is returned when no search_condition_n evaluates to TRUE. FF07D224 CASE END A A ELSE scalar_expression_m WHEN search_condition_n THEN scalar_expression_n Chapter 2: CASE Expressions Searched CASE Expression 30 SQL Functions, Operators, Expressions, and Predicates Recommendation: Do not use the built-in functions CURRENT_DATE or CURRENT_TIMESTAMP in a CASE expression that is specified in a partitioning expression for a partitioned primary index (PPI). In this case, all rows are scanned during reconciliation. Default Title The default title for a CASE expression appears as: Rules for WHEN Search Conditions WHEN search conditions have the following properties: • Can take the form of any comparison operator, such as LIKE, =, or <>. • Can be a quantified predicate, such as ALL or ANY. • Can contain a scalar subquery. • Can contain joins of two tables. For example: SELECT CASE WHEN t1.x=t2.x THEN t1.y ELSE t2.y END FROM t1,t2; • Cannot contain SELECT statements. Restrictions on the Data Types in a CASE Expression The following restrictions apply to CLOB, BLOB, and UDT types in a CASE expression: Data Type Restrictions BLOB A BLOB can only appear in search_condition_n, scalar_expression_m, or scalar_expression_n when it is cast to BYTE or VARBYTE. CLOB A CLOB can only appear in search_condition_n, scalar_expression_m, or scalar_expression_n when it is cast to CHAR or VARCHAR. UDT Multiple UDTs can appear in a CASE expression only when they are identical types because Teradata Database does not perform implicit type conversion on UDTs in CASE expressions. A workaround for this restriction is to use CREATE CAST to define casts that cast between the UDTs, and then explicitly invoke the CAST function in the CASE expression. For more information on CREATE CAST, see SQL Data Definition Language. Chapter 2: CASE Expressions Searched CASE Expression SQL Functions, Operators, Expressions, and Predicates 31 Related Topics Example 1 The following statement is equivalent to the first example of the valued form of CASE on “Example 1” on page 28: SELECT SUM(CASE WHEN part='1' THEN cost ELSE 0 END ) / SUM(cost) FROM t; Example 2 CASE expressions can be used in place of any value-expressions. Note that the following example does not specify an ELSE clause. ELSE clauses are always optional in a CASE expression. If an ELSE clause is not specified and none of the WHEN conditions are TRUE, then a null is returned. SELECT * FROM t WHERE x = CASE WHEN y=2 THEN 1 WHEN (z=3 AND y=5) THEN 2 END; Example 3 The following example uses an ELSE clause. SELECT * FROM t WHERE x = CASE WHEN y=2 THEN 1 ELSE 2 END; For additional notes on … See … error conditions “Error Conditions” on page 33. the result data type of a CASE expression “Rules for the CASE Expression Result Type” on page 34. format of the result of a CASE expression “Format for a CASE Expression” on page 39. nulls and CASE expressions “CASE and Nulls” on page 40. Chapter 2: CASE Expressions Searched CASE Expression 32 SQL Functions, Operators, Expressions, and Predicates Example 4 The following example shows how using a CASE expression can result in significantly enhanced performance by eliminating multiple passes over the data. Without using CASE, you would have to perform multiple queries for each region and then consolidate the answers to the individual queries in a final report. SELECT SalesMonth, SUM(CASE WHEN Region='NE' THEN Revenue ELSE 0 END), SUM(CASE WHEN Region='NW' THEN Revenue ELSE 0 END), SUM(CASE WHEN Region LIKE 'N%' THEN Revenue ELSE 0 END) AS NorthernExposure, NorthernExposure/SUM(Revenue), SUM(Revenue) FROM Sales GROUP BY SalesMonth; Example 5 All employees whose salary is less than $40000 are eligible for an across the board pay increase. The following SELECT statement uses a CASE expression to produce a report showing all employees making under $40000, displaying the first 15 characters of the last name, the salary amount (formatted with $ and punctuation), the number of years of service based on the current date (in the column named On_The_Job) and which of the four categories they qualify for: '15% Increase', '10% Increase', '05% Increase' or 'Not Qualified'. SELECT CAST(last_name AS CHARACTER(15)) ,salary_amount (FORMAT '$,$$9,999.99') ,(date - hire_date)/365.25 (FORMAT 'Z9.99') AS On_The_Job ,CASE WHEN salary_amount < 30000 AND On_The_Job > 8 THEN '15% Increase' WHEN salary_amount < 35000 AND On_The_Job > 10 THEN '10% Increase' WHEN salary_amount < 40000 AND On_The_Job > 10 IF your salary is less than … AND you have greater than this many years of service … THEN you receive this percentage salary increase … $30000.00 8 15 $35000.00 10 10 $40000.00 5 Chapter 2: CASE Expressions Error Conditions SQL Functions, Operators, Expressions, and Predicates 33 THEN '05% Increase' ELSE 'Not Qualified' END AS Plan WHERE salary_amount < 40000 FROM employee ORDER BY 4; The result of this query appears in the following table: Error Conditions The following conditions or expressions are considered illegal in a CASE expression: last_name salary_amount On_The_Job Plan Trader $37,850.00 20.61 05% Increase Charles $39,500.00 18.44 05% Increase Johnson $36,300.00 20.41 05% Increase Hopkins $37,900.00 19.99 05% Increase Morrissey $38,750.00 18.44 05% Increase Ryan $31,200.00 20.41 10% Increase Machado $32,300.00 18.03 10% Increase Short $34,700.00 17.86 10% Increase Lombardo $31,000.00 20.11 10% Increase Phillips $24,500.00 19.95 15% Increase Rabbit $26,500.00 18.03 15% Increase Kanieski $29,250.00 20.11 15% Increase Hoover $25,525.00 20.73 15% Increase Crane $24,500.00 19.15 15% Increase Stein $29,450.00 20.41 15% Increase Condition or Expression Example A condition after the keyword CASE is supplied. SELECT CASE a=1 WHEN 1 THEN 1 ELSE 0 END FROM t; Chapter 2: CASE Expressions Rules for the CASE Expression Result Type 34 SQL Functions, Operators, Expressions, and Predicates Rules for the CASE Expression Result Type Because the expressions in CASE THEN/ELSE clauses can be different data types, determining the result type is not always straightforward. You can use the TYPE attribute function with the CASE expression as the argument to find out the result data type. See “TYPE” on page 630. The following rules apply to the data type of the CASE expression result. THEN/ELSE Expressions Having the Same Non-Character Data Type If all of the THEN and ELSE expressions have the same non-character data type, the result of the CASE expression is that type. For example, if all of the THEN and ELSE expressions have an INTEGER type, the result type of the CASE expression is INTEGER. For information about how the precision and scale of DECIMAL results are calculated, see “Binary Arithmetic Result Data Types” on page 49. An invalid WHEN expression is supplied in a valued CASE expression. SELECT CASE a WHEN a=1 THEN 1 ELSE 0 END FROM t; An invalid WHEN condition is supplied in a searched CASE expression. SELECT CASE WHEN a THEN 1 ELSE 0 END FROM t; SELECT CASE WHEN NULL THEN 'NULL' END FROM table_1; A non-scalar subquery is specified in a WHEN condition of a searched CASE expression. SELECT CASE WHEN t.a IN (SELECT u.a FROM u) THEN 1 ELSE 0 END FROM t; A CASE expression references multiple UDTs that are not identical to each other. SELECT CASE t.shape.gettype() WHEN 1 THEN NEW circle('18,18,324') WHEN 2 THEN NEW square('20,20,400') END; Condition or Expression Example Chapter 2: CASE Expressions Rules for the CASE Expression Result Type SQL Functions, Operators, Expressions, and Predicates 35 THEN/ELSE Character Type Expressions The following rules apply to CASE expressions where the data types of all of the THEN/ELSE expressions are character: • The result of the CASE expression is also a character data type, with the length equal to the maximum length of the different character data types of the THEN/ELSE expressions. • If the data types of all of the THEN/ELSE expressions are CHARACTER (or CHAR), the result data type will be CHARACTER. If one or more expressions are VARCHAR (or LONG VARCHAR), the result data type will be VARCHAR. • The server character set of the result is determined by scanning all the server character sets of the THEN/ELSE character expressions. If any THEN/ELSE character expression is a KANJI1 constant (for example, _Kanji1''XC), then all other THEN/ELSE character expressions must be of KANJI1 server character set. Otherwise, an error is returned. In all other cases, the server character set of the result is set to the server character set of the first THEN/ELSE character expression that is not a constant. The remaining THEN/ELSE character expressions must be translatable to this server character set. If all THEN/ELSE character expressions are constants, the server character set of the result is Unicode. Examples of Character Data in a CASE Expression For the following examples of CHARACTER data behavior, assume the default server character set is KANJI1 and the table definition for the CASE examples is as follow: CREATE table_1 ( i INTEGER, column_l CHARACTER(10) CHARACTER SET LATIN, column_u CHARACTER(10) CHARACTER SET UNICODE, column_j CHARACTER(10) CHARACTER SET KANJISJIS, column_g CHARACTER(10) CHARACTER SET GRAPHIC, column_k CHARACTER(10) CHARACTER SET KANJI1 ); Example 1 The server character set of the result of the following query is UNICODE, because the server character set of the first THEN expression is UNICODE: SELECT i, CASE WHEN i=2 THEN column_u WHEN i=3 THEN column_j WHEN i=4 THEN column_g WHEN i=5 THEN column_k ELSE column_l END FROM table_1 ORDER BY 1; Chapter 2: CASE Expressions Rules for the CASE Expression Result Type 36 SQL Functions, Operators, Expressions, and Predicates Example 2 The result of the following query is a failure because one THEN/ELSE expression is a KANJI1 constant, but the server character sets of all the other THEN/ELSE expressions are not KANJI1. SELECT i, CASE WHEN i=1 THEN column_l WHEN i=2 THEN column_u WHEN i=3 THEN column_j WHEN i=4 THEN column_g WHEN i=5 THEN _Kanji1'4142'XC ELSE column_k END FROM table_1 ORDER BY 1; Example 3 One THEN/ELSE expression in the following query has a KANJI1 constant. The query is successful and the result data type is KANJI1 because the server character set of all the other THEN/ELSE expressions are KANJI1. SELECT i, CASE WHEN i=1 THEN column_k WHEN i=2 THEN ‘abc’ WHEN i=3 THEN 8 WHEN i=4 THEN _Kanji1’4142’XC ELSE 10 END FROM table_1 ORDER BY 1; THEN/ELSE Expressions Having Mixed Data Types The rules for mixed data appear in the following table: IF the THEN/ELSE clause expressions … THEN … consist of BYTE and/or VARBYTE data types if the data types of all of the THEN/ELSE expressions are BYTE, the result data type will be BYTE. If one or more expressions are VARBYTE, the result data type will be VARBYTE. contain a DateTime or Interval data type all of the THEN/ELSE clause expressions must have the same data type. contain a FLOAT (approximate numeric) and no character strings the CASE expression returns a FLOAT result. Note: Some inaccuracy is inherent and unavoidable when FLOAT data types are involved. Chapter 2: CASE Expressions Rules for the CASE Expression Result Type SQL Functions, Operators, Expressions, and Predicates 37 Examples of Numeric Data in a CASE Expression For the following examples of numeric data behavior, assume the following table definitions for the CASE examples: CREATE TABLE dec22 (column_l INTEGER ,column_2 INTEGER ,column_3 DECIMAL(22,2) ); Example 1 In the following statement, the CASE expression fails when column_2 contains the value 1 and column_3 contains the value 11223344556677889900.12 because the result is a DECIMAL value that requires more than 38 digits of precision: SELECT SUM (CASE WHEN column_2=1 THEN column_3 * 6.112233445566778800000 ELSE column_3 END ) FROM dec22; Example 2 The following query corrects the problem in Example 1 by shortening the scale of the multiplier in the THEN expression: SELECT SUM (CASE WHEN column_2=1 THEN column_3 * 6.1122334455667788 are composed only of DECIMAL data the CASE expression returns a DECIMAL result. Note: A DECIMAL arithmetic result can have up to 38 digits. A result larger than 38 digits produces a numeric overflow error. For information about how the precision and scale of DECIMAL results are calculated, see “Binary Arithmetic Result Data Types” on page 49. are composed only of mixed DECIMAL, BYTEINT, SMALLINT, INTEGER, and BIGINT data are a mix of BYTEINT, SMALLINT, INTEGER, and BIGINT data the resulting type is the largest type of any of the THEN/ ELSE clause expressions, where the following list orders the types from largest to smallest: • BIGINT • INTEGER • SMALLINT • BYTEINT are composed only of numeric and character data the numeric data is converted to character. Note: An error is generated if the server character set is GRAPHIC. IF the THEN/ELSE clause expressions … THEN … Chapter 2: CASE Expressions Rules for the CASE Expression Result Type 38 SQL Functions, Operators, Expressions, and Predicates ELSE column_3 END ) FROM dec22; Example 3 In the following query, the CASE expression returns a DECIMAL result because its THEN and ELSE clauses contain both INTEGER and DECIMAL values: SELECT SUM (CASE WHEN column_2=1 THEN column_3 * 6 ELSE column_3 END ) FROM dec22; Examples of Character and Numeric Data in a CASE Expression The following examples illustrate the behavior of queries containing CASE expressions with a THEN/ELSE clause composed of numeric and character data. Example 1 In the following query, the CASE expression returns a VARCHAR result because its THEN and ELSE clause contains both FLOAT and VARCHAR values. The length of the result is 30 since the default format for FLOAT is a string less than 30 characters, and USER is defined as VARCHAR(30) CHARACTER SET UNICODE. SELECT a, CASE WHEN a=1 THEN TIME ELSE USER END FROM table_1 ORDER BY 1; Example 2 For this example, assume the following table definition: CREATE table_1 (i INTEGER, column_l CHARACTER(10) CHARACTER SET LATIN, column_u CHARACTER(10) CHARACTER SET UNICODE, column_j CHARACTER(10) CHARACTER SET KANJISJIS, column_g CHARACTER(10) CHARACTER SET GRAPHIC, column_k CHARACTER(10) CHARACTER SET KANJI1); The following query fails because the server character set is GRAPHIC (because the server character set of the first THEN with a character type is GRAPHIC): SELECT i, CASE WHEN i=1 THEN 4 WHEN i=2 THEN column_g WHEN i=3 THEN 5 WHEN i=4 THEN column_l Chapter 2: CASE Expressions Format for a CASE Expression SQL Functions, Operators, Expressions, and Predicates 39 WHEN i=5 THEN column_k ELSE 10 END FROM table_1 ORDER BY 1; Format for a CASE Expression Default Format The result of a CASE expression is displayed using the default format for the resulting data type. The result of a CASE expression does not apply the explicit format that may be defined for a column appearing in a THEN/ELSE expression. Consider the following table definition: CREATE TABLE duration (i INTEGER ,start_date DATE FORMAT 'EEEEBMMMBDD,BYYYY' ,end_date DATE FORMAT 'DDBM3BY4' ); Assume the default format for the DATE data type is 'YY/MM/DD'. The following query displays the result of the CASE expression using the 'YY/MM/DD' default DATE format, not the format defined for the start_date or end_date columns: SELECT i, CASE WHEN i=1 THEN start_date WHEN i=2 THEN end_date END FROM duration ORDER BY 1; Using Explicit Type Conversion to Change Format To modify the format of the result of a CASE expression, use CAST and specify the FORMAT clause. Here is an example that uses CAST to change the format of the result of the CASE expression in the previous query: SELECT i, ( CAST ((CASE WHEN i=1 THEN start_date WHEN i=2 THEN end_date END) AS DATE FORMAT 'M4BDD,BYYYY')) FROM duration ORDER BY 1; For information on the default data type formats and the FORMAT phrase, see SQL Data Types and Literals. Chapter 2: CASE Expressions CASE and Nulls 40 SQL Functions, Operators, Expressions, and Predicates CASE and Nulls The ANSI SQL:2008 standard specifies that the CASE expression and its related expressions COALESCE and NULLIF must be capable of returning a null result. Nulls and CASE Expressions The rules for null usage in CASE, NULLIF, and COALESCE expressions are as follows. • If no ELSE clause is specified in a CASE expression and the evaluation falls through all the WHEN clauses, the result is null. • Nulls and expressions containing nulls are valid as value_expression_1 in a valued CASE expression. The following examples are valid. SELECT CASE NULL WHEN 10 THEN 'TEN' END; SELECT CASE NULL + 1 WHEN 10 THEN 'TEN' END; Both of the preceding examples return NULL because no ELSE clause is specified, and the evaluation falls through the WHEN clause because NULL is not equal to any value or to NULL. • Comparing NULL to any value or to NULL is always FALSE. When testing for NULL, it is best to use a searched CASE expression using IS NULL or IS NOT NULL in the WHEN condition. The following example is valid. SELECT CASE WHEN column_1 IS NULL THEN 'NULL' END FROM table_1; Often, Teradata Database can detect when an expression that always evaluates to NULL is compared to some other expression or NULL, and gives an error that recommends using IS NULL or IS NOT NULL instead. Note that ANSI SQL does not consider this to be an error; however, Teradata Database reports an error since it is unlikely that comparing NULL in this manner is the intent of the user. The following examples are not legal. SELECT CASE column_1 WHEN NULL THEN 'NULL' END FROM table_1; Chapter 2: CASE Expressions CASE and Nulls SQL Functions, Operators, Expressions, and Predicates 41 SELECT CASE column_1 WHEN NULL + 1 THEN 'NULL' END FROM table_1; SELECT CASE WHEN column_1 = NULL THEN 'NULL' END FROM table_1; SELECT CASE WHEN column_1 = NULL + 1 THEN 'NULL' END FROM table_1; • Nulls and expressions containing nulls are valid as THEN clause expressions. The following example is valid. SELECT CASE WHEN column_1 = 10 THEN NULL END FROM table_1 Note that, unlike the previous examples, the NULL in the THEN clause is an SQL keyword and not the value of a character constant. CASE Shorthands ANSI also defines two shorthand special cases of CASE specifically for handling nulls. • COALESCE expression (see “COALESCE Expression” on page 42) • NULLIF expression (see “NULLIF Expression” on page 44) Chapter 2: CASE Expressions COALESCE Expression 42 SQL Functions, Operators, Expressions, and Predicates COALESCE Expression Purpose COALESCE returns NULL if all its arguments evaluate to null. Otherwise, it returns the value of the first non-null argument in the scalar_expression list. COALESCE is a shorthand expression for the following full CASE expression: CASE WHEN scalar_expression_1 IS NOT NULL THEN scalar_expression_1 ... WHEN scalar_expression_n IS NOT NULL THEN scalar_expression_n ELSE NULL END Syntax where: ANSI Compliance COALESCE is ANSI SQL:2008 compliant. Usage Notes A scalar_expression_n in the argument list may be evaluated twice: once as a search condition and again as a return value for that search condition. Using a nondeterministic function, such as RANDOM, in a scalar_expression_n may have unexpected results, because if the first calculation of scalar_expression_n is not NULL, the second calculation of that scalar_expression_n, which is returned as the value of the COALESCE expression, might be NULL. You can use a scalar subquery in a COALESCE expression. However, if you use a non-scalar subquery (a subquery that returns more than one row), a runtime error is returned. Syntax element … Specifies … scalar_expression_n an argument list. Each COALESCE function must have at least two operands. 1101E227 COALESCE , 2 ( scalar_expression_n ) Chapter 2: CASE Expressions COALESCE Expression SQL Functions, Operators, Expressions, and Predicates 43 For additional information, such as the rules for evaluation and result data type, see “CASE” on page 25. Default Title The default title for a COALESCE expression appears as: Restrictions on the Data Types in a COALESCE Expression The following restrictions apply to CLOB, BLOB, and UDT types in a COALESCE expression: Example 1 The following example returns the home phone number of the named individual (if present), or office phone if HomePhone is null, or MessageService if present and both home and office phone values are null. Returns NULL if all three values are null. SELECT Name, COALESCE (HomePhone, OfficePhone, MessageService) FROM PhoneDir; Example 2 The following example uses COALESCE with an arithmetic operator. SELECT COALESCE(Boxes,0) * 100 FROM Shipments; Example 3 The following example uses COALESCE with a comparison operator. SELECT Name FROM Directory WHERE Organization <> COALESCE (Level1, Level2, Level3); Data Type Restrictions BLOB A BLOB can only appear in the argument list when it is cast to BYTE or VARBYTE. CLOB A CLOB can only appear in the argument list when it is cast to CHAR or VARCHAR. UDT Multiple UDTs can appear in the argument list only when they are identical types because Teradata Database does not perform implicit type conversion on UDTs in a COALESCE expression. Chapter 2: CASE Expressions NULLIF Expression 44 SQL Functions, Operators, Expressions, and Predicates NULLIF Expression Purpose NULLIF returns NULL if its arguments are equal. Otherwise, it returns its first argument, scalar_expression_1. NULLIF is a shorthand expression for the following full CASE expression: CASE WHEN scalar_expression_1=scalar_expression_2 THEN NULL ELSE scalar_expression_1 END Syntax where: ANSI Compliance NULLIF is ANSI SQL:2008 compliant. Usage Notes The scalar_expression_1 argument may be evaluated twice: once as part of the search condition (see the preceding expanded CASE expression) and again as a return value for the ELSE clause. Using a nondeterministic function, such as RANDOM, may have unexpected results if the first calculation of scalar_expression_1 is not equal to scalar_expression_2, in which case the result of the CASE expression is the value of the second calculation of scalar_expression_1, which may be equal to scalar_expression_2. You can use a scalar subquery in a NULLIF expression. However, if you use a non-scalar subquery (a subquery that returns more than one row), a runtime error is returned. Syntax element … Specifies … scalar_expression_1 the scalar expression to the left of the = in the expanded CASE expression, as shown previously in “Purpose.” scalar_expression_2 the scalar expression to the right of the = in the expanded CASE expression, as shown previously in “Purpose.” HH01B094 NULLIF ( scalar_expression1, scalar_expression2 ) Chapter 2: CASE Expressions NULLIF Expression SQL Functions, Operators, Expressions, and Predicates 45 For additional information, such as the rules for evaluation and result data type, see “CASE” on page 25. Default Title The default title for a NULLIF expression appears as: Restrictions on the Data Types in a NULLIF Expression The following restrictions apply to CLOB, BLOB, and UDT types in a NULLIF expression: Examples The following examples show queries on the following table: CREATE TABLE Membership (FullName CHARACTER(39) ,Age SMALLINT ,Code CHARACTER(4) ); Example 1 Here is the ANSI-compliant form of the Teradata SQL NULLIFZERO(Age) function, and is more versatile. SELECT FullName, NULLIF (Age,0) FROM Membership; Example 2 In the following query, blanks indicate no value. SELECT FullName, NULLIF (Code, ' ') FROM Membership; Example 3 The following example uses NULLIF in an expression with an arithmetic operator. SELECT NULLIF(Age,0) * 100; Data Type Restrictions BLOB A BLOB can only appear in the argument list when it is cast to BYTE or VARBYTE. CLOB A CLOB can only appear in the argument list when it is cast to CHAR or VARCHAR. UDT Multiple UDTs can appear in the argument list only when they are identical types and have an ordering definition. Chapter 2: CASE Expressions NULLIF Expression 46 SQL Functions, Operators, Expressions, and Predicates SQL Functions, Operators, Expressions, and Predicates 47 CHAPTER 3 Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions This chapter describes the SQL arithmetic operators and functions/trigonometric and hyperbolic functions. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions Arithmetic Operators 48 SQL Functions, Operators, Expressions, and Predicates Arithmetic Operators Teradata SQL supports the following arithmetic operators. ANSI Compliance Except for MOD and **, the arithmetic operators are ANSI SQL:2008 compliant. Arithmetic Operators and LOBs Arithmetic operators do not support BLOB or CLOB types. Arithmetic Operators and DateTime and Interval Data Types For details on the arithmetic operators permitted for DateTime and Interval data types, see “Arithmetic Operators” on page 229. Arithmetic Operators and Period Data Types For details on the arithmetic operators permitted for Period data types, see “Arithmetic Operators” on page 287. Operator Function ** Exponentiate This is a Teradata extension to the ANSI SQL:2008 standard. * Multiply / Divide MOD Modulo (remainder). MOD calculates the remainder in a division operation. For example, 60 MOD 7 = 4: 60 divided by 7 equals 8, with a remainder of 4. The result takes the sign of the dividend, thus: -17 MOD 4 = -1 -17 MOD -4 = -1 17 MOD -4 = 1 17 MOD 4 = 1 This is a Teradata extension to the ANSI SQL:2008 standard. + Add - Subtract + Unary plus (positive value) - Unary minus (negative value) Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions Binary Arithmetic Result Data Types SQL Functions, Operators, Expressions, and Predicates 49 Arithmetic Operators and UDTs By default, Teradata Database performs implicit type conversion on a UDT argument that has an implicit cast that casts between the UDT and a predefined numeric data type such as FLOAT or INTEGER. To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including arithmetic operators, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see “Implicit Type Conversions” on page 745. Binary Arithmetic Result Data Types The data type of the result of an arithmetic expression depends on the data types of the two operands. Operands are converted to the result type before the operation is performed. For example, before an INTEGER value is added to a FLOAT value, the INTEGER value is converted to FLOAT, the data type of the result. Result Data Type The following table shows the result data type for binary arithmetic operators. The result data type for binary arithmetic operations involving UDT operands is the same as the result data type for the predefined data types to which the UDTs are implicitly cast. For details on the result data type for binary arithmetic operations involving DateTime and Interval types, see “Arithmetic Operators and Result Types” on page 229. When the operand on the left is … And the operand on the right is … And the operator is … Then the result data type is … any type any type ** FLOAT DATE BYTEINT SMALLINT INTEGER BIGINT + - DATE1 BYTEINT SMALLINT INTEGER * / MOD INTEGER4 Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions Binary Arithmetic Result Data Types 50 SQL Functions, Operators, Expressions, and Predicates DATE (continued) BIGINT * / MOD BIGINT4 DECIMAL(k,j) + - DATE2,4 * / MOD DECIMAL(p,j)4,6 FLOAT * / + - MOD FLOAT DATE - INTEGER5 + * / MOD INTEGER4 CHAR(n) VARCHAR(n) , / + - MOD FLOAT3,4 BYTEINT SMALLINT INTEGER BYTEINT SMALLINT INTEGER * / + - MOD INTEGER BIGINT * / + - MOD BIGINT DECIMAL(k,j) * / + - MOD DECIMAL(p,j)6 FLOAT * / + - MOD FLOAT CHAR(n) VARCHAR(n) * / + - MOD FLOAT3 DATE + DATE1 - error * / MOD INTEGER4 BIGINT BYTEINT SMALLINT INTEGER BIGINT * / + - MOD BIGINT DECIMAL(k,j) * / + - MOD DECIMAL(p,j)6 FLOAT * / + - MOD FLOAT CHAR(n) VARCHAR(n) * / + - MOD FLOAT3 DATE + DATE1 - error * / MOD BIGINT4 When the operand on the left is … And the operand on the right is … And the operator is … Then the result data type is … Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions Binary Arithmetic Result Data Types SQL Functions, Operators, Expressions, and Predicates 51 1 If the value of a date result is not in the range of values allowed for the DATE type, an error is reported. The range is any date on the Gregorian calendar from year 1 to year 9999. DECIMAL(m,n) BYTEINT SMALLINT INTEGER BIGINT + - * DECIMAL(p,n)6 / MOD DECIMAL(m,n) DECIMAL(k,j) + - DECIMAL (min(p,(1+max(n,j)+max(m-n,k-j))), max(n,j))7 * DECIMAL(min(p,m+k),(n+j))7 / MOD DECIMAL(p,max(n,j))7 FLOAT * / + - MOD FLOAT CHAR(n) VARCHAR(n) * / + - MOD FLOAT3 DATE + DATE2 - error * DECIMAL(p,n)4,6 / MOD DECIMAL(m,n)4 FLOAT BYTEINT SMALLINT INTEGER BIGINT DECIMAL(k,j) FLOAT * / + - MOD FLOAT DATE * / + - MOD FLOAT4 CHAR(n) VARCHAR(n) * / + - MOD FLOAT3 CHAR(n) VARCHAR(n) BYTEINT SMALLINT INTEGER BIGINT DECIMAL(k,j) FLOAT CHAR(n) VARCHAR(n) * / + - MOD FLOAT3 DATE * / + - MOD FLOAT3,4 When the operand on the left is … And the operand on the right is … And the operator is … Then the result data type is … Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions Binary Arithmetic Result Data Types 52 SQL Functions, Operators, Expressions, and Predicates 2 Fractions of decimal values are truncated when added to or subtracted from date values. Note 1 also applies. 3 If an argument of an arithmetic operator is a character string, the first action is to attempt to convert the character string to a floating value. If this conversion fails, an error is reported. 4 These operations on DATE do not report an error, but results are generally not meaningful. 5 The difference between two dates is the number of days between those dates. Note that this is not the numeric difference between the values. 6 The value of p, the number of digits in the decimal result, depends on: • The value specified for MaxDecimal in DBSControl. For more information on DBSControl and MaxDecimal, see “DBS Control utility” in the Utilities book. • The number of digits in the decimal operand, where the number of digits is k for a DECIMAL(k,j) operand on the right side of the operator or m for a DECIMAL(m,n) operand on the left side of the operator. 7 The value of p in the definition of the decimal result data type depends on the value specified for MaxDecimal in DBSControl and the number of digits in the DECIMAL(m,n) and DECIMAL(k,j) operands. IF MaxDecimal is … AND the number of digits in the decimal operand is … THEN p is … 0 or 15 <= 15 15 > 15 and <=18 18 > 18 38 18 <= 18 18 > 18 38 38 any value 38 IF MaxDecimal is … AND … THEN p is … 0 or 15 m and k <= 15 15 (m or k > 15) and (m and k <= 18) 18 m or k > 18 38 18 m and k <= 18 18 m or k > 18 38 38 m and k = any value 38 Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions Structure of Arithmetic Expressions SQL Functions, Operators, Expressions, and Predicates 53 Error Conditions An error is reported when any of the following events occurs: • Division by zero is attempted. • The numeric range is exceeded. • The exponentiation operator is used with a negative left argument and a right argument that is not a whole number. Decimal Results and Rounding When computing an expression, decimal results that are not exact are rounded, not truncated. For more information on rounding rules and how the RoundHalfwayMagUp field in DBSControl affects rounding, see “Decimal/Numeric Data Types” in SQL Data Types and Literals and “DBS Control utility” in Utilities. Integer Division and Truncation Integer division yields whole results, truncated toward zero. Structure of Arithmetic Expressions Order of Evaluation The following table lists the precedence of operations in arithmetic expressions. Precedence Operation Highest + operand (unary plus) - operand (unary minus) Intermediate operand ** operand (exponentiation) operand * operand (multiplication) operand / operand (division) operand MOD operand (modulo operator) operand + operand (addition) operand - operand (subtraction) Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions Structure of Arithmetic Expressions 54 SQL Functions, Operators, Expressions, and Predicates In general, the order of evaluation is: 1 Operations enclosed in parentheses are performed first. 2 When no parentheses are present, operations are performed in order of precedence. 3 Operators of the same precedence are evaluated from left to right. The Optimizer may reorder evaluations based on associative and commutative properties of the operations involved. Format The format of an arithmetic expression is the same as the default format of the result data type. You can use the FORMAT phrase to change the default format of the result data type. The FORMAT phrase is relevant only in field mode, such as BTEQ applications, and in conversion to a character data type. Example You want to raise the salary for each employee in department 600 by $200 for each year spent with the company (up to a maximum of $2500 per month). To determine who is eligible, and the new salary, enter the following statement: SELECT Name, (Salary+(YrsExp*200))/12 AS Projection FROM Employee WHERE Deptno = 600 AND Projection < 2500 ; This statement returns the following response: Name Projection -------- ---------- Newman P 2483.33 The statement uses parentheses to perform the operation YrsExp * 200 first. Its result is then added to Salary and the total is divided by 12. The parentheses enclosing YrsExp * 200 are not strictly necessary, but the parentheses enclosing Salary + (YrsExp * 200) are necessary, because, if no parentheses were used in this expression, the operation YrsExp * 200 would be divided by 12 and the result added to Salary, producing an erroneous value. The phrase AS Projection in this example associates the arithmetic expression (Salary + (YrsExp * 200)/12) with Projection. Using the AS phrase lets you use the name Projection in the WHERE clause to refer to the entire expression. The result is formatted without a comma separating thousands from hundreds. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions Arithmetic Functions SQL Functions, Operators, Expressions, and Predicates 55 Arithmetic Functions The next sections describe the following arithmetic functions: • ABS • CASE_N • CEILING • EXP • FLOOR • LN • LOG • NULLIFZERO • RANDOM • RANGE_N • SQRT • WIDTH_BUCKET • ZEROIFNULL Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions ABS 56 SQL Functions, Operators, Expressions, and Predicates ABS Purpose Computes the absolute value of an argument. Syntax where: ANSI Compliance ABS is a Teradata extension to the ANSI SQL:2008 standard. Result Type and Attributes The following table lists the default attributes for the result of ABS(arg). For information on data type formats, see SQL Data Types and Literals. Argument Types and Rules If the argument is not numeric, it is converted to a numeric value, based on implicit type conversion rules. If the argument cannot be converted, an error is reported. For more information on implicit type conversion, see “Implicit Type Conversions” on page 745. Syntax element … Specifies … arg a numeric argument. 1101A480 ABS ( arg ) Data Type Format Title Same data type as arga a. Note that the NULL keyword has a data type of INTEGER. ABS(arg) IF the operand is … THEN the format is the default format for … numeric the resulting data type. character FLOAT. a UDT the predefined type to which the UDT is implicitly cast. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions ABS SQL Functions, Operators, Expressions, and Predicates 57 If arg is a character string, it is converted to a numeric value of the FLOAT data type. If arg is a UDT, the following rules apply: • The UDT must have an implicit cast to any of the following predefined types: • Numeric • Character • DateTime • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. • Implicit type conversion of UDTs for system operators and functions, including ABS, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. ABS cannot be applied to the following types of arguments: • BYTE or VARBYTE • BLOB or CLOB • CHARACTER or VARCHAR if the server character set is GRAPHIC Examples Representative ABS arithmetic function expressions and the results are as follows. Expression Result ABS(-12) 12 ABS('23') 2.30000000000000E+001 Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions CASE_N 58 SQL Functions, Operators, Expressions, and Predicates CASE_N Purpose Evaluates a list of conditions and returns the position of the first condition that evaluates to TRUE, provided that no prior condition in the list evaluates to UNKNOWN. Syntax where: ANSI Compliance CASE_N is a Teradata extension to the ANSI SQL:2008 standard. Syntax element … Specifies … conditional_expression a conditional expression or comma-separated list of condition expressions to evaluate. A conditional expression must evaluate to TRUE, FALSE, or UNKNOWN. NO CASE an optional condition that evaluates to TRUE if every conditional_expression in the list evaluates to FALSE. OR UNKNOWN an optional condition to use with NO CASE. The NO CASE OR UNKNOWN condition evaluates to TRUE if every conditional_expression in the list evaluates to FALSE, or if a conditional_expression evaluates to UNKNOWN and all prior conditions in the list evaluate to FALSE. UNKNOWN an optional condition that evaluates to TRUE if a conditional_expression evaluates to UNKNOWN and all prior conditions in the list evaluate to FALSE. 1101A069 A A NO CASE UNKNOWN OR UNKNOWN , UNKNOWN , ) CASE_N , ( conditional_expression Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions CASE_N SQL Functions, Operators, Expressions, and Predicates 59 Evaluation CASE_N evaluates conditional_expressions from left to right until a condition evaluates to TRUE or UNKNOWN, or until every condition evaluates to FALSE. The position of the first conditional_expression is one and the positions of subsequent conditions increment by one up to n, where n is the total number of conditional expressions. Result Type and Attributes The data type, format, and title for CASE_N are as follows. For information on default data type formats, see SQL Data Types and Literals. IF … THEN … a conditional_expression evaluates to TRUE, and all prior conditions evaluate to FALSE CASE_N returns the position of the conditional_expression. a conditional_expression evaluates to UNKNOWN, and all prior conditions evaluate to FALSE IF … THEN CASE_N returns … NO CASE OR UNKNOWN is specified n + 1. UNKNOWN is specified and NO CASE is not specified n + 1. NO CASE and UNKNOWN are specified n + 2. neither UNKNOWN nor NO CASE OR UNKNOWN is specified NULL. every conditional_expression evaluates to FALSE IF … THEN CASE_N returns … NO CASE or NO CASE OR UNKNOWN is specified n + 1. neither NO CASE nor NO CASE OR UNKNOWN is specified NULL. Data Type Format Title INTEGER Default format for INTEGER Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions CASE_N 60 SQL Functions, Operators, Expressions, and Predicates Using CASE_N to Define Partitioned Primary Indexes The primary index for a table or join index controls the distribution and retrieval of the data for that table or join index across the AMPs. If the primary index is a partitioned primary index (PPI), the data can be assigned to user-defined partitions on the AMPs. To define a primary index for a table or join index, you specify the PRIMARY INDEX phrase in the CREATE TABLE or CREATE JOIN INDEX data definition statement. To define a partitioned primary index, you include the PARTITION BY phrase when you define the primary index. The PARTITION BY phrase requires one or more partitioning expressions that determine the partition assignment of a row. You can use CASE_N to construct a partitioning expression such that a row with any value or NULL for the partitioning columns is assigned to some partition. You can also use RANGE_N to construct a partitioning expression. For more information, see “RANGE_N” on page 87. If the PARTITION BY phrase specifies a list of partitioning expressions, the PPI is a multilevel PPI, where each partition for a level is subpartitioned according to the next partitioning expression in the list. Unlike the partitioning expression for a single-level PPI, which can consist of any valid SQL expression (with some exceptions), each expression in the list of partitioning expressions for a multilevel PPI must be a CASE_N or RANGE_N function. You cannot ADD or DROP partitioning expressions that are based on a CASE_N function. To modify a partitioning expression that is based on a CASE_N function, you must use the ALTER TABLE statement with the MODIFY PRIMARY INDEX option to redefine the entire PARTITION BY clause, and the table must be empty. For more information, see “ALTER TABLE” in SQL Data Definition Language. Using CASE_N with CURRENT_DATE or CURRENT_TIMESTAMP in a PPI You can define a partitioning expression that uses CASE_N with the built-in functions CURRENT_DATE or CURRENT_TIMESTAMP. Subsequently, you can use the ALTER TABLE TO CURRENT statement to repartition the table data using a newly resolved current date or timestamp. For more information, see “Rules and Guidelines for Optimizing the Reconciliation of CASE_N PPI Expressions Based On Moving Current Date and Moving Current Timestamp” in SQL Data Definition Language Detailed Topics. Using CASE_N with Character Comparison You can specify conditional expressions in the CASE_N function that compare CHAR, VARCHAR, GRAPHIC or VARGRAPHIC data types. The following usage rules apply: • A CASE_N partitioning expression can use character or graphic comparison except when the comparison involves KANJI1 or KANJISJIS columns or constant expressions. • A CASE_N partitioning expression can use the UPPERCASE qualifier and the following functions: LOWER, UPPER, TRANSLATE, TRIM, VARGRAPHIC, INDEX, MINDEX, POSITION, TRANSLATE_CHK, CHAR2HEXINT. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions CASE_N SQL Functions, Operators, Expressions, and Predicates 61 • Any string literal referenced within a CASE_N expression must be less than 31,000 bytes. • The order of character data used in evaluating the conditional expressions in a CASE_N function is determined by the session collation and case specificity of the expression. • If the expression is not part of a PPI, the current session collation is used. • If the expression is part of a PPI, evaluation is done using the session collation that was in effect when the table or join index was created, or when the partitioning was modified using the ALTER TABLE statement. • The case specificity of column references and literals is determined based on the session default, or an explicit CAST, or a specification in the CREATE TABLE statement when the table was created. The column can be explicitly assigned to be CASESPECIFIC or NOT CASESPECIFIC, and constant expressions can be CAST with these qualifiers. If not explicitly specified, the default of NOT CASESPECIFIC is used if Teradata session transaction semantics are in effect. If ANSI session transaction semantics are in effect, the default is CASESPECIFIC. For example, if a conditional expression is a combination of NOT CASESPECIFIC expressions and a constant with no case specific qualifier (CASESPECIFIC, NOT CASESPECIFIC), the case specificity will be case specific in ANSI mode sessions and not case specific in Teradata mode sessions. Note: All character string comparisons involving graphic data are case specific. • In character comparison operations (=, <, >, <=, >=, <>, BETWEEN, LIKE), if a string literal is shorter than the column data to which it is compared, the string literal is treated as if it is padded with a pad character specific to the character set (for example, a character). Note that the pad character might not collate to the lowest code point in the collation. For a constant of length n, if the column value being compared precisely matches the constant for the first n characters, but contains a character that collates less than the pad character at position n+1, then the column value will collate less than the string literal. See “Example 9” on page 66. Restrictions If CASE_N is used in a PARTITION BY phrase, it: • Can specify a maximum of 65533 conditions (unless it is part of a larger partitioning expression) • Must not contain the system-derived columns PARTITION or PARTITION#L1 through PARTITION#L15 • Must not use Period data types, but can use the following: • BEGIN bound function for which input is a Period data type column and not a Period value expression. • END bound function for which input is a Period data type column and not a Period value expression. • IS [NOT] UNTIL_CHANGED. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions CASE_N 62 SQL Functions, Operators, Expressions, and Predicates • IS [NOT] UNTIL_CLOSED. If CASE_N is used in a partitioning expression for a multilevel PPI, it must define at least two partitions. Note that partition elimination for queries is often limited to constant or using value equality conditions on the partitioning columns, and the Optimizer may not eliminate some partitions when it possibly could. Also, evaluating a complex CASE_N may be costly in terms of CPU cycles and the overhead of CASE_N may cause the table header to be excessively large. Example 1 Here is an example that uses CASE_N and the value of the totalorders column to define the partition to which a row is assigned: CREATE TABLE orders (storeid INTEGER NOT NULL ,productid INTEGER NOT NULL ,orderdate DATE FORMAT 'yyyy-mm-dd' NOT NULL ,totalorders INTEGER) PRIMARY INDEX (storeid, productid) PARTITION BY CASE_N(totalorders < 100, totalorders < 1000, NO CASE, UNKNOWN); In the example, CASE_N specifies four partitions to which a row can be assigned, based on the value of the totalorders column. Example 2 Here is an example that modifies “Example 1” to use CASE_N in a list of partitioning expressions that define a multilevel PPI: CREATE TABLE orders (storeid INTEGER NOT NULL ,productid INTEGER NOT NULL ,orderdate DATE FORMAT 'yyyy-mm-dd' NOT NULL ,totalorders INTEGER NOT NULL) PRIMARY INDEX (storeid, productid) PARTITION BY (CASE_N(totalorders < 100, totalorders < 1000, NO CASE) ,CASE_N(orderdate <= '2005-12-31', NO CASE) ); The example defines six partitions to which a row can be assigned. The first CASE_N expression defines three partitions based on the value of the totalorders column. The second Partition Number Condition 1 The value of the totalorders column is less than 100. 2 The value of the totalorders column is less than 1000, but greater than or equal to 100. 3 The value of the totalorders column is greater than or equal to 1000. 4 The totalorders column is NULL. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions CASE_N SQL Functions, Operators, Expressions, and Predicates 63 CASE_N expression subdivides each of the three partitions into two partitions based on the value of the orderdate column. Example 3 The following example shows the count of rows in each partition if the orders table were to be partitioned using the CASE_N expression. CREATE TABLE orders (orderkey INTEGER NOT NULL ,custkey INTEGER ,orderdate DATE FORMAT 'yyyy-mm-dd' NOT NULL) PRIMARY INDEX (orderkey); INSERT INTO orders (1, 1, '1996-01-01'); INSERT INTO orders (2, 1, '1997-04-01'); The CASE_N expression in the following SELECT statement specifies three conditional expressions and the NO CASE condition. SELECT COUNT(*), CASE_N(orderdate >= '1996-01-01' AND orderdate <= '1996-12-31' AND custkey <> 999999, orderdate >= '1997-01-01' AND orderdate <= '1997-12-31' AND custkey <> 999999, orderdate >= '1998-01-01' AND orderdate <= '1998-12-31' AND custkey <> 999999, NO CASE ) AS Partition_Number Level 1 Partition Number Level 2 Partition Number Condition 1 1 The value of the totalorders column is less than 100 and the value of the orderdate column is less than or equal to '2005-12-31'. 2 The value of the totalorders column is less than 100 and the value of the orderdate column is greater than '2005-12-31'. 2 1 The value of the totalorders column is less than 1000 but greater than or equal to 100, and the value of the orderdate column is less than or equal to '2005-12-31'. 2 The value of the totalorders column is less than 1000 but greater than or equal to 100, and the value of the orderdate column is greater than '2005-12-31'. 3 1 The value of the totalorders column is greater than or equal to 1000 and the value of the orderdate column is less than or equal to '2005-12-31'. 2 The value of the totalorders column is greater than or equal to 1000 and the value of the orderdate column is greater than '2005-12-31'. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions CASE_N 64 SQL Functions, Operators, Expressions, and Predicates FROM orders GROUP BY Partition_Number ORDER BY Partition_Number; The results look like this: Count(*) Partition_Number ----------- ---------------- 1 1 1 2 Example 4 The following example creates a table partitioned with orders data for each quarter in 2008. CREATE TABLE Orders (O_orderkey INTEGER NOT NULL, O_custkey INTEGER, O_orderperiod PERIOD (DATE) NOT NULL, O_orderpriority CHAR (21), O_comment VARCHAR (79)) PRIMARY INDEX (O_orderkey) PARTITION BY CASE_N (END (O_orderperiod) <= date'2008-03-31', /* First Quarter */ END (O_orderperiod) <= date'2008-06-30', /* Second Quarter */ END (O_orderperiod) <= date'2008-09-30', /* Third Quarter */ END (O_orderperiod) <= date'2008-12-31' /* Fourth Quarter */ ); The following SELECT statement scans two partitions and displays the details of the orders placed for the first two quarters. SELECT * FROM Orders WHERE END (O_orderperiod) > date'2008-06-30'; Example 5 The following example uses IS [NOT] UNTIL_CHANGED in the PPI partitioning expression to check whether or not the ending bound of a period value expression is UNTIL_CHANGED. CREATE TABLE TESTUC (A INTEGER, B PERIOD (DATE), C INTEGER) PRIMARY INDEX (A) PARTITION BY CASE_N (END (b) IS UNTIL_CHANGED, END (b) IS NOT UNTIL_CHANGED, UNKNOWN); Example 6 The following example uses IS [NOT] UNTIL_CLOSED in the PPI partitioning expression to check whether or not the ending bound of a transaction time column is UNTIL_CLOSED. CREATE TABLE TESTUCL (A INTEGER, Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions CASE_N SQL Functions, Operators, Expressions, and Predicates 65 B PERIOD (TIMESTAMP (6) WITH TIME ZONE) NOT NULL AS TRANSACTIONTIME, C INTEGER) PRIMARY INDEX (A) PARTITION BY CASE_N (END (b) IS UNTIL_CLOSED, END (b) IS NOT UNTIL_CLOSED, UNKNOWN); Example 7 In this example, the session collation is ASCII. CASE_N (a<'b', a>='ba' and a<'dogg' and b<>'cow', c<>'boy', NO CASE OR UNKNOWN) The following table shows the result value returned by the above CASE_N function given the specified values for a, b, and c. x and y represent any value or NULL. The value 4 is returned when all the conditions are FALSE, or a condition is UNKNOWN with all preceding conditions evaluating to FALSE. Example 8 In this example, the session collation is ASCII. CASE_N (a<'b', a>='ba' and a<'dogg' and b<>'cow', c<>'boy', UNKNOWN) The following table shows the result value returned by the above CASE_N function given the specified values for a, b, and c. The x and y represent any value or NULL. The value 4 is returned if a condition is UNKNOWN with all preceding conditions evaluating to FALSE. NULL is returned if all the conditions are false. a b c Result 'a' x y 1 'boy' 'girl' y 2 'boy' NULL y 4 'boy' 'cow' 'man' 3 'boy' 'cow' 'boy' 4 'dog' 'ball' y 2 'dogg' x NULL 4 'dogg' x 'man' 3 'egg' x 'boy' 4 'egg' x NULL 4 'egg' x 'girl' 3 Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions CASE_N 66 SQL Functions, Operators, Expressions, and Predicates Example 9 In this example, the session collation is ASCII when submitting the CREATE TABLE statement, and the pad character is . The example defines two partitions (numbered 1 and 2) based on the value of a: • The value of a is between 'a ' (a followed by 9 spaces) and 'b '. • The value of a is between 'b ' and 'c '. CREATE SET TABLE t2 (a VARCHAR(10) CHARACTER SET UNICODE NOT CASESPECIFIC, b INTEGER) PRIMARY INDEX (a) PARTITION BY CASE_N(a BETWEEN 'a' AND 'b', a BETWEEN 'b' AND 'c'); The following INSERT statement inserts a character string consisting of a single character between the 'b' and '1'. INSERT t2 ('b 1', 1); The following INSERT statement inserts a character string consisting of a single character between the 'b' and '1'. INSERT t2 ('b 1', 2); The following SELECT statement shows the result of the INSERT statements. Since the character has a lower code point than the character, the first string inserted maps to partition 1. SELECT PARTITION, a, b FROM t2 ORDER BY 1; *** Query completed. 2 rows found. 3 columns returned. *** Total elapsed time was 1 second. a b c Result 'a' x y 1 'boy' 'girl' y 2 'boy' NULL y 4 'boy' 'cow' 'man' 3 'boy' 'cow' 'boy' NULL 'dog' 'ball' y 2 'dogg' x NULL 4 'dogg' x 'man' 3 'egg' NULL 'boy' NULL 'egg' x 'boy' NULL 'egg' x NULL 4 'egg' x 'girl' 3 Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions CASE_N SQL Functions, Operators, Expressions, and Predicates 67 PARTITION a b ----------- ------ ----- 1 b 1 1 (string contains single character) 2 b 1 2 (string contains single character) Related Topics For information on … See … PPI properties and performance considerations Database Design. PPI considerations and capacity planning the specification of a PPI for a table CREATE TABLE in SQL Data Definition Language. the specification of a PPI for a join index CREATE JOIN INDEX in SQL Data Definition Language. the modification of the partitioning of the primary index for a table ALTER TABLE in SQL Data Definition Language. the reconciliation of the partitioning based on newly resolved CURRENT_DATE and CURRENT_TIMESTAMP values ALTER TABLE TO CURRENT in SQL Data Definition Language Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions CEILING 68 SQL Functions, Operators, Expressions, and Predicates CEILING Purpose Returns the smallest integer that is not less than the input argument. Syntax where: ANSI Compliance CEILING is a Teradata extension to the ANSI SQL:2008 standard. Prerequisites CEILING is a domain-specific function; therefore, before you can use this function, you must run the Database Initialization Program (DIP) utility and execute the DIPALL or DIPUDT script. For details, see “Activating Domain-specific Functions” on page 20. Usage CEILING returns the following values: Invocation You can invoke the CEILING system function using the function name alone. For example, CEILING (arg). Syntax element… Specifies… arg a numeric expression. CEILING ( arg ) TD_SYSFNLIB. CEIL 1101A662 IF arg is... THEN CEILING returns... a non-exact number the next integer that is greater than arg. an exact number the input argument arg. NULL NULL. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions CEILING SQL Functions, Operators, Expressions, and Predicates 69 The CEILING function is associated with the system database TD_SYSFNLIB, and you can also invoke the function using the fully qualified syntax. For example, TD_SYSFNLIB.CEILING(arg). Note: If you try to invoke the CEILING system function using the function name alone, but you also have a user-defined function (UDF) named CEILING in the current database or in the SYSLIB database, Teradata Database will execute the user-developed UDF instead of the CEILING system function. You must remove any user-developed functions named CEILING (or CEIL) from the normal UDF search path or invoke the CEILING system function using the fully qualified syntax. For details, see “Invoking Domain-specific Functions” on page 21. Argument Types and Rules CEILING is an overloaded scalar function. It is defined with the following parameter data types: • BYTEINT • SMALLINT • INTEGER • BIGINT • FLOAT All numeric expressions passed to this function must either match one of these declared data types or can be converted to one of these types using the implicit data type conversion rules that apply to UDFs. Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If any argument cannot be converted to one of the declared data types by following UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” and “Parameter Types in Overloaded Functions” in SQL External Routine Programming. If the argument cannot be converted to one of the declared data types, an error is returned indicating that no function exists that matches the DML UDF expression submitted. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result Type and Attributes The result data type depends on the data type of the numeric input argument that is passed to the function as shown in the following table: IF the data type of the input argument is... THEN the result type is... AND the format is the default format for... BYTEINT BYTEINT BYTEINT SMALLINT SMALLINT SMALLINT Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions CEILING 70 SQL Functions, Operators, Expressions, and Predicates The default title for CEILING is: CEILING(arg). For information on default data type formats, see SQL Data Types and Literals. Example 1 The following query will return the FLOAT value 1.6E1, since 16 is the smallest integer that is not less than the FLOAT value 15.7E0. SELECT CEILING(157E-1); Example 2 In the following query, the DECIMAL value 15.7 will be implicitly cast to the FLOAT value 157E-1. The query will return the result FLOAT value 1.6E1, since 16 is the smallest integer that is not less than the FLOAT value 15.7E0. SELECT CEILING(15.7); Example 3 In the following query, the DECIMAL value -12.3 will be implicitly cast to the FLOAT value -123E-1. The query will return the result FLOAT value -1.2E1, since -12 is the smallest integer that is not less than the FLOAT value -12.3E0. SELECT CEILING(-12.3); INTEGER INTEGER INTEGER BIGINT BIGINT BIGINT FLOAT FLOAT FLOAT IF the data type of the input argument is... THEN the result type is... AND the format is the default format for... Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions EXP SQL Functions, Operators, Expressions, and Predicates 71 EXP Purpose Raises e (the base of natural logarithms) to the power of the argument, where e = 2.71828182845905. Syntax where: ANSI Compliance EXP is a Teradata extension to the ANSI SQL:2008 standard. Result Type and Attributes The following table lists the default attributes for the result of EXP(arg). For information on default data type formats, see SQL Data Types and Literals. Argument Types and Rules If arg is not FLOAT, it is converted to FLOAT, based on implicit type conversion rules. If the argument cannot be converted, an error is reported. For more information on implicit type conversion, see “Implicit Type Conversions” on page 745. If arg is a UDT, the following rules apply: • The UDT must have an implicit cast to any of the following predefined types: • Numeric • Character • DATE Syntax element … Specifies … arg a numeric argument. 1101A484 EXP ( arg ) Data Type Format Title FLOAT Default format for the resulting data type EXP(arg) Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions EXP 72 SQL Functions, Operators, Expressions, and Predicates To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. • Implicit type conversion of UDTs for system operators and functions, including EXP, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. EXP cannot be applied to the following types of arguments: • BYTE or VARBYTE • BLOB or CLOB • CHARACTER or VARCHAR if the server character set is GRAPHIC Usage Notes Executing EXP may sometimes result in a numeric overflow error. Examples Representative EXP arithmetic function expressions and the results are as follows. Expression Result EXP(1) 2.71828182845905E+000 EXP(0) 1.00000000000000E+000 Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions FLOOR SQL Functions, Operators, Expressions, and Predicates 73 FLOOR Purpose Returns the largest integer equal to or less than the input argument. Syntax where: ANSI Compliance FLOOR is a Teradata extension to the ANSI SQL:2008 standard. Prerequisites FLOOR is a domain-specific function; therefore, before you can use this function, you must run the Database Initialization Program (DIP) utility and execute the DIPALL or DIPUDT script. For details, see “Activating Domain-specific Functions” on page 20. Usage FLOOR returns the following values: Invocation You can invoke the FLOOR system function using the function name alone. For example, FLOOR(arg). Syntax element… Specifies… arg a numeric expression. FLOOR ( arg ) TD_SYSFNLIB. 1101A663 IF arg is... THEN FLOOR returns... a non-exact number the next largest integer that is equal to or less than arg. an exact number the input argument arg. NULL NULL. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions FLOOR 74 SQL Functions, Operators, Expressions, and Predicates The FLOOR function is associated with the system database TD_SYSFNLIB, and you can also invoke the function using the fully qualified syntax. For example, TD_SYSFNLIB.FLOOR(arg). Note: If you try to invoke the FLOOR system function using the function name alone, but you also have a user-defined function (UDF) named FLOOR in the current database or in the SYSLIB database, Teradata Database will execute the user-developed UDF instead of the FLOOR system function. You must remove any user-developed functions named FLOOR from the normal UDF search path or invoke the FLOOR system function using the fully qualified syntax. For details, see “Invoking Domain-specific Functions” on page 21. Argument Types and Rules FLOOR is an overloaded scalar function. It is defined with the following parameter data types: • BYTEINT • SMALLINT • INTEGER • BIGINT • FLOAT All numeric expressions passed to this function must either match one of these declared data types or can be converted to one of these types using the implicit data type conversion rules that apply to UDFs. Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If any argument cannot be converted to one of the declared data types by following UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” and “Parameter Types in Overloaded Functions” in SQL External Routine Programming. If the argument cannot be converted to one of the declared data types, an error is returned indicating that no function exists that matches the DML UDF expression submitted. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result Type and Attributes The result data type depends on the data type of the numeric input argument that is passed to the function as shown in the following table: IF the data type of the input argument is... THEN the result type is... AND the format is the default format for... BYTEINT BYTEINT BYTEINT SMALLINT SMALLINT SMALLINT INTEGER INTEGER INTEGER Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions FLOOR SQL Functions, Operators, Expressions, and Predicates 75 The default title for FLOOR is: FLOOR(arg). For information on default data type formats, see SQL Data Types and Literals. Example 1 The following query will return the FLOAT value 1.3E1, since 13 is the largest integer that is less than the FLOAT value 13.6E0. SELECT FLOOR (136E-1); Example 2 In the following query, the DECIMAL value -6.5 will be implicitly cast to the FLOAT value -6.5E0. The query will return the result FLOAT value -7E0, since -7 is the largest integer that is less than the FLOAT value -6.5E0. SELECT FLOOR(-6.5); BIGINT BIGINT BIGINT FLOAT FLOAT FLOAT IF the data type of the input argument is... THEN the result type is... AND the format is the default format for... Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions LN 76 SQL Functions, Operators, Expressions, and Predicates LN Purpose Computes the natural logarithm of the argument. Syntax where: ANSI Compliance LN is a Teradata extension to the ANSI SQL:2008 standard. Result Type and Attributes The data type, format, and title for LN(arg) are as follows. For information on default data type formats, see SQL Data Types and Literals. Argument Types and Rules If arg is not FLOAT, it is converted to FLOAT based on implicit type conversion rules. If the argument cannot be converted, an error is reported. For more information on implicit type conversion, see “Implicit Type Conversions” on page 745. If arg is a UDT, the following rules apply: • The UDT must have an implicit cast to any of the following predefined types: • Numeric • Character • DATE Syntax element … Specifies … arg a nonzero, positive numeric argument. 1101A485 LN ( arg ) Data Type Format Title FLOAT Default format for FLOAT LN(arg) Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions LN SQL Functions, Operators, Expressions, and Predicates 77 To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. • Implicit type conversion of UDTs for system operators and functions, including LN, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. LN cannot be applied to the following types of arguments: • BYTE or VARBYTE • BLOB or CLOB • CHARACTER or VARCHAR if the server character set is GRAPHIC Examples Representative LN arithmetic function expressions and the results are as follows. Expression Result LN(2.71828182845905) 1.00000000000000E+000 LN(0) Error Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions LOG 78 SQL Functions, Operators, Expressions, and Predicates LOG Purpose Computes the base 10 logarithm of an argument. Syntax where: ANSI Compliance LOG is a Teradata extension to the ANSI SQL:2008 standard. Result Type and Attributes The data type, format, and title for LOG(arg) are as follows. For information on default data type formats, see SQL Data Types and Literals. Argument Types and Rules If arg is not FLOAT, it is converted to FLOAT based on implicit type conversion rules. If the argument cannot be converted, an error is reported. For more information on implicit type conversion, see “Implicit Type Conversions” on page 745. If arg is a UDT, the following rules apply: • The UDT must have an implicit cast to any of the following predefined types: • Numeric • Character • DATE Syntax element … Specifies … arg a nonzero, positive numeric argument. 1101A486 LOG ( arg ) Data Type Format Title FLOAT Default format for FLOAT LOG(arg) Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions LOG SQL Functions, Operators, Expressions, and Predicates 79 To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. • Implicit type conversion of UDTs for system operators and functions, including LOG, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. LOG cannot be applied to the following types of arguments: • BYTE or VARBYTE • BLOB or CLOB • CHARACTER or VARCHAR if the server character set is GRAPHIC Examples Representative LOG arithmetic function expressions and the results are as follows. Expression Result LOG(50) 1.69897000433602E+000 LOG(100) 2.00000000000000E+000 Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions NULLIFZERO 80 SQL Functions, Operators, Expressions, and Predicates NULLIFZERO Purpose Converts data from zero to null to avoid problems with division by zero. Syntax where: ANSI Compliance NULLIFZERO is a Teradata extension to the ANSI SQL:2008 standard. The ANSI form of this function is the CASE shorthand expression NULLIF. For more information, see “NULLIF Expression” on page 44. Result Type and Attributes Here are the default attributes for the result of NULLIFZERO(arg). For information on data type formats, see SQL Data Types and Literals. Syntax element … Specifies … arg a numeric argument, or an argument that can be converted to a numeric argument based on implicit type conversion rules. 1101F225 NULLIFZERO ( arg ) Data Type and Format Title NULLIFZERO(arg) IF arg is … THEN the data type is … AND the format is the … numeric the same type as arga a. Note that the NULL keyword has a data type of INTEGER. same format as arg. character FLOAT default format for FLOAT. a UDT the type to which the UDT is implicitly cast the format of the data type to which the UDT is implicitly cast. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions NULLIFZERO SQL Functions, Operators, Expressions, and Predicates 81 Result Value Argument Types and Rules If arg is not numeric, it is converted to a numeric value, based on implicit type conversion rules. If the argument cannot be converted, an error is reported. For more information on implicit type conversion, see “Implicit Type Conversions” on page 745. If arg is a character string, it is converted to a numeric value of FLOAT data type. If arg is a UDT, the following rules apply: • The UDT must have an implicit cast to any of the following predefined types: • Numeric • Character • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. • Implicit type conversion of UDTs for system operators and functions, including NULLIFZERO, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. NULLIFZERO cannot be applied to the following types of arguments: • BYTE or VARBYTE • BLOB or CLOB • CHARACTER or VARCHAR if the server character set is GRAPHIC Example 1 The following expressions return an error if the value of x or expression is zero. 6 / x 6 / expression On the other hand, the following expressions return null, which is not an error because there is no violation of the divide by zero rule. 6 / NULLIFZERO(x) 6 / NULLIFZERO(expression) IF the value of arg is … THEN NULLIFZERO returns … nonzero the value of the numeric argument null or zero NULL Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions NULLIFZERO 82 SQL Functions, Operators, Expressions, and Predicates Example 2 The following request returns a null in the second column because the HCap field value for Newman is zero. In BTEQ (field mode) this appears as a ‘?’. SELECT empno, NULLIFZERO(hcap) FROM employee WHERE empno = 10019 ; Related Topics For additional expressions involving checks for nulls, see: • “COALESCE Expression” on page 42 • “NULLIF Expression” on page 44 • “ZEROIFNULL” on page 107 Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANDOM SQL Functions, Operators, Expressions, and Predicates 83 RANDOM Purpose Returns a random integer number for each row of the results table. Syntax where: ANSI Compliance RANDOM is a Teradata extension to the ANSI SQL:2008 standard. Result Type and Attributes The data type, format, and title for RANDOM(x,y) are as follows. For information on default data type formats, see SQL Data Types and Literals. Computation RANDOM uses the linear congruential algorithm and 48-bit integer arithmetic. Syntax element … Specifies … lower_bound an integer constant to define the lower bound on the closed interval over which a random number is to be selected. The limits for lower_bound range from -2147483648 to 2147483647, inclusive. lower_bound must be less than or equal to upper_bound. upper_bound an integer constant to define the upper bound on the closed interval over which a random number is to be selected. The limits for upper_bound range from -2147483648 to 2147483647, inclusive. upper_bound must be greater than or equal to lower_bound. 1101C025 RANDOM ( lower_bound, upper_bound ) Data Type Format Title INTEGER Default format for INTEGER Random(x,y) Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANDOM 84 SQL Functions, Operators, Expressions, and Predicates The algorithm works by generating a sequence of 48-bit integer values, Xi, using the following equation: where: Multiple RANDOM Calls Within a SELECT List You can call RANDOM any number of times in the SELECT list, for example: SELECT RANDOM(1,100), RANDOM(1,100); Each call defines a new random value. Restrictions The following rules and restrictions apply to the use of the RANDOM function. • RANDOM can only be called in one of the following SELECT query clauses: • WHERE • GROUP BY • ORDER BY • HAVING/QUALIFY • RANDOM cannot be referenced by position in a GROUP BY or ORDER BY clause. • RANDOM cannot be nested inside aggregate or ordered analytical functions. • RANDOM cannot be used in the expression list of an INSERT statement to create a primary index or partitioning column value. For example: INSERT t1 (RANDOM(1,10),...) RANDOM causes an error to be reported in this case if the first column in the table is a primary index or partitioning column. This variable … Represents … X a random number over a defined closed interval n an integer >= 0 a 0x5DEECE66D c 0xB % the modulo operator m 248 Xn+1 = (aXn + c) % m Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANDOM SQL Functions, Operators, Expressions, and Predicates 85 Using RANDOM as a Condition on an Index Because the RANDOM function is evaluated for each selected row, a condition on an index column that includes the RANDOM function results in an all-AMP operation. For example, consider the following table definition: CREATE TABLE t1 (c1 INTEGER ,c2 VARCHAR(9)) PRIMARY INDEX ( c1 ); The following SELECT statement results in an all-AMP operation: SELECT * FROM t1 WHERE c1 = RANDOM(1,12); Example Suppose you have a table named sales_table with the following subset of columns. The following SELECT statement returns a random number between 1 and 3, inclusive, for each row in the results table. SELECT store_id, product_id, sales, RANDOM(1,3) FROM sales_table; The results table might look like this. Store_ID Product_ID Sales 1003 C 20000 1002 C 35000 1001 C 60000 1002 D 50000 1003 D 50000 1001 D 35000 1001 A 100000 1002 A 40000 1001 E 30000 Store_ID Product_ID Sales RANDOM(1,3) 1003 C 20000 1 1002 C 35000 2 1001 C 60000 2 Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANDOM 86 SQL Functions, Operators, Expressions, and Predicates 1002 D 50000 3 1003 D 50000 2 1001 D 35000 3 1001 A 100000 2 1002 A 40000 1 1001 E 30000 2 Store_ID Product_ID Sales RANDOM(1,3) Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANGE_N SQL Functions, Operators, Expressions, and Predicates 87 RANGE_N Purpose Evaluates an expression and maps the result into one of a list of specified ranges and returns the position of the range in the list. Syntax where: Syntax element … Specifies … test_expression an expression that results in a BYTEINT, SMALLINT, INTEGER, DATE, CHAR, VARCHAR, GRAPHIC or VARGRAPHIC data type. NO RANGE UNKNOWN OR UNKNOWN , UNKNOWN , RANGE_N ( test_expression BETWEEN 1101B068 A B B C D C A start_expression AND end_expression * EACH range_size * AND end_expression * start_expression AND end_expression EACH range_size * D | range_list | ) AND end_expression , start_expression AND end_expression EACH range_size , start_expression AND end_expression * EACH range_size range_list Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANGE_N 88 SQL Functions, Operators, Expressions, and Predicates start_expression * a constant or constant expression that defines the starting boundary of a range. The data type of start_expression must be the same as the data type of test_expression, or must be such that it can be implicitly cast to the same data type as test_expression. If an ending boundary is not specified, the range is defined by its starting boundary, inclusively, up to but not including the starting boundary of the next range. Use an asterisk ( * ) for the starting boundary of the first range in the list to indicate the lowest possible value (all values and NULL are greater than a starting boundary specified as an asterisk). An asterisk is compatible with any data type. end_expression * a constant or constant expression that defines the ending boundary of a range. The data type of end_expression must be the same as the data type of test_expression, or must be such that it can be implicitly cast to the same data type as test_expression. The last range in the list must specify an ending boundary. For all other ranges, if an ending boundary is not specified, the range is defined by its starting boundary, inclusively, up to but not including the starting boundary of the next range. Use an asterisk ( * ) for the ending boundary of the last range in the list to indicate the highest possible value (all values and NULL are less than an ending boundary specified as an asterisk). EACH range_size a constant or constant expression with a value greater than zero. A range that specifies an EACH phrase is equivalent to a series of ranges, where the first range in the series starts at start_expression, and subsequent ranges start at start_expression + (range_size * n), where n starts at one and increments by one while start_expression + (range_size * n) is less than or equal to end_expression, or less than the next start_expression in the list of ranges. For DATE types, the calculation of valid dates in subsequent ranges uses ADD_MONTHS instead of the + arithmetic operator. For more information on ADD_MONTHS, see “ADD_MONTHS” on page 236. The data type of range_size must be compatible for adding to test_expression. Note: If the data type of test_expression is a character type (CHAR, VARCHAR, GRAPHIC or VARGRAPHIC), you cannot specify the EACH phrase. NO RANGE an optional range to handle a test_expression that does not map into any of the specified ranges. OR UNKNOWN an option to use with NO RANGE. The NO RANGE OR UNKNOWN option handles a test_expression that does not map into any of the specified ranges, or a test_expression that evaluates to NULL when RANGE_N does not specify the range BETWEEN * AND *. UNKNOWN an option to handle a test_expression that evaluates to NULL when RANGE_N does not specify the range BETWEEN * AND *. Syntax element … Specifies … Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANGE_N SQL Functions, Operators, Expressions, and Predicates 89 ANSI Compliance RANGE_N is a Teradata extension to the ANSI SQL:2008 standard. Range Definition A range is defined by a starting boundary and an optional ending boundary. If an ending boundary is not specified, the range is defined by its starting boundary, inclusively, up to but not including the starting boundary of the next range. The list of ranges must specify ranges in increasing order, where the ending boundary of a range is less than the starting boundary of the next range. Evaluation RANGE_N evaluates test_expression and determines whether the result is within a range in the list of ranges. The position of the first range is one and the positions of subsequent ranges increment by one up to n, where n is the total number of ranges. IF … THEN … the result of test_expression is within a range RANGE_N returns the position of the range. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANGE_N 90 SQL Functions, Operators, Expressions, and Predicates Result Type and Attributes The data type, format, and title for RANGE_N are as follows. For information on default data type formats, see SQL Data Types and Literals. the result of test_expression is NULL IF RANGE_N … THEN … does not specify one of the following: • BETWEEN * AND * • UNKNOWN • NO RANGE OR UNKNOWN RANGE_N returns NULL. specifies the range BETWEEN * AND * RANGE_N returns 1, regardless of whether NO RANGE, NO RANGE OR UNKNOWN, or UNKNOWN is specified. does not specify the range BETWEEN * AND * IF … THEN RANGE_N returns … NO RANGE OR UNKNOWN is specified n + 1. UNKNOWN is specified and NO RANGE is not specified n + 1. NO RANGE and UNKNOWN are specified n + 2. test_expression is outside all the ranges in the list IF … THEN RANGE_N returns … NO RANGE or NO RANGE OR UNKNOWN is specified n + 1. neither NO RANGE nor NO RANGE OR UNKNOWN is specified NULL. IF … THEN … Data Type Format Title INTEGER Default format of the INTEGER data type Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANGE_N SQL Functions, Operators, Expressions, and Predicates 91 Using RANGE_N to Define Partitioned Primary Indexes The primary index for a table or join index controls the distribution of the data for that table or join index across the AMPs, as well as its retrieval. If the primary index is a partitioned primary index (PPI), the data can be assigned to user-defined partitions on the AMPs. To define a primary index for a table or join index, you specify the PRIMARY INDEX phrase in the CREATE TABLE or CREATE JOIN INDEX data definition statement. To define a partitioned primary index, you include the PARTITION BY phrase when you define the primary index. The PARTITION BY phrase requires one or more partitioning expressions that determine the partition assignment of a row. You can use RANGE_N to construct a partitioning expression such that a row with any value or NULL for the partitioning columns is assigned to some partition. You can also use CASE_N to construct a partitioning expression. For more information, see “CASE_N” on page 58. If the PARTITION BY phrase specifies a list of partitioning expressions, the PPI is a multilevel PPI, where each partition for a level is subpartitioned according to the next partitioning expression in the list. Unlike the partitioning expression for a single-level PPI, which can consist of any valid SQL expression (with some exceptions), each expression in the list of partitioning expressions for a multilevel PPI must be a CASE_N or RANGE_N function. Using RANGE_N with CURRENT_DATE or CURRENT_TIMESTAMP in a PPI You can define a partitioning expression that uses RANGE_N with the built-in functions CURRENT_DATE or CURRENT_TIMESTAMP. Use of CURRENT_DATE or CURRENT_TIMESTAMP in a partitioning expression is most appropriate when the data must be partitioned as one or more current partitions and one or more history partitions where the current and history partitions are based on the resolved CURRENT_DATE or CURRENT_TIMESTAMP in the partitioning expression. This allows you to periodically reconcile the table to move older data from the current partition into one or more history partitions using the ALTER TABLE TO CURRENT statement instead of redefining the partitioning using explicit dates which must be determined each time the ALTER TABLE DROP/ADD RANGE is done. For more information, see “Rules and Guidelines for Optimizing the Reconciliation of RANGE_N PPI Expressions Based On Moving Current Date and Moving Current Timestamp” in SQL Data Definition Language Detailed Topics. Using RANGE_N with Character Data You can specify character expressions (CHAR, VARCHAR, GRAPHIC or VARGRAPHIC) as the test_expression and/or the range boundaries in a RANGE_N function. The following usage rules apply: • A RANGE_N partitioning expression can use the UPPERCASE qualifier and the following functions: LOWER, UPPER, TRANSLATE, TRIM, VARGRAPHIC, INDEX, MINDEX, POSITION, TRANSLATE_CHK, CHAR2HEXINT. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANGE_N 92 SQL Functions, Operators, Expressions, and Predicates • If test_expression is a character data type, you cannot specify the EACH phrase. • Any string literal referenced within a RANGE_N expression must be less than 31,000 bytes. • If test_expression is a character data type, and the length of any of the range boundaries (minus trailing pad characters) is greater than the length of test_expression, an error is returned. • For character RANGE_N partitioning, the increasing order of ranges is determined by the session collation and case specificity of the test_expression. If the test_expression is a combination of NOT CASESPECIFIC expressions and a constant with no case specific qualifier (CASESPECIFIC, NOT CASESPECIFIC), the case specificity will be case specific in ANSI mode sessions and not case specific in Teradata mode sessions. Note: All character string comparisons involving graphic data are case specific. • An error is returned if any of the specified ranges are defined with null boundaries, are not increasing, or overlap. For character test values, increasing order is determined by the session collation and case specificity of the test_expression. • In character comparison operations (=, <, >, <=, >=, <>, BETWEEN, LIKE), if a string literal is shorter than the column data to which it is compared, the string literal is treated as if it is padded with a pad character specific to the character set (for example, a character). Therefore, if a character test_expression is defined with a longer length than a character range boundary, comparison of the test _expression to that range boundary will behave as if the range boundary is padded with pad characters. Note that the pad character might not collate to the lowest code point in the collation. For a range boundary of length n, if the test_expression precisely matches that range boundary for the first n characters, but contains a character that collates less than the pad character at position n+1, then the test_expression will collate less than the range boundary. See “Example 10” on page 99. Restrictions If RANGE_N appears in a PARTITION BY phrase, it: • Can specify a maximum of 65,533 ranges (unless it is part of a larger partitioning expression) • Must not contain the system-derived columns PARTITION or PARTITION#L1 through PARTITION#L15 • Must not use Period data types, but can use the BEGIN or END bound functions on a Period data type column when they result in a DATE data type. If RANGE_N is used in a partitioning expression for a multilevel PPI, it must define at least two partitions. If RANGE_N specifies CURRENT_DATE or CURRENT_TIMESTAMP in a partitioning expression, you cannot use ALTER TABLE to add or drop ranges for the table. You must use the ALTER TABLE TO CURRENT statement to achieve this function. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANGE_N SQL Functions, Operators, Expressions, and Predicates 93 Using a UDT as the Test Expression The test_expression should not be an expression that results in a UDT data type. An error is reported if this occurs when RANGE_N is used to define a PPI. If RANGE_N is not used to define a PPI, you should explicitly cast the expression so that it is BYTEINT, SMALLINT, INTEGER, DATE, CHAR, VARCHAR, GRAPHIC or VARGRAPHIC instead of depending upon any implicit conversions. Example 1 Here is an example that uses RANGE_N and the value of the totalorders column to define the partition to which a row is assigned: CREATE TABLE orders (storeid INTEGER NOT NULL ,productid INTEGER NOT NULL ,orderdate DATE FORMAT 'yyyy-mm-dd' NOT NULL ,totalorders INTEGER) PRIMARY INDEX (storeid, productid) PARTITION BY RANGE_N(totalorders BETWEEN *, 100, 1000 AND *, UNKNOWN); In the example, RANGE_N specifies four partitions to which a row can be assigned, based on the value of the totalorders column: Example 2 Here is an example that modifies “Example 1” to use RANGE_N in a list of partitioning expressions that define a multilevel PPI: CREATE TABLE orders (storeid INTEGER NOT NULL ,productid INTEGER NOT NULL ,orderdate DATE FORMAT 'yyyy-mm-dd' NOT NULL ,totalorders INTEGER NOT NULL) PRIMARY INDEX (storeid, productid) PARTITION BY (RANGE_N(totalorders BETWEEN *, 100, 1000 AND *) ,RANGE_N(orderdate BETWEEN *, '2005-12-31' AND *) ); The example defines six partitions to which a row can be assigned. The first RANGE_N expression defines three partitions based on the value of the totalorders column. The second RANGE_N expression subdivides each of the three partitions into two partitions based on the value of the orderdate column. Partition Number Condition 1 The value of the totalorders column is less than 100. 2 The value of the totalorders column is less than 1000, but greater than or equal to 100. 3 The value of the totalorders column is greater than or equal to 1000. 4 The totalorders column is NULL, so the range is UNKNOWN. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANGE_N 94 SQL Functions, Operators, Expressions, and Predicates Example 3 Here is an example that defines a partitioned primary index that specifies one partition to which rows are assigned, for any value of the totalorders column, including NULL: CREATE TABLE orders (storeid INTEGER NOT NULL ,productid INTEGER NOT NULL ,orderdate DATE FORMAT 'yyyy-mm-dd' NOT NULL ,totalorders INTEGER) PRIMARY INDEX (storeid, productid) PARTITION BY RANGE_N(totalorders BETWEEN * AND *); Example 4 The following example shows the count of rows in each partition if the table were to be partitioned using the RANGE_N expression. CREATE TABLE orders (orderkey INTEGER NOT NULL ,custkey INTEGER ,orderdate DATE FORMAT 'yyyy-mm-dd') PRIMARY INDEX (orderkey); INSERT INTO orders (1, 100, '1998-01-01'); INSERT INTO orders (2, 100, '1998-04-01'); INSERT INTO orders (3, 109, '1998-04-01'); INSERT INTO orders (4, 101, '1998-04-10'); INSERT INTO orders (5, 100, '1998-07-01'); INSERT INTO orders (6, 109, '1998-07-10'); INSERT INTO orders (7, 101, '1998-08-01'); INSERT INTO orders (8, 101, '1998-12-01'); INSERT INTO orders (9, 111, '1999-01-01'); Level 1 Partition Number Level 2 Partition Number Condition 1 1 The value of the totalorders column is less than 100 and the value of the orderdate column is less than '2005-12-31'. 2 The value of the totalorders column is less than 100 and the value of the orderdate column is greater than or equal to '2005-12-31'. 2 1 The value of the totalorders column is less than 1000 but greater than or equal to 100, and the value of the orderdate column is less than '2005-12-31'. 2 The value of the totalorders column is less than 1000 but greater than or equal to 100, and the value of the orderdate column is greater than or equal to '2005-12-31'. 3 1 The value of the totalorders column is greater than or equal to 1000 and the value of the orderdate column is less than '2005-12-31'. 2 The value of the totalorders column is greater than or equal to 1000 and the value of the orderdate column is greater than or equal to '2005-12-31'. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANGE_N SQL Functions, Operators, Expressions, and Predicates 95 INSERT INTO orders (10, 111, NULL); The RANGE_N expression in the following SELECT statement uses the EACH phrase to define a series of 12 ranges, where the first range starts at '1998-01-01' and the ranges that follow have starting boundaries that increment sequentially by one month intervals. SELECT COUNT(*), RANGE_N(orderdate BETWEEN DATE '1998-01-01' AND DATE '1998-12-31' EACH INTERVAL '1' MONTH ) AS Partition_Number FROM orders GROUP BY Partition_Number ORDER BY Partition_Number; The results look like this: Count(*) Partition_Number ----------- ---------------- 2 ? 1 1 3 4 2 7 1 8 1 12 Example 5 The following example creates a table with partitioning defined using a RANGE_N expression involving the END bound function. The table creates 10 partitions where each partition represents the sales history for one year. CREATE TABLE SalesHistory (product_code CHAR (8), quantity_sold INTEGER, transaction_period PERIOD (DATE)) PRIMARY INDEX (product_code) PARTITION BY RANGE_N (END (transaction_period) BETWEEN date'2006-01-01' AND date '2015-12-31' EACH INTERVAL'1' YEAR); The following SELECT statement scans five partitions of the sales history before the year 2010. SELECT * FROM SalesHistory WHERE transaction_period < period (date'2010-01-01'); Example 6 Start_expression with CURRENT_DATE If CURRENT_DATE or CURRENT_TIMESTAMP is specified in the start_expression of the first range in RANGE_N, and if this start_expression when resolved with a new CURRENT_DATE or CURRENT_TIMESTAMP falls on a partition boundary, then all partitions prior to the partition matched are dropped. Otherwise, the entire table is repartitioned with the new partitioning expression. Consider the following CREATE TABLE statement submitted on April 1, 2006: CREATE TABLE ppi (i INT, j DATE) Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANGE_N 96 SQL Functions, Operators, Expressions, and Predicates PRIMARY INDEX (i) PARTITION BY RANGE_N (j BETWEEN CURRENT_DATE AND CURRENT_DATE + INTERVAL '1' YEAR - INTERVAL '1' DAY EACH INTERVAL '1' MONTH); The last resolved date is April 1, 2006. If you submit an ALTER TABLE TO CURRENT statement on June 1, 2006, the start_expression, newly resolved to CURRENT_DATE ('2006- 06-01'), falls on a partition boundary of the third partition. Therefore, partitions 1 and 2 are dropped, and the last reconciled date is set to the newly resolved CURRENT_DATE. However, if you submitted the ALTER TABLE TO CURRENT statement on June 10, 2006 instead of June 1, 2006, the start_expression, newly resolved to CURRENT_DATE ('2006-06- 10'), does not fall on a partition boundary. Therefore, all rows are scanned and the rows are repartitioned based on the new partitioning expression. The partition boundary after this statement aligns with the 10th day of the month instead of the earlier 1st day of the month. Example 7 The following table definition is created in the year 2007 (the current year at the time). The table is partitioned to record 5 years of order history plus orders for the current year and one future year. CREATE TABLE Orders (o_orderkey INTEGER NOT NULL, o_custkey INTEGER, o_orderstatus CHAR(1) CASESPECIFIC, o_totalprice DECIMAL(13,2) NOT NULL, o_orderdate DATE FORMAT 'yyyy-mm-dd' NOT NULL, o_orderpriority CHAR(21), o_comment VARCHAR(79)) PRIMARY INDEX (o_orderkey) PARTITION BY RANGE_N( o_orderdate BETWEEN DATE '2002-01-01' AND DATE '2008-12-31' EACH INTERVAL '1' MONTH) UNIQUE INDEX (o_orderkey); If, in 2008, you want to alter the table such that it continues to maintain 5 years of history plus the current year and one future year, you can submit the following statement in 2008: ALTER TABLE Orders MODIFY PRIMARY INDEX (o_orderkey) DROP RANGE WHERE PARTITION BETWEEN 1 AND 12 ADD RANGE BETWEEN DATE '2009-01-01' AND DATE '2009-12-31' EACH INTERVAL '1' MONTH WITH DELETE; In this case, you must compute the new dates and specify them explicitly in the ADD RANGE clause. This requires manual intervention every year the statement is submitted. Alternatively, you can define the table using CURRENT_DATE as follows. This makes it easier to alter the partitioning. CREATE TABLE Orders (o_orderkey INTEGER NOT NULL, o_custkey INTEGER, o_orderstatus CHAR(1) CASESPECIFIC, o_totalprice DECIMAL(13,2) NOT NULL, Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANGE_N SQL Functions, Operators, Expressions, and Predicates 97 o_orderdate DATE FORMAT 'yyyy-mm-dd' NOT NULL, o_orderpriority CHAR(21), o_comment VARCHAR(79)) PRIMARY INDEX (o_orderkey) PARTITION BY RANGE_N(o_orderdate BETWEEN CAST(((EXTRACT(YEAR FROM CURRENT_DATE)-5-1900)*10000+0101) AS DATE) AND CAST(((EXTRACT(YEAR FROM CURRENT_DATE)+1-1900)*10000+1231) AS DATE) EACH INTERVAL '1' MONTH) UNIQUE INDEX (o_orderkey); You can schedule the following ALTER TABLE statement to occur yearly. This statement rolls the partition window forward by efficiently dropping and adding partitions. ALTER TABLE Orders TO CURRENT WITH DELETE; With the use of CURRENT_DATE, you do not need to modify the ALTER TABLE statement each time you want to repartition the data based on the new dates. In both cases, the partitioning starts on a year boundary. In the first example, the ALTER TABLE statement does not change this, so partitioning continues to start on a year boundary. However, you can specifiy an ALTER TABLE statement that changes the partitioning to start on a different boundary. For example, you can roll forward to start on a particular month in a year by specifying the desired dates in the ALTER TABLE statement. In the second example, which uses CURRENT_DATE, you can only roll forward to start on a year boundary. However, you can modify the example as follows so that partitioning can be used to roll forward to start at the beginning of a month. This case assumes that, as of the CREATE TABLE date, the Orders table will contain the last 71 months of history plus the current month and 12 months in the future (a total of 84 months). CREATE TABLE Orders (o_orderkey INTEGER NOT NULL, o_custkey INTEGER, o_orderstatus CHAR(1) CASESPECIFIC, o_totalprice DECIMAL(13,2) NOT NULL, o_orderdate DATE FORMAT 'yyyy-mm-dd' NOT NULL, o_orderpriority CHAR(21), o_comment VARCHAR(79)) PRIMARY INDEX (o_orderkey) PARTITION BY RANGE_N(o_orderdate BETWEEN CAST(((EXTRACT(YEAR FROM CURRENT_DATE)-1900)*10000 + EXTRACT(MONTH FROM CURRENT_DATE)*100 + 01) AS DATE) - INTERVAL '71' MONTH AND CAST(((EXTRACT(YEAR FROM CURRENT_DATE)+1-1900)*10000 + EXTRACT(MONTH FROM CURRENT_DATE)*100 + 01) AS DATE)+ INTERVAL '13' MONTH - INTERVAL '1' DAY EACH INTERVAL '1' MONTH) UNIQUE INDEX (o_orderkey); You can schedule the following ALTER TABLE statement to occur monthly or less frequently (but before running out of future months). This statement rolls the partition window forward by dropping and adding partitions so that the Orders table continues to contain the last 71 months of history plus the current month and 12 months in the future. ALTER TABLE Orders TO CURRENT WITH DELETE; Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANGE_N 98 SQL Functions, Operators, Expressions, and Predicates You can define the following simpler partitioning , but it might not be optimized, and the entire table might be scanned to reconcile rows when you submit an ALTER TABLE TO CURRENT statement. This case assumes that, as of the CREATE TABLE date, the Orders table will contain about 2,191 days of history plus the current day and about 365 days in the future (a total of about 7 years). CREATE TABLE Orders (o_orderkey INTEGER NOT NULL, o_custkey INTEGER, o_orderstatus CHAR(1) CASESPECIFIC, o_totalprice DECIMAL(13,2) NOT NULL, o_orderdate DATE FORMAT 'yyyy-mm-dd' NOT NULL, o_orderpriority CHAR(21), o_comment VARCHAR(79)) PRIMARY INDEX (o_orderkey) PARTITION BY RANGE_N(o_orderdate BETWEEN CURRENT_DATE - INTERVAL '6' YEAR AND CURRENT_DATE + INTERVAL '1' YEAR EACH INTERVAL '1' MONTH) UNIQUE INDEX (o_orderkey); You can schedule the following ALTER TABLE statement to occur daily or less frequently (but before running out of future days). This statement rolls the partition window forward by dropping and adding partitions only if the CURRENT_DATE is the same day of the month as the day when the last CREATE TABLE or ALTER TABLE TO CURRENT statement was submitted. Otherwise, the entire table is scanned to reconcile the rows. ALTER TABLE Orders TO CURRENT WITH DELETE; This can be very inefficient if the ALTER TABLE statement is not submitted on the same day of the month as the day when the last CREATE TABLE or ALTER TABLE TO CURRENT statement was submitted. Performance degrades as the number of days between the last resolved date and the new resolved date increases due to the increasing number of rows that must be moved. For example, if the last resolved date was January 1, 2008, and the next ALTER TABLE TO CURRENT statement is submitted on February 2, 2008, all the rows of the table will be moved to new partitions. Example 8 The following example defines 5 ranges. The session collation is ASCII. RANGE_N(animal BETWEEN *, 'ape', 'bird', 'bull' AND 'cow', 'dog' AND *, NO RANGE, UNKNOWN) where: Range Includes... 1 all values less than 'ape'. 2 strings greater than or equal to 'ape' and less than 'bird'. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANGE_N SQL Functions, Operators, Expressions, and Predicates 99 If the value of animal matches one of the defined ranges, RANGE_N returns the number of the matched range. If the value of animal is greater than 'cow' but less than 'dog', it does not match any of the ranges, so RANGE_N returns 6 because NO RANGE is specified. If the value of animal is NULL, RANGE_N returns 7 because UNKNOWN is specified. Example 9 The following example defines 5 ranges. The session collation is ASCII. RANGE_N(animal BETWEEN *, 'ape', 'bird', 'bull' AND 'cow', 'dog' AND *, UNKNOWN) where: If the value of animal matches one of the defined ranges, RANGE_N returns the number of the matched range. If the value of animal is greater than 'cow' but less than 'dog', it does not match any of the ranges, so RANGE_N returns NULL because NO RANGE is not specified. If the value of animal is NULL, RANGE_N returns 6 because UNKNOWN is specified. Example 10 In this example, the session collation is ASCII when submitting the CREATE TABLE statement, and the pad character is . The example defines two ranges (numbered 1 and 2): • Any values greater than or equal to 'a ' (a followed by 9 spaces) or less than 'b ' are mapped to partition 1. 3 strings greater than or equal to 'bird' and less than 'bull'. 4 strings between 'bull' and 'cow'. 5 strings greater than or equal to 'dog'. Range Includes... Range Includes... 1 all values less than 'ape'. 2 strings greater than or equal to 'ape' and less than 'bird'. 3 strings greater than or equal to 'bird' and less than 'bull'. 4 strings between 'bull' and 'cow'. 5 strings greater than or equal to 'dog'. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions RANGE_N 100 SQL Functions, Operators, Expressions, and Predicates • Any values greater than or equal to 'b ' or less than 'c ' are mapped to partition 2. CREATE SET TABLE t2 (a VARCHAR(10) CHARACTER SET UNICODE NOT CASESPECIFIC, b INTEGER) PRIMARY INDEX (a) PARTITION BY RANGE_N(a BETWEEN 'a','b' AND 'c'); The following INSERT statement inserts a character string consisting of a single character between the 'b' and '1'. INSERT t2 ('b 1', 1); The following INSERT statement inserts a character string consisting of a single character between the 'b' and '1'. INSERT t2 ('b 1', 2); The following SELECT statement shows the result of the INSERT statements. Since the character has a lower code point than the character, the first string inserted maps to partition 1. SELECT PARTITION, a, b FROM t2 ORDER BY 1; *** Query completed. 2 rows found. 3 columns returned. *** Total elapsed time was 1 second. PARTITION a b ----------- ------ ----- 1 b 1 1 (string contains single character) 2 b 1 2 (string contains single character) Related Topics For information on … See … PPI properties and performance considerations Database Design. PPI considerations and capacity planning specifying a PPI for a table CREATE TABLE in SQL Data Definition Language. specifying a PPI for a join index CREATE JOIN INDEX in SQL Data Definition Language. modifying the partitioning of the primary index for a table ALTER TABLE in SQL Data Definition Language. the reconciliation of the partitioning based on newly resolved CURRENT_DATE and CURRENT_TIMESTAMP values ALTER TABLE TO CURRENT in SQL Data Definition Language Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions SQRT SQL Functions, Operators, Expressions, and Predicates 101 SQRT Purpose Computes the square root of an argument. Syntax where: ANSI Compliance SQRT is a Teradata extension to the ANSI SQL:2008 standard. Result Type and Attributes The data type, format, and title for SQRT(arg) are as follows. For information on default data type formats, see SQL Data Types and Literals. Argument Types and Rules If arg is not FLOAT, it is converted to FLOAT based on implicit type conversion rules. If the argument cannot be converted, an error is reported. For more information on implicit type conversion, see “Implicit Type Conversions” on page 745. If arg is a UDT, the following rules apply: • The UDT must have an implicit cast to any of the following predefined types: • Numeric • Character • DATE Syntax element … Specifies … arg a positive, numeric argument. 1101A487 SQRT ( arg ) Data Type Format Title FLOAT Default format for FLOAT SQRT(arg) Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions SQRT 102 SQL Functions, Operators, Expressions, and Predicates To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. • Implicit type conversion of UDTs for system operators and functions, including SQRT, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. SQRT cannot be applied to the following types of arguments: • BYTE or VARBYTE • BLOB or CLOB • CHARACTER or VARCHAR if the server character set is GRAPHIC Examples Representative SQRT arithmetic function expressions and the results are as follows. Expression Result SQRT(2) 1.41421356237309E+000 SQRT(-2) Error Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions WIDTH_BUCKET SQL Functions, Operators, Expressions, and Predicates 103 WIDTH_BUCKET Purpose Returns the number of the partition to which value_expression is assigned. Syntax where: ANSI Compliance WIDTH_BUCKET is ANSI SQL:2008 compliant. Result Type and Attributes The data type, format, and title for WIDTH_BUCKET(x, l, u, y) are as follows. For information on default data type formats, see SQL Data Types and Literals. Syntax element … Specifies the … value_expression value for which a partition number is to be returned. lower_bound lower boundary for the range of values to be partitioned equally. upper_bound upper boundary for the range of values to be partitioned equally. partition_count number of partitions to be created. This value also specifies the width of the partitions by default. The number of partitions created is partition_count + 2. Partition 0 and partition partition_count + 1 account for values of value_expression that are outside the lower and upper boundaries. 1101A492 WIDTH BUCKET ( value_expression, lower_bound, upper_bound, partition_count ) Data Type Format Title INTEGER the default format for INTEGER Width_bucket(x, l, u, y) Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions WIDTH_BUCKET 104 SQL Functions, Operators, Expressions, and Predicates Argument Types and Rules Use the following table for rules concerning WIDTH_BUCKET arguments. If an argument cannot be implicitly converted to an acceptable type, an error is reported. For more information on implicit type conversion, see “Implicit Type Conversions” on page 745. Rules The following rules apply to WIDTH_BUCKET: • If any argument is null, then the result is also null. • If partition_count <=0 or if partition_count > 2147483646, an error is returned to the requestor. Data Type Rules Numeric WIDTH_BUCKET accepts all numeric data types as arguments. The arguments value_expression, lower_bound, and upper_bound are converted to REAL before processing. The partition_count argument is converted to INTEGER before processing. Character WIDTH_BUCKET accepts character strings that represent numeric values, and converts them to the appropriate numeric type. • TIME, TIMESTAMP, or Period • INTERVAL • BYTE or VARBYTE • BLOB or CLOB • CHARACTER or VARCHAR if the server character set is GRAPHIC WIDTH_BUCKET does not accept these types of arguments. UDT The following rules apply to UDT arguments: • The UDT must have an implicit cast to any of the following predefined types: • Numeric • Character • DATE To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. • Implicit type conversion of UDTs for system operators and functions, including WIDTH_BUCKET, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions WIDTH_BUCKET SQL Functions, Operators, Expressions, and Predicates 105 • If lower_bound = upper_bound, an error is returned to the requestor. • If lower_bound < upper_bound, then the rules in the following table apply. • If lower_bound > upper_bound, then the rules in the following table apply. Example You want to create a histogram for the salaries of all employees whose salary amount ranges between $70000 and $200000. The width of each partition, or bucket, within the specified range is to be $32500. The employee salary table contains eight employees: salary first_name last_name -------- ------------ ----------- 50000 William Crawford 150000 Todd Crawford 220000 Bob Stone 199999 Donald Stone 70000 Betty Crawford 70000 James Crawford 70000 Mary Lee 120000 Mary Stone IF … THEN the result is … value_expression < lower_bound 0. value_expression >= upper_bound partition_count +1. If the result cannot be represented by the data type specified for the result, then an error is returned. anything else the greatest exact numeric value with scale 0 that is less than or equal to the following expression. IF … THEN the result is … value_expression > lower_bound 0. value_expression <= upper_bound partition_count +1. If the result cannot be represented by the data type specified for the result, then an error is returned. anything else the least exact numeric value with scale 0 that is less than or equal to the following expression. (partition_count)(value_expression – lower_bound) (upper_bound – lower_bound) ----------------------------------------------------------------------------------------------------------------------------- ? ? ? ? + 1 (partition_count)(lower_bound – value_expression) (lower_bound – upper_bound) ----------------------------------------------------------------------------------------------------------------------------- ? ? ? ? + 1 Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions WIDTH_BUCKET 106 SQL Functions, Operators, Expressions, and Predicates You perform the following SELECT statement. SELECT salary, WIDTH_BUCKET(salary,70000,200000,4),COUNT(salary) FROM emp_salary GROUP BY 1 ORDER BY 1; The report produced by this statement looks like this. salary Width_bucket(salary,70000,200000,4) Count(salary) -------- ------------------------------------ ---------------- 50000 0 1 70000 1 3 120000 2 1 150000 3 1 199999 4 1 220000 5 1 Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions ZEROIFNULL SQL Functions, Operators, Expressions, and Predicates 107 ZEROIFNULL Purpose Converts data from null to 0 to avoid cases where a null result creates an error. Syntax where: ANSI Compliance ZEROIFNULL is a Teradata extension to the ANSI SQL:2008 standard. Result Type and Attributes Here are the default attributes for the result of ZEROIFNULL(arg). For information on data type formats, see SQL Data Types and Literals. Syntax element … Specifies … arg a numeric argument. 1101F226 ZEROIFNULL ( arg ) Data Type Format Title Same data type as arga a. Note that the NULL keyword has a data type of INTEGER. ZEROIFNULL(arg) IF the operand is … THEN the format is the … numeric same format as arg. character default format for FLOAT. UDT format of the predefined type to which the UDT is implicitly cast. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions ZEROIFNULL 108 SQL Functions, Operators, Expressions, and Predicates Argument Types and Rules If the argument is not numeric, it is converted to a numeric value according to implicit type conversion rules. If the argument cannot be converted, an error is reported. For more information on implicit type conversion, see “Implicit Type Conversions” on page 745. If arg is a character string, it is converted to a numeric value of FLOAT data type. If arg is a UDT, the following rules apply: • The UDT must have an implicit cast to any of the following predefined types: • Numeric • Character • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. • Implicit type conversion of UDTs for system operators and functions, including ZEROIFNULL, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. ZEROIFNULL cannot be applied to the following types of arguments: • BYTE or VARBYTE • BLOB or CLOB • CHARACTER or VARCHAR if the server character set is GRAPHIC Example In this example, you can test the Salary column for null. SELECT empno, ZEROIFNULL(salary) FROM employee ; A nonzero value is returned for each employee number, indicating that no nulls exist in the Salary column. IF the value of arg is … THEN ZEROIFNULL returns … not null the value of the numeric argument. null or zeroa a. A structured UDT column value is null only when you explicitly place a NULL value in the column, not when a structured UDT instance has an attribute that is set to NULL. zero. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions ZEROIFNULL SQL Functions, Operators, Expressions, and Predicates 109 Related Topics For additional expressions involving checks for nulls, see: • “COALESCE Expression” on page 42 • “NULLIF Expression” on page 44 • “NULLIFZERO” on page 80 Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions Trigonometric Functions (COS, SIN, TAN, ACOS, ASIN, ATAN, ATAN2) 110 SQL Functions, Operators, Expressions, and Predicates Trigonometric Functions (COS, SIN, TAN, ACOS, ASIN, ATAN, ATAN2) Purpose Performs the trigonometric or inverse trigonometric function of an argument. Syntax where: ANSI Compliance Trigonometric and inverse trigonometric functions are Teradata extensions to the ANSI SQL:2008 standard. Definitions Syntax element … Specifies … arg any valid numeric expression that expresses an angle in radians. x the x-coordinate of a point to use in the arctangent calculation. y the y-coordinate of a point to use in the arctangent calculation. 1101A482 COS ( arg ) SIN TAN ACOS ASIN ATAN ATAN2 ( x, y ) Function Definition Arccosine The arccosine is the angle whose cosine is the argument. Arcsine The arcsine is the angle whose sine is the argument. Arctangent The arctangent is the angle whose tangent is the argument. Cosine The cosine of an angle is the ratio of two sides of a right triangle. The ratio is the length of the side adjacent to the angle divided by the length of the hypotenuse. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions Trigonometric Functions (COS, SIN, TAN, ACOS, ASIN, ATAN, ATAN2) SQL Functions, Operators, Expressions, and Predicates 111 Result Type and Attributes Here are the default data type, format, and title for the result of the trigonometric and inverse trigonometric functions. For information on default data type formats, see SQL Data Types and Literals. Result Value Sine The sine of an angle is the ratio of two sides of a right triangle. The ratio is the length of the side opposite to the angle divided by the length of the hypotenuse. Tangent The tangent of an angle is the ratio of two sides of a right triangle. The ratio is the length of the side opposite to the angle divided by the length of the side adjacent to the angle. Function Definition Data Type Format Title FLOAT Default format for FLOAT Cos(arg) Sin(arg) Tan(arg) ArcCos(arg) ArcSin(arg) ArcTan(arg) Atan2(x,y) Function Result Value COS(arg) The cosine of arg in radians in the range -1 to 1, inclusive. SIN(arg) The sine of arg in radians in the range -1 to 1, inclusive. TAN(arg) The tangent of arg in radians. ACOS(arg) An angle in the range 0 to p radians, inclusive. ASIN(arg) An angle in the range -p/2 to p/2 radians, inclusive. ATAN(arg) An angle in the range -p/2 to p/2 radians, inclusive. ATAN2(x,y) An angle between -p and p radians, excluding -p. A positive result represents a counterclockwise angle from the x-axis. A negative result represents a clockwise angle. ATAN2(x,y) equals ATAN(y/x), except that x can be 0 in ATAN2(x,y) and x cannot be 0 in ATAN(y/x) since this results in a divide by zero error. If both x and y are 0, an error is returned. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions Trigonometric Functions (COS, SIN, TAN, ACOS, ASIN, ATAN, ATAN2) 112 SQL Functions, Operators, Expressions, and Predicates Argument Types and Rules Arguments that are not FLOAT are converted to FLOAT based on implicit type conversion rules. If an argument cannot be converted, an error is reported. For more information on implicit type conversion, see “Implicit Type Conversions” on page 745. If an argument is a UDT, the following rules apply: • The UDT must have an implicit cast to any of the following predefined types: • Numeric • Character • DATE To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. • Implicit type conversion of UDTs for system operators and functions, including trigonometric and inverse trigonometric functions, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. Trigonometric and inverse trigonometric functions cannot take the following types of arguments: • BYTE or VARBYTE • BLOB or CLOB • CHARACTER or VARCHAR if the server character set is GRAPHIC Examples The following are representative function expressions and results. Expression Result COS(5-4) 5.40302305868140E -001 SIN(LOG(0.5)) -2.96504042171437E -001 SIN(RADIANS(180.0)) 1.22464679914735E-016 TAN(ABS(-3)) -1.42546543074278E -001 ACOS(-0.5) 2.09439510239320E 000 ASIN(1) 1.57079632679490E 000 ATAN(1+2) 1.24904577239825E 000 ATAN2(1,1) 7.85398163397448E -001 Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions DEGREES RADIANS SQL Functions, Operators, Expressions, and Predicates 113 DEGREES RADIANS Purpose DEGREES takes a value specified in radians and converts it to degrees. RADIANS takes a value specified in degrees and converts it to radians. Syntax where: ANSI Compliance DEGREES and RADIANS are Teradata extensions to the ANSI SQL:2008 standard. Result Title The following table lists the default titles for DEGREES(arg) and RADIANS(arg). Syntax element … Specifies … arg a numeric expression. IF the function is … THEN arg is interpreted as an angle in … DEGREES radians. RADIANS degrees. 1101A481 DEGREES ( arg ) RADIANS Function Title DEGREES(arg) (5.72957795130823E001*arg) RADIANS(arg) (1.74532925199433E-002*arg) Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions DEGREES RADIANS 114 SQL Functions, Operators, Expressions, and Predicates Result Type and Format The following table lists the result type and format of DEGREES(arg) and RADIANS(arg). For information on data type formats, see SQL Data Types and Literals. Argument Types and Rules If the argument is not numeric, it is converted to a numeric value, based on implicit type conversion rules. If the argument cannot be converted, an error is reported. For more information on implicit type conversion, see “Implicit Type Conversions” on page 745. If arg is a character string, it is converted to a numeric value of the FLOAT data type. If arg is a UDT, the following rules apply: • The UDT must have an implicit cast to any of the following predefined types: • Numeric • Character • DateTime • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. • Implicit type conversion of UDTs for system operators and functions, including DEGREES and RADIANS, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. Neither DEGREES nor RADIANS can be applied to the following types of arguments: • BYTE or VARBYTE • BLOB or CLOB • CHARACTER or VARCHAR if the server character set is GRAPHIC Data Type Format Same data type as arga a. Note that the NULL keyword has a data type of INTEGER. IF the operand is … THEN the format is the default format for … numeric the resulting data type. character FLOAT. a UDT the predefined type to which the UDT is implicitly cast. Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions DEGREES RADIANS SQL Functions, Operators, Expressions, and Predicates 115 Usage Notes DEGREES and RADIANS are useful when working with trigonometric functions such as SIN and COS, which expect arguments to be specified in radians, and inverse trigonometric functions such as ASIN and ACOS, which return values specified in radians. Examples Representative DEGREES and RADIANS function expressions and the results are as follows. Expression Result SIN(RADIANS(60.0)) 8.66025403784439E-001 DEGREES(1.0) 5.72957795130823E 001 Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions Hyperbolic Functions (COSH, SINH, TANH, ACOSH, ASINH, ATANH) 116 SQL Functions, Operators, Expressions, and Predicates Hyperbolic Functions (COSH, SINH, TANH, ACOSH, ASINH, ATANH) Purpose Performs the hyperbolic or inverse hyperbolic function of an argument. Syntax where: ANSI Compliance Hyperbolic and inverse hyperbolic functions are Teradata extensions to the ANSI SQL:2008 standard. Result Type and Attributes Here are the default attributes for the result of hyperbolic and inverse hyperbolic functions. For information on default data type formats, see SQL Data Types and Literals. Syntax element … Specifies … arg any real number. 1101A483 COSH ( arg ) SINH TANH ACOSH ASINH ATANH Data Type Format Title FLOAT Default format for FLOAT Hyperbolic Cos(arg) Hyperbolic Sin(arg) Hyperbolic Tan(arg) Hyperbolic ArcCos(arg) Hyperbolic ArcSin(arg) Hyperbolic ArcTan(arg) Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions Hyperbolic Functions (COSH, SINH, TANH, ACOSH, ASINH, ATANH) SQL Functions, Operators, Expressions, and Predicates 117 Result Value Argument Types and Rules If arg is not FLOAT, it is converted to a FLOAT value, based on implicit type conversion rules. If the argument cannot be converted, an error is reported. For more information on implicit type conversion, see “Implicit Type Conversions” on page 745. If arg is a UDT, the following rules apply: • The UDT must have an implicit cast to any of the following predefined types: • Numeric • Character • DATE To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. • Implicit type conversion of UDTs for system operators and functions, including hyperbolic and inverse hyperbolic functions, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. Hyperbolic and inverse hyperbolic functions cannot be applied to the following types of arguments: • BYTE or VARBYTE • BLOB or CLOB • CHARACTER or VARCHAR if the server character set is GRAPHIC Function Result COSH(arg) Hyperbolic cosine of arg. SINH(arg) Hyperbo lic sine of arg. TANH(arg) Hyperbolic tangent of arg. ACOSH(arg) Inverse hyperbolic cosine of arg. The inverse hyperbolic cosine is the value whose hyperbolic cosine is a number so that: acosh(cosh(arg)) = arg ASINH(arg) Inverse hyperbolic sine of arg. The inverse hyperbolic sine is the value whose hyperbolic sine is a number so that: asinh(sinh(arg)) = arg ATANH(arg) Inverse hyperbolic tangent of arg. The inverse hyperbolic tangent is the value whose hyperbolic tangent is a number so that: atanh(tanh(arg)) = arg Chapter 3: Arithmetic Operators and Functions / Trigonometric and Hyperbolic Functions Hyperbolic Functions (COSH, SINH, TANH, ACOSH, ASINH, ATANH) 118 SQL Functions, Operators, Expressions, and Predicates Examples The following are representative hyperbolic and inverse hyperbolic function expressions and results. Expression Result COSH(EXP(1)) 7.61012513866229E 000 SINH(1) 1.17520119364380E 000 TANH(0) 0.00000000000000E 000 ACOSH(3) 1.76274717403909E 000 ASINH(LOG(0.1)) -8.81373587019543E -001 ATANH(LN(0.5)) -8.53988047997524E -001 SQL Functions, Operators, Expressions, and Predicates 119 CHAPTER 4 Byte/Bit Manipulation Functions This chapter describes the functions that provide support for byte/bit manipulation operations. Prerequisites The byte/bit manipulation functions in this chapter are domain-specific functions; therefore, before you can use these functions, you must run the Database Initialization Program (DIP) utility and execute the DIPALL or DIPUDT script. For details, see “Activating Domainspecific Functions” on page 20. Bit and Byte Numbering Model The following diagrams show the logical bit and byte numbering model employed by the byte/ bit manipulation functions described in this chapter. The model is big endian or little endian independent. Note that the numbering system used for numeric data types is consistent with that used for byte strings. This simplifies the development of appropriate bit masks. Users of the byte/bit manipulation functions should mentally visualize the numeric and byte data types as shown below when contemplating what masks (bit_mask_arg) need to be applied to the target data (target_arg). BYTEINT Example A BYTEINT value of 40 with a binary representation of 00101000: 1101A689 msb MSB lsb : most and least significant bits LSB : Most and Least Significant Bytes Bit 7 . . . Bit 0 : Bit Numbering BYTE 1 : Computer Science binary representation Chapter 4: Byte/Bit Manipulation Functions Bit and Byte Numbering Model 120 SQL Functions, Operators, Expressions, and Predicates SMALLINT Example A SMALLINT value of 10,280 with a binary representation of 0010100000101000: INTEGER Example An INTEGER value of 673,720,360 with a binary representation of 00101000 00101000 00101000 00101000: 1101A690 msb MSB lsb LSB Bit 7 . . . Bit 0 00101000 1101A691 msb MSB lsb : most and least significant bits LSB : Most and Least Significant Bytes Bit 15 . . . Bit 0 : Bit Numbering BYTE 1 BYTE 2 : Computer Science binary representation 1101A692 msb MSB lsb LSB Bit 15 . . . Bit 0 00101000 00101000 1101A693 msb MSB lsb LSB Bit 32 . . . Bit 0 : Bit Numbering BYTE 1 BYTE 2 BYTE 3 BYTE 4 : Computer Science binary representation 1101A694 msb MSB lsb LSB Bit 32 . . . Bit 0 00101000 00101000 00101000 00101000 Chapter 4: Byte/Bit Manipulation Functions Bit and Byte Numbering Model SQL Functions, Operators, Expressions, and Predicates 121 BIGINT Example A BIGINT value of 2,893,606,913,523,066,920 with a binary representation of 00101000 00101000 00101000 00101000 00101000 00101000 00101000 00101000: BYTE and VARBYTE Example 1 A VARBYTE(8) with 8 bytes: Example 2 A VARBYTE(8) with 3 bytes: Example 3 Example of BYTE(4): 1101A695 msb MSB lsb LSB Bit 63 . . . Bit 0 BYTE 1 BYTE 2 BYTE 3 BYTE 4 BYTE 5 BYTE 6 BYTE 7 BYTE 8 1101A696 msb MSB lsb LSB Bit 63 . . . Bit 0 00101000 00101000 00101000 00101000 00101000 00101000 00101000 00101000 1101A697 msb MSB lsb LSB Bit 63 . . . Bit 0 BYTE 1 BYTE 2 BYTE 3 BYTE 4 BYTE 5 BYTE 6 BYTE 7 BYTE 8 1101A698 msb MSB lsb LSB Bit 23 . . . Bit 0 // Bit Numbering BYTE 1 BYTE 2 BYTE 3 Chapter 4: Byte/Bit Manipulation Functions Bit and Byte Numbering Model 122 SQL Functions, Operators, Expressions, and Predicates HEXADECIMAL BYTE LITERALS With respect to byte-bit system functions, hexadecimal byte literals are interpreted as follows: A 2-byte hexadecimal byte literal: '00FF'XB A 4-byte hexadecimal byte literal: '01020304'XB Note that hexadecimal byte literals are represented by an even number of hexadecimal digits. Hexadecimal literals are extended on the right with zeros when required. For example: A 3-byte hexadecimal byte literal, '112233'XB, becomes a 4-byte hexadecimal byte literal: '11223300'XB For more information, see “Hexadecimal Byte Literals” in SQL Data Types and Literals. 1101A699 msb MSB lsb LSB Bit 31 . . . Bit 0 BYTE 1 BYTE 2 BYTE 3 BYTE 4 1101A700 msb MSB lsb LSB Bit 15 . . . Bit 0 00 FF 1101A701 msb MSB lsb LSB Bit 31 . . . Bit 0 01 02 03 04 1101A702 msb MSB lsb LSB Bit 31 . . . Bit 0 11 22 33 00 Chapter 4: Byte/Bit Manipulation Functions Performing Bit-Byte Operations against Arguments with Non-Equal Lengths SQL Functions, Operators, Expressions, and Predicates 123 Performing Bit-Byte Operations against Arguments with Non-Equal Lengths This section applies only to the BITOR, BITXOR, and BITAND functions. If the target_arg and bit_mask_arg arguments passed to these functions differ in length, the arguments are processed as follows: • The target_arg and bit_mask_arg arguments are aligned on their least significant byte/bit. • The smaller argument is padded with zeros to the left until it becomes the same size as the larger argument. Teradata Database pads to the left (instead of to the right) so that the hexadecimal byte literals, serving as bit masks, will not have to be changed every time the size of a byte string is changed. Example The following query performs the BITAND operation on an INTEGER and a single-byte hexadecimal byte literal. SELECT BITAND(287454020, 'FFFF'XB); The INTEGER value 287,454,020 has a hexadecimal value of 0x11223344 and a bit numbering representation of: The hexadecimal byte literal 0xFFFF has a bit numbering representation of: To process the BITAND operation, the two arguments are aligned on their least significant byte/bit as follows: 1101A703 msb MSB lsb LSB Bit 31 . . . Bit 0 11 22 33 44 1101A704 msb MSB lsb LSB Bit 15 . . . Bit 0 FF FF Chapter 4: Byte/Bit Manipulation Functions Performing Bit-Byte Operations against Arguments with Non-Equal Lengths 124 SQL Functions, Operators, Expressions, and Predicates The shorter-length hexadecimal byte literal 0xFFFF is padded with zeros to the left until it is the same length as the INTEGER value 287,454,020. When both operands are the same size, the BITAND operation is performed, producing the following result: 1101A705 MSB LSB Bit 31 . . . Bit 0 11 22 33 44 1101A706 MSB LSB Bit 15 . . . Bit 0 FF FF 1101A707 MSB LSB Bit 31 . . . Bit 0 11 22 33 44 1101A708 MSB LSB Bit 31 . . . Bit 0 00 00 FF FF 1101A709 MSB LSB Bit 31 . . . Bit 0 00 00 33 44 Chapter 4: Byte/Bit Manipulation Functions BITAND SQL Functions, Operators, Expressions, and Predicates 125 BITAND Purpose Performs the logical AND operation on the corresponding bits from the two input arguments. Syntax where: ANSI Compliance BITAND is a Teradata extension to the ANSI SQL:2008 standard. Description This function takes two bit patterns of equal length and performs the logical AND operation on each pair of corresponding bits. If the bits at the same position are both 1, then the result is 1; otherwise, the result is 0. If either input argument is NULL, the function returns NULL. If the target_arg and bit_mask_arg arguments differ in length, the arguments are processed as follows: • The target_arg and bit_mask_arg arguments are aligned on their least significant byte/bit. • The smaller argument is padded with zeros to the left until it becomes the same size as the larger argument. For more information, see “Performing Bit-Byte Operations against Arguments with Non- Equal Lengths” on page 123. Invocation BITAND is a domain-specific function. For information on activating and invoking domainspecific functions, see “Domain-specific Functions” on page 20. Syntax element… Specifies… target_arg a numeric or variable byte expression. bit_mask_arg a fixed byte value, a variable byte value, or a numeric expression. BITAND ( target_arg, bit_mask_arg ) TD_SYSFNLIB. 1101A671 Chapter 4: Byte/Bit Manipulation Functions BITAND 126 SQL Functions, Operators, Expressions, and Predicates Argument Types and Rules BITAND is an overloaded scalar function. The data type of the target_arg parameter can be one of the following: • BYTEINT • SMALLINT • INTEGER • BIGINT • VARBYTE(n) The data type of the bit_mask_arg parameter varies depending upon the data type of the target_arg parameter. The following (target_arg, bit_mask_arg) input combinations are permitted: The maximum supported size (n) for VARBYTE is 8192 bytes. All expressions passed to this function must either match these declared data types or can be converted to these types using the implicit data type conversion rules that apply to UDFs. For example, BITAND(BYTEINT, INTEGER) is allowed because it can be implicitly converted to BITAND(INTEGER,INTEGER). Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If any argument cannot be converted to one of the declared data types by following UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” and “Parameter Types in Overloaded Functions” in SQL External Routine Programming. If any argument cannot be converted to one of the declared data types, an error is returned indicating that no function exists that matches the DML UDF expression submitted. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. target_arg type bit_mask_arg type BYTEINT BYTE(1) BYTEINT BYTEINT SMALLINT BYTE(2) SMALLINT SMALLINT INTEGER BYTE(4) INTEGER INTEGER BIGINT BYTE(8) BIGINT BIGINT VARBYTE(n) VARBYTE(n) Chapter 4: Byte/Bit Manipulation Functions BITAND SQL Functions, Operators, Expressions, and Predicates 127 Result Type and Attributes The result data type depends on the data type of the target_arg input argument that is passed to the function as shown in the following table: The maximum supported size (n) for VARBYTE is 8192 bytes. The default title for BITAND is: BITAND(target_arg, bit_mask_arg). For information on default data type formats, see SQL Data Types and Literals. Example In the following query, the input argument 23 has a data type of BYTEINT and a binary representation of 00010111. The input argument 20 has a data type of BYTEINT and a binary representation of 00010100. The bitwise AND product of the two arguments results in a BYTEINT value of 20, or binary 00010100, which is returned by the query. SELECT BITAND(23,20); IF the data type of target_arg is... THEN the result type is... AND the result format is the default format for... BYTEINT BYTEINT BYTEINT SMALLINT SMALLINT SMALLINT INTEGER INTEGER INTEGER BIGINT BIGINT BIGINT VARBYTE(n) VARBYTE(n) VARBYTE(n) Chapter 4: Byte/Bit Manipulation Functions BITNOT 128 SQL Functions, Operators, Expressions, and Predicates BITNOT Purpose Performs a bitwise complement on the binary representation of the input argument. Syntax where: ANSI Compliance BITNOT is a Teradata extension to the ANSI SQL:2008 standard. Description The bitwise NOT, or complement, is a unary operation which performs logical negation on each bit, forming the ones' complement of the specified binary value. The digits in the argument which were 0 become 1, and vice versa. BITNOT returns NULL if target_arg is NULL. Invocation BITNOT is a domain-specific function. For information on activating and invoking domainspecific functions, see “Domain-specific Functions” on page 20. Argument Types and Rules BITNOT is an overloaded scalar function. It is defined with the following parameter data types: • BYTEINT • SMALLINT • INTEGER • BIGINT • VARBYTE(n) Syntax element… Specifies… target_arg a numeric or variable byte expression. BITNOT ( target_arg ) TD_SYSFNLIB. 1101A669 Chapter 4: Byte/Bit Manipulation Functions BITNOT SQL Functions, Operators, Expressions, and Predicates 129 The maximum supported size (n) for VARBYTE is 8192 bytes. All expressions passed to this function must either match these declared data types or can be converted to these types using the implicit data type conversion rules that apply to UDFs. Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If an argument cannot be converted to one of the declared data types by following UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” and “Parameter Types in Overloaded Functions” in SQL External Routine Programming. If the argument cannot be converted to one of the declared data types, an error is returned indicating that no function exists that matches the DML UDF expression submitted. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result Type and Attributes The result data type depends on the data type of the target_arg input argument that is passed to the function as shown in the following table: The maximum supported size (n) for VARBYTE is 8192 bytes. The default title for BITNOT is: BITNOT(target_arg). For information on default data type formats, see SQL Data Types and Literals. Example In the following query, the input argument 2 has a data type of BYTEINT and a binary representation of 00000010. Performing a BITNOT operation on this value results in a BYTEINT value of -2, or binary 11111101. SELECT BITNOT(2); IF the data type of target_arg is... THEN the result type is... AND the result format is the default format for... BYTEINT BYTEINT BYTEINT SMALLINT SMALLINT SMALLINT INTEGER INTEGER INTEGER BIGINT BIGINT BIGINT VARBYTE(n) VARBYTE(n) VARBYTE(n) Chapter 4: Byte/Bit Manipulation Functions BITOR 130 SQL Functions, Operators, Expressions, and Predicates BITOR Purpose Performs the logical OR operation on the corresponding bits from the two input arguments. Syntax where: ANSI Compliance BITOR is a Teradata extension to the ANSI SQL:2008 standard. Description This function takes two bit patterns of equal length and performs the logical OR operation on each pair of corresponding bits as follows: If the target_arg and bit_mask_arg arguments differ in length, the arguments are processed as follows: • The target_arg and bit_mask_arg arguments are aligned on their least significant byte/bit. • The smaller argument is padded with zeros to the left until it becomes the same size as the larger argument. Syntax element… Specifies… target_arg a numeric or variable byte expression. bit_mask_arg a fixed byte value, a variable byte value, or a numeric expression. BITOR ( target_arg, bit_mask_arg ) TD_SYSFNLIB. 1101A668 IF... THEN the result is... either of the bits from the input arguments is 1 1 both of the bits from the input arguments are 0 0 any of the input arguments is NULL NULL Chapter 4: Byte/Bit Manipulation Functions BITOR SQL Functions, Operators, Expressions, and Predicates 131 For more information, see “Performing Bit-Byte Operations against Arguments with Non- Equal Lengths” on page 123. Invocation BITOR is a domain-specific function. For information on activating and invoking domainspecific functions, see “Domain-specific Functions” on page 20. Argument Types and Rules BITOR is an overloaded scalar function. The data type of the target_arg parameter can be one of the following: • BYTEINT • SMALLINT • INTEGER • BIGINT • VARBYTE(n) The data type of the bit_mask_arg parameter varies depending upon the data type of the target_arg parameter. The following (target_arg, bit_mask_arg) input combinations are permitted: The maximum supported size (n) for VARBYTE is 8192 bytes. All expressions passed to this function must either match these declared data types or can be converted to these types using the implicit data type conversion rules that apply to UDFs. For example, BITOR(BYTEINT, INTEGER) is allowed because it can be implicitly converted to BITOR(INTEGER,INTEGER). Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If any argument cannot be converted to target_arg type bit_mask_arg type BYTEINT BYTE(1) BYTEINT BYTEINT SMALLINT BYTE(2) SMALLINT SMALLINT INTEGER BYTE(4) INTEGER INTEGER BIGINT BYTE(8) BIGINT BIGINT VARBYTE(n) VARBYTE(n) Chapter 4: Byte/Bit Manipulation Functions BITOR 132 SQL Functions, Operators, Expressions, and Predicates one of the declared data types by following UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” and “Parameter Types in Overloaded Functions” in SQL External Routine Programming. If any argument cannot be converted to one of the declared data types, an error is returned indicating that no function exists that matches the DML UDF expression submitted. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result Type and Attributes The result data type depends on the data type of the target_arg input argument that is passed to the function as shown in the following table: The maximum supported size (n) for VARBYTE is 8192 bytes. The default title for BITOR is: BITOR(target_arg, bit_mask_arg). For information on default data type formats, see SQL Data Types and Literals. Example In the following query, the input argument 23 has a data type of BYTEINT and a binary representation of 00010111. The input argument 45 has a data type of BYTEINT and a binary representation of 00101101. The bitwise OR product of the two arguments results in a BYTEINT value of 63, or binary 00111111, which is returned by the query. SELECT BITOR(23,45); IF the data type of target_arg is... THEN the result type is... AND the result format is the default format for... BYTEINT BYTEINT BYTEINT SMALLINT SMALLINT SMALLINT INTEGER INTEGER INTEGER BIGINT BIGINT BIGINT VARBYTE(n) VARBYTE(n) VARBYTE(n) Chapter 4: Byte/Bit Manipulation Functions BITXOR SQL Functions, Operators, Expressions, and Predicates 133 BITXOR Purpose Performs a bitwise XOR operation on the binary representation of the two input arguments. Syntax where: ANSI Compliance BITXOR is a Teradata extension to the ANSI SQL:2008 standard. Description The bitwise exclusive OR takes two bit patterns of equal length and performs the logical XOR operation on each pair of corresponding bits. The result in each position is 1 if the two bits are different, and 0 if they are the same. If either input argument is NULL, the function returns NULL. If the target_arg and bit_mask_arg arguments differ in length, the arguments are processed as follows: • The target_arg and bit_mask_arg arguments are aligned on their least significant byte/bit. • The smaller argument is padded with zeros to the left until it becomes the same size as the larger argument. For more information, see “Performing Bit-Byte Operations against Arguments with Non- Equal Lengths” on page 123. Invocation BITXOR is a domain-specific function. For information on activating and invoking domainspecific functions, see “Domain-specific Functions” on page 20. Syntax element… Specifies… target_arg a numeric or variable byte expression. bit_mask_arg a fixed byte value, a variable byte value, or a numeric expression. BITXOR ( target_arg, bit_mask_arg ) TD_SYSFNLIB. 1101A670 Chapter 4: Byte/Bit Manipulation Functions BITXOR 134 SQL Functions, Operators, Expressions, and Predicates Argument Types and Rules BITXOR is an overloaded scalar function. The data type of the target_arg parameter can be one of the following: • BYTEINT • SMALLINT • INTEGER • BIGINT • VARBYTE(n) The data type of the bit_mask_arg parameter varies depending upon the data type of the target_arg parameter. The following (target_arg, bit_mask_arg) input combinations are permitted: The maximum supported size (n) for VARBYTE is 8192 bytes. All expressions passed to this function must either match these declared data types or can be converted to these types using the implicit data type conversion rules that apply to UDFs. For example, BITXOR(BYTEINT, INTEGER) is allowed because it can be implicitly converted to BITXOR(INTEGER,INTEGER). Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If any argument cannot be converted to one of the declared data types by following UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” and “Parameter Types in Overloaded Functions” in SQL External Routine Programming. If any argument cannot be converted to one of the declared data types, an error is returned indicating that no function exists that matches the DML UDF expression submitted. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. target_arg type bit_mask_arg type BYTEINT BYTE(1) BYTEINT BYTEINT SMALLINT BYTE(2) SMALLINT SMALLINT INTEGER BYTE(4) INTEGER INTEGER BIGINT BYTE(8) BIGINT BIGINT VARBYTE(n) VARBYTE(n) Chapter 4: Byte/Bit Manipulation Functions BITXOR SQL Functions, Operators, Expressions, and Predicates 135 Result Type and Attributes The result data type depends on the data type of the target_arg input argument that is passed to the function as shown in the following table: The maximum supported size (n) for VARBYTE is 8192 bytes. The default title for BITXOR is: BITXOR(target_arg, bit_mask_arg). For information on default data type formats, see SQL Data Types and Literals. Example In the following query, the input argument 12 has a data type of BYTEINT and a binary representation of 00001100. The input argument 45 has a data type of BYTEINT and a binary representation of 00101101. The bitwise XOR product of the two arguments results in a BYTEINT value of 33, or binary 00100001, which is returned by the query. SELECT BITXOR(12,45); IF the data type of target_arg is... THEN the result type is... AND the result format is the default format for... BYTEINT BYTEINT BYTEINT SMALLINT SMALLINT SMALLINT INTEGER INTEGER INTEGER BIGINT BIGINT BIGINT VARBYTE(n) VARBYTE(n) VARBYTE(n) Chapter 4: Byte/Bit Manipulation Functions COUNTSET 136 SQL Functions, Operators, Expressions, and Predicates COUNTSET Purpose Returns the count of the binary bits within the target_arg expression that are either set to 1 or set to 0 depending on the target_value_arg value. Syntax where: ANSI Compliance COUNTSET is a Teradata extension to the ANSI SQL:2008 standard. Description COUNTSET takes the target_arg input expression and counts the number of bits within the expression that are either set to 1 or set to 0, depending on the value of target_value_arg. The target_value_arg parameter only accepts a value of 0 or 1. If a value for target_value_arg is not specified, the default value of 1 is used, and COUNTSET counts the bit values that are set to 1. If any of the input arguments is NULL, the function returns NULL. Invocation COUNTSET is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Argument Types and Rules COUNTSET is an overloaded scalar function. It is defined with the following parameter data types for the following (target_arg[, target_value_arg]) input combinations: Syntax element… Specifies… target_arg a numeric or variable byte expression. target_value_arg an optional integer value. Only a value of 0 or 1 is allowed. If target_value_arg is not specified, the default is 1. COUNTSET ( target_arg ) TD_SYSFNLIB. 1101A676 , target_value_arg Chapter 4: Byte/Bit Manipulation Functions COUNTSET SQL Functions, Operators, Expressions, and Predicates 137 The maximum supported size (n) for VARBYTE is 8192 bytes. All expressions passed to this function must either match these declared data types or can be converted to these types using the implicit data type conversion rules that apply to UDFs. Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If any argument cannot be converted to one of the declared data types by following UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” and “Parameter Types in Overloaded Functions” in SQL External Routine Programming. If any argument cannot be converted to one of the declared data types, an error is returned indicating that no function exists that matches the DML UDF expression submitted. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result Type and Attributes The result data type is INTEGER. The result format is the default format for INTEGER. The default title for COUNTSET is: COUNTSET(target_arg[, target_value_arg]). For information on default data type formats, see SQL Data Types and Literals. Example The following query takes the input argument 23, which has a data type of BYTEINT and a binary representation of 00010111. Since target_value_arg is not specified, the default value of 1 is used. Therefore, the function counts the number of bit values that are set to 1. The query result is an INTEGER value of 4. SELECT COUNTSET(23); target_arg type target_value_arg type (optional) BYTEINT INTEGER SMALLINT INTEGER INTEGER INTEGER BIGINT INTEGER VARBYTE(n) INTEGER Chapter 4: Byte/Bit Manipulation Functions GETBIT 138 SQL Functions, Operators, Expressions, and Predicates GETBIT Purpose Returns the value of the bit specified by target_bit_arg from the target_arg byte expression. Syntax where: ANSI Compliance GETBIT is a Teradata extension to the ANSI SQL:2008 standard. Description GETBIT gets the bit specified by target_bit_arg from the target_arg byte expression and returns either 0 or 1 to indicate the value of that bit. The range of input values for target_bit_arg can vary from 0 (bit 0 is the least significant bit) to the (sizeof(target_arg) - 1). If target_bit_arg is negative or out-of-range (meaning that it exceeds the size of target_arg), an error is returned. If either input argument is NULL, the function returns NULL. Invocation GETBIT is a domain-specific function. For information on activating and invoking domainspecific functions, see “Domain-specific Functions” on page 20. Argument Types and Rules GETBIT is an overloaded scalar function. It is defined with the following parameter data types for the following (target_arg, target_bit_arg) input combinations: Syntax element… Specifies… target_arg a numeric or variable byte expression. target_bit_arg an integer expression. GETBIT ( target_arg, target_bit_arg ) TD_SYSFNLIB. 1101A672 Chapter 4: Byte/Bit Manipulation Functions GETBIT SQL Functions, Operators, Expressions, and Predicates 139 The maximum supported size (n) for VARBYTE is 8192 bytes. All expressions passed to this function must either match these declared data types or can be converted to these types using the implicit data type conversion rules that apply to UDFs. Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If any argument cannot be converted to one of the declared data types by following UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” and “Parameter Types in Overloaded Functions” in SQL External Routine Programming. If any argument cannot be converted to one of the declared data types, an error is returned indicating that no function exists that matches the DML UDF expression submitted. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result Type and Attributes GETBIT returns a BYTEINT value of either 0 or 1, reflecting the value of the bit residing at the target_bit_arg position of the target_arg byte expression. The result format is the default format for BYTEINT. The default title for GETBIT is: GETBIT(target_arg, target_bit_arg). For information on default data type formats, see SQL Data Types and Literals. Example The following query gets the value of the third bit of the input argument 23, which has a data type of BYTEINT and a binary representation of 00010111. The query result is a BYTEINT value of 1 or binary 00000001. SELECT GETBIT(23,2); target_arg type target_bit_arg type BYTEINT INTEGER SMALLINT INTEGER INTEGER INTEGER BIGINT INTEGER VARBYTE(n) INTEGER Chapter 4: Byte/Bit Manipulation Functions ROTATELEFT 140 SQL Functions, Operators, Expressions, and Predicates ROTATELEFT Purpose Returns the expression target_arg rotated by the specified number of bits (num_bits_arg) to the left, with the most significant bits wrapping around to the left. Syntax where: ANSI Compliance ROTATELEFT is a Teradata extension to the ANSI SQL:2008 standard. Description ROTATELEFT functions as follows: Note: When operating against an integer value (BYTEINT, SMALLINT, INTEGER, or BIGINT), rotating a bit into the most significant position will result in the integer becoming negative. This is because all integers in Teradata Database are signed integers. Syntax element… Specifies… target_arg a numeric or variable expression. num_bits_arg an integer expression indicating the number of bit positions to rotate. ROTATELEFT ( target_arg, num_bits_arg ) TD_SYSFNLIB. 1101A664 IF... THEN the function... num_bits_arg is equal to zero returns target_arg unchanged. num_bits_arg is negative rotates the bits to the right instead of the left. target_arg and/or num_bits_arg are NULL returns NULL. num_bits_arg is larger than the size of target_arg rotates (num_bits_arg MOD sizeof(target_arg)) bits. The scope of the rotation operation is bounded by the size of the target_arg expression. Chapter 4: Byte/Bit Manipulation Functions ROTATELEFT SQL Functions, Operators, Expressions, and Predicates 141 Invocation ROTATELEFT is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Argument Types and Rules ROTATELEFT is an overloaded scalar function. It is defined with the following parameter data types for the following (target_arg, num_bits_arg) input combinations: The maximum supported size (n) for VARBYTE is 8192 bytes. All expressions passed to this function must either match these declared data types or can be converted to these types using the implicit data type conversion rules that apply to UDFs. Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If any argument cannot be converted to one of the declared data types by following UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” and “Parameter Types in Overloaded Functions” in SQL External Routine Programming. If any argument cannot be converted to one of the declared data types, an error is returned indicating that no function exists that matches the DML UDF expression submitted. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result Type and Attributes The result data type depends on the data type of the target_arg input argument that is passed to the function as shown in the following table: target_arg type num_bits_arg type BYTEINT INTEGER SMALLINT INTEGER INTEGER INTEGER BIGINT INTEGER VARBYTE(n) INTEGER IF the data type of target_arg is... THEN the result type is... AND the result format is the default format for... BYTEINT BYTEINT BYTEINT SMALLINT SMALLINT SMALLINT INTEGER INTEGER INTEGER Chapter 4: Byte/Bit Manipulation Functions ROTATELEFT 142 SQL Functions, Operators, Expressions, and Predicates The maximum supported size (n) for VARBYTE is 8192 bytes. The default title for ROTATELEFT is: ROTATELEFT(target_arg, num_bits_arg). For information on default data type formats, see SQL Data Types and Literals. Example 1 In the following query, the input argument 16 has a data type of BYTEINT and a binary representation of 00010000. When this value is rotated left by two bits, the result in binary is 01000000. This value translates to a BYTEINT value of 64, which is the result returned by the query. SELECT ROTATELEFT(16,2); Example 2 In the following query, the input argument 64 has a data type of BYTEINT and a binary representation of 01000000. When this value is rotated left by three bits, the result in binary is 00000010. This value translates to a BYTEINT value of 2, which is the result returned by the query. SELECT ROTATELEFT(64,3); BIGINT BIGINT BIGINT VARBYTE(n) VARBYTE(n) VARBYTE(n) IF the data type of target_arg is... THEN the result type is... AND the result format is the default format for... Chapter 4: Byte/Bit Manipulation Functions ROTATERIGHT SQL Functions, Operators, Expressions, and Predicates 143 ROTATERIGHT Purpose Returns the expression target_arg rotated by the specified number of bits (num_bits_arg) to the right, with the least significant bits wrapping around to the left. Syntax where: ANSI Compliance ROTATERIGHT is a Teradata extension to the ANSI SQL:2008 standard. Description ROTATERIGHT functions as follows: Note: When operating against an integer value (BYTEINT, SMALLINT, INTEGER, or BIGINT), rotating a bit into the most significant position will result in the integer becoming negative. This is because all integers in Teradata Database are signed integers. Syntax element… Specifies… target_arg a numeric or variable expression. num_bits_arg an integer expression indicating the number of bit positions to rotate. ROTATERIGHT ( target_arg, num_bits_arg ) TD_SYSFNLIB. 1101A665 IF... THEN the function... num_bits_arg is equal to zero returns target_arg unchanged. num_bits_arg is negative rotates the bits to the left instead of the right. target_arg and/or num_bits_arg are NULL returns NULL. num_bits_arg is larger than the size of target_arg rotates (num_bits_arg MOD sizeof(target_arg)) bits. The scope of the rotation operation is bounded by the size of the target_arg expression. Chapter 4: Byte/Bit Manipulation Functions ROTATERIGHT 144 SQL Functions, Operators, Expressions, and Predicates Invocation ROTATERIGHT is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Argument Types and Rules ROTATERIGHT is an overloaded scalar function. It is defined with the following parameter data types for the following (target_arg, num_bits_arg) input combinations: The maximum supported size (n) for VARBYTE is 8192 bytes. All expressions passed to this function must either match these declared data types or can be converted to these types using the implicit data type conversion rules that apply to UDFs. Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If any argument cannot be converted to one of the declared data types by following UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” and “Parameter Types in Overloaded Functions” in SQL External Routine Programming. If any argument cannot be converted to one of the declared data types, an error is returned indicating that no function exists that matches the DML UDF expression submitted. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result Type and Attributes The result data type depends on the data type of the target_arg input argument that is passed to the function as shown in the following table: target_arg type num_bits_arg type BYTEINT INTEGER SMALLINT INTEGER INTEGER INTEGER BIGINT INTEGER VARBYTE(n) INTEGER IF the data type of target_arg is... THEN the result type is... AND the result format is the default format for... BYTEINT BYTEINT BYTEINT SMALLINT SMALLINT SMALLINT INTEGER INTEGER INTEGER Chapter 4: Byte/Bit Manipulation Functions ROTATERIGHT SQL Functions, Operators, Expressions, and Predicates 145 The maximum supported size (n) for VARBYTE is 8192 bytes. The default title for ROTATERIGHT is: ROTATERIGHT(target_arg, num_bits_arg). For information on default data type formats, see SQL Data Types and Literals. Example 1 In the following query, the input argument 32 has a data type of BYTEINT and a binary representation of 00100000. When this value is rotated right by two bits, the result in binary is 00001000. This value translates to a BYTEINT value of 8, which is the result returned by the query. SELECT ROTATERIGHT(32,2); Example 2 In the following query, the input argument 4 has a data type of BYTEINT and a binary representation of 00000100. When this value is rotated right by four bits, the result in binary is 01000000. This value translates to a BYTEINT value of 64, which is the result returned by the query. SELECT ROTATERIGHT(4,4); BIGINT BIGINT BIGINT VARBYTE(n) VARBYTE(n) VARBYTE(n) IF the data type of target_arg is... THEN the result type is... AND the result format is the default format for... Chapter 4: Byte/Bit Manipulation Functions SETBIT 146 SQL Functions, Operators, Expressions, and Predicates SETBIT Purpose Sets the value of the bit specified by target_bit_arg to the value of target_value_arg in the target_arg byte expression. Syntax where: ANSI Compliance SETBIT is a Teradata extension to the ANSI SQL:2008 standard. Description SETBIT takes the target_arg input and sets the bit specified by target_bit_arg to the value, 0 or 1, as provided by the target_value_arg argument. The target_value_arg parameter only accepts a value of 0 or 1. If a value for target_value_arg is not specified, the default value of 1 is used. The range of input values for target_bit_arg can vary from 0 (bit 0 is the least significant bit) to the (sizeof(target_arg) - 1). If target_bit_arg is negative or out-of-range (meaning that it exceeds the size of target_arg), an error is returned. If any of the input arguments is NULL, the function returns NULL. Invocation SETBIT is a domain-specific function. For information on activating and invoking domainspecific functions, see “Domain-specific Functions” on page 20. Syntax element… Specifies… target_arg a numeric or variable byte expression. target_bit_arg an integer expression. target_value_arg an optional integer value. Only a value of 0 or 1 is allowed. If target_value_arg is not specified, the default is 1. SETBIT ( target_arg, target_bit_arg ) TD_SYSFNLIB. 1101A673 , target_value_arg Chapter 4: Byte/Bit Manipulation Functions SETBIT SQL Functions, Operators, Expressions, and Predicates 147 Argument Types and Rules SETBIT is an overloaded scalar function. It is defined with the following parameter data types for the following (target_arg, target_bit_arg[,target_value_arg]) input combinations: The maximum supported size (n) for VARBYTE is 8192 bytes. All expressions passed to this function must either match these declared data types or can be converted to these types using the implicit data type conversion rules that apply to UDFs. Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If any argument cannot be converted to one of the declared data types by following UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” and “Parameter Types in Overloaded Functions” in SQL External Routine Programming. If any argument cannot be converted to one of the declared data types, an error is returned indicating that no function exists that matches the DML UDF expression submitted. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result Type and Attributes The result data type depends on the data type of the target_arg input argument that is passed to the function as shown in the following table: The maximum supported size (n) for VARBYTE is 8192 bytes. target_arg type target_bit_arg type target_value_arg type (optional) BYTEINT INTEGER INTEGER SMALLINT INTEGER INTEGER INTEGER INTEGER INTEGER BIGINT INTEGER INTEGER VARBYTE(n) INTEGER INTEGER IF the data type of target_arg is... THEN the result type is... AND the result format is the default format for... BYTEINT BYTEINT BYTEINT SMALLINT SMALLINT SMALLINT INTEGER INTEGER INTEGER BIGINT BIGINT BIGINT VARBYTE(n) VARBYTE(n) VARBYTE(n) Chapter 4: Byte/Bit Manipulation Functions SETBIT 148 SQL Functions, Operators, Expressions, and Predicates The default title for SETBIT is: SETBIT(target_arg, target_bit_arg[,target_value_arg]). For information on default data type formats, see SQL Data Types and Literals. Example 1 The following query takes the input argument 23, which has a data type of BYTEINT and a binary representation of 00010111, and sets the value of the third bit to 1. The query result is a BYTEINT value of 23 or binary 00010111. SELECT SETBIT(23,2); Example 2 The following query takes the input argument 23, which has a data type of BYTEINT and a binary representation of 00010111, and sets the value of the third bit to 0. The query result is a BYTEINT value of 19 or binary 00010011. SELECT SETBIT(23,2,0); Chapter 4: Byte/Bit Manipulation Functions SHIFTLEFT SQL Functions, Operators, Expressions, and Predicates 149 SHIFTLEFT Purpose Returns the expression target_arg shifted by the specified number of bits (num_bits_arg) to the left. The bits in the most significant positions are lost, and the bits in the least significant positions are filled with zeros. Syntax where: ANSI Compliance SHIFTLEFT is a Teradata extension to the ANSI SQL:2008 standard. Description SHIFTLEFT functions as follows: Syntax element… Specifies… target_arg a numeric or variable expression. num_bits_arg an integer expression indicating the number of bit positions to shift. SHIFTLEFT ( target_arg, num_bits_arg ) TD_SYSFNLIB. 1101A667 IF... THEN the function... num_bits_arg is equal to zero returns target_arg unchanged. num_bits_arg is negative shifts the bits to the right instead of the left. target_arg and/or num_bits_arg are NULL returns NULL. num_bits_arg is larger than the size of target_arg returns an error. The scope of the shift operation is bounded by the size of the target_arg expression. Specifying a shift that is outside the range of target_arg results in an SQL error. Chapter 4: Byte/Bit Manipulation Functions SHIFTLEFT 150 SQL Functions, Operators, Expressions, and Predicates Note: When operating against an integer value (BYTEINT, SMALLINT, INTEGER, or BIGINT), shifting a bit into the most significant position will result in the integer becoming negative. This is because all integers in Teradata Database are signed integers. Invocation SHIFTLEFT is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Argument Types and Rules SHIFTLEFT is an overloaded scalar function. It is defined with the following parameter data types for the following (target_arg, num_bits_arg) input combinations: The maximum supported size (n) for VARBYTE is 8192 bytes. All expressions passed to this function must either match these declared data types or can be converted to these types using the implicit data type conversion rules that apply to UDFs. Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If any argument cannot be converted to one of the declared data types by following UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” and “Parameter Types in Overloaded Functions” in SQL External Routine Programming. If any argument cannot be converted to one of the declared data types, an error is returned indicating that no function exists that matches the DML UDF expression submitted. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result Type and Attributes The result data type depends on the data type of the target_arg input argument that is passed to the function as shown in the following table: target_arg type num_bits_arg type BYTEINT INTEGER SMALLINT INTEGER INTEGER INTEGER BIGINT INTEGER VARBYTE(n) INTEGER Chapter 4: Byte/Bit Manipulation Functions SHIFTLEFT SQL Functions, Operators, Expressions, and Predicates 151 The maximum supported size (n) for VARBYTE is 8192 bytes. The default title for SHIFTLEFT is: SHIFTLEFT(target_arg, num_bits_arg). For information on default data type formats, see SQL Data Types and Literals. Example In the following query, the input argument 3 has a data type of BYTEINT and a binary representation of 00000011. When this value is shifted left by two bits, the result in binary is 00001100. This value translates to a BYTEINT value of 12, which is the result returned by the query. SELECT SHIFTLEFT(3,2); IF the data type of target_arg is... THEN the result type is... AND the result format is the default format for... BYTEINT BYTEINT BYTEINT SMALLINT SMALLINT SMALLINT INTEGER INTEGER INTEGER BIGINT BIGINT BIGINT VARBYTE(n) VARBYTE(n) VARBYTE(n) Chapter 4: Byte/Bit Manipulation Functions SHIFTRIGHT 152 SQL Functions, Operators, Expressions, and Predicates SHIFTRIGHT Purpose Returns the expression target_arg shifted by the specified number of bits (num_bits_arg) to the right. The bits in the least significant positions are lost, and the bits in the most significant positions are filled with zeros. Syntax where: ANSI Compliance SHIFTRIGHT is a Teradata extension to the ANSI SQL:2008 standard. Description SHIFTRIGHT functions as follows: Syntax element… Specifies… target_arg a numeric or variable expression. num_bits_arg an integer expression indicating the number of bit positions to shift. SHIFTRIGHT ( target_arg, num_bits_arg ) TD_SYSFNLIB. 1101A666 IF... THEN the function... num_bits_arg is equal to zero returns target_arg unchanged. num_bits_arg is negative shifts the bits to the left instead of the right. target_arg and/or num_bits_arg are NULL returns NULL. num_bits_arg is larger than the size of target_arg returns an error. The scope of the shift operation is bounded by the size of the target_arg expression. Specifying a shift that is outside the range of target_arg results in an SQL error. Chapter 4: Byte/Bit Manipulation Functions SHIFTRIGHT SQL Functions, Operators, Expressions, and Predicates 153 Note: When operating against an integer value (BYTEINT, SMALLINT, INTEGER, or BIGINT), shifting a bit out of the most significant position will result in the integer becoming negative. This is because all integers in Teradata Database are signed integers. Invocation SHIFTRIGHT is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Argument Types and Rules SHIFTRIGHT is an overloaded scalar function. It is defined with the following parameter data types for the following (target_arg, num_bits_arg) input combinations: The maximum supported size (n) for VARBYTE is 8192 bytes. All expressions passed to this function must either match these declared data types or can be converted to these types using the implicit data type conversion rules that apply to UDFs. Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If any argument cannot be converted to one of the declared data types by following UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” and “Parameter Types in Overloaded Functions” in SQL External Routine Programming. If any argument cannot be converted to one of the declared data types, an error is returned indicating that no function exists that matches the DML UDF expression submitted. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result Type and Attributes The result data type depends on the data type of the target_arg input argument that is passed to the function as shown in the following table: target_arg type num_bits_arg type BYTEINT INTEGER SMALLINT INTEGER INTEGER INTEGER BIGINT INTEGER VARBYTE(n) INTEGER Chapter 4: Byte/Bit Manipulation Functions SHIFTRIGHT 154 SQL Functions, Operators, Expressions, and Predicates The maximum supported size (n) for VARBYTE is 8192 bytes. The default title for SHIFTRIGHT is: SHIFTRIGHT(target_arg, num_bits_arg). For information on default data type formats, see SQL Data Types and Literals. Example In the following query, the input argument 3 has a data type of BYTEINT and a binary representation of 00000011. When this value is shifted right by two bits, the result in binary is 00000000. This value translates to a BYTEINT value of 0, which is the result returned by the query. SELECT SHIFTRIGHT(3,2); IF the data type of target_arg is... THEN the result type is... AND the result format is the default format for... BYTEINT BYTEINT BYTEINT SMALLINT SMALLINT SMALLINT INTEGER INTEGER INTEGER BIGINT BIGINT BIGINT VARBYTE(n) VARBYTE(n) VARBYTE(n) Chapter 4: Byte/Bit Manipulation Functions SUBBITSTR SQL Functions, Operators, Expressions, and Predicates 155 SUBBITSTR Purpose Extracts a bit substring from the target_arg input expression based on the specified bit position. Syntax where: ANSI Compliance SUBBITSTR is a Teradata extension to the ANSI SQL:2008 standard. Description SUBBITSTR extracts a bit substring from the target_arg string expression starting at the bit position specified by position_arg. See “Bit and Byte Numbering Model” on page 119 for the range of bit positions for each data type. The num_bits_arg value specifies the length of the bit substring to be extracted and indicates the number of bits that the function should return. Note that since the return value of the function is a VARBYTE string, the number of bits returned will be rounded to the byte boundary greater than the number of bits requested. The bits returned will be right-justified, and the excess bits (those exceeding the requested number of bits) will be filled with zeroes. If position_arg is negative or out-of-range (meaning that it exceeds the size of target_arg), an error is returned. If num_bits_arg is negative, or is greater than the number of bits remaining once the starting position_arg is taken into account, an error is returned. Syntax element… Specifies… target_arg a numeric or variable byte expression. position_arg an integer expression indicating the starting position of the bit substring to be extracted. num_bits_arg an integer expression indicating the length of the bit substring to be extracted. This specifies the number of bits for the function to return. SUBBITSTR ( target_arg, position_arg, num_bits_arg ) TD_SYSFNLIB. 1101A674 Chapter 4: Byte/Bit Manipulation Functions SUBBITSTR 156 SQL Functions, Operators, Expressions, and Predicates If any of the input arguments is NULL, the function returns NULL. Invocation SUBBITSTR is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Argument Types and Rules SUBBITSTR is an overloaded scalar function. It is defined with the following parameter data types for the following (target_arg, position_arg, num_bits_arg) input combinations: The maximum supported size (n) for VARBYTE is 8192 bytes. All expressions passed to this function must either match these declared data types or can be converted to these types using the implicit data type conversion rules that apply to UDFs. Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If any argument cannot be converted to one of the declared data types by following UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” and “Parameter Types in Overloaded Functions” in SQL External Routine Programming. If any argument cannot be converted to one of the declared data types, an error is returned indicating that no function exists that matches the DML UDF expression submitted. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result Type and Attributes The result data type is a VARBYTE string. The size (number of bytes) of the VARBYTE string depends on the data type of the target_arg input argument and the number of bits requested. For example: target_arg type position_arg type num_bits_arg type BYTEINT INTEGER INTEGER SMALLINT INTEGER INTEGER INTEGER INTEGER INTEGER BIGINT INTEGER INTEGER VARBYTE(n) INTEGER INTEGER IF the data type of target_arg is... THEN the result type is... AND the result format is the default format for... BYTEINT VARBYTE(1) VARBYTE(1) Chapter 4: Byte/Bit Manipulation Functions SUBBITSTR SQL Functions, Operators, Expressions, and Predicates 157 The maximum supported size (n) for VARBYTE is 8192 bytes. The default title for SUBBITSTR is: SUBBITSTR(target_arg, position_arg, num_bits_arg). For information on default data type formats, see SQL Data Types and Literals. Example The following query takes the input argument 20, which has a data type of BYTEINT and a binary representation of 00010100, and requests that 3 bits be returned starting at the third bit. The 3 bits returned are 101, which are placed into a right-justified zero-filled byte. The result from the query is a value of 5, or binary 00000101, with the result data type being VARBYTE(1). SELECT SUBBITSTR(20,2,3); SMALLINT VARBYTE(2) VARBYTE(2) INTEGER VARBYTE(4) VARBYTE(4) BIGINT VARBYTE(8) VARBYTE(8) VARBYTE(n) VARBYTE(m) where m is the smallest number of bytes to accommodate the requested number of bits. VARBYTE(m) IF the data type of target_arg is... THEN the result type is... AND the result format is the default format for... Chapter 4: Byte/Bit Manipulation Functions TO_BYTE 158 SQL Functions, Operators, Expressions, and Predicates TO_BYTE Purpose Converts a numeric data type to the Teradata Database server byte representation (byte value) of the input value. Syntax where: ANSI Compliance TO_BYTE is a Teradata extension to the ANSI SQL:2008 standard. Description The number of bytes returned by the function varies according to the data type of the target_arg value. For information on the server representation of integral values, see SQL Data Types and Literals. If target_arg is NULL, the function returns NULL. Invocation TO_BYTE is a domain-specific function. For information on activating and invoking domainspecific functions, see “Domain-specific Functions” on page 20. Argument Types and Rules TO_BYTE is an overloaded scalar function. It is defined with the following parameter data types: • BYTEINT • SMALLINT • INTEGER Syntax element… Specifies… target_arg a numeric expression. TO_BYTE ( target_arg ) TD_SYSFNLIB. 1101A675 Chapter 4: Byte/Bit Manipulation Functions TO_BYTE SQL Functions, Operators, Expressions, and Predicates 159 • BIGINT All expressions passed to this function must either match these declared data types or can be converted to these types using the implicit data type conversion rules that apply to UDFs. Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If an argument cannot be converted to one of the declared data types by following UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” and “Parameter Types in Overloaded Functions” in SQL External Routine Programming. If the argument cannot be converted to one of the declared data types, an error is returned indicating that no function exists that matches the DML UDF expression submitted. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result Type and Attributes The result data type is a BYTE value (a fixed byte data type). The size of the byte string returned varies according to the data type of the target_arg input argument as shown in the following table: The default title for TO_BYTE is: TO_BYTE(target_arg). For information on default data type formats, see SQL Data Types and Literals. Example In the following query, the input argument 23 has a data type of BYTEINT and a binary representation of 00010111. Performing a TO_BYTE operation on this value results in the value 00010111 being returned with the data type of BYTE(1). SELECT TO_BYTE(23); IF the data type of target_arg is... THEN the result type is... AND the result format is the default format for... BYTEINT BYTE(1) BYTE(1) SMALLINT BYTE(2) BYTE(2) INTEGER BYTE(4) BYTE(4) BIGINT BYTE(8) BYTE(8) Chapter 4: Byte/Bit Manipulation Functions TO_BYTE 160 SQL Functions, Operators, Expressions, and Predicates SQL Functions, Operators, Expressions, and Predicates 161 CHAPTER 5 Comparison Operators This chapter describes SQL comparison operators. Comparison Operators Purpose Comparison operators test the truth of relations between expressions. Comparison operators are a type of logical predicate and can appear in conditional expressions in: • IF, WHILE, REPEAT, and CASE statements in stored procedures • WHEN clauses in searched CASE expressions • WHERE, ON, and HAVING clauses to qualify or disqualify rows in a SELECT statement • CASE_N functions Syntax where: ANSI Compliance The following comparison operators are ANSI SQL:2008 compliant. Syntax element … Specifies … scalar_expression an expression to be evaluated in comparison with a second scalar_expression. Comparison operators do not support BLOB or CLOB type expressions. You can explicitly cast BLOBs to BYTE or VARBYTE and cast CLOBs to CHARACTER or VARCHAR, and use the result with comparison operators. An expression that results in a UDT data type can only be compared with another expression that results in the same UDT data type. comparison_operator the type of comparison to be evaluated for truth. For a list of the supported comparison operators, see “Supported Comparison Operators” on page 162. FF07D160 scalar_expression comparison_operator scalar_expression Chapter 5: Comparison Operators Comparison Operators 162 SQL Functions, Operators, Expressions, and Predicates The following comparison operators are Teradata extensions to the ANSI SQL:2008 standard. Their use is deprecated. Supported Comparison Operators Teradata Database supports the following comparison operators. Further Information on Predicates • = • > • < • <> • <= • >= • EQ • ^= • NE • NOT= • LT • LE • GT • GE ANSI Operator Teradata Extensions Function = EQ Tests for equality. <> ^= NE NOT= Tests for inequality. < LT Tests for less than. <= LE Tests for less than or equal. > GT Tests for greater than. >= GE Tests for greater than or equal. FOR more information on … SEE … using predicates in conditional expressions in searched CASE expressions Chapter 2: “CASE Expressions.” using predicates in conditional expressions in WHERE, ON, or HAVING clauses in SELECT statements “The SELECT Statement” in SQL Data Manipulation Language. using predicates in conditional expressions in IF, WHILE, or REPEAT statements in stored procedures SQL Stored Procedures and Embedded SQL. Chapter 5: Comparison Operators Comparison Operators in Logical Expressions SQL Functions, Operators, Expressions, and Predicates 163 Comparison Operators in Logical Expressions Syntax A logical expression using comparison operators has the following valid forms. where: other logical predicates, including: Chapter 13: “Logical Predicates.” • [NOT] EXISTS • [NOT] IN • LIKE • IS [NOT] NULL • OVERLAPS • [NOT] BETWEEN … AND … predicate quantifiers: • ALL • ANY • SOME FOR more information on … SEE … Syntax Element … Specifies … operator one of the comparison operators. expression_1 expression_2 an SQL scalar expression. quantifier one of the following quantifier keywords: • ANY • SOME • ALL For information, see “ANY/ALL/SOME Quantifiers” on page 573. 1101D219 expression_1 expression_2 quantifier , operator expression_1 operator quantifier ( constant ) expression_1 operator ( subquery ) quantifier ( expression_1 ) operator ( subquery ) , Chapter 5: Comparison Operators Comparison Operators in Logical Expressions 164 SQL Functions, Operators, Expressions, and Predicates Results A logical expression that uses a comparison operator evaluates to TRUE, FALSE, or UNKNOWN. Using Subqueries in Comparison Operations A subquery is a SELECT statement that returns values used to satisfy the comparison operation. The subquery must be enclosed in parentheses, and it does not end with a semicolon. The subquery must refer to at least one table. A table that is in the WHERE clause, but that is not referred to in any other parts of the subquery, is not applicable. A comparison operation may be used with a subquery whether or not a quantifier is used. If a quantifier is not used, however, then an error condition results if the subquery returns more than one value. If a subquery returns no values, and if a quantifier is not used, then the result of the comparison is false. Therefore, if the following form is used, the subquery must return either no values (in which case the comparison evaluates to false), or it returns one value. expression > (subquery) With the following form, subquery must select the same number of expressions as are specified in the expression list. The two expression lists are equal if each of the respective expressions are equal. constant one or more constant values. A constant may be any of the following: • Defined value • Macro parameter • Built-in value such as TIME, DATE, or USER The comparison operation may compare an expression against a list of explicit constants. The data types of expression and constant must be compatible. If the data types of the operands differ, Teradata Database performs an implicit conversion from one type to another in some cases. For details, see “Implicit Type Conversion of Comparison Operands” on page 168. subquery an SQL SELECT statement. Using a subquery in a condition is restricted in certain cases. Syntax Element … Specifies … 1101B041 ( expression ) comparison_operator ALL (subquery ) ANY SOME , Chapter 5: Comparison Operators Comparisons That Produce TRUE Results SQL Functions, Operators, Expressions, and Predicates 165 If the respective expressions are not equal, then the result of the comparison is determined by comparing the first pair of expressions (from the left) for which the comparison is not true. A subquery in a comparison operation cannot specify a SELECT AND CONSUME statement. Example The following statement uses the ALL quantifier to compare two expressions with the values returned from a subquery to find the employee(s) with the most years of experience in the group of employees having the highest salary: SELECT EmpNo, Name, DeptNo, JobTitle, Salary, YrsExp FROM Employee WHERE (Salary,YrsExp) >= ALL (SELECT Salary,YrsExp FROM Employee) ; Comparisons That Produce TRUE Results Conditions The following table provides the conditions when comparisons produce TRUE results. For simplicity, assume the syntax: expression_1 — operator — expression_2 expression_1 and expression_2 must contain the same number of scalar values and range from 1 through n rows, represented by r, so that the rth components of expression_1 and expression_2 are expression_1r and expression_2r. The dth item in the range is notated as row d such that the dth component of expression_1 is notated as expression_1d and the dth component of expression_2 is notated as expression_2d. The data types of expression_1 and expression_2 must be compatible. If the data types of the expressions differ, Teradata Database performs an implicit conversion from one type to another in some cases. For details, see “Implicit Type Conversion of Comparison Operands” on page 168. For an explanation of the symbols used in this table, see “Predicate Calculus Notation Used In This Book” on page 956. This comparison … Is TRUE iff … expression_1 = expression_2 ? r, expression_1r = expression_2r is TRUE. expression_1 <> expression_2 ? d such that expression_1d <> expression_2d is TRUE. expression_1 < expression_2 ? d such that expression_1d < expression_2d is TRUE and for all r < d, expression_1r = expression_2r is TRUE. expression_1 > expression_2 ? d such that expression_1d >expression_2d is TRUE and for all r > d, expression_1r = expression_2r is TRUE. Chapter 5: Comparison Operators Data Type Evaluation 166 SQL Functions, Operators, Expressions, and Predicates Null Expressions If any expression in a comparison is null, the result of the comparison is unknown. For a comparison to provide a TRUE result when comparing fields that might result in nulls, the statement must include the IS [NOT] NULL operator. Floating Point Expressions Calculations involving floating point values often produce results that are not what you expect. If you perform a floating point calculation and then compare the results against some expected value, it is unlikely that you get the intended result. Instead of comparing the results of a floating point calculation, make sure that the result is greater or less than what is needed, with a given error. Here is an example: SELECT i, SUM(a) as sum_a, SUM(b) as sum_b FROM t1 GROUP BY i HAVING ABS(sum_a - sum_b) > 1E-10; For more information on potential problems associated with floating point values in comparison operations, see SQL Data Types and Literals. Data Type Evaluation Different data types define equality and inequality differently. The following table explains the foundations for how the various data types are compared: expression_1 <= expression_2 expression_1 < expression_2 is TRUE or expression_1 = expression_2 is TRUE. expression_1 => expression_2 expression_1 > expression_2 is TRUE or expression_1 = expression_2 is TRUE. This comparison … Is TRUE iff … This data type … Is evaluated in this way … Numeric Algebraically, with negatives considered to be smaller irrespective of their absolute value. Chapter 5: Comparison Operators Data Type Evaluation SQL Functions, Operators, Expressions, and Predicates 167 Byte Bit-by-bit from left to right. A 0 bit is less than a 1 bit. IF … THEN … every pairwise comparison is equal the two byte strings are equal. any pairwise comparison is not equal that comparison determines the result. two byte strings of different lengths are compared the shorter string is padded to the right with binary zeros to make the lengths equal prior to making the comparison. Character Character-by-character from left to right. Exact comparisons depend on the collation sequence assigned and whether the comparison is case specific or case blind. The available collations are: • ASCII • EBCDIC • MULTINATIONAL • CHARSET_COLL • JIS_COLL IF … THEN … every pairwise comparison is equal the two character strings are equal. any pairwise comparison is not equal that comparison determines the result. For more information on character comparison, see “Character String Comparisons” on page 172. DateTime Chronologically. For information on how Time Zone affects Time comparison, see “Time Zone Sort Order” on page 221. Interval According to sign and magnitude. Period Assuming p1 and p2 are Period value expressions, the evaluation of a Period comparison predicate uses the following logic: IF BEGIN(p1) = BEGIN(p2) is TRUE, return END(p1) operator END(p2) ELSE return (BEGIN(p1) operator BEGIN(p2)) For details on BEGIN and END, see Chapter 9: “Period Functions and Operators.” UDT According to the ordering definition of the UDT. Teradata Database generates ordering functionality for distinct UDTs where the source types are not LOBs. To create an ordering definition for structured UDTs or distinct UDTs where the source types are LOBs, or to replace system-generated ordering functionality, use CREATE ORDERING. For more information on CREATE ORDERING, see SQL Data Definition Language. This data type … Is evaluated in this way … Chapter 5: Comparison Operators Implicit Type Conversion of Comparison Operands 168 SQL Functions, Operators, Expressions, and Predicates Implicit Type Conversion of Comparison Operands Expression operands must be of the same data type before a comparison operation can occur. Data Types on Which Implicit Conversion is Performed If operand data types differ, then Teradata Database performs an implicit conversion according to the following table. Implicit conversions are Teradata extensions to the ANSI SQL:2008 standard. IF one expression operand is … AND the other expression operand is … THEN Teradata Database compares the data as … Character Character Character. For more details, see “Character String Comparisons” on page 172. Character Date Datea. BYTEINT SMALLINT INTEGER FLOAT FLOATa,b. Period Period. CHAR(k) VARCHAR(k) where k <= 16 BIGINT FLOATa,b. Note: Teradata Database returns an error if a comparison involves either of the following combination of operand types: • BIGINT and CHAR(k) or VARCHAR(k) where k > 16. • DECIMAL(m,n) where m > 16 and CHAR(k) or VARCHAR(k) where k > 16. DECIMAL(m,n) CHAR(k) VARCHAR(k) where k > 16 DECIMAL(m,n) where m <= 16 BYTEINT SMALLINT SMALLINT. BYTEINT SMALLINT INTEGER INTEGER. BYTEINT SMALLINT INTEGER BIGINT BIGINT BIGINT. Chapter 5: Comparison Operators Implicit Type Conversion of Comparison Operands SQL Functions, Operators, Expressions, and Predicates 169 BYTEINT DECIMAL(m,n) where m <= 18 and m-n >= 3 DECIMAL(18,n). SMALLINT DECIMAL(m,n) where m <= 18 and m-n >= 5 INTEGER DECIMAL(m,n) DATE where m <= 18 and m-n >= 10 BYTEINT DECIMAL(m,n) where m > 18 or m-n < 3 DECIMAL(38,n). SMALLINT DECIMAL(m,n) where m > 18 or m-n < 5 INTEGER DECIMAL(m,n) DATE where m > 18 or m-n < 10 BIGINT DECIMAL(m,n) DECIMAL(m,n) DECIMAL(k,j) where max(m-n,k-j) + max(j,n) <= 18 DECIMAL(18,max(j,n)). DECIMAL(k,j) where max(m-n,k-j) + max(j,n) > 18 DECIMAL(38,max(j,n)). DATE BYTEINT SMALLINT INTEGER INTEGER. BIGINT BIGINT. FLOAT FLOAT. FLOAT BYTEINT SMALLINT INTEGER BIGINT DECIMAL(m,n) FLOAT. Period Character Period. a. Returns an error for character data with GRAPHIC server character set. b. Comparisons between character and numeric data types require that the character field be convertible to a numeric value. IF one expression operand is … AND the other expression operand is … THEN Teradata Database compares the data as … Chapter 5: Comparison Operators Comparison of ANSI DateTime and Interval in USING Clause 170 SQL Functions, Operators, Expressions, and Predicates Implicit Conversion of DateTime Types In comparisons involving DateTime operands that differ, Teradata Database performs an implicit conversion according to the following table. Data Types on Which Implicit Conversion is Not Performed The following table identifies data types on which Teradata Database does not perform implicit type conversion. Comparison of ANSI DateTime and Interval in USING Clause External values for ANSI DateTime and Interval data are expressed as fixed length character strings in the designated client character set for the session. When you import ANSI DateTime and Interval values with a USING phrase, you must explicitly cast them from the external character format to the proper ANSI DateTime and Interval types for comparison. IF one expression operand is … AND the other expression operand is … THEN Teradata Database compares the data as … TIMESTAMP DATEb b. ANSIDate dateform mode or IntegerDate dateform mode DATE. See “Implicit TIMESTAMP-TIMESTAMP to-DATE Conversion” on page 897. WITH TIME ZONE Intervala a. The INTERVAL type must have only one field, e.g. INTERVAL YEAR. Exact Numeric Numeric. See “Implicit INTERVAL-to-Numeric Conversion” on page 824. Type Rules Byte Byte data types can only be compared with byte data types. Attempts to compare a byte type with another type produces an error. TIME Teradata Database does not perform implicit type conversion from TIME to TIMESTAMP and from TIMESTAMP to TIME in comparison operations. TIMESTAMP UDT Teradata Database does not perform implicit type conversion on UDTs for comparison operations. A UDT value can only be compared with another value of the same UDT type. To compare UDTs with other data types, you must use explicit data type conversion. For more information, see Chapter 20: “Data Type Conversions.” Chapter 5: Comparison Operators Proper Forms of DATE Types in Comparisons SQL Functions, Operators, Expressions, and Predicates 171 For example, consider the following statement, where the data type of the TimeField column is TIME(2): USING (TimeVal CHARACTER(11), NumVal INTEGER) UPDATE TABLE_1 SET TimeField=:TimeVal, NumField=:NumVal WHERE CAST(:TimeVal AS TIME(2)) > TimeField; Although you can use TimeVal CHAR(11) directly for assignment in this USING phrase, you must CAST the column data definition explicitly as TIME(2) in order to compare the field value TimeField in the table because TimeField is an ANSI TIME defined as TIME(2). Proper Forms of DATE Types in Comparisons A DATE operand must be submitted in the proper form in order to achieve a correct comparison. Arithmetic on DATE operands causes an error if a created value is not a valid date. Therefore, although a date value can be submitted in integer form for comparison purposes, a column that contains date data should be defined as data type DATE, not INTEGER. If an integer is used for input to DATE (this is not recommended), the way to enter the first date of the year 2000 is 1000101. For more information, see “Teradata Date and Time Expressions” on page 233. Proper forms for submitting a DATE operand are: • An integer in the form (year-1900)*10000 + month*100 + day. The form YYMMDD is only valid for the years 1900 - 1999. For the years 2000 - 2099, the form is 1YYMMDD. • As a character string in the same form as the date against which the compare is being done or as the date field the assignment is being done. • A character string that is qualified with a data type phrase defining the appropriate data conversion, and a FORMAT phrase defining the format. • As an ANSI date literal, which is always valid for a date comparison with any date format. Examples The following examples use a comparison operator on a value in the Employee.DOB column (defined as DATE FORMAT 'MMMbDDbYYYY') to illustrate correct forms for a DATE operand. Example 1 In the first example, the operand is entered as an integer. SELECT * FROM Employee WHERE DOB = 420327 ; Chapter 5: Comparison Operators Character String Comparisons 172 SQL Functions, Operators, Expressions, and Predicates Example 2 In the second example, the character string is entered in a form that agrees with the format of the DOB column. SELECT * FROM Employee WHERE DOB = 'Mar 27 1942'; Example 3 In the third example, the value is entered as a character string, and so is cast with both a data type phrase (DATE) and a FORMAT phrase. SELECT * FROM Employee WHERE DOB = CAST ('03/27/42' AS DATE FORMAT 'MM/DD/YY'); Example 4 In the fourth example, the value is entered as an ANSI date literal, which works regardless of the date format of the column. SELECT * FROM Employee WHERE DOB = DATE '1942-03-27'; Character String Comparisons Comparison of Character Strings of Unequal Length If character strings of unequal length are being compared, the shorter of the two is padded on the right with pad characters before the comparison occurs. Character Strings and Server Character Sets When comparing character strings, data characters must have the same server character set. If they do not, then the system translates them using the implicit translation rules described in “Implicit Character-to-Character Translation” on page 765. Effect of Collation on Character String Comparisons Collations control character ordering. The results of character comparisons depends on the collation sequence of the character set in use. You can set the default collation to a sequence that is compatible with the character set for your session. Use the HELP SESSION SQL statement to determine the collation setting for your current session. The availability of diacritical or Japanese character sets, and your default collation sequence are under the control of your database administrator. Chapter 5: Comparison Operators Character String Comparisons SQL Functions, Operators, Expressions, and Predicates 173 To ensure that sorting and comparison of character data are identical with the same operations performed by the client, users on a Japanese language site should set collation to CHARSET_COLL. For collation details, see: • “SET SESSION COLLATION” in SQL Data Definition Language • International Character Set Support • “ORDER BY Clause” in SQL Data Manipulation Language Case Sensitivity All character data, except for CLOBs, accessed in the execution of a Teradata SQL statement has an attribute of CASESPECIFIC or NOT CASESPECIFIC, either by default or by explicit designation. Character string comparisons use this attribute to determine whether the comparison is case blind or case specific. Case specificity does not apply to CLOBs. This is not an ANSI SQL:2008 compatible attribute—ANSI does all character comparisons as the equivalent of CASESPECIFIC. The CASESPECIFIC attribute has higher precedence over the NOT CASESPECIFC attribute: The exception is comparisons on GRAPHIC character data, which are always CASESPECIFIC. To apply a case specification attribute to a character string, you can: • Use the default case specification for the session. Default case specification applies to all character data, including literals. • Use the CASESPECIFIC or NOT CASESPECIFIC phrase with a character column in a CREATE TABLE or ALTER TABLE statement. For example: CREATE TABLE Students (StudentID INTEGER ,Firstname CHAR(10) CASESPECIFIC IF … THEN the comparison is … either argument is CASESPECIFIC case specific. both arguments are NOT CASESPECIFIC case blind. IF the session mode is … THEN the default case specification is … ANSI CASESPECIFIC. Teradata NOT CASESPECIFIC. The exception is character data of type GRAPHIC, which is always CASESPECIFIC. Chapter 5: Comparison Operators Character String Comparisons 174 SQL Functions, Operators, Expressions, and Predicates ,Lastname CHAR(20) NOT CASESPECIFIC); Table columns carry the attribute assigned at the time the columns were defined or altered unless a CASESPECIFIC or NOT CASESPECIFIC phrase is used in their access. • Apply the CASESPECIFIC or NOT CASESPECIFIC phrase to a character expression in the comparison. For example, the following statement applies the CASESPECIFIC phrase to a character literal: SELECT * FROM Students WHERE Firstname = 'Ike' (CASESPECIFIC); Use this to override the default case specification for character data, or to override the case specification attribute assigned at the time a character column was defined or altered. For case blind comparisons, any lowercase single byte Latin letters are converted to uppercase before comparison begins. The prepared strings are compared and any trailing pad characters are ignored. A case blind comparison always considers lowercase and uppercase Cyrillic, Greek and fullwidth ASCII letters to be equivalent. To distinguish lowercase and uppercase Cyrillic, Greek, and fullwidth ASCII letters you must explicitly declare CASESPECIFIC comparison. These options work for the KANJISJIS character set as if the data were first converted to the Unicode type and then the options applied. Using UPPER for Case Blind Comparisons Case blind comparisons can be accomplished using the UPPER function, to make sure a character string value contains no lowercase Latin letters. The UPPER function is not the same as declaring a value UPPERCASE. For a description of the UPPER function, see “UPPER” on page 553. Example Consider the following query: SELECT * FROM STUDENTS WHERE Firstname = 'George'; The behavior of the comparison Firstname = 'George' under different case specification attributes and session modes is described in the table that follows. Chapter 5: Comparison Operators Comparison of KANJI1 Characters SQL Functions, Operators, Expressions, and Predicates 175 Comparison of KANJI1 Characters The following sections describe how Teradata Database compares KANJI1 characters. Equality Comparison Comparison of character strings, which can contain mixed single byte and multibyte character data, is handled as follows: • If expression_1 and expression_2 have different server character sets, then they are converted to the same type. For details, see “Implicit Character-to-Character Translation” on page 765. • If expression_1 and expression_2 are of different lengths, the shorter string is padded with enough pad characters to make both the same length. • Session mode is identified: IF column Firstname is … THEN … CASESPECIFIC IF the session mode is … THEN 'George' is … AND the match succeeds for rows with Firstname containing … ANSI CASESPECIFIC 'George' When either character string is CASESPECIFIC, the comparison is case specific. Teradata NOT CASESPECIFIC NOT CASESPECIFIC IF the session mode is … THEN 'George' is … AND the match succeeds for rows with Firstname containing … ANSI CASESPECIFIC 'George' When either character string is CASESPECIFIC, the comparison is case specific. Teradata NOT CASESPECIFIC any combination of cases that spell the name George, such as: • 'george' • 'GEORGE' • 'George' When both character strings are NOT CASESPECIFIC, the comparison is case blind. Chapter 5: Comparison Operators Comparison of KANJI1 Characters 176 SQL Functions, Operators, Expressions, and Predicates To override the default case specification of a character expression, apply the CASESPECIFIC or NOT CASESPECIFIC phrase. • Case specification is determined: • Trailing pad characters are ignored. Nonequality Comparison Nonequality comparisons are handled as follows: 1 If expression_1 and expression_2 are of different lengths, the shorter string is padded with enough pad characters to make both the same length. 2 Session mode is identified. To override the default case specification of a character expression, apply the CASESPECIFIC or NOT CASESPECIFIC phrase. 3 Characters identified as single byte characters under the current character set are converted according to the collation sequence in effect for the session. 4 For the KanjiEUC character set, the ss3 0x8F character is converted to 0xFF. This means that a user-defined KanjiEUC codeset 3 is not properly ordered with respect to other KanjiEUC code sets. In this mode … The default case specification for a character string is … ANSI CASESPECIFIC. Teradata NOT CASESPECIFIC. Unless the CASESPECIFIC phrase is applied to one or both of the expressions, any simple Latin letters in both expression_1 and expression_2 are converted to uppercase before comparison begins. IF … THEN the comparison is … either argument is CASESPECIFIC case specific. both arguments are NOT CASESPECIFIC case blind. In this mode … The default case specification for a character string is … ANSI CASESPECIFIC. Teradata NOT CASESPECIFIC. Unless the CASESPECIFIC qualifier is applied to one or both of the expressions, any simple Latin letters in both expression_1 and expression_2 are converted to uppercase before comparison begins. Chapter 5: Comparison Operators Comparison Operators and the DEFAULT Function in Predicates SQL Functions, Operators, Expressions, and Predicates 177 The ordering of other KanjiEUC codesets is proper; that is, ordering is the same as the binary ordering on the client system. 5 The prepared strings are compared and trailing pad characters are ignored. Nonequality comparisons involve the collation in effect for the session. Five collations are available: • EBCDIC • ASCII • MULTINATIONAL • CHARSET_COLL • JIS_COLL Collation can be set at the user level with the COLLATION option of the CREATE USER or MODIFY USER statements, and at the session level with the [[.]SET] SESSION COLLATION statement or the CLIv2 CHARSET call. If the MULTINATIONAL collation sequence is in effect, the collation sequence of a Japanese language site is determined by the collation setting installed during start-up. For further details on collation sequences, see International Character Set Support. Comparison Operators and the DEFAULT Function in Predicates The DEFAULT function returns the default value of a column. It has two forms: one that specifies a column name and one that omits the column name. Predicates using comparison operators support both forms of the DEFAULT function, but when the DEFAULT function omits the column name, the following conditions must be true: • The comparison can only involve a single column reference. • The DEFAULT function cannot be part of an expression. For example, the following statement uses DEFAULT to compare the values of the Dept_No column with the default value of the Dept_No column. Because the comparison operation involves a single column reference, Teradata Database can derive the column context of the DEFAULT function even though the column name is omitted. SELECT * FROM Employee WHERE Dept_No < DEFAULT; Note that if the DEFAULT function evaluates to null, the predicate is unknown and the WHERE condition is false. For more information on the DEFAULT function, see “DEFAULT” on page 621. Chapter 5: Comparison Operators Comparison Operators and the DEFAULT Function in Predicates 178 SQL Functions, Operators, Expressions, and Predicates SQL Functions, Operators, Expressions, and Predicates 179 CHAPTER 6 Set Operators This chapter describes SQL set operators. Overview of Set Operators The SQL set operators manipulate the results sets of two or more queries by combining the results of each individual query into a single results set. Teradata SQL Set Operators Teradata SQL supports the following set operators: Set operators appear in query expressions. A query expression is a set of queries combined by the set operators INTERSECT, MINUS/EXCEPT, and UNION. Syntax for query_term Syntax for query_factor Set Operator Function INTERSECT Returns result rows that appear in all answer sets generated by the individual SELECT statements. MINUS / EXCEPT Result is those rows returned by the first SELECT except for those also selected by the second SELECT. MINUS is the same as EXCEPT. UNION Combines the results of two or more SELECT statements. FF07D178 (query_expression ) SELECT statement HH01A061 query_term query_factor INTERSECT query_term ALL Chapter 6: Set Operators Overview of Set Operators 180 SQL Functions, Operators, Expressions, and Predicates Syntax for query_expression where: ANSI Compliance INTERSECT, EXCEPT, and UNION are ANSI SQL:2008 compliant. MINUS and the ALL option are Teradata extensions to the ANSI standard. Syntax Element … Specifies … query_term SELECT statement a SELECT statement. For details, see SQL Data Manipulation Language. query_expression an optional expression that might or might not include set operators, other expressions, and an ORDER BY clause. query_factor INTERSECT a set operator returning the result rows appearing in all answer sets. ALL an optional keyword, allowing duplicate rows to be returned. query_expression UNION MINUS/EXCEPT optional set operators specifying how the two or more queries or subqueries are to combine and determine what result rows are required to be returned. ALL an optional keyword, allowing duplicate rows to be returned. ORDER BY the ORDER BY clause to order the result rows returned. For details, see SQL Data Manipulation Language. expression an expression used in the ORDER BY clause to determine the sort order of returned rows in the result. ASC DESC the sort order for the returned result rows. ASC is the default. FF07D179 query_expression query_factor UNION MINUS EXCEPT ALL query_factor (query_expression ) ORDER BY expression , ASC DESC Chapter 6: Set Operators Rules for Set Operators SQL Functions, Operators, Expressions, and Predicates 181 Rules for Set Operators Duplicate Rows By default, duplicate rows are not returned. To permit duplicate rows to be returned, specify the ALL option. For an example, see “Retaining Duplicate Rows Using the ALL Option” on page 183. Operations That Support Set Operators You can use set operators within the following operations: • Simple queries • Derived tables Note: You cannot use the HASH BY or LOCAL ORDER BY clauses in derived tables with set operators. • Subqueries • INSERT … SELECT clauses • View definitions SELECT statements connected by set operators can include all of the normal clause options for SELECT except the WITH clause. SELECT AND CONSUME Statement Set operations do not operate on SELECT AND CONSUME statements. Support for ORDER BY Clause A query expression can include only one ORDER BY specification, at the end. Restrictions on the Data Types Involved in Set Operations The following restrictions apply to CLOB, BLOB, and UDT types involved in set operations: Data Type Restrictions BLOB You cannot use set operators with CLOB or BLOB types. CLOB Chapter 6: Set Operators Precedence of Set Operators 182 SQL Functions, Operators, Expressions, and Predicates Precedence of Set Operators The precedence for processing set operators is as follows: 1 INTERSECT 2 UNION and MINUS/EXCEPT The set operators evaluate from left to right if no parentheses explicitly specify another order. Example For example, consider the following query. SELECT statement_1 UNION SELECT statement_2 EXCEPT SELECT statement_3 INTERSECT SELECT statement_4; The operations are performed in the following order: 1 Intersect the results of statement_3 and statement_4. 2 Union the results of statement_1 and statement_2. 3 Subtract the intersected rows from the union. Using Parentheses to Customize Precedence To override precedence, use parentheses. Operations in parentheses are performed first. For example, consider the following form: ( ( SELECT statement_1 UNION UDT • Multiple UDTs involved in set operations must be identical types because Teradata Database does not perform implicit type conversion on UDTs involved in set operations. A workaround for this restriction is to use CREATE CAST to define casts that cast between the UDTs and then explicitly invoke the CAST function within the set operation. • UDTs involved in set operations must have ordering definitions. Teradata Database generates ordering functionality for distinct UDTs where the source types are not LOBs. To create an ordering definition for structured UDTs or distinct UDTs where the source types are LOBs, or to replace system-generated ordering functionality, use CREATE ORDERING. For more information on CREATE CAST and CREATE ORDERING, see SQL Data Definition Language. Data Type Restrictions Chapter 6: Set Operators Retaining Duplicate Rows Using the ALL Option SQL Functions, Operators, Expressions, and Predicates 183 SELECT statement_2 ) EXCEPT ( SELECT statement_3 UNION SELECT statement_4 ) ) EXCEPT SELECT statement_5 INTERSECT SELECT statement_6; The following list explains the precedence of operators for this example. 1 UNION SELECT statement_1 and SELECT statement_2. 2 UNION SELECT statement_3 and SELECT statement_4. 3 Subtract the result of the second UNION from the result of the first UNION. 4 INTERSECT SELECT statement_5 and SELECT statement_6. 5 Subtract the INTERSECT result from the remainder of the UNION operations. Retaining Duplicate Rows Using the ALL Option Unless you specify the ALL option, duplicate rows are eliminated from the final result. The ALL option retains duplicate rows for the result set to which it is applied. Example The following query returns duplicate rows for each result set, including the final: SELECT statement_1 UNION ALL SELECT statement_2 MINUS ALL SELECT statement_3 INTERSECT ALL SELECT statement_4 Attributes of a Set Result The data type, title, and format clauses contained in the first SELECT statement determine the data type, title, and format information that appear in the final result. Attributes for all other SELECT statements in the query are ignored. Example 1 SELECT level, param, 'GMKSA' (TITLE 'OWNER') FROM gmksa WHERE cycle = '03' UNION Chapter 6: Set Operators Attributes of a Set Result 184 SQL Functions, Operators, Expressions, and Predicates SELECT level, param, 'GMKSA CONTROL' FROM gmksa_control WHERE cycle = '03' ORDER BY 1, 2; The query returns the following results set: ***QUERY COMPLETED. 5 ROWS FOUND. 3 COLUMNS RETURNED. LEVEL PARAM OWNER ----- ----- ----- 00 A GMKSA 00 T GMKSA 85 X GMKSA SF A GMKSA SF T GMKSA The first SELECT specifies GMKSA, which is CHAR(5)—that data type is then forced on the second SELECT. As a result, GMKSA_CONTROL entries are dropped because the first five characters are the same. Because this query does not specify the ALL option, duplicate rows are dropped. Example 2 In the next query, the SELECT order is reversed: SELECT level, param 'GMKSA CONTROL' (TITLE 'OWNER') FROM gmksa_control WHERE cycle = '03' UNION SELECT level, param, 'GMKSA' FROM gmksa WHERE cycle = '03' ORDER BY 1, 2; This query returns the following answer set: ***QUERY COMPLETED.10 ROWS FOUND. 3 COLUMNS RETURNED. LEVEL PARAM OWNER ----- ----- ------------- 00 A GMKSA 00 A GMKSA CONTROL 00 T GMKSA 00 T GMKSA CONTROL 85 X GMKSA 85 X GMKSA CONTROL SF A GMKSA SF A GMKSA CONTROL SF T GMKSA SF T GMKSA CONTROL In this case, because the first SELECT specified ‘GMKSA CONTROL’, the rows were not duplicates and were included in the answer set. Example 3 This example demonstrates how a poorly formed query can cause truncation of the results. SELECT level, param, 'GMKSA ' (TITLE 'OWNER') Chapter 6: Set Operators Set Operators With Derived Tables SQL Functions, Operators, Expressions, and Predicates 185 FROM gmksa WHERE cycle = '03' UNION SELECT level, param,'GMKSA CONTROL' FROM gmksa_control WHERE cycle = '03' ORDER BY 1, 2; This query returns the following answer set: ***QUERY COMPLETED.10 ROWS FOUND. 3 COLUMNS RETURNED. LEVEL PARAM OWNER ----- ----- ------------ 00 A GMKSA 00 A GMKSA CONTRO 00 T GMKSA 00 T GMKSA CONTRO 85 X GMKSA 85 X GMKSA CONTRO SF A GMKSA SF A GMKSA CONTRO SF T GMKSA SF T GMKSA CONTRO This query returned the expected rows; note, however, that because of the way the name was specified in the first SELECT, there was some truncation. Set Operators With Derived Tables Derived tables support set operators, as demonstrated in the following example: Example SELECT x1 FROM table_1, (SELECT x2 FROM table_2 UNION SELECT x3 FROM table_3 ) derived_table; SELECT x1,y1 FROM table_1, (SELECT * FROM table_2) derived_table(column_1, column_2) WHERE column_2 = 1 ; Restrictions You cannot use the HASH BY or LOCAL ORDER BY clauses in derived tables with set operators. The following example returns an error. Chapter 6: Set Operators Set Operators in Subqueries 186 SQL Functions, Operators, Expressions, and Predicates Example The following table function "add2int" takes two integers as input and returns the two integers and their summation. CREATE TABLE t1 (a1 INTEGER, b1 INTEGER); CREATE TABLE t2 (a2 INTEGER, b2 INTEGER); REPLACE FUNCTION add2int (a INTEGER, b INTEGER) RETURNS TABLE (addend1 INTEGER, addend2 INTEGER, mysum INTEGER) SPECIFIC add2int LANGUAGE C NO SQL PARAMETER STYLE SQL NOT DETERMINISTIC CALLED ON NULL INPUT EXTERNAL NAME 'CS!add3int!add2int.c'; /* Query Q1 */ WITH dt(a1, b1) AS ( SELECT a1, b1 FROM t1 UNION ALL SELECT a2, b2 FROM t2 ) SELECT * FROM TABLE (add2int(dt.a1, dt.b1) HASH BY b1 LOCAL ORDER BY b1) tf; Set Operators in Subqueries Set operators are permitted in subqueries. The following examples demonstrate their correct use. Example 1 SELECT x1 FROM table_1 WHERE (x1,y1) IN (SELECT * FROM table_2 UNION SELECT * FROM table_3); Example 2 SELECT * FROM table_1 WHERE table_1.x1 IN Chapter 6: Set Operators Set Operators in Subqueries SQL Functions, Operators, Expressions, and Predicates 187 (SELECT x2 FROM table_2 UNION (SELECT x3 FROM table_3 UNION SELECT x4 FROM table_4)); Example 3 SELECT * FROM table_1 WHERE x1 IN (SELECT SUM(x2) FROM table_2 UNION SELECT x3 FROM table_3); Example 4 SELECT * FROM table_1 WHERE x1 IN (SELECT MAX(x2) FROM table_2 UNION SELECT MIN(x3) FROM table_3); Example 5 SELECT * FROM table_1 WHERE X1 IN (SELECT x2 FROM table_2 UNION SELECT x3 FROM table_3 UNION SELECT x4 FROM table_4); Example 6 SELECT x1 FROM table_1 WHERE x1 IN ANY (SELECT x2 FROM table_2 INTERSECT SELECT x3 FROM table_3 MINUS SELECT x4 FROM table_4); Example 7 UPDATE table_1 SET x1=1 Chapter 6: Set Operators Set Operators in INSERT … SELECT Statements 188 SQL Functions, Operators, Expressions, and Predicates WHERE table_1.x1 IN (SELECT x2 FROM table_2 UNION SELECT x3 FROM table_3 UNION SELECT x4 FROM table_4); Set Operators in INSERT … SELECT Statements Set operators are permitted in INSERT … SELECT statements. The following examples demonstrate their correct use. Example 1 The first example demonstrates a simple INSERT … SELECT using set operators. INSERT table1 (x1,y1) SELECT * FROM table_2 UNION SELECT x3,y3 FROM table_3; Example 2 The second example demonstrates an INSERT … SELECT from a view that uses set operators. REPLACE VIEW v AS SELECT * FROM table_1 UNION SELECT * FROM table_2; INSERT table_3(x3,y3) SELECT * FROM v; Example 3 This example demonstrates an INSERT … SELECT from a derived table with set operators. INSERT table_1 SELECT * FROM (SELECT x2,y2 FROM table_2 UNION SELECT * FROM table_3 DerivedTable ); Chapter 6: Set Operators Set Operators in View Definitions SQL Functions, Operators, Expressions, and Predicates 189 Set Operators in View Definitions Set operators are permitted within view definitions. For example, the following REPLACE VIEW statement uses UNION within a view definition: REPLACE VIEW view_1 AS SELECT x1,y1 FROM table_1 UNION SELECT x2,y2 FROM table_2; Support for the GROUP BY Clause GROUP BY can be used within views with set operators. For details, see “GROUP BY and ORDER BY Clauses” on page 192. Chapter 6: Set Operators Set Operators in View Definitions 190 SQL Functions, Operators, Expressions, and Predicates Restrictions The following limitations apply to view definitions that specify set operators: • UPDATE, DELETE, and INSERT are not applicable. The following example does not work: REPLACE VIEW V AS SELECT X FROM TABLE_1 UNION SELECT Y FROM TABLE_1; UPDATE V SET X=0; An attempt to perform this sequence of statements produces the following error message: ***Failure 3823 VIEW 'v' may not be used for Help Index/ Constraint/Statistics, Update, Delete or Insert. • WITH CHECK OPTION is not applicable. The following example does not work: REPLACE VIEW ERRV( c ) AS SELECT * FROM TABLE_1 UNION SELECT * FROM TABLE_2 WHERE TABLE_2.X=2 WITH CHECK OPTION; An attempt to perform this statement causes the following error message: ***Failure 3847 Illegal use of a WITH clause. • Column level privileges cannot be granted. The following example does not work: GRANT UPDATE ( c ) ON TABLE_VIEW TO USER_NAME; An attempt to perform this statement causes the following error message: ***Failure 3499: GRANT cannot be used on views with set operators. • A view definition that uses set operators cannot specify an ORDER BY clause, but a SELECT statement applied on the view can use ORDER BY. For details, see “GROUP BY and ORDER BY Clauses” on page 192. Examples The following examples provide correct uses of set operators within view definitions. Example 1 REPLACE VIEW v AS SELECT x1 FROM TABLE_1 UNION SELECT x2 FROM TABLE_2 UNION Chapter 6: Set Operators Queries Connected by Set Operators SQL Functions, Operators, Expressions, and Predicates 191 SELECT x3 FROM TABLE_3; SELECT * FROM v; Example 2 REPLACE VIEW view_2 AS SELECT * FROM view_1 UNION SELECT * FROM table_3 UNION SELECT * FROM table_4; SELECT * FROM view_2 ORDER BY 1,2; Example 3 REPLACE VIEW v AS SELECT x1 FROM table_1 WHERE x1 IN (SELECT x2 FROM table_2 UNION SELECT x3 FROM table_3 ); SELECT * FROM v; Queries Connected by Set Operators Certain rules and restrictions apply to SELECT statements connected by set operators that might not apply elsewhere. Number of Expressions in SELECT Statements All SELECT statements must have the same number of expressions. If the first SELECT statement contains three expressions, all succeeding SELECT statements must contain three expressions. You can use a null expression in a SELECT statement as a place holder for a missing expression. In the following example, the second expression is null. SELECT EmpNo, NULL (CHAR(5)) FROM Employee; Chapter 6: Set Operators Queries Connected by Set Operators 192 SQL Functions, Operators, Expressions, and Predicates WITH Clause WITH clauses cannot be used in SELECT statements connected by set operators. GROUP BY and ORDER BY Clauses GROUP BY clauses are allowed in individual SELECT statements of a query expression but apply only to that SELECT statement and not to the result set. ORDER BY clauses are allowed only in the last SELECT statement of a query expression and specify the order of the result set. ORDER BY clauses can contain only numeric literals. For example, to order by the first column in your result set, specify ORDER BY 1. View definitions with set operators can use GROUP BY but cannot use ORDER BY. A SELECT statement applied to a view definition with set operators can use GROUP BY and ORDER BY. The following examples are correct uses of these operations within a view definition: REPLACE VIEW v AS SELECT x1,y1 FROM table1 UNION SELECT x2,y2 FROM table2; SELECT * FROM v ORDER BY 1; SELECT SUM(x1), y1 FROM v GROUP BY 2; You can also apply independent GROUP BY operations to each unioned SELECT. The following example demonstrates how to do this: REPLACE VIEW v(column_1,column_2) AS SELECT MIN(x1),y1 FROM table_1 GROUP BY 2 UNION ALL SELECT MIN(x2),y2 FROM table_2 GROUP BY 2 UNION ALL SELECT x3,y3 FROM table_3; SELECT SUM(v.column_1) (NAMED sum_c1),column_2 GROUP BY 2 ORDER BY 2; SELECT * FROM table_1 Chapter 6: Set Operators Queries Connected by Set Operators SQL Functions, Operators, Expressions, and Predicates 193 WHERE (x1,y1) IN (SELECT SUM(x2), y2 FROM table_2 GROUP BY 2 UNION SELECT SUM(x3), y3 FROM table_3 GROUP BY 2 ); Table Name in SELECT Statements Each SELECT statement must identify the table that the data is to come from even if all SELECT statements reference the same table. Data Type Compatibility Corresponding fields in each SELECT statement must have data types that are compatible. For example, if the first field in the first SELECT statement is a character data type, then the first field in each succeeding SELECT statement must be a character data type. Corresponding numeric types do not have to be the same, but they must be compatible. For example, a field in one SELECT statement can be defined as INTEGER and the corresponding field in another SELECT statement can be defined as SMALLINT. The data types in the first SELECT statement determine the data types of corresponding columns in the result set. The following table provides details about data type compatibility. Data Type Details Character Character types in the first SELECT statement determine the length of character strings in the result set. This can lead to truncation of character strings in the result set if the length of a character type in the first SELECT statement is less than the length of corresponding character types in succeeding SELECT statements. Numeric Numeric types in the first SELECT statement determine the size of numeric types in the result set. All corresponding numeric fields in succeeding SELECT statements are converted to the numeric data type in the first SELECT statement. This can lead to a numeric overflow error if the size of a numeric type in the first SELECT statement is smaller than the size of corresponding numeric types in succeeding SELECT statements and the values returned by the succeeding statements do not fit into the smaller data type. Chapter 6: Set Operators Queries Connected by Set Operators 194 SQL Functions, Operators, Expressions, and Predicates For examples that show how the length of the character type in the first SELECT statement affects the result set, see “Attributes of a Set Result” on page 183. For examples that show how the numeric data type in the first SELECT statement affects the result set, see “Example 6: Effect of the Order of SELECT Statements on Data Type” on page 206. TIME TIMESTAMP PERIOD(TIME) PERIOD(TIMESTAMP) TIME, TIMESTAMP, PERIOD(TIME), and PERIOD(TIMESTAMP) types in the first SELECT statement determine the precision of corresponding columns in the result set. All corresponding fields in succeeding SELECT statements are implicitly converted to the data type in the first SELECT statement. If a corresponding field does not have a time zone and the data type in the first SELECT statement does, the time zone is set to the current session time zone displacement. If the precision of a corresponding field is lower than the precision of the data type in the first SELECT statement, trailing zeros are appended to the fractional digits as needed. If the precision of corresponding fields in succeeding SELECT statements is higher than the precision of the data type in the first SELECT statement, an error is reported. Data Type Details Chapter 6: Set Operators INTERSECT Operator SQL Functions, Operators, Expressions, and Predicates 195 INTERSECT Operator Purpose Returns only the rows that exist in the result of both queries. Syntax where: ANSI Compliance INTERSECT is ANSI SQL:2008 compliant. The ALL option is a Teradata extension to the ANSI standard. Rules for INTERSECT The following rules apply to the use of INTERSECT: • In addition to using INTERSECT within simple queries, you can use INTERSECT within the following operations: • Derived tables Note: You cannot use the HASH BY or LOCAL ORDER BY clauses in derived tables with set operators. • Subqueries • INSERT … SELECT statements • View definitions • Each query connected by INTERSECT is executed to produce a result consisting of a set of rows. The intersection must include the same number of columns from each table in each Syntax element … Specifies … query_expression_1 a complete SELECT statement to be INTERSECTed with query_expression_2. See “Syntax for query_factor” on page 179. ALL that duplicate rows are to be retained for the INTERSECT. query_expression_2 a complete SELECT statement to be INTERSECTed with query_expression_1. See “Syntax for query_term” on page 179. FF07D176 ALL query_expression_1 INTERSECT query_expression_2 Chapter 6: Set Operators INTERSECT Operator 196 SQL Functions, Operators, Expressions, and Predicates SELECT statement (more formally, they must be of the same degree), and the data types of these columns should be compatible. • INTERSECT cannot be used within the following: • SELECT AND CONSUME statements. • WITH RECURSIVE clause • CREATE RECURSIVE VIEW statements Attributes of a Set Result The data type, title, and format clauses contained in the first SELECT statement in the intersection determine the data type, title, and format information that appear in the final result. Attributes for all other SELECT statements in the query are ignored. Data Type of Nulls When you specify an explicit NULL for any intersection operation, its data type is INTEGER. For an example of this principle using the UNION operator, see “Example 5: Effect of Explicit NULLs on Data Type of a UNION” on page 205. On the other hand, column data defined as NULL has neither value nor data type and evaluates like any other null in a scalar expression. Duplicate Row Handling Unless the ALL option is used, duplicate rows are eliminated from the final result. If the ALL option is specified, duplicate rows are retained. The ALL option can be specified for as many INTERSECT operators as are used in a multistatement query. Example Assume that two tables contain the following rows: SPart table SLocation table SuppNo PartNo SuppNo SuppLoc 100 P2 100 London 101 P1 101 London 102 P1 102 Toronto 103 P2 103 Tokyo Chapter 6: Set Operators INTERSECT Operator SQL Functions, Operators, Expressions, and Predicates 197 To then select supplier number (SuppNo) for suppliers located in London (SuppLoc) who supply part number P1 (PartNo), use the following request: SELECT SuppNo FROM SLocation WHERE SuppLoc = 'London' INTERSECT SELECT SuppNo FROM SPart WHERE PartNo = 'P1'; The result of this request is: SuppNo ------ 101 Chapter 6: Set Operators MINUS/EXCEPT Operator 198 SQL Functions, Operators, Expressions, and Predicates MINUS/EXCEPT Operator Purpose Returns the results rows that appear in query_expression_1 and not in query_expression_2. Syntax where: ANSI Compliance EXCEPT is ANSI SQL:2008 compliant. MINUS and the ALL option are Teradata extensions to the ANSI SQL:2008 standard. Usage Notes Besides simple queries, MINUS or EXCEPT can be used within the following operations: • Derived tables Note: You cannot use the HASH BY or LOCAL ORDER BY clauses in derived tables with set operators. • Subqueries • INSERT … SELECT statements • View definitions MINUS and EXCEPT cannot be used within the following operations: • SELECT AND CONSUME statements. • WITH RECURSIVE clause • CREATE RECURSIVE VIEW statements Syntax element … Specifies … query_expression_1 a complete SELECT statement whose results table is to be MINUSed with query_expression_2. ALL that duplicate rows are to be retained for the MINUS operation. query_expression_2 a complete SELECT statement to be MINUSed from query_expression_1. FF07D177 ALL query_expression_1 MINUS query_expression_2 EXCEPT Chapter 6: Set Operators MINUS/EXCEPT Operator SQL Functions, Operators, Expressions, and Predicates 199 Each query connected by MINUS or EXCEPT is executed to produce a result consisting of a set of rows. The exception must include the same number of columns from each table in each SELECT statement (more formally, they must be of the same degree), and the data types of these columns should be compatible. All the result sets are then combined into a single result set, which has the data types of the columns specified in the first SELECT statement in the exception. MINUS/EXCEPT and NULL When you specify an explicit NULL for any exception operation, its data type is INTEGER. For an example of this principle using the UNION operator, see “Example 5: Effect of Explicit NULLs on Data Type of a UNION” on page 205. On the other hand, column data defined as NULL has neither value nor data type and evaluates like any other null in a scalar expression. Duplicate Rows Unless the ALL option is used, duplicate rows are eliminated from the final result. If the ALL option is specified, duplicate rows are retained. The ALL option can be specified for as many MINUS operators as are used in a multistatement query. Chapter 6: Set Operators UNION Operator 200 SQL Functions, Operators, Expressions, and Predicates UNION Operator Purpose Combines two or more SELECT results tables into a single result. Syntax where: ANSI Compliance UNION is ANSI SQL:2008 compliant. Valid UNION Operations Besides simple queries, UNION can be used within the following operations: • Derived tables Note: You cannot use the HASH BY or LOCAL ORDER BY clauses in derived tables with set operators. • Subqueries • INSERT … SELECT statements • Non-recursive CREATE VIEW statements UNION ALL is the only valid set operator in a WITH RECURSIVE clause or CREATE RECURSIVE VIEW statement that defines a recursive query. Unsupported Operations UNION cannot be used within the following: Syntax element … Specifies … query_expression_1 a complete SELECT statement to be unioned with query_expression_2. For details, see “Syntax for query_expression” on page 180. ALL that duplicate rows are to be retained for the UNION. query_expression_2 a complete SELECT statement to be unioned with query_expression_1. For details, see “Syntax for query_factor” on page 179. FF07D175 ALL query_expression_1 UNION query_expression_2 Chapter 6: Set Operators UNION Operator SQL Functions, Operators, Expressions, and Predicates 201 • SELECT AND CONSUME statements. • WITH RECURSIVE clause (unless the ALL option is also specified) • CREATE RECURSIVE VIEW statements (unless the ALL option is also specified) Description of a UNION Operation Each query connected by UNION is performed to produce a result consisting of a set of rows. The union must include the same number of columns from each table in each SELECT statement (more formally, they must be of the same degree), and the data types of these columns should be compatible. All the result sets are then combined into a single result set that has the data type of the columns specified in the first SELECT statement in the union. For an example, see “Example 6: Effect of the Order of SELECT Statements on Data Type” on page 206. UNION and NULL When you specify an explicit NULL for any union operation, its data type is INTEGER. For an example, see “Example 5: Effect of Explicit NULLs on Data Type of a UNION” on page 205. On the other hand, column data defined as NULL has neither value nor data type and evaluates like any other null in a scalar expression. Duplicate Rows Unless the ALL option is used, duplicate rows are eliminated from each result set and from the final result. If the ALL option is used, duplicate rows are retained for the applicable result set. You can specify the ALL option for each UNION operator in the query to retain every occurrence of duplicate rows in the final result. Unexpected Row Length Errors: Sorting Rows for UNION Before performing the sort operation used to check for duplicates in some union operations, Teradata Database creates a sort key and appends it to the rows to be sorted. If the length of this temporary data structure exceeds the system limit of 64K bytes, the operation fails and returns an error to the requestor. Depending on the situation, the message text is one of the following:. • A data row is too long. • Maximum row length exceeded in database_object_name. See Messages for explanations of these messages. Example 1 To select the name, project, and the number of hours spent by employees assigned to project OE1-0001, plus the names of employees not assigned to a project, the following query could be used: Chapter 6: Set Operators UNION Operator 202 SQL Functions, Operators, Expressions, and Predicates SELECT Name, Proj_Id, Hours FROM Employee,Charges WHERE Employee.Empno = Charges.Empno AND Proj_Id IN ('OE1-0001') UNION SELECT Name, NULL (CHAR (8)), NULL (DECIMAL (4,2)) FROM Employee WHERE Empno NOT IN (SELECT Empno FROM Charges); This query returns the following rows: In this example, null expressions are used in columns 2 and 3 of the second SELECT statement. The null expressions are used as place markers so that both SELECT statements in the query contain the same number of expressions. Example 2 To determine the department number and names of all employees in departments 500 and 600, the UNION operator could be used as follows: Name Project Id Hours Aguilar J ? ? Brandle B ? ? Chin M ? Clements D ? ? Kemper R Marston A ? ? Phan A ? ? Regan R ? ? Russell S ? ? Smith T Watson L Inglis C 0E1-0001 30.0 Inglis C 0E1-001 30.5 Leidner P 0E1-001 10.5 Leidner P 0E1-001 23.0 Moffit H 0E1-001 12.0 Moffit H 0E1-001 35.5 Chapter 6: Set Operators UNION Operator SQL Functions, Operators, Expressions, and Predicates 203 SELECT DeptNo, Name FROM Employee WHERE DeptNo = 500 UNION SELECT DeptNo, Name FROM Employee WHERE DeptNo = 600 ; This query returns the following rows: The same results could have been returned with a simpler query, such as the following: SELECT Name, DeptNo FROM Employee WHERE (DeptNo = 500) OR (DeptNo = 600); The advantage to formulating the query using the UNION operator is that if the DeptNo column is the primary index for the Employee table, then using the UNION operator guarantees that the basic selects are prime key operations. There is no guarantee that a query using the OR operation will make use of the primary index. Example 3 In addition, the UNION operator is useful if you must merge lists of values taken from two or more tables. For example, if departments 500 and 600 had their own Employee tables, the following query could be used to select data from two different tables and merge that data into a single list: SELECT Name, DeptNo FROM Employee_dept_500 UNION SELECT Name, DeptNo DeptNo Name 500 Carter J 500 Inglis C 500 Marston A 500 Omura H 500 Reed C 500 Smith T 500 Watson L 600 Aguilar J 600 Kemper R 600 Newman P 600 Regan R Chapter 6: Set Operators UNION Operator 204 SQL Functions, Operators, Expressions, and Predicates FROM Employee_dept_600 ; Example 4 Suppose you want to know the number of man-hours charged by each employee who is working on a project. In addition, suppose you also wanted the result to include the names of employees who are not working on a project. To do this, you would have to perform a union operation as illustrated in the following example. SELECT Name, Proj_Id, Hours FROM Employee, Charges WHERE Employee.EmpNo = Charges.EmpNo UNION SELECT Name, Null (CHAR(8)), Null (DECIMAL(4,2)), FROM Employee WHERE EmpNo NOT IN (SELECT EmpNo FROM Charges ) UNION SELECT Null (VARCHAR(12)), Proj_Id, Hours FROM Charges WHERE EmpNo NOT IN (SELECT EmpNo FROM Employee ); The first portion of the statement joins the Employee table with the Charges table on the EmpNo column. The second portion accounts for the employees who might be listed in the Employee table, but not the Charges table. The third portion of the statement accounts for the employees who might be listed in the Charges table and not in the Employee table. This ensures that all the information asked for is included in the response. UNION Operator and the Outer Join “Example 4” on page 204 does not illustrate an outer join. That operation returns all rows in the joined tables for which there is a match on the join condition and rows from the “left” join table, or the “right” join table, or both tables for which there is no match. Moreover, nonmatching rows are extended with null values. It is possible, however, to achieve an outer join using inner joins and the UNION operator, though the union of any two inner joins is not the equivalent of an outer join. The following example shows how to achieve an outer join using two inner joins and the UNION operator. Notice how the second inner join uses null values. SELECT Offering.CourseNo, Offerings.Location, Enrollment.EmpNo FROM Offerings, Enrollment WHERE Offerings.CourseNo = Enrollment.CourseNo UNION SELECT Offerings.CourseNo, Offerings.Location, NULL FROM Offerings, Enrollment WHERE Offerings.CourseNo <> Enrollment.CourseNo; Chapter 6: Set Operators UNION Operator SQL Functions, Operators, Expressions, and Predicates 205 The above UNION operation returns results equivalent to the results of the left outer join example shown above. Example 5: Effect of Explicit NULLs on Data Type of a UNION Set operator results evaluate to the data type of the columns defined in the first SELECT statement in the operation. When a column in the first SELECT is defined as an explicit NULL, the data type of the result is not intuitive. Consider the following two examples, which you might intuitively think would evaluate to the same result but do not. In the first, an explicit NULL is selected as a column value. SELECT 'p', NULL FROM TableVM UNION SELECT 'q', 145.87 FROM TableVM; BTEQ returns the result as follows. 'p' Null --- ----------- p ? q 145 The expected value for the second row of the Null column probably differs from what you might expect—a decimal value of 145.87. What if the order of the two SELECTs in the union is reversed? SELECT 'q', 145.87 FROM TableVM UNION SELECT 'p', NULL FROM TableVM; BTEQ returns the result as follows. 'q' 145.87 --- ----------- p ? q 145.87 The value for q is now reported as its true data type—DECIMAL—and without truncation. Why the difference? O.CourseNo O.Location E.EmpNo C100 El Segundo 235 C100 El Segundo 668 C200 Dayton ? C400 El Segundo ? Chapter 6: Set Operators UNION Operator 206 SQL Functions, Operators, Expressions, and Predicates In the first union example, the explicit NULL is specified for the second column in the first SELECT statement. The second column in the second SELECT statement, though specified as a DECIMAL number, evaluates to an integer because in this context, NULL, though having no value, does have the data type INTEGER, and that type is retained for the result of the union. The second union example carries the data type for the value 145.87—DECIMAL—through to the result. You can confirm the unconverted data type for NULL and 145.87 by performing the following SELECT statement. SELECT TYPE(NULL), TYPE(145.87) BTEQ returns the result as follows. Type(Null) Type(145.87) ----------------- ---------------------- INTEGER DECIMAL(5,2) Example 6: Effect of the Order of SELECT Statements on Data Type The result of any UNION is always expressed using the data type of the selected value of the first SELECT. This means that SELECT A UNION SELECT B does not always return the same result as SELECT B UNION SELECT A unless you explicitly convert the output data type to ensure the same result in either case. Consider the following complex unioned queries: SELECT MIN(X8.i1) FROM t8 X8 LEFT JOIN t1 X1 ON X8.i1=X1.i1 AND X8.i1 IN (SELECT COUNT(*) FROM t8 X8 LEFT JOIN t1 X1 ON X8.i1=X1.i1 AND X8.i1 = ANY (SELECT COUNT(*) FROM t7 X7 WHERE X7.i1 = ANY (SELECT AVG(X1.i1) FROM t1 X1))) UNION SELECT AVG(X4.i1) FROM t4 X4 WHERE X4.i1 = ANY (SELECT (X8.i1) FROM t1 X1 RIGHT JOIN t8 X8 ON X8.i1=X1.i1 AND X8.i1 = IN (SELECT MAX(X8.i1) FROM t8 X8 LEFT JOIN t1 X1 ON X8.i1=X1.i1 AND (SELECT (X4.i1) FROM t6 X6 RIGHT JOIN t4 X4 ON X6.i1=i1)))); Chapter 6: Set Operators UNION Operator SQL Functions, Operators, Expressions, and Predicates 207 The result is the following report. Minimum(i1) ----------- -2 0 You might intuitively expect that reversing the order of the queries on either side of the UNION would produce the same result. Because the data types of the selected value of the first SELECT can differ, this is not always true, as the following query on the same database demonstrates. SELECT AVG(X4.i1) FROM t4 X4 WHERE X4.i1 = ANY (SELECT (X8.i1) FROM t1 X1 RIGHT JOIN t8 X8 ON X8.i1 = X1.i1 AND X8.i1 = ANY (SELECT MAX(X8.i1) FROM t8 X8 LEFT JOIN t1 X1 ON X8.i1 = X1.i1 AND (SELECT (X4.i1) FROM t6 X6 RIGHT JOIN t4 X4 ON X6.i1 = i ) ) ) UNION SELECT MIN(X8.i1) FROM t8 X8 LEFT JOIN t1 X1 ON X8.i1 = X1.i1 AND X8.i1 IN (SELECT COUNT(*) FROM t8 X8 LEFT JOIN t1 X1 ON X8.i1 = X1.i1 AND X8.i1 = ANY (SELECT COUNT(*) FROM t7 X7 WHERE X7.i1 = ANY (SELECT AVG(X1.i1) FROM t1 X1 ) ); The result is the following report. Average(i1) ----------- -2 1 The actual average is < 0.5. Why the difference when the order of SELECTs in the UNION is reversed? The following table explains the seemingly paradoxical results. Chapter 6: Set Operators UNION Operator 208 SQL Functions, Operators, Expressions, and Predicates WHEN the first SELECT specifies this function … The result data type is … AND the value returned as the result is … AVG REAL 1 MIN INTEGER truncated to 0 SQL Functions, Operators, Expressions, and Predicates 209 CHAPTER 7 DateTime and Interval Functions and Expressions This chapter describes functions and expressions that operate on ANSI DateTime and Interval values, and also describes functions and expressions that operate on Teradata DATE values, which are extensions to the ANSI SQL:2008 standard. Overview ANSI DateTime Data Types ANSI DateTime data types include: • DATE • TIME • TIME WITH TIME ZONE • TIMESTAMP • TIMESTAMP WITH TIME ZONE Interval Data Types There are two categories of ANSI Interval data types: • Year-Month Intervals, which include: • YEAR • YEAR TO MONTH • MONTH • Day-Time Intervals, which include: • DAY • DAY TO HOUR • DAY TO MINUTE • DAY TO SECOND • HOUR • HOUR TO MINUTE • HOUR TO SECOND • MINUTE • MINUTE TO SECOND • SECOND Chapter 7: DateTime and Interval Functions and Expressions ANSI DateTime and Interval Data Type Assignment Rules 210 SQL Functions, Operators, Expressions, and Predicates ANSI DateTime and Interval Data Type Assignment Rules Data Type Compatibility and Conversion The following rules apply to assignments involving ANSI DateTime or Interval data types: IF the source type is … AND the target type is … THEN … DATE DATE the types are compatible and assignments do not require conversion. For compatibility with existing Teradata assignments, non-ANSI operations such as assigning a DATE to an INTEGER or an INTEGER to a DATE (with validity checking) follow existing Teradata assignment rules. TIME TIME the types are compatible and assignments do not require conversion. The Teradata system value TIME is encoded as a REAL and is not compatible with ANSI TIME or TIME WITH TIME ZONE. TIMESTAMP TIMESTAMP the types are compatible and assignments do not require conversion. Year-Month INTERVAL Year-Month INTERVAL Day-Time INTERVAL Day-Time INTERVAL Numeric DATE Teradata Database performs implicit type conversion before the assignment. See “Implicit Type Conversions” on page 745 for details. DATE • Character • Numeric • TIMESTAMP Character • DATE • TIME • TIMESTAMP TIME TIMESTAMP TIMESTAMP • DATE • TIME Intervala a. The INTERVAL type must have only one field, e.g. INTERVAL YEAR. Exact Numeric Exact Numeric Intervala Chapter 7: DateTime and Interval Functions and Expressions ANSI DateTime and Interval Data Type Assignment Rules SQL Functions, Operators, Expressions, and Predicates 211 For all other source/target data type combinations in assignments involving ANSI DateTime or Interval data types, the types must be explicitly converted. To perform explicit conversions on ANSI DateTime or Interval data types, use the CAST function: where: For more information, see “CAST in Explicit Data Type Conversions” on page 752. Interval Data Type Assignment Rules The following rules apply to Year-Month INTERVAL assignments. Syntax element … Specifies … expression an expression with known data type to be cast as a different data type. ansi_sql_data_type the new data type for expression. data_definition_list the new data type or data attributes or both for expression. 1101A627 CAST AS ansi_sql_data_type data_definition_list ( expression ) WHEN … THEN … the types match assignment is straightforward. the source is INTERVAL YEAR and the target is INTERVAL YEAR TO MONTH the value for MONTH in the target is set to zero. the source is INTERVAL MONTH and the target is INTERVAL YEAR TO MONTH the source is extended to include the YEAR field initialized to zero, and the resulting interval is normalized. For example, if the source is '15' then the extended source is '0-15', normalized to '1-03'. the target is INTERVAL MONTH and the source is either INTERVAL YEAR or INTERVAL YEAR TO MONTH the source is converted to INTERVAL MONTH before assignment. For example, if the source is '2-11', it is converted to '35'. the least significant field of the source is lower than that of the target the values of fields in the source with precision lower than the least significant field of the target are truncated. For example, if a source of INTERVAL '32' MONTH is assigned to a target column of type INTERVAL YEAR, the value stored is '2'. Chapter 7: DateTime and Interval Functions and Expressions Scalar Operations on ANSI SQL:2008 DateTime and Interval Values 212 SQL Functions, Operators, Expressions, and Predicates The following rules apply to Day-Time INTERVAL assignments. Scalar Operations on ANSI SQL:2008 DateTime and Interval Values Teradata SQL defines a set of permissible scalar operations for ANSI DateTime and Interval values. Scalar operations include: Data Type Compatibility The Teradata Database convention of performing implicit conversions to resolve expressions of mixed data types is not supported for operations that include ANSI DateTime or Interval values. WHEN … THEN … the types match assignment is straightforward. the target is of lower significance than the least significant field of the source values for those fields are set to zero. For example, if the source is INTERVAL '49:30' HOUR TO MINUTE and it is assigned to a target column of type INTERVAL HOUR(4) TO SECOND(2), the value stored is '49:30:00.00'. the target has fields of higher significance than the most significant field of the source the source type is extended to match the target type, setting the new fields to zeros, and normalizing the content as the final step. For example, if the source is INTERVAL '49:30' HOUR TO MINUTE and it is assigned to a target column of type INTERVAL DAY TO MINUTE, the value stored is '2 1:30'. the least significant field of the source is lower than that of the target the values of fields in the source with precision lower than the least significant field of the target are truncated. For example, if the source is INTERVAL '10:12:58' HOUR TO SECOND and it is assigned to a target column of type INTERVAL HOUR TO MINUTE, the value stored is '10:12'. Operation Description DateTime Expressions Expressions providing a result that is a DateTime value. DateTime expressions have arguments that are also DateTime or Interval expressions. Interval Expressions Expressions providing a result that is an Interval. Interval expressions may include components that are Interval, DateTime, or Numeric expressions. Chapter 7: DateTime and Interval Functions and Expressions ANSI DateTime Expressions SQL Functions, Operators, Expressions, and Predicates 213 To convert ANSI DateTime or Interval expressions, use the CAST function. See “CAST in Explicit Data Type Conversions” on page 752. The following restrictions apply to the values appearing in all DateTime and Interval scalar operations: ANSI DateTime Expressions Purpose Perform a computation on a DATE, TIME, or TIMESTAMP value (or value expression) and return a single value of the same type. Definition A DateTime expression is any expression that returns a result that is a DATE, TIME, or TIMESTAMP value. date_time_expression Syntax IF … THEN … two DateTime values appear in the same DateTime expression both must be DATE types ELSE both must be TIME types ELSE both must be TIMESTAMP types. You cannot mix DATE, TIME, and TIMESTAMP values across type. a DateTime and Interval values appear in the same DateTime expression the Interval value must contain only DateTime fields that are also contained within the DateTime value. two Interval values appear in the same Interval expression both must be Year-Month intervals ELSE both must be Day-Time intervals. You cannot mix Year-Month with Day-Time intervals. FF07D266 interval_expression date_time_term + date_time_term date_time_expression ± interval_term Chapter 7: DateTime and Interval Functions and Expressions ANSI DateTime Expressions 214 SQL Functions, Operators, Expressions, and Predicates date_time_term Syntax where: Syntax element … Specifies … date_time_expression an expression that evaluates to a DATE, TIME, or TIMESTAMP value. The form of the expression is one of the following: • a single date_time_term. • the sum of an interval_expression and a date_time_term expression. • the sum or difference of a date_time_expression and an interval_term. date_time_term a single date_time_primary or a date_time_primary with a time zone specifier of AT LOCAL, AT [TIME ZONE] expression, or AT [TIME ZONE] time_zone_string. interval_expression one of the following: • a single interval_term. • an interval_term added to or subtracted from an interval_expression. • the difference between a date_time_expression and a date_time_term (enclosed by parentheses) preceding a start TO end phrase. For more information on interval_expression and interval_term, see “ANSI Interval Expressions” on page 222. date_time_primary one of the following elements, any of which must have the appropriate DateTime type: • Column reference • DateTime literal value For details on DateTime literals, see SQL Data Types and Literals. • DateTime function reference For example, the result of a CASE expression or CAST function or DateTime built-in function such as CURRENT_DATE or CURRENT_TIME. • Scalar function reference • Aggregate function reference • (table_expression) A scalar subquery. • (date_time_timestamp_expression) 1101A677 date_time_primary expression time_zone_string AT LOCAL TIME ZONE Chapter 7: DateTime and Interval Functions and Expressions ANSI DateTime Expressions SQL Functions, Operators, Expressions, and Predicates 215 AT LOCAL and AT TIME ZONE Time Zone Specifiers A date_time_primary can include an AT LOCAL or AT [TIME ZONE] clause only if the date_time_primary evaluates to a TIME or TIMESTAMP value or is the built-in function CURRENT_DATE or DATE. The effect is to adjust date_time_term to be in accordance with the specified time zone displacement. The expression that specifies the time zone displacement in an AT [TIME ZONE] clause is implicitly converted, as needed and if allowed, to a time zone displacement or time zone string depending on its data type as defined in the following table: AT LOCAL that the default time zone displacement based on the current session time zone is used. The current session time zone may be specified as a time zone string or a time zone displacement expressed as an Interval data type that defines the local time zone offset. AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement. Syntax element … Specifies … Data type of expression Implicit Conversion INTERVAL HOUR(n) TO MINUTE where n is not 2 CAST(expression AS INTERVAL HOUR(2) TO MINUTE) INTERVAL HOUR INTERVAL DAY INTERVAL DAY TO HOUR INTERVAL DAY TO MINUTE INTERVAL DAY TO SECOND INTERVAL HOUR INTERVAL HOUR TO SECOND INTERVAL MINUTE INTERVAL MINUTE TO SECOND INTERVAL SECOND CAST(expression AS INTERVAL HOUR(2) TO MINUTE) BYTEINT SMALLINT INTEGER BIGINT DECIMAL/NUMERIC if the fractional precision is 0 CAST(CAST(expression AS INTERVAL HOUR(2)) AS INTERVAL HOUR(2) TO MINUTE) DECIMAL/NUMERIC if the fractional precision is greater than 0 CAST(CAST((expression)*60 AS INTERVAL MINUTE(4)) AS INTERVAL HOUR(2) TO MINUTE) Chapter 7: DateTime and Interval Functions and Expressions ANSI DateTime Expressions 216 SQL Functions, Operators, Expressions, and Predicates Note: There is a general restriction that in Numeric-to-Interval conversions, the INTERVAL type must have only one DateTime field. However, this restriction is not an issue when implicitly converting the expression of an AT clause because the conversion is done with two CAST statements. If the conversion to INTERVAL HOUR(2) TO MINUTE results in a value that is not between INTERVAL -'12:59' HOUR TO MINUTE and INTERVAL '14:00' HOUR TO MINUTE, an error is returned. You can specify two kinds of time zone strings in the AT [TIME ZONE] time_zone_string clause: • Time zone strings that do not follow separate daylight saving time (DST) and standard time zone displacements from Coordinated Universal Time (UTC) time. • Time zone strings that follow different DST and standard time zone displacements from UTC time. The following time zone strings are supported: Character with CHARACTER SET UNICODE CAST(CAST(expression AS INTERVAL HOUR(2)) AS INTERVAL HOUR(2) TO MINUTE) If an error occurs for the above CAST statement, Teradata Database attempts the following: CAST(expression AS INTERVAL HOUR(2) TO MINUTE) If an error occurs for this CAST statement also, Teradata Database treats the character value as a time zone string. Character that is not CHARACTER SET UNICODE TRANSLATE(expression USING source_repertoire_name_TO_Unicode) where source_repertoire_name is the server character set of expression. The translated value is then processed as above for a character value with CHARACTER SET UNICODE. other An error is returned. Data type of expression Implicit Conversion Chapter 7: DateTime and Interval Functions and Expressions ANSI DateTime Expressions SQL Functions, Operators, Expressions, and Predicates 217 Teradata Database resolves the time zone string and calculates the time zone displacement for the session or requested query. Note: Teradata Database will automatically adjust the time zone displacement to account for the start or end of daylight saving time only if you specify a time zone using a time zone string that follows different DST and standard time zone displacements. GMT format strings represent time zone strings that follow only one standard time and does not have a separate Strings that do not follow separate DST and standard time zone displacements • 'GMT' • 'GMT+1' • 'GMT+10' • 'GMT+11' • 'GMT+11:30' • 'GMT+12' • 'GMT+13' • 'GMT+14' • 'GMT+2' • 'GMT+3' • 'GMT+3:30' • 'GMT+4' • 'GMT+4:30' • 'GMT+5' • 'GMT+5:30' • 'GMT+5:45' • 'GMT+6' • 'GMT+6:30' • 'GMT+7' • 'GMT+8' • 'GMT+8:45' • 'GMT+9' • 'GMT+9:30' • 'GMT-1' • 'GMT-10' • 'GMT-11' • 'GMT-2' • 'GMT-3' • 'GMT-4' • 'GMT-5' • 'GMT-6' • 'GMT-6:30' • 'GMT-7' • 'GMT-8' Strings that follow different DST and standard time zone displacements • 'Africa Egypt' • 'Africa Morocco' • 'Africa Namibia' • 'America Alaska' • 'America Aleutian' • 'America Argentina' • 'America Atlantic' • 'America Brazil' • 'America Central' • 'America Chile' • 'America Cuba' • 'America Eastern' • 'America Mountain' • 'America Newfoundland' • 'America Pacific' • 'America Paraguay' • 'America Uruguay' • 'Asia Gaza' • 'Asia Iran' • 'Asia Iraq' • 'Asia Irkutsk' • 'Asia Israel' • 'Asia Jordan' • 'Asia Kamchatka' • 'Asia Krasnoyarsk' • 'Asia Lebanon' • 'Asia Magadan' • 'Asia Omsk' • 'Asia Syria' • 'Asia Vladivostok' • 'Asia West Bank' • 'Asia Yakutsk' • 'Asia Yekaterinburg' • 'Australia Central' • 'Australia Eastern' • 'Australia Western' • 'Europe Central' • 'Europe Eastern' • 'Europe Kaliningrad' • 'Europe Moscow' • 'Europe Samara' • 'Europe Western' • 'Indian Mauritius' • 'Mexico Central' • 'Mexico Northwest' • 'Mexico Pacific' • 'Pacific New Zealand' • 'Pacific Samoa' Chapter 7: DateTime and Interval Functions and Expressions ANSI DateTime Expressions 218 SQL Functions, Operators, Expressions, and Predicates daylight saving time. For example, the time zone string 'GMT+5:30' can be used for India in order to use the displacement interval 5:30, which is applicable all year around. Teradata Database resolves the time zone string based on the rules and time zone displacement information stored in the system UDF (user-defined function), GetTimeZoneDisplacement. If the time zone strings provided by Teradata do not meet your requirements, you may add new time zone strings or modify the existing time zone strings by modifying or adding new rules to the GetTimeZoneDisplacement UDF. For details, see “GetTimeZoneDisplacement” on page 246. You can also use the AT clause to explicitly specify a time zone in the following cases: • With the following built-in functions: • “CURRENT_DATE” on page 671. • “CURRENT_TIME” on page 677. • “CURRENT_TIMESTAMP” on page 681. • “DATE” on page 687. • “TIME” on page 699. Note: If you specify these built-in functions with an AT LOCAL clause, the value returned depends on the setting of the DBS Control flag TimeDateWZControl. • When converting DateTime data types using the CAST function or Teradata conversion syntax. You can specify the time zone used for the CAST or conversion as the source time zone, a specific time zone displacement or time zone string, or the current session time zone. For more information, see Chapter 20: “Data Type Conversions.” • With the EXTRACT function to specify a time zone for the source expression before extracting the fields. For more information about time zones, see “DateTime and Interval Data Types” in SQL Data Types and Literals. Related Topics Gregorian Calendar Rules DateTime expressions always operate within the rules of the Gregorian calendar. For more information on… See… Setting session time zones SET TIME ZONE, CREATE USER, MODIFY USER in SQL Data Definition Language. System time zone settings "System TimeZone Hour" and "System TimeZone Minute" in Utilities. Automatic adjustment of the system time to account for daylight saving time "SDF file" and "Locale Definition Utility (tdlocaledef)" in Utilities. Chapter 7: DateTime and Interval Functions and Expressions ANSI DateTime Expressions SQL Functions, Operators, Expressions, and Predicates 219 When an evaluation results in a value outside the permissible range for any contained field or results in a value impermissible according to the natural rules for DATE and TIME values, then an error is returned. For example, the following operation returns an error because it evaluates to a date that is not valid (‘1996-09-31’). SELECT DATE '1996-08-31' + INTERVAL '1' MONTH; The desired result is obtained with a slight rephrasing of the second operand. SELECT DATE '1996-08-31' + INTERVAL '30' DAY; This operation returns the desired result, ‘1996-09-30’. No error is returned. Evaluation Types Expressions involving DateTime values evaluate to a DateTime type, with DATE being the least significant type and TIMESTAMP the most significant. Adding and Subtracting Interval Values DateTime expressions formed by adding an Interval to a DateTime value or by subtracting an Interval from a DateTime value are performed by adding or subtracting values of the appropriate component fields and carrying overflow from lower precision fields with the appropriate modulo to represent proper arithmetic in terms of the calendar and clock. An interval_expression or interval_term may only contain DateTime fields that are contained in the corresponding date_time_expression or date_time_term. When an Interval value is added to or subtracted from a TIME or TIMESTAMP value, the time zone displacement value associated with the result is identical to that associated with the TIME or TIMESTAMP value. Computations With Time Zones If you perform arithmetic on DateTime expressions containing time zones, the results are computed in the following way. Call the DateTime value of the expression DV and the time zone value component (normalized to UTC) TZ. The result is computed as DV - TZ. DateTime expressions involving … Evaluate to a … Dates date. Times time. Timestamps timestamp. Chapter 7: DateTime and Interval Functions and Expressions ANSI DateTime Expressions 220 SQL Functions, Operators, Expressions, and Predicates Example 1: date_time_primary In this example, the date_time_primary is a built-in time function. CURRENT_TIME Example 2: date_time_term With an Interval Column Time Zone Specifier In this example, the date_time_term is a date_time_primary column value named f1. TS.f1 is a value of type TIME or TIMESTAMP and intrvl.a is a column interval value of type INTERVAL HOUR(2) TO MINUTE. SELECT f1 AT TIME ZONE intrvl.a FROM TS; Example 3: date_time_term With an Interval Literal Time Zone Specifier In this example, the date_time_term is a date_time_primary column value named f1. The specified interval is an interval literal value of type INTERVAL HOUR TO MINUTE. SELECT f1 AT TIME ZONE INTERVAL '01:00' HOUR TO MINUTE FROM TS; Example 4: date_time_term With a Time Zone String Time Zone Specifier In this example, the date_time_term is a date_time_primary column value named f1. TS.f1 is a value of type TIME or TIMESTAMP and the time zone displacement is based on the time zone string 'America Pacific'. SELECT f1 AT TIME ZONE 'America Pacific' FROM TS; Example 5: date_time_expression In this example, the date_time_expression is an interval_expression added to a date_time_term. Note that you can only add these terms—subtraction of a date_time_term from an interval_expression is not permitted. SELECT INTERVAL '20' YEAR + CURRENT_DATE; Example 6: date_time_expression With Addition In this example, the date_time_expression is comprised of another date_time_expression added to an interval_term. The columns subscribe_date and subscription_interval are typed DATE and INTERVAL MONTH(4), respectively. SUBSCRIBE_DATE + SUBSCRIPTION_INTERVAL Example 7: date_time_expression With Subtraction You can also subtract an interval_term from a date_time_expression. Chapter 7: DateTime and Interval Functions and Expressions ANSI DateTime Expressions SQL Functions, Operators, Expressions, and Predicates 221 In this example, an interval_term is subtracted from the date_time_expression. The columns expiration_date and subscription_interval are typed DATE and INTERVAL MONTH(4), respectively. EXPIRATION_DATE - SUBSCRIPTION_INTERVAL Time Zone Sort Order Time zones are ordered chronologically, using the same time zone. Examples Consider the following examples using ordered SELECT statements on a table having a column with type TIMESTAMP(0) WITH TIME ZONE. The identical ordering demonstrated in these ORDER BY SELECTs applies to all time zone comparison operations. SELECT f1 TIMESTAMPFIELD FROM timestwz ORDER BY f1; This statement returns the following results table. TIMESTAMPFIELD ------------------------- 1997-10-07 15:43:00+08:00 1997-10-07 15:43:00-00:00 1997-10-07 15:47:52-08:00 Note how the values are displayed with the stored time zone information, but that the ordering is not immediately evident. Now note how normalizing the time zones by means of a CAST function indicates chronological ordering explicitly. SELECT CAST(f1 AS TIMESTAMP(0)) TIMESTAMP_NORMALIZED FROM timestwz ORDER BY f1; This statement returns the following results table. TIMESTAMP_NORMALIZED ------------------- 1997-10-06 23:43:00 1997-10-07 07:43:00 1997-10-07 15:45:52 While the ordering is the same as for the previous query, the display of TIMESTAMP values has been normalized to the time zone in effect for the session, which is ‘-08:00’. A different treatment of the time zones, this time to reflect local time, indicates the same chronological ordering but from a different perspective. SELECT f1 AT LOCAL LOCALIZED FROM timestwz ORDER BY f1; This statement returns the following results table. Chapter 7: DateTime and Interval Functions and Expressions ANSI Interval Expressions 222 SQL Functions, Operators, Expressions, and Predicates LOCALIZED ------------------------- 1997-10-06 23:43:00-08:00 1997-10-07 07:43:00-08:00 1997-10-07 15:45:52-08:00 ANSI Interval Expressions Purpose Performs a computation on an Interval value (or value expression) and returns a single value of the same type. Definition An interval expression is any expression that returns a result that is an INTERVAL value. interval_expression Syntax interval_term Syntax numeric_term Syntax numeric_factor Syntax where: 1101A010 interval_expression interval_term ± interval_term ( date_time_expression date_time_term ) start TO end FF07D268 interval_term interval_primary * numeric_factor numeric_term * interval_factor ± / FF07D270 numeric_term numeric_factor * numeric_factor / FF07D269 ± numeric_primary Chapter 7: DateTime and Interval Functions and Expressions ANSI Interval Expressions SQL Functions, Operators, Expressions, and Predicates 223 Syntax element … Specifies … interval_expression an expression that evaluates to an INTERVAL value. The form of theexpression is one of the following: • a single interval_term • the sum or difference of an interval_term and an interval_expression • the difference between a date_time_expression and a date_time_term (enclosed by parentheses) preceding a start TO end phrase interval_term one of the following expressions: • a single interval_factor • an interval_term multiplied or divided by a numeric_factor • the product of a numeric_term and an interval_factor interval_factor a signed interval_primary. date_time_expression an expression that evaluates to a DATE, TIME, or TIMESTAMP value. The form of the expression is one of the following: • a single date_time_term • the sum of an interval_expression and a date_time_term expression • the sum or difference of a date_time_expression and an interval_term For more information on date_time_expression, see “ANSI DateTime Expressions” on page 213. date_time_term a single date_time_primary or a date_time_primary with a time zone specifier of AT LOCAL, AT [TIME ZONE] expression, or AT [TIME ZONE] time_zone_string. For more information on date_time_term, see “ANSI DateTime Expressions” on page 213. start a DateTime value with the following syntax that defines the beginning of a date or time interval: where: • precision specifies the permitted range of digits, ranging from one to four. The default precision is two. • fractional_seconds_precision specifies the fractional precision for values of SECOND, ranging from zero to six. The default is six. MONTH and SECOND values are only permitted when used without TO end. 1101A018 (precision YEAR MONTH DAY HOUR MINUTE SECOND ) ,fractional_seconds_precision Chapter 7: DateTime and Interval Functions and Expressions ANSI Interval Expressions 224 SQL Functions, Operators, Expressions, and Predicates Examples of Interval Expression Components and Their Processing The following examples illustrate the components of an interval expression and describe how those components are processed. TO end a DateTime value with the following syntax that defines the end of a date or time interval: where fractional_seconds_precision specifies the fractional precision for values of SECOND, ranging from zero to six. The default is six. The value for end must be less significant than the value for start. If start is a YEAR value, then end must be a MONTH value. numeric_factor a signed numeric_primary. numeric_term a numeric_factor or a numeric_term multiplied or divided by a numeric_factor. numeric_primary one of the following elements, any of which must have the appropriate numeric type: • Column reference • Numeric literal value • Scalar function reference • Aggregate function reference • (table_expression) A scalar subquery. • (numeric_expression) interval_primary one of the following elements, any of which must have the appropriate INTERVAL type: • Column reference • Interval literal value For details on Interval literals, see SQL Data Types and Literals. • Scalar function reference • Aggregate function reference • (table_expression) A scalar subquery. • (interval_expression) Syntax element … Specifies … 1101A017 (fractional_seconds_precision) MONTH HOUR MINUTE SECOND Chapter 7: DateTime and Interval Functions and Expressions ANSI Interval Expressions SQL Functions, Operators, Expressions, and Predicates 225 Example of interval_term The definition for interval_term can be expressed in four forms. • interval_factor • interval_term * numeric_factor • interval_term / numeric_factor • numeric_term * interval_factor This example uses the second definition. SELECT (INTERVAL '3-07' YEAR TO MONTH) * 4; The interval_term in this operation is INTERVAL '3-07' YEAR TO MONTH. The numeric_factor is 4. The processing involves the following stages: 1 The interval is converted into 43 months as an INTEGER value. 2 The INTEGER value is multiplied by 4, giving the result 172 months. 3 The result is converted to '14-4'. Example of numeric_factor This example uses a numeric_factor with an INTERVAL YEAR TO MONTH typed value. SELECT INTERVAL '10-02' YEAR TO MONTH * 12/5; The numeric_factor in this operation is the integer 12. The processing involves the following stages: 1 The interval is multiplied by 12, giving the result as an interval. 2 The interval result is divided by 5, giving '24-04'. Note that very different results are obtained by using parentheses to change the order of evaluation as follows. SELECT INTERVAL '10-02' YEAR TO MONTH * (12/5); The numeric_factor in this operation is (12/5). The processing involves the following stages: 1 The numeric_factor is computed, giving the result 2.4, which is truncated to 2 because the value is an integer by default. 2 The interval is multiplied by 2, giving '20-04'. Chapter 7: DateTime and Interval Functions and Expressions ANSI Interval Expressions 226 SQL Functions, Operators, Expressions, and Predicates Example of interval_term / numeric_factor The following example uses an interval_term value divided by a numeric_factor value. SELECT INTERVAL '10-03' YEAR TO MONTH / 3; The interval_term is INTERVAL '10-03' YEAR TO MONTH. The numeric_factor is 3. The processing involves the following stages: 1 The interval value is decomposed into a value of months. Ten years and three months evaluate to 123 months. 2 The interval total is divided by the numeric_factor 3, giving '3-05'. The next example is similar to the first except that it shows how truncation is used in integer arithmetic. SELECT INTERVAL '10-02' YEAR TO MONTH / 3; The interval_term is INTERVAL '10-02' YEAR TO MONTH. The numeric_factor is 3. The processing involves the following stages: 1 The interval value is decomposed into a value of months. Ten years and two months evaluate to 122 months. 2 The interval total is divided by the numeric_factor 3, giving 40.67 months, which is truncated to 40 because the value is an integer. 3 The interval total is converted back to the appropriate format, giving INTERVAL '3-04'. Example of numeric_term * interval_primary In this format, the value for numeric_term can include instances of multiplication and division. SELECT 12/5 * INTERVAL '10-02' YEAR TO MONTH; The numeric_term is 12/5. The interval_primary is INTERVAL '10-02' YEAR TO MONTH. The processing involves the following stages: 1 The numeric_term 12/5 is evaluated, giving 2.4, which is truncated to 2 because the value is an integer by default. 2 The interval_primary is multiplied by 2, giving '20-04'. Example of numeric_term * ± interval_primary This example multiplies a negative interval_primary by a numeric_term and adds the negative result to an interval_term. SELECT (RACE_DURATION + (2 * INTERVAL -'30' DAY)); Chapter 7: DateTime and Interval Functions and Expressions ANSI Interval Expressions SQL Functions, Operators, Expressions, and Predicates 227 The numeric_term in this case is the numeric_primary 2. The interval_primary is INTERVAL -'30' DAY. RACE_DURATION is an interval_term, with type INTERVAL DAY TO SECOND. The processing involves the following stages: 1 The interval_primary is converted to an exact numeric, or 60 days. 2 The operations indicated in the arithmetic are performed on the operands (which are both numeric at this point), producing an exact numeric result having the appropriate scale and precision. In this example, 60 days are subtracted from RACE_DURATION, which is an INTERVAL type of INTERVAL DAY TO SECOND. 3 The numeric result is converted back into the indicated INTERVAL type, DAY TO SECOND. Example of interval_expression The definition for interval_expression can be expressed in three forms. • interval_term • interval_expression + interval_term • (date_time_expression - date_time_term) start TO end This example uses the second definition. SELECT (CAST(INTERVAL '125' MONTH AS INTERVAL YEAR(2) TO MONTH)) + INTERVAL '12' YEAR; The interval_expression is INTERVAL '125' MONTH. The interval_term is INTERVAL '12' YEAR. The processing involves the following stages: 1 The CAST function converts the interval_expression value of 125 months to 10 years and 5 months. 2 The interval_term amount of 12 years is added to the interval_expression amount, giving 22 years and 5 months. 3 The result is converted to the appropriate data type, which is INTERVAL YEAR(2) TO MONTH, giving '22-05'. This example uses the third definition for interval_expression. You must ensure that the values for date_time_expression and date_time_term are comparable. SELECT (TIME '23:59:59.99' - CURRENT_TIME(2)) HOUR(2) TO SECOND(2); The date_time_expression is TIME '23:59:59.99'. The date_term is the date_time_primary - CURRENT_TIME(2). Chapter 7: DateTime and Interval Functions and Expressions ANSI Interval Expressions 228 SQL Functions, Operators, Expressions, and Predicates The processing involves the following stages: 1 Assume that the current system time is 18:35:37.83. 2 The HOUR(2) TO SECOND(2) time interval 18:35:37.83 is subtracted from the TIME value 23:59:59.99, giving the result '5:24:22.16'. Here is another example that uses the third definition for interval_expression to find the difference in minutes between two TIMESTAMP values. First define a table: CREATE TABLE BillDateTime (start_time TIMESTAMP(0) ,end_time TIMESTAMP(0)); Now, determine the difference in minutes: SELECT (end_time - start_time) MINUTE(4) FROM BillDateTime; The processing involves the following stages: 1 The start_time TIMESTAMP value is subtracted from the end_time TIMESTAMP value, giving an interval result. 2 The MINUTE(4) specifies an interval unit of minutes with a precision of four digits, which allows for a maximum of 9999 minutes, or approximately one week. Rules The following rules apply to Interval expressions. • Expressions involving intervals are evaluated by converting the operands to integers, evaluating the resulting arithmetic expression, and then converting the result back to the appropriate interval. • The data type of both an interval_expression and an interval_primary is INTERVAL. • An interval_expression must contain either year-month interval components or day-time interval components. Mixing of INTERVAL types is not permitted. • Expressions involving intervals always evaluate to an interval, even if the expressions contain DateTime or Numeric expressions. Normalization of Intervals with Multiple Fields Because of the way the Parser normalizes multiple field INTERVAL values, the defined precision for an INTERVAL value may not be large enough to contain the value once it has been normalized. IF an interval_expression contains … THEN the result … only one component of type INTERVAL is of the same INTERVAL type. a single DateTime value or a start TO end phrase contains the DateTime fields specified for the DateTime or start TO end phrase values. more than one component of type INTERVAL is of an INTERVAL type including all the DateTime fields of the INTERVAL types of the component fields. Chapter 7: DateTime and Interval Functions and Expressions Arithmetic Operators SQL Functions, Operators, Expressions, and Predicates 229 For example, inserting a value of '99-12' into a column defined as INTERVAL YEAR(2) TO MONTH causes an overflow error because the Parser normalizes the value to '100-00'. When an attempt is made to insert that value into a column defined to have a 2-digit YEAR field, it fails because it is a 3-digit year. Here is an example that returns an overflow error because it violates the permissible range values for the type. First define the table. CREATE TABLE BillDateTime (column_1 INTERVAL YEAR ,column_2 INTERVAL YEAR(1) TO MONTH ,column_3 INTERVAL YEAR(2) TO MONTH ,column_4 INTERVAL YEAR(3) TO MONTH ); Now insert the value INTERVAL '999-12' YEAR TO MONTH using this INSERT statement. INSERT BillDateTime (column_1, column_4) VALUES ( INTERVAL '40' YEAR, INTERVAL '999-12' YEAR TO MONTH ); The result is an overflow error because the valid range for INTERVAL YEAR(3) TO MONTH values is -'999-11' to '999-11'. You might expect the value '999-12' to work, but it fails because the Parser normalizes it to a value of '1000-00' YEAR TO MONTH. Because the value for year is then four digits, an overflow occurs and the operation fails. Arithmetic Operators Operations on ANSI DateTime and Interval values can include the scalar arithmetic operators +, -, *, and /. However, the operators are only valid on specific combinations of DateTime and Interval values. Arithmetic Operators and Result Types The following arithmetic operations are permitted for DateTime and Interval data types: First Value Type Operator Second Value Type Result Type DateTime - DateTime Interval DateTime + Interval DateTime DateTime - Interval DateTime Interval + DateTime DateTime Interval + Interval Interval Interval - Interval Interval Interval * Number Interval Chapter 7: DateTime and Interval Functions and Expressions Arithmetic Operators 230 SQL Functions, Operators, Expressions, and Predicates Adding or Subtracting Numbers from DATE Teradata SQL extends the ANSI SQL:2008 standard to allow the operations of adding or subtracting a number of days from an ANSI DATE value. Teradata SQL treats the number as an INTERVAL DAY value. For more information, see “DATE and Integer Arithmetic” on page 233. Calculating the Difference Between Two DateTime Values Teradata Database calculates the interval difference between two DATE, TIME or TIMESTAMP values according to the ANSI SQL standard. Units smaller than the unit of the result are ignored when calculating the interval value. For example, when computing the difference in months for two DATE values, the day values in each of the two operands are ignored. Similarly when computing the difference in hours for two TIMESTAMP values, the minutes and the seconds values of the operands are ignored. Example 1 The following query calculates the difference in days between the two DATE values. SELECT (DATE '2007-05-10' - DATE '2007-04-28') DAY; The result is the following: (2007-05-10 - 2007-04-28) DAY ----------------------------- 12 The following query calculates the difference in months between the two DATE values. SELECT (DATE '2007-05-10' - DATE '2007-04-28') MONTH; The result is the following: (2007-05-10 - 2007-04-28) MONTH ------------------------------- 1 There is a difference of 12 days between the two dates, which does not constitute one month. However, Teradata Database ignores the day values during the calculation and only considers the month values, so the result is an interval of one month indicating the difference between April and May. Interval / Number Interval Number * Interval Interval First Value Type Operator Second Value Type Result Type Chapter 7: DateTime and Interval Functions and Expressions Aggregate Functions and ANSI DateTime and Interval Data Types SQL Functions, Operators, Expressions, and Predicates 231 Example 2: Add Interval to DATE The following example adds an Interval value to a DateTime value: CREATE TABLE Subscription (id CHARACTER(13) ,subscribe_date DATE ,subscribe_interval INTERVAL MONTH(4)); INSERT Subscription (subscribe_date, subscribe_interval) VALUES (CURRENT_DATE, INTERVAL ’24’ MONTH); SELECT subscribe_date + subscribe_interval FROM Subscription; The result is a DateTime value. Aggregate Functions and ANSI DateTime and Interval Data Types DateTime Data Types The following aggregate functions are valid for ANSI SQL:2008 DateTime types. Interval Data Types The following aggregate functions are valid for Interval types. For this function … The result is … For more information, see … AVG(arg) the typ e of the argument. “AVG” on page 350. MAX(arg) the type of the argument, based on the comparison rules for DateTime types. “MAX” on page 372. MIN(arg) “MIN” on page 375. COUNT(arg) INTEGER, if the mode is Teradata. “COUNT” on page 356. DECIMAL(n,0), if the mode is ANSI, where: n is … if MaxDecimal in DBSControl is … 15 0, 15, or 18 38 38 For this function … The result is … For more information, see … AVG(arg) the type of the argument. “AVG” on page 350. Chapter 7: DateTime and Interval Functions and Expressions Scalar Operations and DateTime Functions 232 SQL Functions, Operators, Expressions, and Predicates Scalar Operations and DateTime Functions DateTime functions are those functions that operate on either DateTime or Interval values and provide a DateTime value as a result. The supported DateTime functions are: • CURRENT_DATE • CURRENT_TIME • CURRENT_TIMESTAMP • EXTRACT To avoid any synchronization problems, operations among any of these functions are guaranteed to use identical definitions for DATE, TIME, or TIMESTAMP so that the following are always true: • CURRENT_DATE = CURRENT_DATE • CURRENT_TIME = CURRENT_TIME • CURRENT_TIMESTAMP = CURRENT_TIMESTAMP • CURRENT_DATE and CURRENT_TIMESTAMP always identify the same DATE • CURRENT_TIME and CURRENT_TIMESTAMP always identify the same TIME The values reflect the time when the request started and do not change during the duration of the request. Example The following example uses the CURRENT_DATE DateTime function: SELECT INTERVAL '20' YEAR + CURRENT_DATE; COUNT(arg) INTEGER, if the mode is Teradata. “COUNT” on page 356. DECIMAL(n,0), if the mode is ANSI, where: n is … if MaxDecimal in DBSControl is … 15 0, 15, or 18 38 38 MAX(arg) the type of the argument, based on the comparison rules for DateTime types. “MAX” on page 372. MIN(arg) “MIN” on page 375. SUM(arg) the type of the argument. “SUM” on page 418. For this function … The result is … For more information, see … Chapter 7: DateTime and Interval Functions and Expressions Teradata Date and Time Expressions SQL Functions, Operators, Expressions, and Predicates 233 Related Topics Teradata Date and Time Expressions Teradata SQL provides a data type for DATE values and stores TIME values as encoded numbers with type REAL. This is a Teradata extension of the ANSI SQL:2008 standard and its use is strongly deprecated. Since both DATE and TIME are encoded values, not simple integers or real numbers, arithmetic operations on these values are restricted. ANSI DATE and TIME values are stored using appropriate DateTime types and have their own set of rules for DateTime assignment and expressions. For information, see “ANSI DateTime and Interval Data Type Assignment Rules” on page 210 and “Scalar Operations on ANSI SQL:2008 DateTime and Interval Values” on page 212. DATE and Integer Arithmetic The following arithmetic functions can be performed with date and an integer (INTEGER is interpreted as a number of days): • DATE + INTEGER • INTEGER + DATE • DATE - INTEGER These expressions are not processed as simple addition or subtraction, but rather as explained in the following process: 1 The encoded date value is converted to an intermediate value which is the number of days since some system-defined fixed date. 2 The integer value is then added or subtracted, forming another value as number of days, since the fixed base date. 3 The result is converted back to a date, valid in the Gregorian calendar. For more information on … See … CURRENT_DATE “CURRENT_DATE” on page 671 CURRENT_TIME “CURRENT_TIME” on page 677 CURRENT_TIMESTAMP “CURRENT_TIMESTAMP” on page 681 EXTRACT “EXTRACT” on page 242 Chapter 7: DateTime and Interval Functions and Expressions Scalar Operations on Teradata DATE Values 234 SQL Functions, Operators, Expressions, and Predicates DATE and Date Arithmetic The DATE - DATE expression is not processed as a simple subtraction, but rather as explained in the following process: 1 The encoded date values are converted to intermediate values which are each the number of days since a system-defined fixed date. 2 The second of these values is then subtracted from the first, giving the number of days between the two dates. 3 The result is returned as if it were in the ANSI SQL:2008 form INTERVAL DAY, though the value itself is an integer. Other arithmetic operations on date values may provide results, but those results are not meaningful. Example DATE/2 provides an integer result, but the value has no meaning. There are no simple arithmetic operations that have meaning for time values. The reason is that a time value is simply a real number with time encoded as: (HOUR*10000 + MINUTE*100 + SECOND) where SECOND may include a fractional value. Scalar Operations on Teradata DATE Values The operations of addition and subtraction are allowed as follows, where integer values represent the number of days: Adding 90 days, for example, is not identical to adding 3 months, because of the varying number of days in months. Also, adding multiples of 365 days is not identical to adding years because of leap years. Note that scalar operations on Teradata DATE expressions are performed using ANSI SQL:2008 data types, so an expression of the type date_expression - numeric_expression is treated as if the numeric_expression component were typed as INTERVAL DAY. Argument 1 Operation Argument 2 Result DATE + INTEGER DATE DATE - INTEGER DATE INTEGER + DATE DATE DATE - DATE INTEGER Chapter 7: DateTime and Interval Functions and Expressions Scalar Operations on Teradata DATE Values SQL Functions, Operators, Expressions, and Predicates 235 ANSI SQL:2008 DateTime and Interval values have their own set of scalar operations and with the exception of the scalar operations defined here for DATE, do not support the implicit conversions to resolve expressions of mixed data types. ADD_MONTHS Function The ADD_MONTHS function provides for adding or subtracting months or years, handling the variable number of days involved. For details, see “ADD_MONTHS” on page 236. EXTRACT Function Use the EXTRACT function to get the year, month, or day from a date. The result has INTEGER data type. For details, see “EXTRACT” on page 242. Chapter 7: DateTime and Interval Functions and Expressions ADD_MONTHS 236 SQL Functions, Operators, Expressions, and Predicates ADD_MONTHS Purpose Adds an integer number of months to a DATE or TIMESTAMP expression and normalizes the result. Date Syntax Timestamp Syntax where: ANSI Compliance ADD_MONTHS is a Teradata extension to the ANSI SQL:2008 standard. FF07D202 ADD_MONTHS (date_expression, integer_expression ) Syntax element … Specifies … date_expression one of the following, to which integer_expression months are to be added: • A DATE value enclosed in apostrophes • A DATE literal • The CURRENT_DATE keyword • The DATE keyword • A UDT that has an implicit cast that casts between the UDT and a character or DATE type. CURRENT_DATE and DATE specify the current system DATE value. timestamp_expression one of the following, to which integer_expression months are to be added: • A TIMESTAMP literal • The CURRENT_TIMESTAMP keyword • A UDT that has an implicit cast that casts between the UDT and a character or TIMESTAMP type. CURRENT_TIMESTAMP specifies the current system TIMESTAMP value. integer_expression the number of integer months to be added to date_expression or timestamp_expression. FF07D208 ADD_MONTHS (timestamp_expression, integer_expression ) Chapter 7: DateTime and Interval Functions and Expressions ADD_MONTHS SQL Functions, Operators, Expressions, and Predicates 237 Rules ADD_MONTHS observes the following rules: • If either argument of ADD_MONTHS is NULL, then the result is NULL. • If the result is not in the range ‘0000-01-01’ to ‘9999-12-31’, then an error is reported. • Results of an ADD_MONTHS function that are invalid dates are normalized to ensure that all reported dates are valid. Support for UDTs To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including ADD_MONTHS, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see “Implicit Type Conversions” on page 745. Scalar Arithmetic on Months Issues Consistent handling of a target month having fewer days than the month in the source date is an important issue for scalar arithmetic on month intervals because the concept of a month has no fixed definition. All scalar function operations on dates use the Gregorian calendar. Peculiarities of the Gregorian calendar ensure that arithmetic operations such as adding 90 days (to represent three months) or 730 days (to represent two years) to a DATE value generally do not provide the desired result. For more information, see “Gregorian Calendar Rules” on page 218. The ADD_MONTHS function uses an algorithm that lets you add or subtract a number of months to a date_expression or timestamp_expression and to obtain consistently valid results. IF this argument is a UDT … THEN Teradata Database performs implicit type conversion if the UDT has an implicit cast that casts between the UDT and any of the following predefined types … date_expression • Character • Date • Timestamp timestamp_expression integer_expression Numeric Chapter 7: DateTime and Interval Functions and Expressions ADD_MONTHS 238 SQL Functions, Operators, Expressions, and Predicates When deciding whether to use the Teradata SQL ADD_MONTHS function or ANSI SQL:2008 DateTime interval arithmetic, you are occasionally faced with choosing between returning a result that is valid, but probably neither desired nor expected, or not returning any result and receiving an error message. A third option that does not rely on system-defined functions is to use the Teradata Databasedefined Calendar view for date arithmetic. For information, see “CALENDAR View” in the Data Dictionary book. Normalization Behavior of ADD_MONTHS The standard approach to interval month arithmetic is to increment MONTH and YEAR values as appropriate and retain the source value for DAY. This is a problem for the case when the target DAY value is smaller than the source DAY value from the source date. For example, what approach should be taken to handle the result of adding one MONTH to a source DATE value of ‘1999-01-31’? Using the standard approach, the answer would be ‘1999- 02-31’, but February 31 is not a valid date. The behavior of ADD_MONTHS is equivalent to that of the ANSI SQL:2008 compliant operations DATE ± INTERVAL ‘n’ MONTH and TIMESTAMP ± INTERVAL ‘n’ MONTH with one important difference. The difference between these two scalar arithmetic operations is their behavior when a invalid date value is returned by the function. • ANSI SQL:2008 arithmetic returns an error. • ADD_MONTHS arithmetic makes normative adjustments and returns a valid date. Definition of Normalization The normalization process is explained more formally as follows. When the DAY field of the source date_expression or timestamp_expression is greater than the resulting target DAY field, ADD_MONTHS sets DD equal to the last day of the month + n to normalize the reported date or timestamp. Define date_expression as ‘YYYY-MM-DD’ for simplicity. For a given date_expression, you can then express the syntax of ADD_MONTHS as follows. ADD_MONTHS('YYYY-MM-DD' , n) Recalling that n can be negative, and substituting ‘YYYY-MM-DD’ for date_expression, you can redefine ADD_MONTHS in terms of ANSI SQL:2008 dates and intervals as follows. ADD_MONTHS('YYYY-MM-DD', n) = 'YYYY-MM-DD' ± INTERVAL 'n' MONTH The equation is true unless an invalid date such as 1999-09-31 results, in which case the ANSI expression traps the invalid date exception and returns an error. ADD_MONTHS, on the other hand, processes the exception and returns a valid, though not necessarily expected, date. The algorithm ADD_MONTHS uses to produce its normalized result is as follows, expressed as pseudocode. Chapter 7: DateTime and Interval Functions and Expressions ADD_MONTHS SQL Functions, Operators, Expressions, and Predicates 239 WHEN DD > last_day_of_the_month(MM+n) THEN SET DD = last_day_of_the_month(MM+n) This property is also true for the date portion of any timestamp_expression. Note that normalization produces valid results for leap years. Non-Intuitive Results of ADD_MONTHS Because of the normalization made by ADD_MONTHS, many results of the function are not intuitive, and their inversions are not always symmetrical. For example, compare the results of “Example 5” on page 240 with the results of “Example 7” on page 241. This is because the function always produces a valid date, but not necessarily an expected date. Correctness in the case of interval month arithmetic is a relative term. Any definition is arbitrary and cannot be generalized, so the word ‘expected’ is a better choice for describing the behavior of ADD_MONTHS. The following SELECT statements return dates that are both valid and expected: SELECT ADD_MONTHS ('1999-08-15' , 1); This statement returns 1999-09-15. SELECT ADD_MONTHS ('1999-09-30' , -1); This statement returns 1999-08-30. The following SELECT statement returns a valid date, but its ‘correctness’ depends on how you choose to define the value ‘one month.’ SELECT ADD_MONTHS ('1999-08-31' , 1); This statement returns 1999-09-30, because September has only 30 days and the nonnormalized answer of 1999-09-31 is not a valid date. ADD_MONTHS Summarized ADD_MONTHS returns a new date_expression or timestamp_expression with YEAR and MONTH fields adjusted to provide a correct date, but a DAY field adjusted only to guarantee a valid date, which might not be a date you expect intuitively. If this behavior is not acceptable for your application, use ANSI SQL:2008 DateTime interval arithmetic instead. For more information, see “ANSI Interval Expressions” on page 222. Remember that ADD_MONTHS changes the DAY value of the result only when an invalid date_expression or timestamp_expression would otherwise be reported. For examples of this behavior, see the example set listed under “Non-Intuitive Examples” on page 240. Chapter 7: DateTime and Interval Functions and Expressions ADD_MONTHS 240 SQL Functions, Operators, Expressions, and Predicates Intuitive Examples “Example 1” through “Example 5” are simple, intuitive examples of the ADD_MONTHS function. All results are both valid and expected. Example 1 This statement returns the current date plus 13 years. SELECT ADD_MONTHS (CURRENT_DATE, 12*13); Example 2 This statement returns the date 6 months ago. SELECT ADD_MONTHS (CURRENT_DATE, -6); Example 3 This statement returns the current TIMESTAMP plus four months. SELECT ADD_MONTHS (CURRENT_TIMESTAMP, 4); Example 4 This statement returns the TIMESTAMP nine months from January 1, 1999. Note the literal form, which includes the keyword TIMESTAMP. SELECT ADD_MONTHS (TIMESTAMP '1999-01-01 23:59:59', 9); Example 5 This statement adds one month to January 30, 1999. SELECT ADD_MONTHS ('1999-01-30', 1); The result is 1999-02-28. Non-Intuitive Examples “Example 6” through “Example 10” illustrate how the results of an ADD_MONTHS function are not always what you might expect them to be when the value for DAY in date_expression or the date component of timestamp_expression is 29, 30, or 31. All examples use a date_expression for simplicity. In every case, the function behaves as designed. Chapter 7: DateTime and Interval Functions and Expressions ADD_MONTHS SQL Functions, Operators, Expressions, and Predicates 241 Example 6 The result of the SELECT statement in this example is a date in February, 1996. The result would be February 31, 1996 if that were a valid date, but because February 31 is not a valid date, ADD_MONTHS normalizes the answer. That answer, because the DAY value in the source date is greater than the last DAY value for the target month, is the last valid DAY value for the target month. SELECT ADD_MONTHS ('1995-12-31', 2); The result of this example is 1996-02-29. Note that 1996 was a leap year. If the interval were 14 months rather than 2, the result would be '1997-02-28'. Example 7 This statement performs the converse of the ADD_MONTHS function in “Example 5” on page 240. You might expect it to return ‘1999-01-30’, which is the source date in that example, but it does not. SELECT ADD_MONTHS ('1999-02-28' , -1); ADD_MONTHS returns the result 1999-01-28. The function performs as designed and this result is not an error, though it might not be what you would expect from reading “Example 5.” Example 8 You might expect the following statement to return ‘1999-03-31’, but it does not. SELECT ADD_MONTHS ('1999-02-28' , 1); ADD_MONTHS returns the result 1999-03-28. Example 9 You might expect the following statement to return ‘1999-03-31’, but it does not. SELECT ADD_MONTHS ('1999-04-30' , -1); ADD_MONTHS returns the result 1999-03-30. Example 10 You might expect the following statement to return '1999-05-31', but it does not. SELECT ADD_MONTHS ('1999-04-30' , 1); ADD_MONTHS returns the result 1999-05-30. Chapter 7: DateTime and Interval Functions and Expressions EXTRACT 242 SQL Functions, Operators, Expressions, and Predicates EXTRACT Purpose Extracts a single specified full ANSI SQL:2008 field from any DateTime or Interval value, converting it to an exact numeric value. Syntax where: Syntax element … Specifies … YEAR that the integer value for YEAR is to be extracted from the date represented by value. MONTH that the integer value for MONTH is to be extracted from the date represented by value. DAY that the integer value for DAY is to be extracted from the date represented by value. HOUR that the integer value for HOUR is to be extracted from the date represented by value. MINUTE that the integer value for MINUTE is to be extracted from the date represented by value. TIMEZONE_HOUR that the integer value for TIMEZONE_HOUR is to be extracted from the date represented by value. TIMEZONE_MINUTE that the integer value for TIMEZONE_MINUTE is to be extracted from the date represented by value. SECOND that the integer value for SECOND is to be extracted from the date represented by value. value an expression that results in a DateTime, Interval, or UDT value. FF07D144 EXTRACT MONTH ( YEAR FROM value) DAY HOUR MINUTE SECOND TIMEZONE_HOUR TIMEZONE_MINUTE Chapter 7: DateTime and Interval Functions and Expressions EXTRACT SQL Functions, Operators, Expressions, and Predicates 243 ANSI Compliance EXTRACT is partially ANSI SQL:2008 compliant. ANSI SQL:2008 EXTRACT allows extraction of any field in any DateTime or Interval value. In addition to the ANSI SQL:2008 extract function, Teradata SQL also supports HOUR, MINUTE, or SECOND extracted from a floating point value. Arguments IF value is … THEN … a character string expression that represents a date the string must match the 'YYYY-MM-DD' format. a character string expression that represents a time the string must match the 'HH:MI:SS.SSSSSS' format. a floating point type value must be a time value encoded with the algorithm HOUR * 10000 + MINUTE * 100 + SECOND. Only HOUR, MINUTE, and SECOND can be extracted from a floating point value. Externally created time values can be appropriately encoded and stored in a REAL column to any desired precision if the encoding creates a value representable by REAL without precision loss. Do not store time values as REAL in any new applications. Instead, use the more rigorously defined ANSI SQL:2008 DateTime data types. a UDT the UDT must have an implicit cast that casts between the UDT and any of the following predefined types: • Numeric • Character • DateTime To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including EXTRACT, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see “Implicit Type Conversions” on page 745. not a character string expression or floating point type or UDT the expression must evaluate to a DateTime or Interval type. Chapter 7: DateTime and Interval Functions and Expressions EXTRACT 244 SQL Functions, Operators, Expressions, and Predicates Results EXTRACT returns an exact numeric value for ANSI SQL:2008 DateTime values. EXTRACT returns values adjusted for the appropriate time zone if the data type of the argument is TIME or TIMESTAMP. If no time zone is specified for the argument, then the time zone displacement based on the current session time zone is used. Otherwise, the explicit time zone of the argument is used. You can use the AT clause to explicitly specify a time zone for the argument. For details, see “ANSI DateTime Expressions” on page 213. If value is NULL, the result is NULL. Example 1 The following example returns the year, as an integer, from the current date. SELECT EXTRACT (YEAR FROM CURRENT_DATE); Example 2 Assuming PurchaseDate is a DATE field, this example returns the month of the date value formed by adding 90 days to PurchaseDate as an integer. SELECT EXTRACT (MONTH FROM PurchaseDate+90) FROM SalesTable; Example 3 The following returns 12 as an integer. SELECT EXTRACT (DAY FROM '1996-12-12'); Example 4 This example returns an error because the character literal does not evaluate to a valid date. SELECT EXTRACT (DAY FROM '1996-02-30'); If you extract … THEN … SECOND IF value has a seconds fractional precision of … THEN the result is … zero INTEGER. greater than zero DECIMAL with the scaling as specified for the SECOND field in its data description. anything else the result is INTEGER, with 32 bits of precision. Chapter 7: DateTime and Interval Functions and Expressions EXTRACT SQL Functions, Operators, Expressions, and Predicates 245 Example 5 The following returns an error because the character string literal does not match the ANSI SQL:2008 date format. SELECT EXTRACT (DAY FROM '96-02-15'); If the argument to EXTRACT is a value of type DATE, the value contained is warranted to be a valid date, for which EXTRACT cannot return an error. Example 6 The following example relates to non-ANSI DateTime definitions. If the argument is a character literal formatted as a time value, it is converted to REAL and processed. In this example, 59 is returned. SELECT EXTRACT (MINUTE FROM '23:59:17.3'); Example 7 This example returns the hour, as an integer, from the current time. SELECT EXTRACT (HOUR FROM CURRENT_TIME); Current time is retrieved as the system value TIME, to the indicated precision. Example 8 The following example returns the seconds as DECIMAL(8,2). This is based on the fractional seconds precision of 2 for CURRENT_TIME. SELECT EXTRACT (SECOND FROM CURRENT_TIME (2)); Chapter 7: DateTime and Interval Functions and Expressions GetTimeZoneDisplacement 246 SQL Functions, Operators, Expressions, and Predicates GetTimeZoneDisplacement Purpose Returns the rules and time zone displacement information for a specified time zone string. Syntax where: ANSI Compliance GetTimeZoneDisplacement is a Teradata extension to the ANSI SQL:2008 standard. Result GetTimeZoneDisplacement returns a string of bytes containing the rules and time zone displacement information for the specified time zone string. The result data type is BYTE. The information returned is: Syntax element … Specifies … time_zone_string a valid time zone string specified using a VARBYTE data type. For a list of time zone strings supported by Teradata, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. If time_zone_string is invalid or unsupported, GetTimeZoneDisplacement returns a value of 1 in the first byte to indicate that the time zone string does not exist. GetTimeZoneDisplacement (time_zone_string) 1101A720 Byte Value First byte • 1, if the time zone string is not found. That is, the time zone string specified in the input argument is invalid or unsupported. • 0, if the time zone string is found. Chapter 7: DateTime and Interval Functions and Expressions GetTimeZoneDisplacement SQL Functions, Operators, Expressions, and Predicates 247 Usage Notes GetTimeZoneDisplacement is a system user-defined function (UDF) that Teradata Database invokes internally to resolve a time zone string specified in an SQL statement or Specification for Data Formatting (SDF) file. Users do not invoke this function directly; however, users can modify this UDF to add new time zone strings or add or modify the rules of an existing time zone string. Adding or Modifying Time Zone Strings Teradata Database provides a set of time zone strings that represent commonly used time zones. For a list of supported time zone strings, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. The GetTimeZoneDisplacement UDF stores and maintains these time zone strings and the related rules for converting between UTC and the time in the local time zone. If the supplied time zone strings do not meet your requirements, you may add or modify the time zone strings by modifying the GetTimeZoneDisplacement UDF, which is located in the SYSLIB database. The source code for the UDF is available as part of the DBS package and is located at /tdbms/etc/dem/src. To define new time zone strings or add or modify the rules of an existing time zone string: 1 Make a backup copy of the existing GetTimeZoneDisplacement UDF. 2 To modify an existing time zone string: a Find the time zone string entry in the TZ_DST structure of the GetTimeZoneDisplacement UDF. b Modify the rules and information associated with the time zone string entry or add new rules to the entry. 3 To add a new time zone string: a Create a new entry in the TZ_DST structure for the new time zone string and its related rules. b Place the new time zone string entry in the correct alphabetical position within the TZ_DST structure. Second byte • 1, if the time zone string has separate daylight saving time and standard time zone displacements from Coordinated Universal Time (UTC) time. In this case, the next 480 or so bytes store the set of rules describing a valid standard time zone displacement, daylight saving time zone displacement, and the start and end time for daylight saving time. A maximum of 6 rules are stored for each time zone string. • 0, if the time zone string does not have separate daylight saving time and standard time zone displacements from UTC time. In this case, the next 4 bytes store the time zone displacement hour and minute values. Byte Value Chapter 7: DateTime and Interval Functions and Expressions GetTimeZoneDisplacement 248 SQL Functions, Operators, Expressions, and Predicates 4 Recompile the UDF using the REPLACE FUNCTION statement. For more information, see “CREATE FUNCTION (External Form)/ REPLACE FUNCTION (External Form)” in SQL Data Definition Language. For example: Database SYSLIB; DROP FUNCTION GetTimeZoneDisplacement; REPLACE FUNCTION GetTimeZoneDisplacement (tzstringinfo VARBYTE(130)) RETURNS BYTE(340) LANGUAGE C NO SQL PARAMETER STYLE SQL EXTERNAL; //or specify the path of the new source code. The TZ_DST Structure The TZ_DST structure is an array of TZwithDST elements where each element describes a time zone string and its related rules. The definition of the TZwithDST structure is: typedef struct TZwithDST { CHARACTER_LATIN tzstring[TZSTRINGSIZE]; int number_of_rules; DSTRules TZRules[TZRulesEntries]; SMALLINT Standardtzdispl_hour; SMALLINT Standardtzdispl_minute; } TZwithDST; where: Each DSTRules element of the TZRules array describes a rule for the time zone string. The definition of the DSTRules structure is: typedef struct DSTRules { startendDSTInfo startDST; startendDSTInfo endDST; yearDisplInfo validyrs; } DSTRules; Field Description tzstring The name of the time zone string. For example, "America Pacific." The maximum length of a time zone string is 130 bytes. number_of_rules The number of rules related to this time zone string. A maximum of 6 rules is allowed for each time zone string. TZRules An array where each DSTRules element describes a rule. These rules are used to calculate the time zone displacement for the time zone string. Standardtzdispl_hour The standard time zone displacement hour. Standardtzdispl_minute The standard time zone displacement minute. Chapter 7: DateTime and Interval Functions and Expressions GetTimeZoneDisplacement SQL Functions, Operators, Expressions, and Predicates 249 where: You can specify the following for startDST and endDST. Enter zero if a field is not applicable. Field Description startDST Specifies the date and time when daylight saving time (DST) starts. endDST Specifies the date and time when daylight saving time ends. validyrs Specifies the years in which the DST start and end dates apply. The following information related to this year range is included: • start_year - the year when these DST rules start. • end_year - the year when these DST rules end. • Standardtzdispl_hour - the standard time zone displacement hour. • Standardtzdispl_minute - the standard time zone displacement minute. • DSTtzdispl_hour - the time zone displacement hour for daylight saving time. • DSTtzdispl_minute -the time zone displacement minute for daylight saving time. Field Description rule_type Indicates how the start and end date for DST is specified. The valid values are: • 0 - No DST start or end information is specified. The standard time zone displacement is used. • 1 - DST starts or ends on the specified fixed date. The date is specified by the month and day_of_month fields. • 2 - DST starts or ends on the 1st, 2nd, or 3rd weekday of the month as indicated by the month, day_of_week, and week_of_month fields. • 3 - DST starts or ends on the 2nd to the last, 3rd to the last, or the last weekday of the month as indicated by the month, day_of_week, and week_of_month fields. • 4 - DST starts or ends on the next weekday on or immediately after the date specified in the day_of_month field. The month and weekday are specified in the month and day_of_week fields. For example, for time zone string 'America Pacific', the start date rule is the first Sunday after March 8th, which gives us March 14th for the year 2010. month The month when DST starts or ends. Valid values are 0- 12. This field is used for rule_type 1, 2, 3, and 4. For example, for time zone string 'America Pacific', the start date rule is the first Sunday after March 8; therefore, this field has a value of 3 in the startDST structure to represent March. Chapter 7: DateTime and Interval Functions and Expressions GetTimeZoneDisplacement 250 SQL Functions, Operators, Expressions, and Predicates Example Assume that you want to add a new time zone string 'Europe Azores', which has one rule with the following time zone displacement information: • DST starts on the last Sunday in March at 12:00 am local time. • DST ends on the last Sunday in October at 1:00 am local time. • The standard time zone offset from UTC is -1. • The daylight saving time offset from UTC is 0. • The start year for the rule is 2009. • The end year for the rule is 2010. day_of_month If rule_type is 1, this field specifies the day of the month when DST starts or ends. For example, if DST ends at 12:00 am local time on August 21, this field contains the value 21 in the endDST structure. If rule_type is 4, DST starts or ends on the next weekday on or immediately after the date specified by this field. For example, for time zone string 'America Pacific', the start date rule is the first Sunday after March 8; therefore, this field has a value of 8 in the startDST structure. When rule_type is 0, 2 or 3, this field is not used and the value is 0. day_of_week The valid values are 0-7 representing the weekdays Sunday-Saturday. This field is used for rule_type 2, 3 and 4. For example, for time zone string 'America Pacific', the start date rule is the first Sunday after March 8; therefore, this field has a value of 0 in the startDST structure to represent Sunday. week_of_month The valid values are 1, 2, 3, 4, 5, -1, and -2 representing the 1st, 2nd, 3rd, 4th, 5th, last, and second to the last weekday of the month. This field is used for rule_type 2 and 3. For example, for time zone string 'Europe Azores', the start date rule is the last Sunday in March; therefore, this field has a value of -1 in the startDST structure to represent the last week of the month. loctime The local time when DST starts or ends. For example, "02:00:00" indicates that DST starts or ends at 2:00 am local time. Field Description Chapter 7: DateTime and Interval Functions and Expressions GetTimeZoneDisplacement SQL Functions, Operators, Expressions, and Predicates 251 Based on this information, the new time zone string entry for 'Europe Azores' is: {"Europe Azores", 1, <= 1 rule defined for 'Europe Azores' {{{3, 3, 0, 0, -1, "00:00:00"}, <= Start of rule 1, startDST information {3, 10, 0, 0, -1, "01:00:00"}, <= endDST information {2009, 2010, -1, 0, 0, 0}}, <= validyrs information {{0, 0, 0, 0, 0, "00:00:00"}, <= Start of rule 2 {0, 0, 0, 0, 0, "00:00:00"}, {0, 0, 0, 0, 0, 0}}, {{0, 0, 0, 0, 0, "00:00:00"}, <= Start of rule 3 {0, 0, 0, 0, 0, "00:00:00"}, {0, 0, 0, 0, 0, 0}}, {{0, 0, 0, 0, 0, "00:00:00"}, <= Start of rule 4 {0, 0, 0, 0, 0, "00:00:00"}, {0, 0, 0, 0, 0, 0}}, {{0, 0, 0, 0, 0, "00:00:00"}, <= Start of rule 5 {0, 0, 0, 0, 0, "00:00:00"}, {0, 0, 0, 0, 0, 0}}, {{0, 0, 0, 0, 0, "00:00:00"}, <= Start of rule 6 {0, 0, 0, 0, 0, "00:00:00"}, {0, 0, 0, 0, 0, 0}} }, -1, 0 <= Standard time zone displacement }, Note that the time zone string entry has space for 6 rules but only one rule is used for the start year 2009 and end year 2010. You must place the new 'Europe Azores' time zone string in between the 'Australia Western' and 'Europe Central' time zone strings in the TZ_DST structure to maintain the alphabetical order of the structure. Related Topics For more information on… See… Setting session time zones SET TIME ZONE, CREATE USER, MODIFY USER in SQL Data Definition Language. System time zone settings "System TimeZone Hour" and "System TimeZone Minute" in Utilities. Automatic adjustment of the system time to account for daylight saving time "SDF file" and "Locale Definition Utility (tdlocaledef)" in Utilities. Chapter 7: DateTime and Interval Functions and Expressions GetTimeZoneDisplacement 252 SQL Functions, Operators, Expressions, and Predicates SQL Functions, Operators, Expressions, and Predicates 253 CHAPTER 8 Calendar Functions This chapter describes the functions that provide support for DateTime operations that use calendar attributes. Prerequisites Before you can use these functions, you must run the Database Initialization Program (DIP) utility and execute the DIPUDT script. The DIPALL or DIPUDT script will create the calendar functions in the SYSLIB database. For more information about the DIP utility, see Utilities. If you have a user-developed UDF with the same name as a calendar function, you must remove that user-developed UDF from the normal UDF search path before you can invoke the calendar function. If the calendar function is not found in the current database, Teradata Database searches for the function in the SYSLIB database. Alternatively, you may invoke the calendar function by using the fully qualified syntax, SYSLIB.calendar_function_name. Chapter 8: Calendar Functions day_of_week 254 SQL Functions, Operators, Expressions, and Predicates day_of_week Purpose Returns the day of the week which the specified date falls upon. Syntax where: ANSI Compliance day_of_week is a Teradata extension to the ANSI SQL:2008 standard. Argument Types day_of_week is an overloaded scalar function. It is defined with the following parameter data types: • DATE • TIMESTAMP • TIMESTAMP WITH TIME ZONE If the argument passed to the function does not match one of these declared data types, an error is returned indicating that the function does not exist. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result The result is an INTEGER value between 1 to 7, representing the day of the week, where Sunday = 1 and Saturday = 7. Syntax element… Specifies… expression an expression that results in a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value. 1101A725 SYSLIB. day_of_week (expression) Chapter 8: Calendar Functions day_of_week SQL Functions, Operators, Expressions, and Predicates 255 Usage Notes The day_of_week function provides improved performance compared to using the Sys_Calendar.Calendar system view to obtain similar results. For more information about the CALENDAR system view, see Data Dictionary. Example If the current date is October 18, 2010, which is a Monday, the following queries return the value 2 as the result since Monday is the 2nd day of the week. SELECT SYSLIB.day_of_week(CURRENT_DATE); SELECT SYSLIB.day_of_week(DATE '2010-10-18'); Chapter 8: Calendar Functions day_of_month 256 SQL Functions, Operators, Expressions, and Predicates day_of_month Purpose Returns the number of days from the beginning of the month to the specified date. Syntax where: ANSI Compliance day_of_month is a Teradata extension to the ANSI SQL:2008 standard. Argument Types day_of_month is an overloaded scalar function. It is defined with the following parameter data types: • DATE • TIMESTAMP • TIMESTAMP WITH TIME ZONE If the argument passed to the function does not match one of these declared data types, an error is returned indicating that the function does not exist. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result The result is an INTEGER value between 1 to 31. Usage Notes The day_of_month function provides improved performance compared to using the Sys_Calendar.Calendar system view to obtain similar results. Syntax element… Specifies… expression an expression that results in a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value. 1101A726 SYSLIB. day_of_month (expression) Chapter 8: Calendar Functions day_of_month SQL Functions, Operators, Expressions, and Predicates 257 For more information about the CALENDAR system view, see Data Dictionary. Example If the current date is May 27, 2010, the following queries return the value 27 as the result since May 27, 2010 is the 27th day from the beginning of the month of May. SELECT SYSLIB.day_of_month(CURRENT_DATE); SELECT SYSLIB.day_of_month(DATE '2010-05-27'); Chapter 8: Calendar Functions day_of_year 258 SQL Functions, Operators, Expressions, and Predicates day_of_year Purpose Returns the number of days from the beginning of the year (January 1st) to the specified date. Syntax where: ANSI Compliance day_of_year is a Teradata extension to the ANSI SQL:2008 standard. Argument Types day_of_year is an overloaded scalar function. It is defined with the following parameter data types: • DATE • TIMESTAMP • TIMESTAMP WITH TIME ZONE If the argument passed to the function does not match one of these declared data types, an error is returned indicating that the function does not exist. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result The result is an INTEGER value between 1 to 366. Usage Notes The day_of_year function provides improved performance compared to using the Sys_Calendar.Calendar system view to obtain similar results. Syntax element… Specifies… expression an expression that results in a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value. 1101A727 SYSLIB. day_of_year (expression) Chapter 8: Calendar Functions day_of_year SQL Functions, Operators, Expressions, and Predicates 259 For more information about the CALENDAR system view, see Data Dictionary. Example If the current date is February 10, 2010, the following queries return the value 41 as the result since February 10, 2010 is the 41st day from the beginning of the year. SELECT SYSLIB.day_of_year(CURRENT_DATE); SELECT SYSLIB.day_of_year(DATE '2010-02-10'); Chapter 8: Calendar Functions day_of_calendar 260 SQL Functions, Operators, Expressions, and Predicates day_of_calendar Purpose Returns the number of days from the beginning of the calendar starting on 01/01/1900 to the specified date. Syntax where: ANSI Compliance day_of_calendar is a Teradata extension to the ANSI SQL:2008 standard. Argument Types day_of_calendar is an overloaded scalar function. It is defined with the following parameter data types: • DATE • TIMESTAMP • TIMESTAMP WITH TIME ZONE If the argument passed to the function does not match one of these declared data types, an error is returned indicating that the function does not exist. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result The result is an INTEGER value representing the number of days since and including 01/01/ 1900. Syntax element… Specifies… expression an expression that results in a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value. 1101A728 SYSLIB. day_of_calendar (expression) Chapter 8: Calendar Functions day_of_calendar SQL Functions, Operators, Expressions, and Predicates 261 Usage Notes The day_of_calendar function provides improved performance compared to using the Sys_Calendar.Calendar system view to obtain similar results. For more information about the CALENDAR system view, see Data Dictionary. Example If the current date is January 05, 1901, the following queries return the value 370 as the result since January 05, 1901 is the 370th day since January 01, 1900. SELECT SYSLIB.day_of_calendar(CURRENT_DATE); SELECT SYSLIB.day_of_calendar(DATE '1901-01-05'); Chapter 8: Calendar Functions weekday_of_month 262 SQL Functions, Operators, Expressions, and Predicates weekday_of_month Purpose Returns the nth occurrence of the weekday in the month for the specified date. Syntax where: ANSI Compliance weekday_of_month is a Teradata extension to the ANSI SQL:2008 standard. Argument Types weekday_of_month is an overloaded scalar function. It is defined with the following parameter data types: • DATE • TIMESTAMP • TIMESTAMP WITH TIME ZONE If the argument passed to the function does not match one of these declared data types, an error is returned indicating that the function does not exist. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result The result is an INTEGER value between 1 to 5, representing the nth occurrence of the weekday in the month. Syntax element… Specifies… expression an expression that results in a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value. 1101A729 SYSLIB. weekday_of_month (expression) Chapter 8: Calendar Functions weekday_of_month SQL Functions, Operators, Expressions, and Predicates 263 Usage Notes The weekday_of_month function provides improved performance compared to using the Sys_Calendar.Calendar system view to obtain similar results. For more information about the CALENDAR system view, see Data Dictionary. Example If the current date is May 01, 2010, the following queries return the value 1 as the result since May 01, 2010 falls on the first Saturday of the month. SELECT SYSLIB.weekday_of_month(CURRENT_DATE); SELECT SYSLIB.weekday_of_month(DATE '2010-05-01'); Chapter 8: Calendar Functions week_of_month 264 SQL Functions, Operators, Expressions, and Predicates week_of_month Purpose Returns the nth full week from the beginning of the month to the specified date. Syntax where: ANSI Compliance week_of_month is a Teradata extension to the ANSI SQL:2008 standard. Argument Types week_of_month is an overloaded scalar function. It is defined with the following parameter data types: • DATE • TIMESTAMP • TIMESTAMP WITH TIME ZONE If the argument passed to the function does not match one of these declared data types, an error is returned indicating that the function does not exist. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result The result is an INTEGER value between 0 to 5, representing the nth full week from the beginning of the month, where the first partial week is 0. Syntax element… Specifies… expression an expression that results in a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value. 1101A730 SYSLIB. week_of_month (expression) Chapter 8: Calendar Functions week_of_month SQL Functions, Operators, Expressions, and Predicates 265 Usage Notes The week_of_month function provides improved performance compared to using the Sys_Calendar.Calendar system view to obtain similar results. For more information about the CALENDAR system view, see Data Dictionary. Example If the current date is May 01, 2010, the following queries return the value 0 as the result since May 01, 2010 falls on the first partial week of May. SELECT SYSLIB.week_of_month(CURRENT_DATE); SELECT SYSLIB.week_of_month(DATE '2010-05-01'); Chapter 8: Calendar Functions week_of_year 266 SQL Functions, Operators, Expressions, and Predicates week_of_year Purpose Returns the nth full week from the beginning of the year (January 1st) to the specified date. Syntax where: ANSI Compliance week_of_year is a Teradata extension to the ANSI SQL:2008 standard. Argument Types week_of_year is an overloaded scalar function. It is defined with the following parameter data types: • DATE • TIMESTAMP • TIMESTAMP WITH TIME ZONE If the argument passed to the function does not match one of these declared data types, an error is returned indicating that the function does not exist. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result The result is an INTEGER value between 0 to 53, representing the nth full week from the beginning of the year, where the first partial week is 0. Syntax element… Specifies… expression an expression that results in a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value. 1101A731 SYSLIB. week_of_year (expression) Chapter 8: Calendar Functions week_of_year SQL Functions, Operators, Expressions, and Predicates 267 Usage Notes The week_of_year function provides improved performance compared to using the Sys_Calendar.Calendar system view to obtain similar results. For more information about the CALENDAR system view, see Data Dictionary. Example If the current date is May 04, 2010, the following queries return the value 18 as the result since May 04, 2010 falls on the 18th week of the year. SELECT SYSLIB.week_of_year(CURRENT_DATE); SELECT SYSLIB.week_of_year(DATE '2010-05-04'); Chapter 8: Calendar Functions week_of_calendar 268 SQL Functions, Operators, Expressions, and Predicates week_of_calendar Purpose Returns the number of weeks from the beginning of the calendar starting on 01/01/1900 to the specified date. Syntax where: ANSI Compliance week_of_calendar is a Teradata extension to the ANSI SQL:2008 standard. Argument Types week_of_calendar is an overloaded scalar function. It is defined with the following parameter data types: • DATE • TIMESTAMP • TIMESTAMP WITH TIME ZONE If the argument passed to the function does not match one of these declared data types, an error is returned indicating that the function does not exist. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result The result is an INTEGER value representing the number of full weeks since and including the week of 01/01/1900, where the first partial week is 0. Syntax element… Specifies… expression an expression that results in a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value. 1101A732 SYSLIB. week_of_calendar (expression) Chapter 8: Calendar Functions week_of_calendar SQL Functions, Operators, Expressions, and Predicates 269 Usage Notes The week_of_calendar function provides improved performance compared to using the Sys_Calendar.Calendar system view to obtain similar results. For more information about the CALENDAR system view, see Data Dictionary. Example If the current date is January 10, 1901, the following queries return the value 53 as the result since January 10, 1901 falls on the 53rd week since January 01, 1900. SELECT SYSLIB.week_of_calendar(CURRENT_DATE); SELECT SYSLIB.week_of_calendar(DATE '1901-01-10'); Chapter 8: Calendar Functions month_of_quarter 270 SQL Functions, Operators, Expressions, and Predicates month_of_quarter Purpose Returns the number of months from the beginning of the quarter to the specified date. Syntax where: ANSI Compliance month_of_quarter is a Teradata extension to the ANSI SQL:2008 standard. Argument Types month_of_quarter is an overloaded scalar function. It is defined with the following parameter data types: • DATE • TIMESTAMP • TIMESTAMP WITH TIME ZONE If the argument passed to the function does not match one of these declared data types, an error is returned indicating that the function does not exist. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result The result is an INTEGER value between 1 to 3. Usage Notes The month_of_quarter function provides improved performance compared to using the Sys_Calendar.Calendar system view to obtain similar results. Syntax element… Specifies… expression an expression that results in a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value. 1101A733 SYSLIB. month_of_quarter (expression) Chapter 8: Calendar Functions month_of_quarter SQL Functions, Operators, Expressions, and Predicates 271 For more information about the CALENDAR system view, see Data Dictionary. Example If the current date is June 12, 2010, the following queries return the value 3 as the result because June 12, 2010 falls on the 3rd month of the 2nd quarter. SELECT SYSLIB.month_of_quarter(CURRENT_DATE); SELECT SYSLIB.month_of_quarter(DATE '2010-06-12'); Chapter 8: Calendar Functions month_of_year 272 SQL Functions, Operators, Expressions, and Predicates month_of_year Purpose Returns the number of months from the beginning of the year (January 1st) to the specified date. Syntax where: ANSI Compliance month_of_year is a Teradata extension to the ANSI SQL:2008 standard. Argument Types month_of_year is an overloaded scalar function. It is defined with the following parameter data types: • DATE • TIMESTAMP • TIMESTAMP WITH TIME ZONE If the argument passed to the function does not match one of these declared data types, an error is returned indicating that the function does not exist. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result The result is an INTEGER value between 1 to 12. Syntax element… Specifies… expression an expression that results in a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value. 1101A734 SYSLIB. month_of_year (expression) Chapter 8: Calendar Functions month_of_year SQL Functions, Operators, Expressions, and Predicates 273 Usage Notes The month_of_year function provides improved performance compared to using the Sys_Calendar.Calendar system view to obtain similar results. For more information about the CALENDAR system view, see Data Dictionary. Example If the current date is August 29, 2010, the following queries return the value 8 as the result because August 29, 2010 falls on the 8th month of the year. SELECT SYSLIB.month_of_year(CURRENT_DATE); SELECT SYSLIB.month_of_year(DATE '2010-08-29'); Chapter 8: Calendar Functions month_of_calendar 274 SQL Functions, Operators, Expressions, and Predicates month_of_calendar Purpose Returns the number of months from the beginning of the calendar starting on 01/01/1900 to the specified date. Syntax where: ANSI Compliance month_of_calendar is a Teradata extension to the ANSI SQL:2008 standard. Argument Types month_of_calendar is an overloaded scalar function. It is defined with the following parameter data types: • DATE • TIMESTAMP • TIMESTAMP WITH TIME ZONE If the argument passed to the function does not match one of these declared data types, an error is returned indicating that the function does not exist. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result The result is an INTEGER value representing the number of months since and including January, 1900. Syntax element… Specifies… expression an expression that results in a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value. 1101A735 SYSLIB. month_of_calendar (expression) Chapter 8: Calendar Functions month_of_calendar SQL Functions, Operators, Expressions, and Predicates 275 Usage Notes The month_of_calendar function provides improved performance compared to using the Sys_Calendar.Calendar system view to obtain similar results. For more information about the CALENDAR system view, see Data Dictionary. Example If the current date is August 29, 1901, the following queries return the value 20 as the result since August 29, 1901 falls on the 20th month since January 01, 1900. SELECT SYSLIB.month_of_calendar(CURRENT_DATE); SELECT SYSLIB.month_of_calendar(DATE '1901-08-29'); Chapter 8: Calendar Functions quarter_of_year 276 SQL Functions, Operators, Expressions, and Predicates quarter_of_year Purpose Returns the quarter number of the year for the specified date. Syntax where: ANSI Compliance quarter_of_year is a Teradata extension to the ANSI SQL:2008 standard. Argument Types quarter_of_year is an overloaded scalar function. It is defined with the following parameter data types: • DATE • TIMESTAMP • TIMESTAMP WITH TIME ZONE If the argument passed to the function does not match one of these declared data types, an error is returned indicating that the function does not exist. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result The result is an INTEGER value between 1 to 4, representing the quarter number from the beginning of the year, where 1 = first quarter (Jan/Feb/Mar) and 4 = fourth quarter (Oct/Nov/ Dec). Syntax element… Specifies… expression an expression that results in a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value. 1101A736 SYSLIB. quarter_of_year (expression) Chapter 8: Calendar Functions quarter_of_year SQL Functions, Operators, Expressions, and Predicates 277 Usage Notes The quarter_of_year function provides improved performance compared to using the Sys_Calendar.Calendar system view to obtain similar results. For more information about the CALENDAR system view, see Data Dictionary. Example If the current date is November 14, 1983, the following queries return the value 4 as the result since November 14, 1983 falls on the 4th quarter of the year. SELECT SYSLIB.quarter_of_year(CURRENT_DATE); SELECT SYSLIB.quarter_of_year(DATE '1983-11-14'); Chapter 8: Calendar Functions quarter_of_calendar 278 SQL Functions, Operators, Expressions, and Predicates quarter_of_calendar Purpose Returns the number of quarters from the beginning of the calendar starting on 01/01/1900 to the specified date. Syntax where: ANSI Compliance quarter_of_calendar is a Teradata extension to the ANSI SQL:2008 standard. Argument Types quarter_of_calendar is an overloaded scalar function. It is defined with the following parameter data types: • DATE • TIMESTAMP • TIMESTAMP WITH TIME ZONE If the argument passed to the function does not match one of these declared data types, an error is returned indicating that the function does not exist. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result The result is an INTEGER value representing the number of quarters since and including the first quarter of 1900. Syntax element… Specifies… expression an expression that results in a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value. 1101A737 SYSLIB. quarter_of_calendar (expression) Chapter 8: Calendar Functions quarter_of_calendar SQL Functions, Operators, Expressions, and Predicates 279 Usage Notes The quarter_of_calendar function provides improved performance compared to using the Sys_Calendar.Calendar system view to obtain similar results. For more information about the CALENDAR system view, see Data Dictionary. Example If the current date is November 14, 1901, the following queries return the value 8 as the result since November 14, 1901 falls on the 8th quarter since January 01, 1900. SELECT SYSLIB.quarter_of_calendar(CURRENT_DATE); SELECT SYSLIB.quarter_of_calendar(DATE '1901-11-14'); Chapter 8: Calendar Functions year_of_calendar 280 SQL Functions, Operators, Expressions, and Predicates year_of_calendar Purpose Returns the year of the specified date. Syntax where: ANSI Compliance year_of_calendar is a Teradata extension to the ANSI SQL:2008 standard. Argument Types year_of_calendar is an overloaded scalar function. It is defined with the following parameter data types: • DATE • TIMESTAMP • TIMESTAMP WITH TIME ZONE If the argument passed to the function does not match one of these declared data types, an error is returned indicating that the function does not exist. For more information on overloaded functions, see “Function Name Overloading” in SQL External Routine Programming. Result The result is an INTEGER value in 4 digit format representing the year of the specified date. Usage Notes The year_of_calendar function provides improved performance compared to using the Sys_Calendar.Calendar system view to obtain similar results. Syntax element… Specifies… expression an expression that results in a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value. 1101A738 SYSLIB. year_of_calendar (expression) Chapter 8: Calendar Functions year_of_calendar SQL Functions, Operators, Expressions, and Predicates 281 For more information about the CALENDAR system view, see Data Dictionary. Example If the current date is November 14, 1977, the following queries return the value 1977 as the result, which is the year of the specified date. SELECT SYSLIB.year_of_calendar(CURRENT_DATE); SELECT SYSLIB.year_of_calendar(DATE '1977-11-14'); Chapter 8: Calendar Functions year_of_calendar 282 SQL Functions, Operators, Expressions, and Predicates SQL Functions, Operators, Expressions, and Predicates 283 CHAPTER 9 Period Functions and Operators This chapter describes the Period functions and operators. Chapter 9: Period Functions and Operators Period Value Constructor 284 SQL Functions, Operators, Expressions, and Predicates Period Value Constructor Purpose Initializes an instance of a Period data type. Syntax where: Result Value The following rules apply to the result value: • If the beginning or ending bound is NULL, or both the bounds are NULL, the result is NULL. Syntax element ... Specifies ... datetime_expression any expression that evaluates to a DATE, TIME, or TIMESTAMP data type. UNTIL_CHANGED a DATE or TIMESTAMP value that is considered to be forever or until it is changed. For PERIOD(DATE) types, UNTIL_CHANGED has a value of DATE '9999-12-31' and for PERIOD(TIMESTAMP[(n)] [WITH TIME ZONE]) types, UNTIL_CHANGED has a value of TIMESTAMP '9999-12-31 23:59:59.999999 00:00'(with the precision truncated to the precision of the beginning bound and the time zone omitted if the beginning bound does not have a time zone). UNTIL_CLOSED an ending bound for the Period value of a temporal table transactiontime column that indicates that the row is an open row. UNTIL_CLOSED has a data type of TIMESTAMP(6) WITH TIME ZONE and a value of TIMESTAMP '9999-12- 31 23:59:59:999999+00:00'. For more information about temporal tables, see Temporal Table Support. PERIOD (datetime_expression) 1182A015 PERIOD (datetime_expression, datetime_expression) PERIOD (datetime_expression, UNTIL_CHANGED) PERIOD (datetime_expression, UNTIL_CLOSED) Chapter 9: Period Functions and Operators Period Value Constructor SQL Functions, Operators, Expressions, and Predicates 285 • If the beginning and ending bounds are NULL or if the beginning bound is NULL and the ending bound is UNTIL_CHANGED, then the type of the period defaults to PERIOD(TIMESTAMP(0)). • If only the beginning bound is specified, the result ending bound is the beginning bound plus one granule of the result element type. If the result ending bound exceeds or becomes equal to the maximum allowed DATE or TIMESTAMP value for result data type of PERIOD(DATE) or PERIOD(TIMESTAMP(n) [WITH TIME ZONE]), respectively, an error is reported. • If an ending bound is specified as a value expression and the beginning bound and ending bound have different precisions, the result precision is the higher of the two precisions. Otherwise, the result precision is the precision of the beginning bound. • UNTIL_CHANGED sets the result ending element to a maximum DATE or TIMESTAMP value depending on the data type of the beginning bound. If the data type of the beginning bound is TIMESTAMP(n) WITH TIME ZONE, the result ending element is set to the maximum TIMESTAMP(n) WITH TIME ZONE value at UTC (that is, the time zone displacement for the ending bound is INTERVAL '00:00' HOUR TO MINUTE). • If the beginning bound or the ending bound or the beginning and ending bounds include a time zone value, and the ending bound is not UNTIL_CHANGED, the result data type is WITH TIME ZONE. If only one of the bounds includes a time zone value, the time zone field of the other is set to the current session time zone displacement. If both bounds include time zone values, the result bounds include the corresponding time zone value. • The result Period data type has an element type that is the same as the DateTime data type of the beginning bound except with the precision and time zone as defined previously. • The handling of leap seconds for Period data types with TIME and TIMESTAMP element types is as follows. If the value for the beginning or ending bound contains leap seconds, the seconds portion gets adjusted to 59.999999 with the precision truncated to the result precision. During this process, if the beginning and ending bounds are the same, an error is reported. Usage Rules The following rules apply to the Period value constructor: • The beginning bound must have a DateTime data type and, if an ending bound is specified, the data types of the beginning and ending bounds must be comparable. • The ending bound where the data type of the beginning bound is DATE or TIMESTAMP can be set to UNTIL_CHANGED. • If the ending bound is set to UNTIL_CLOSED, the following must be true: • The data type of the beginning bound value must be comparable with TIMESTAMP(6) WITH TIME ZONE. • The constructor is only valid in an assignment operation where the target column to which the result is assigned is a transaction-time column. • Because the only way to set the value of a transaction-time column is by using nontemporal DML, the constructor is only valid in a nontemporal DML statement. Chapter 9: Period Functions and Operators Period Value Constructor 286 SQL Functions, Operators, Expressions, and Predicates • Teradata Database reports an error if any of the following are true: • UNTIL_CHANGED is specified for the beginning bound. • The result beginning bound is greater than or equal to the result ending bound. • The data types of the beginning and ending bounds are not comparable. • UNTIL_CHANGED is specified for the ending bound and the data type of the beginning bound is TIME(n) [WITH TIME ZONE]. • UNTIL_CLOSED is specified for the beginning bound. Example In the following example, assume t1 is a table with an INTEGER column c1 and a PERIOD(DATE) column c2 and t2 is a table with an INTEGER column a and two DATE columns b and c. This example shows the Period value constructor used in two INSERT statements. INSERT INTO t1 VALUES (1, PERIOD(DATE '2005-02-03', DATE '2006-02-04')); INSERT INTO t1 SELECT a, PERIOD(b, c) FROM t2; Chapter 9: Period Functions and Operators Arithmetic Operators SQL Functions, Operators, Expressions, and Predicates 287 Arithmetic Operators Purpose Adds or subtracts an Interval value to or from a Period value, or adds a Period value to an Interval value. Syntax where: Usage Notes Assuming that p is a Period expression of element type DATE or TIMESTAMP and v is an Interval value expression: • p + v and v + p are both equivalent to: PERIOD(BEGIN(p) + v, CASE WHEN END(p) IS UNTIL_CHANGED THEN END(p) ELSE (END(p) + v) END) • p - v is equivalent to: PERIOD(BEGIN (p) - v, CASE WHEN END(p) IS UNTIL_CHANGED THEN END(p) ELSE (END(p) - v) END) Assuming that p is a Period expression of element type TIME and v is an interval value expression: • p + v and v + p are both equivalent to: PERIOD(BEGIN(p) + v, END(p) + v) • p - v is equivalent to: PERIOD(BEGIN (p) - v, END(p) - v) Usage Rules The following rules apply to arithmetic operators and Period data types: Syntax element ... Specifies ... period_expression any expression that evaluates to a Period data type. interval_expression an expression that evaluates to an INTERVAL data type. For information on INTERVAL data types, see SQL Data Types and Literals. period_expression + interval_expression _ 1101A586 interval_expression + period_expression _ Chapter 9: Period Functions and Operators Arithmetic Operators 288 SQL Functions, Operators, Expressions, and Predicates • The interval value expression must be a valid interval expression and must follow the rules of an Interval expression (see “ANSI Interval Expressions” on page 222). Otherwise, an error is reported. For example, the interval expression (DATE '2006-02-03' - DATE '2005- 02-03') DAY, results in a value of 365 days which cannot fit into the default precision 2 of the interval qualifier DAY; therefore, an error is reported. • The period arithmetic operations of adding or subtracting an Interval to or from a period or adding a period to an Interval follow the rules of DateTime expressions. Otherwise, errors are reported. See “ANSI DateTime Expressions” on page 213 for details on DateTime expression rules. • An interval value expression can be subtracted from a Period expression but not vice versa. If a period expression is subtracted from an interval value expression, an error is reported. • For a Period expression with an element type of TIME, if the Period arithmetic operation results in a beginning bound less than the ending bound, an error is reported. • For a period of element type DATE or TIMESTAMP, if the ending bound is UNTIL_CHANGED, the ending bound in the result ending bound is UNTIL_CHANGED. If the ending bound is not UNTIL_CHANGED and the ending bound in the result evaluates to an UNTIL_CHANGED value, an error is reported. • For a period arithmetic operation, one of the operands must be an INTERVAL data type. Otherwise, an error is reported. Chapter 9: Period Functions and Operators Comparison of Period Types SQL Functions, Operators, Expressions, and Predicates 289 Comparison of Period Types Two Period values are comparable if their element types are of same DateTime data type. The DateTime data types are DATE, TIME and TIMESTAMP. The PERIOD(DATE) date type is comparable with the PERIOD(DATE) data type, a PERIOD(TIME(n)[WITH TIME ZONE]) data type is comparable with a PERIOD(TIME(m)[WITH TIME ZONE]) data type, and a PERIOD(TIMESTAMP(n)[WITH TIME ZONE]) data type is comparable with a PERIOD(TIMESTAMP(m)[WITH TIME ZONE]) data type. Teradata extends this to allow a CHARACTER and VARCHAR value to be implicitly cast as a Period data type for some operators and, therefore, have a Period data type. Since the Period data type is the data type of the other Period value expression, these Period value expressions will be comparable. DateTime and Period data are saved internally with the maximum precision of 6 although the specified precision may be less than this and is padded with zeroes. Thus, the comparison operations with differing precisions work without any additional logic. Additionally, the internal value is saved in UTC for a Time or Timestamp value, or for a Period value with an element type of TIME or TIMESTAMP. All comparable Period value expressions can be compared directly due to this internal representation irrespective of whether they contain a time zone value, or whether they have the same precision. Note: The time zone values are ignored when comparing values. All comparison operations involving UNTIL_CLOSED in a temporal table transaction-time column use the internal value of UNTIL_CLOSED (TIMESTAMP '9999-12- 31 23:59:59:999999+00:00') to evaluate the result. For more information abut temporal tables, see Temporal Table Support. The following table describes the comparison operators. Operator Purpose EQ or = Assume p1 and p2 are Period value expressions and have comparable Period data types. If BEGIN(p1) = BEGIN(p2) AND END(p1) = END(p2), the result of the comparison is TRUE; otherwise, the result is FALSE. If either Period value expression is NULL, the result is UNKNOWN. If the Period value expressions have different element types, one of them must be explicitly CAST as the other. If one Period value expression has a Period data type and the other Period value expression has CHARACTER or VARCHAR data type, the CHARACTER or VARCHAR expression is implicitly converted, before comparison, to the data type of the Period value expression based on the format of the Period value expression. Chapter 9: Period Functions and Operators Comparison of Period Types 290 SQL Functions, Operators, Expressions, and Predicates LT or < Assume p1 and p2 are Period value expressions and have comparable Period data types. If BEGIN(p1) < BEGIN(p2) OR (BEGIN(p1) = BEGIN(p2) AND END(p1) < END(p2)), the result of the comparison is TRUE; otherwise, the result is FALSE. If either Period value expression is NULL, the result is UNKNOWN. If the Period value expressions have different element types, one of them must be explicitly CAST as the other. If one Period value expression has a Period data type and the other Period value expression has CHARACTER or VARCHAR data type, the CHARACTER or VARCHAR operand is implicitly converted, before comparison, to the data type of the Period value expression based on the format of the Period value expression. If the ending bound value of a temporal table transaction-time column is UNTIL_CLOSED, the ending bound value is only less than a TIMESTAMP column value or TIMESTAMP literal if the column value or literal is the maximum TIMESTAMP value with leap seconds. This can be possible only if the ending bound of the transaction-time column is used in a comparison with the timestamp value. For more information about temporal tables, see Temporal Table Support. GT or > Assume p1 and p2 are Period value expressions and have comparable Period data types. If BEGIN(p1) > BEGIN(p2) OR (BEGIN(p1) = BEGIN(p2) AND END(p1) > END(p2)), the result of the comparison is TRUE; otherwise, it is FALSE. If either Period expression is NULL, the result is UNKNOWN. If one Period expression has a Period data type and the other Period expression has CHARACTER or VARCHAR data type, the CHARACTER or VARCHAR Period value expression is implicitly converted, before comparison, to the data type of the Period value expression based on the format of the Period value expression. NE or <> or NOT= or ^= or LE or <= or GE or >= These comparison operators are supported for comparable Period value expressions. Also, if one Period value expression has a Period data type and the other Period value expression has CHARACTER or VARCHAR data type, the CHARACTER or VARCHAR Period value expression is implicitly converted, before comparison, to the data type of the Period value expression based on the format of the Period value expression. Their behavior should be easily understandable from a reading of the previous operators. Note: NE, NOT=, ^=, GT, GE, LT, and LE are non-ANSI operators. Operator Purpose Chapter 9: Period Functions and Operators BEGIN SQL Functions, Operators, Expressions, and Predicates 291 BEGIN Purpose Bound function that returns the beginning bound of the Period argument. Syntax where: Return Value The result data type of the BEGIN function is same as the element type of the Period value expression. If the argument is NULL, the result is NULL. Format and Title The format is the default format for the element type of the Period value expression. The title is BEGIN(period_value_expression). Error Conditions If the argument does not have a Period data type, an error is reported. Example In the following example, BEGIN is used in the WHERE clause. SELECT * FROM employee WHERE BEGIN(period1) = DATE '2004-06-19'; Assume the query is executed on the following table employee where period1 is a PERIOD(DATE) column: ename dept period1 ----- ----------- ---------------------------- Jones Sales ('2004-01-02', '2004-01-05') Adams Marketing ('2004-06-19', '2005-02-09') Mary Development ('2004-06-19', '2005-01-05') Simon Sales ('2004-06-22', '2005-01-07') Syntax element ... Specifies ... period_value_expression any expression that evaluates to a Period data type. BEGIN(period_value_expression) 1101A595 Chapter 9: Period Functions and Operators BEGIN 292 SQL Functions, Operators, Expressions, and Predicates The result is as follows: ename dept period1 ----- ----------- ---------------------------- Adams Marketing ('2004-06-19', '2005-02-09') Mary Development ('2004-06-19', '2005-01-05') Chapter 9: Period Functions and Operators CONTAINS SQL Functions, Operators, Expressions, and Predicates 293 CONTAINS Purpose Predicate that operates on two Period expressions or one Period expression and one DateTime expression and evaluates to TRUE, FALSE, or UNKNOWN. If both expressions have a Period data type, returns TRUE if the beginning bound of the first expression is less than or equal to the beginning bound of the second expression and the ending bound of the first expression is greater than or equal to the ending bound of the second expression; otherwise, returns FALSE. If the first expression is a Period expression and the second expression is a DateTime expression, returns TRUE if the beginning bound of the Period expression is less than or equal to the DateTime expression and the ending bound of the Period expression is greater than the DateTime expression; otherwise, returns FALSE. If the first expression is a DateTime expression and the second expression is a Period expression, returns TRUE if the DateTime expression is less than or equal to beginning bound of the Period expression and the DateTime expression plus one granule is greater than or equal to the ending bound of the Period expression; otherwise, returns FALSE. If either expression is NULL, the operator returns UNKNOWN. Syntax where: Error Conditions If either expression evaluates to a data type that is other than a Period or DateTime, an error is reported. Syntax element... Specifies... datetime_expression any expression that evaluates to a DATE, TIME, or TIMESTAMP data type. period_expression any expression that evaluates to a Period data type. Note: The Period expression specified must be comparable with the other expression. Implicit casting to a Period data type is not supported. period_expression period_expression datetime_expression CONTAINS NOT 1101A582 datetime_expression CONTAINS period_expression NOT Chapter 9: Period Functions and Operators CONTAINS 294 SQL Functions, Operators, Expressions, and Predicates If the expressions do not have comparable data types, an error is reported. Example In the following example, the CONTAINS operator is used in the WHERE clause. SELECT * FROM employee WHERE period2 CONTAINS period1; Assume the query is executed on the following table employee where period1 and period2 are PERIOD(DATE) columns: The result is as follows: ename period1 period2 Adams ('2005-02-03', '2006-02-03') ('2005-02-03', '2006-02-03') Mary ('2005-04-02', '2006-01-03') ('2005-02-03', '2006-02-03') Jones ('2004-01-02', '2004-03-05') ('2004-03-05', '2004-10-07') Randy ('2004-01-02', '2004-03-05') ('2004-03-07', '2004-10-07') Simon ? ('2005-02-03', '2005-07-27') ename period1 period2 Adams ('2005-02-03', '2006-02-03') ('2005-02-03', '2006-02-03') Mary ('2005-04-02', '2006-01-03') ('2005-02-03', '2006-02-03') Chapter 9: Period Functions and Operators END SQL Functions, Operators, Expressions, and Predicates 295 END Purpose Bound function that returns the ending bound of the Period argument. Syntax where: Return Value The result type of the END function is same as the element type of the Period value expression. If the argument is NULL, the result is NULL. Format and Title The format is the default format for the element type of the Period value expression. The title is END(period_value_expression). Error Conditions If an argument of any data type other than a Period data type is passed, an error is reported. Example In the following example, END is used in the WHERE clause. SELECT * FROM employee WHERE END(period1) = DATE '2005-01-07'; Assume the query is executed on the following table employee with PERIOD(DATE) column period1: ename dept period1 ----- ----------- ---------------------------- Jones Sales ('2004-01-02', '2004-01-05') Adams Marketing ('2004-06-19', '2005-02-09') Mary Development ('2004-06-19', '2005-01-05') Simon Sales ('2004-06-22', '2005-01-07') Syntax element ... Specifies ... period_value_expression any expression that evaluates to a Period data type. END(period_value_expression) 1101A596 Chapter 9: Period Functions and Operators END 296 SQL Functions, Operators, Expressions, and Predicates The result is as follows: ename dept period1 ----- ----------- ---------------------------- Simon Sales ('2004-06-22', '2005-01-07') Chapter 9: Period Functions and Operators IS UNTIL_CHANGED/IS NOT UNTIL_CHANGED SQL Functions, Operators, Expressions, and Predicates 297 IS UNTIL_CHANGED/IS NOT UNTIL_CHANGED Purpose Predicate that tests whether the ending bound of a Period value expression is (or is not) UNTIL_CHANGED. Syntax where: Usage Notes You can only compare UNTIL_CHANGED to the ending bound of a Period value with an element type of DATE or TIMESTAMP [WITH TIME ZONE]. Therefore, the result type of the END function must be DATE or TIMESTAMP [WITH TIME ZONE]. For information about the END function, see “END” on page 295. In comparisons, the precision of the UNTIL_CHANGED value is truncated to the precision of the ending bound value being compared. That is, the number of digits after the decimal point for UNTIL_CHANGED depends upon the precision of the ending bound to which it is compared. The time zone is omitted if the ending bound value has no time zone. If the ending bound value is NULL, IS [NOT] UNTIL_CHANGED returns UNKNOWN. You cannot use IS [NOT] UNTIL_CHANGED on the ending bound of a transaction-time column. Example Consider the following employee table, where the column eduration is defined as a PERIOD(DATE) type: ename eid eduration ---------- ----------- ------------------------ Adams 210677 ('05/03/01', '06/05/21') Gunther 199347 ('04/06/06', '99/12/31') Syntax element … Specifies … period_value_expression any expression that evaluates to a PERIOD(TIMESTAMP WITH TIME ZONE), PERIOD(TIMESTAMP), or PERIOD(DATE) type. END ( period_value_expression ) IS UNTIL_CHANGED NOT 1101A639 Chapter 9: Period Functions and Operators IS UNTIL_CHANGED/IS NOT UNTIL_CHANGED 298 SQL Functions, Operators, Expressions, and Predicates Montoya 199340 ('04/06/02', '99/12/31') Chan 210427 ('04/09/24', '99/12/31') Fuller 197899 ('03/05/27', '03/11/30') The following query uses IS UNTIL_CHANGED to compare the ending bound value of the eduration column to UNTIL_CHANGED: SELECT ename, eid FROM employee WHERE END(eduration) IS UNTIL_CHANGED; The result is the following: ename eid ---------- ----------- Gunther 199347 Montoya 199340 Chan 210427 Chapter 9: Period Functions and Operators IS UNTIL_CLOSED/IS NOT UNTIL_CLOSED SQL Functions, Operators, Expressions, and Predicates 299 IS UNTIL_CLOSED/IS NOT UNTIL_CLOSED Purpose Predicate that tests the ending bound value of a temporal table transaction-time column to see whether the row is open (the ending bound value is UNTIL_CLOSED) or closed (the ending bound value is not UNTIL_CLOSED). For more information about temporal tables, see Temporal Table Support. Syntax where: Usage Notes When a row is created in a temporal table that has a transaction-time dimension (column), Teradata Database sets the ending bound of the column to UNTIL_CLOSED and the row is considered open. When the row is closed, Teradata Database sets the ending bound value to the closing timestamp. Use IS [NOT] UNTIL_CLOSED to test whether a row in a temporal table that has transaction time is open or closed. IS UNTIL_CLOSED evaluates to true if the ending bound of the specified transaction-time column is the maximum timestamp value, 9999-12-31 23:59:59.999999+00:00. Syntax element … Specifies … period_value_expression a reference to a transaction-time column. 1182A013 END ( period_value_expression ) IS UNTIL_CLOSED NOT Chapter 9: Period Functions and Operators INTERVAL 300 SQL Functions, Operators, Expressions, and Predicates INTERVAL Purpose Finds the difference between the ending and beginning bounds of a Period argument and returns this difference as the duration of the argument in terms of a specified interval qualifier. Syntax where: Return Value The result type is the interval data type corresponding to the specified interval qualifier. The result of the INTERVAL (p) IQ function is the value of (END(p) - BEGIN(p)) IQ, where argument p is a Period expression and IQ is an interval qualifier. The function finds the difference between the argument's ending bound and the beginning bound and returns the resulting difference as an interval value based on the specified interval qualifier. Syntax element ... Specifies ... period_expression any expression that evaluates to a Period data type. Note: Implicit casting to a Period data type is not supported. interval_qualifier any interval qualifier appropriate for the argument's element type. The interval qualifiers are as follows: Year-Month intervals: • YEAR • YEAR TO MONTH • MONTH Day-Time Intervals: • DAY • DAY TO HOUR, MINUTE or SECOND • HOUR • HOUR TO MINUTE or SECOND • MINUTE • MINUTE to SECOND • SECOND INTERVAL (period_expression) interval_qualifier 1101A577 Chapter 9: Period Functions and Operators INTERVAL SQL Functions, Operators, Expressions, and Predicates 301 If the argument is NULL, the result is NULL. Format and Title The format is the default format for the interval data type corresponding to the specified interval qualifier. The title is INTERVAL(period _expression) interval_qualifier. Error Conditions An error may be reported: • If the argument of the INTERVAL function does not have a Period data type. • If the argument has a PERIOD(DATE) data type and the interval qualifier is not YEAR, YEAR TO MONTH, MONTH, or DAY. • If the argument has a PERIOD(TIME(n) [WITH TIME ZONE]) data type and the interval qualifier is not HOUR, HOUR TO MINUTE, HOUR TO SECOND, MINUTE, MINUTE TO SECOND or SECOND. • If the result of an INTERVAL expression violates the rules specified for the precision of an interval qualifier, an existing error is reported. For example, assume p1 is a PERIOD(TIMESTAMP(0)) expression that has a value of PERIOD '(2006-01-01 12:12:12, 2007-01-01 12:12:12)'. If INTERVAL(p1) DAY is specified, the default precision for the DAY interval qualifier is 2, and, since the result is 365 days which is a three digit value that cannot fit into a DAY(2) interval qualifier, an error is reported. • If the argument of the INTERVAL function is a period of element type DATE or TIMESTAMP(n) [WITH TIME ZONE] and the ending bound value is UNTIL_CHANGED. Example In the following example, INTERVAL is used in a selection list. SELECT INTERVAL (period1) MONTH FROM employee; Assume the query is executed on the following table employee with PERIOD(DATE) column period1: ename dept period1 ----- ----------- ---------------------------- Jones Sales ('2004-01-02', '2004-03-05') The result is as follows: INTERVAL(eduration) MONTH ------------------------- 2 Chapter 9: Period Functions and Operators LAST 302 SQL Functions, Operators, Expressions, and Predicates LAST Purpose Bound function that returns the last value of the Period argument (that is, the ending bound minus one granule of the element type of the argument). Syntax where: Return Value The result type of the LAST function is same as the element type of the Period value expression. If the argument is NULL, the result is NULL. Format and Title The format is the default format for the element type of the Period value expression. The title is LAST(period_value_expression). Error Conditions If an argument has a data type other than a Period data type, an error is reported. Example In the following example, LAST is used in the WHERE clause. SELECT * FROM employee WHERE LAST(period1) = DATE '2004-01-04'; Assume the query is executed on the following table employee with PERIOD(DATE) column period1: ename dept period1 ----- ----------- ---------------------------- Jones Sales ('2004-01-02', '2004-01-05') Adams Marketing ('2004-06-19', '2005-02-09') Mary Development ('2004-06-19', '2005-01-05') Simon Sales ('2004-06-22', '2005-01-07') Syntax element ... Specifies ... period_value_expression any expression that evaluates to a Period data type. LAST(period_value_expression) 1101A597 Chapter 9: Period Functions and Operators LAST SQL Functions, Operators, Expressions, and Predicates 303 The result is as follows: ename dept period1 ----- ----------- ---------------------------- Jones Sales ('2004-01-02', '2004-01-05') Chapter 9: Period Functions and Operators MEETS 304 SQL Functions, Operators, Expressions, and Predicates MEETS Purpose Predicate that operates on two Period expressions or one Period expression and one DateTime expression and evaluates to TRUE, FALSE, or UNKNOWN. If both expressions have a Period data type, returns TRUE if the ending bound of the first expression is equal to the beginning bound of the expression or the ending bound of the second expression is equal to the beginning bound of the first expression; otherwise, returns FALSE. If one expression is a Period expression and the other expression is a DateTime expression, returns TRUE if the ending bound of the Period expression is equal to the DateTime expression or if the DateTime expression plus one granule is equal to the beginning bound of the Period expression; otherwise, returns FALSE. If either expression is NULL, the operator returns UNKNOWN. Syntax where: Error Conditions If either expression evaluates to a data type other than a Period or DateTime, an error is reported. If the expressions are not comparable, an error is reported. Example In the following example, the MEETS operator is used in the WHERE clause. SELECT * FROM employee WHERE period2 MEETS period1; Syntax element... Specifies... datetime_expression any expression that evaluates to a DATE, TIME, or TIMESTAMP data type. period_expression any expression that evaluates to a Period data type. Note: The Period expression specified must be comparable with the other expression. Implicit casting to a Period data type is not supported. period_expression period_expression datetime_expression MEETS NOT 1101A581 datetime_expression MEETS period_expression NOT Chapter 9: Period Functions and Operators MEETS SQL Functions, Operators, Expressions, and Predicates 305 Assume the query is executed on the following table employee where period1 and period2 are PERIOD(DATE) columns: The result is as follows: ename period1 period2 Adams ('2005-02-03', '2006-02-03') ('2005-02-03', '2006-02-03') Mary ('2005-04-02', '2006-01-03') ('2005-02-03', '2006-02-03') Jones ('2004-01-02', '2004-03-05') ('2004-03-05', '2004-10-07') Randy ('2004-01-02', '2004-03-05') ('2004-03-07', '2004-10-07') Simon ? ('2005-02-03', '2005-07-27') ename period1 period2 Jones ('2004-01-02','2004-03-05') ('2004-03-05', '2004-10-07') Chapter 9: Period Functions and Operators NEXT 306 SQL Functions, Operators, Expressions, and Predicates NEXT Purpose Proximity function that returns the succeeding value of the argument such that there is one granule of the argument type between the argument and the returned value. Syntax where: Return Value The return data type is the same as that of the argument (that is, a DateTime data type). If the value of the argument is NULL, the result is NULL. Format and Title The format is the default format for the proximity argument's data type. The title is NEXT(datetime_expression). Error Conditions If the argument does not have a DateTime data type, an error is reported. If the result is outside the permissible range of a value for the argument's data type, an error is reported. For example, if NEXT(DATE '9999-12-31') is specified, an error is reported. Example In the following example, NEXT is used in the WHERE clause. SELECT * FROM employee WHERE NEXT(END(period1)) = DATE '2004-03-06'; Assume the query is executed on the following table employee where period1 is a PERIOD(DATE) column: Syntax element ... Specifies ... datetime_expression any expression that evaluates to a DATE, TIME, or TIMESTAMP data type. NEXT (datetime_expression) 1101A579 Chapter 9: Period Functions and Operators NEXT SQL Functions, Operators, Expressions, and Predicates 307 ename dept period1 ----- ----------- ---------------------------- Jones Sales ('2004-01-02', '2004-03-05') Simon Sales ? The result is as follows: ename dept period1 ----- ----------- ---------------------------- Jones Sales ('2004-01-02', '2004-03-05') Chapter 9: Period Functions and Operators OVERLAPS 308 SQL Functions, Operators, Expressions, and Predicates OVERLAPS Purpose Predicate that tests whether two time periods overlap one another. Syntax where: ANSI Compliance OVERLAPS is ANSI SQL:2008 compliant. Time Periods Each time period to the left and right of the OVERLAPS keyword is one of the following expression types: • DateTime, DateTime • DateTime, Interval • Row subquery • Period Each time period represents a start and end DateTime, using an explicit Period value, DateTime values or a DateTime and an Interval. 1101A612 ( datetime_expression, datetime_expression OVERLAPS datetime_expression, datetime_expression datetime_expression, interval_expression ) ( ) period_expression period_expression row_subquery datetime_expression, interval_expression row_subquery Syntax element … Specifies … datetime_expression a start and end DateTime. interval_expression an end DateTime. row_subquery an element of a row subquery in a SELECT statement. The subquery cannot specify a SELECT AND CONSUME statement. period_expression any expression that evaluates to a Period data type. Chapter 9: Period Functions and Operators OVERLAPS SQL Functions, Operators, Expressions, and Predicates 309 If the start and end DateTime values in a time period are not ordered chronologically, they are manipulated to make them so prior to making the comparison, using the rule that end_DateTime >= start_DateTime for all cases. If a time period contains a null start_DateTime and a non-null end_DateTime, then the values are switched to indicate a non-null start_DateTime and a null end_DateTime. If both time periods have a Period data type, the data types must be comparable. If only one time period is a Period type, the other time period must evaluate to a DateTime type that is comparable to the element type of the Period. Note: Implicit casting to a Period data type is not supported. Results Consider the general case of an OVERLAPS comparison, stated as follows. (S1, E1) OVERLAPS (S2, E2) The result of OVERLAPS is as follows. (S1 > S2 AND NOT (S1 >= E2 AND E1 >= E2)) OR (S2 > S1 AND NOT (S2 >= E1 AND E2 >= E1)) OR (S1 = S2 AND (E1 = E2 OR E1 <> E2)) For Period data types, where p1 is the first Period expression and p2 is the second Period expression, the values of S1, E1, S2, and E2 are as follows: S1 = BEGIN(p1) E1 = END(p1) S2 = BEGIN(p2) E2 = END(p2) Rules The following rules apply to the OVERLAPS comparison. • When you specify two DateTime types, they must be comparable. • When you specify two Period types, they must be comparable. • If you specify a Period type for either one or both time periods, the period expression must not include an explicit NULL. • If the first columns of each left and right time periods are DateTime types, they must have the same data type: both DATE, both TIME, or both TIMESTAMP. • If only one time period is a Period type, the first column of the other time period must have the same data type as the element type of the Period. • If neither time period is a Period type, then the second column of each left and right time period must either be the same DateTime type as its corresponding first column (that is, the two types must be compatible) or it must be an Interval type that involves only DateTime fields where the precision is such that its value can be added to that of the corresponding DateTime type. Chapter 9: Period Functions and Operators OVERLAPS 310 SQL Functions, Operators, Expressions, and Predicates Example 1 The following example compares two time spans that share a single common point, CURRENT_TIME. The result returned is FALSE because when two time spans share a single point, they do not overlap by definition. SELECT 'OVERLAPS' WHERE (CURRENT_TIME(0), INTERVAL '1' HOUR) OVERLAPS (CURRENT_TIME(0), INTERVAL -'1' HOUR); Example 2 The following example is nearly identical to the previous one, except that the arguments have been adjusted to overlap by one second. The result is TRUE and the value ‘OVERLAPS’ is returned. SELECT 'OVERLAPS' WHERE (CURRENT_TIME(0), INTERVAL '1' HOUR) OVERLAPS (CURRENT_TIME(0) + INTERVAL '1' SECOND,INTERVAL -'1' HOUR); Example 3 Here is an example that uses the datetime_expression, datetime_expression form of OVERLAPS. The two DATE periods overlap each other, so the result is TRUE. SELECT 'OVERLAPS' WHERE (DATE '2000-01-15',DATE '2002-12-15') OVERLAPS (DATE '2001-06-15',DATE '2005-06-15'); Example 4 The following example is the same as the previous one, but in row_subquery form: SELECT 'OVERLAPS' WHERE (SELECT DATE '2000-01-15', DATE '2002-12-15') OVERLAPS (SELECT DATE '2001-06-15', DATE '2005-06-15'); Example 5 The null value in the following example means the second datetime_expression has a start time of 2001-06-13 15:00:00 and a null end time. SELECT 'OVERLAPS' WHERE (TIMESTAMP '2001-06-12 10:00:00', TIMESTAMP '2001-06-15 08:00:00') OVERLAPS (TIMESTAMP '2001-06-13 15:00:00', NULL); Because the start time for the second expression falls within the TIMESTAMP interval defined by the first expression, the result is TRUE. Example 6 In the following example, the OVERLAPS predicate operates on PERIOD(DATE) columns. SELECT * FROM employee WHERE period2 OVERLAPS period1; Chapter 9: Period Functions and Operators OVERLAPS SQL Functions, Operators, Expressions, and Predicates 311 Assume the query is executed on the following table employee; where period1 and period2 are PERIOD(DATE) columns: The result is as follows: Example 7 Consider the following table and query: CREATE TABLE project (id INTEGER, analysis_phase PERIOD(DATE)) UNIQUE PRIMARY INDEX (id); INSERT project (1, PERIOD(DATE'2010-06-21',DATE'2010-06-25')); SELECT 'OVERLAPS' FROM project WHERE analysis_phase OVERLAPS PERIOD(DATE'2010-06-24',NULL); The SELECT statement returns an error because one of the operands of OVERLAP is a Period type with a period expression specifying an explicit NULL. Ename period1 period2 Adams ('2005-02-03', '2006-02-03') ('2005-02-03', '2006-02-03') Mary ('2005-04-02', '2006-01-03') ('2005-02-03', '2006-02-03') Jones ('2004-01-02', '2004-03-05') ('2004-03-05', '2004-10-07') Randy ('2004-01-02', '2004-03-05') ('2004-03-07', '2004-10-07') Simon ? ('2005-02-03', '2005-07-27') Ename period1 period2 Adams ('2005-02-03', '2006-02-03') ('2005-02-03', '2006-02-03') Mary ('2005-04-02', '2006-01-03') ('2005-02-03', '2006-02-03') Chapter 9: Period Functions and Operators P_INTERSECT 312 SQL Functions, Operators, Expressions, and Predicates P_INTERSECT Purpose Operator that returns the portion of the Period expressions that is common between the Period expressions if they overlap. If the Period expressions do not overlap, or if either Period expression is NULL, P_INTERSECT returns NULL. Syntax where: Return Value If the Period expressions do not overlap, the result is NULL. If either Period expression is NULL, the result is NULL. Otherwise, the result has a Period data type that is comparable to the Period expressions. If the Period expressions have PERIOD(TIMESTAMP(n) [WITH TIME ZONE]) or PERIOD(TIME(n) [WITH TIME ZONE]) data types but different precisions, the result is a Period value of the higher precision data type. If neither Period expression has a time zone, the resulting period does not have a time zone; otherwise, the resulting period has a time zone and the value of the time zone in the result is determined using the following rules: • If both Period expressions have a time zone, the time zone displacement of a result bound is obtained from the corresponding bound of the Period expression as defined by the Period value constructor that follows. • If only one of the Period expressions has a time zone, the other Period expression is considered to be at the current session time zone and the result is computed as follows. Assuming p1 and p2 are Period expressions and the result element type as determined above is rt, the result of p1 P_INTERSECT p2 is as follows if p1 OVERLAPS p2 is TRUE: PERIOD( CASE WHEN CAST(BEGIN(p1) AS rt) >= CAST(BEGIN(p2) AS rt) THEN CAST(BEGIN(p1) AS rt) Syntax element ... Specifies ... period_expression any expression that evaluates to a Period data type. Note: The Period expressions specified must be comparable. Implicit casting to a Period data type is not supported. period_expression P_INTERSECT period_expression 1101A584 Chapter 9: Period Functions and Operators P_INTERSECT SQL Functions, Operators, Expressions, and Predicates 313 ELSE CAST(BEGIN(p2) AS rt) END, CASE WHEN CAST(END(p1) AS rt) <= CAST(END(p2) AS rt) THEN CAST(END(p1) AS rt) ELSE CAST(END(p2) AS rt) END) Internally, Period values are saved in UTC and the OVERLAPS operator is evaluated using these UTC represented formats and the P_INTERSECT operation is performed if they overlap. Format and Title The format is the default format for the resulting Period data type. The title is period_expression P-INTERSECT period_expression. Error Conditions If either expression is not a Period expression, an error is reported. If the Period expressions are not comparable, an error is reported. Example In the following example, the P_INTERSECT operator is used in the selection list. SELECT period2 P_INTERSECT period1 FROM product_tests WHERE pid = 11804; Assume the query is executed on the following table product_tests where period1 is a PERIOD(TIME(1)) column and period2 is a PERIOD(TIME(0)) column: pid period1 period2 ----- ---------------------------- ------------------------ 11804 ('10:10:10.1', '11:10:10.1') ('10:10:10', '10:10:11') 10996 ('11:10:10.1', '11:40:40.1') ('10:10:10', '10:10:11') The result is as follows: (period2 P_INTERSECT period1) ----------------------------- ('10:10:10.1', '10:10:11.0') Chapter 9: Period Functions and Operators P_NORMALIZE 314 SQL Functions, Operators, Expressions, and Predicates P_NORMALIZE Purpose Operator that returns a Period value that is the combination of the two Period expressions if the Period expressions overlap or meet. If the Period expressions neither meet nor overlap, P_NORMALIZE returns NULL. If either Period expression is NULL, P_NORMALIZE returns NULL. Syntax where: Return Value Assuming p1 and p2 are comparable Period expressions and ((BEGIN(p1) >= BEGIN(p2) AND BEGIN(p1) <= END(p2)) OR (BEGIN(p2) >= BEGIN(p1) AND BEGIN(p2) <= END(p1))) is TRUE, p1 P_NORMALIZE p2 returns PERIOD(minimum(BEGIN(p1), BEGIN(p2)), maximum(END(p1), END(p2))). If either Period expression is NULL or ((BEGIN(p1) >= BEGIN(p2) AND BEGIN(p1) <= END(p2)) OR (BEGIN(p2) >= BEGIN(p1) AND BEGIN(p2) <= END(p1))) is FALSE, the result is NULL. Note that the P_NORMALIZE operator returns a Period value if the Period expressions satisfy the MEETS or OVERLAPS condition. If the Period expressions have PERIOD(TIME(n) [WITH TIME ZONE]) or PERIOD(TIMESTAMP(n) [WITH TIME ZONE]) data type but have different precisions, the result has the higher of the two precisions. If one of the Period expressions contains a time zone, the result contains a time zone for each element. The result time zones are determined using the following rules: • If both Period expressions have a time zone, the time zone displacement of a result bound is obtained from the corresponding bound of the Period expressions as defined by the Period value constructor that follows. Syntax element ... Specifies ... period_expression any expression that evaluates to a Period data type. Note: The Period expressions specified must be comparable. Implicit casting to a Period data type is not supported. period_expression P_NORMALIZE period_expression 1101A594 Chapter 9: Period Functions and Operators P_NORMALIZE SQL Functions, Operators, Expressions, and Predicates 315 • If only one of the Period expressions has a time zone, the other Period expression is considered to be at the current session time zone and the result is computed as follows. Assuming p1 and p2 are Period expressions and the result element type as determined above is rt, the result of p1 P_NORMALIZE p2 is as follows if p1 OVERLAPS p2 OR p1 MEETS p2 is TRUE: PERIOD( CASE WHEN CAST(BEGIN(p1) AS rt) <= CAST(BEGIN(p2) AS rt) THEN CAST(BEGIN(p1) AS rt) ELSE CAST(BEGIN(p2) AS rt) END, CASE WHEN CAST(END(p1) AS rt) >= CAST(END(p2) AS rt) THEN CAST(END(p1) AS rt) ELSE CAST(END(p2) AS rt) END) Internally, Period values are saved in UTC and the OVERLAPS or MEETS operator is evaluated using these UTC represented formats and the P_NORMALIZE operation is performed if they overlap or meet. Format and Title The format is the default format for the resulting Period data type. The title is period_expression P-NORMALIZE period_expression. Error Conditions If either expression is not a Period expression, an error is reported. If the Period expressions are not comparable, an error is reported. Example In the following example, the P_NORMALIZE operator is used to collapse two Period columns. SELECT period2 P_NORMALIZE period1 FROM product_tests WHERE pid = 11215; Assume the query is executed on the following table product_tests where period1 is PERIOD(TIME(1)) column and period2 is PERIOD(TIME(0)) column: pid period1 period2 ----- ---------------------------- ------------------------ 11804 ('10:10:10.1', '11:10:10.1') ('10:10:10', '10:10:11') 10996 ('11:10:10.1', '11:40:40.1') ('10:10:10', '10:10:11') 11215 ('10:40:10.1', '11:20:20.1') ('11:10:10', '11:50:10') The result is as follows: (period2 P_NORMALIZE period1) ----------------------------- ('10:40:10.1', '11:50:10.0') Chapter 9: Period Functions and Operators PRECEDES 316 SQL Functions, Operators, Expressions, and Predicates PRECEDES Purpose Predicate that operates on two Period expressions or one Period expression and one DateTime expression and evaluates to TRUE, FALSE, or UNKNOWN. If both expressions have a Period data type, returns TRUE if the ending bound of the first expression is less than or equal to the beginning bound of the second expression; otherwise, returns FALSE. If the first expression is a Period expression and the second expression is a DateTime expression, returns TRUE if the ending bound of the first expression is less than or equal to the second expression; otherwise, returns FALSE. If the first expression is a DateTime value expression and the second expression has a Period data type, returns TRUE if the first expression is less than the beginning bound of the second expression; otherwise, returns FALSE. If either expression is NULL, the operator returns UNKNOWN. Syntax where: Error Conditions If either expression is other than a Period data type or a DateTime value expression, an error is reported. If the Period expressions are not comparable, an error is reported. Example In the following example, the PRECEDES operator is used in the WHERE clause. Syntax element... Specifies... datetime_expression any expression that evaluates to a DATE, TIME, or TIMESTAMP data type. period_expression any expression that evaluates to a Period data type. Note: The Period expression specified must be comparable with the other expression. Implicit casting to a Period data type is not supported. period_expression period_expression datetime_expression PRECEDES NOT 1101A580 datetime_expression PRECEDES period_expression NOT Chapter 9: Period Functions and Operators PRECEDES SQL Functions, Operators, Expressions, and Predicates 317 SELECT * FROM employee WHERE period1 PRECEDES period2; Assume the query is executed on the following table employee where period1 and period2 are PERIOD(DATE) columns: The result is as follows: ename period1 period2 Adams ('2005-02-03', '2006-02-03') ('2005-02-03', '2006-02-03') Mary ('2005-04-02', '2006-01-03') ('2005-02-03', '2006-02-03') Jones ('2004-01-02', '2004-03-05') ('2004-03-05', '2004-10-07') Randy ('2004-01-02', '2004-03-05') ('2004-03-07', '2004-10-07') Simon ? ('2005-02-03', '2005-07-27') ename period1 period2 Jones ('2004-01-02','2004-03-05') ('2004-03-05', '2004-10-07') Randy ('2004-01-02','2004-03-05') ('2004-03-07', '2004-10-07') Chapter 9: Period Functions and Operators PRIOR 318 SQL Functions, Operators, Expressions, and Predicates PRIOR Purpose Proximity function that returns the preceding value of the argument such that there is one granule of the argument type between the returned value and the argument. Syntax where: Return Value The return data type is the same as that of the argument; that is, a DateTime data type. If the value of the argument is NULL, the result is NULL. Format and Title The format is the default format for the argument's data type. The title is PRIOR(proximity_argument). Error Conditions If the argument does not have a DateTime data type, an error is reported. If the result is outside the permissible range of the argument's data type, an error is reported. For example, if PRIOR(DATE '0001-01-01') is specified, an error is reported. Example In the following example, PRIOR is used in the WHERE clause. SELECT * FROM employee WHERE PRIOR(END(period1)) = DATE '2004-03-04'; Assume the query is executed on the following table employee where period1 is a PERIOD(DATE) column: ename dept period1 Syntax element ... Specifies ... datetime_expression any expression that evaluates to a DATE, TIME, or TIMESTAMP data type. PRIOR (datetime_expression) 1101A578 Chapter 9: Period Functions and Operators PRIOR SQL Functions, Operators, Expressions, and Predicates 319 ----- ----------- ---------------------------- Jones Sales ('2004-01-02', '2004-03-05') Simon Sales ? The result is as follows: ename dept period1 ----- ----------- ---------------------------- Jones Sales ('2004-01-02', '2004-03-05') Chapter 9: Period Functions and Operators LDIFF 320 SQL Functions, Operators, Expressions, and Predicates LDIFF Purpose Operator that returns the portion of the first Period expression that exists before the beginning of the second Period expression when the Period expressions overlap. When the Period expressions overlap but there is no portion of the first Period expression before the beginning of the second Period expression or the Period expressions do not overlap, LDIFF returns NULL. If either Period expression is NULL, LDIFF returns NULL. Syntax where: Return Value Assuming p1 and p2 are comparable Period expressions, p1 LIDFF p2 returns PERIOD(BEGIN(p1), BEGIN(p2)) if p1 OVERLAPS p2 is TRUE and BEGIN(p1) is less than BEGIN(p2). If either Period expression is NULL, p1 OVERLAPS p2 is FALSE, or BEGIN(p1) is not less than BEGIN(p2), the result is NULL. If the Period expressions have PERIOD(TIME(n) [WITH TIME ZONE]) or PERIOD(TIMESTAMP(n) [WITH TIME ZONE]) data types but have different precisions, the result has the higher of the two precisions. If one of the Period expressions contains time zones and the other does not, the result contains a time zone for each element. The result time zones are evaluated using the following rules: • If both Period expressions have a time zone, the time zone displacement of a result bound is obtained from the corresponding bound of the expressions as defined by the Period value constructor that follows. • If only one of the Period expressions has a time zone, the other Period expression is considered to be at the current session time zone and the result is computed as follows. Assuming p1 and p2 are Period expressions and the result element type as determined above is rt, the result of p1 LDIFF p2 is as follows if p1 OVERLAPS p2 is TRUE: Syntax element ... Specifies ... period_expression any expression that evaluates to a Period data type. Note: The Period expressions specified must be comparable. Implicit casting to a Period data type is not supported. period_expression LDIFF period_expression 1101A592 Chapter 9: Period Functions and Operators LDIFF SQL Functions, Operators, Expressions, and Predicates 321 PERIOD( CASE WHEN CAST(BEGIN(p1) AS rt) < CAST(BEGIN(p2) AS rt) THEN CAST(BEGIN(p1) AS rt) ELSE NULL END, CASE WHEN CAST(BEGIN(p1) AS rt) < CAST(BEGIN(p2) AS rt) THEN CAST(BEGIN(p2) AS rt) ELSE NULL END) Internally, Period values are saved in UTC and the OVERLAPS operator is evaluated using these UTC represented formats and the LDIFF operation is performed if they overlap. Format and Title The format is the default format for the resulting Period data type. The title is period_expression LDIFF period_expression. Error Conditions If either expression is not a Period expression, an error is reported. If the Period expressions are not comparable, an error is reported. Example In the following example, the LDIFF operator is used to find the left difference of the first Period expression with the second Period expression. SELECT ename, period2 LDIFF period1 FROM employee; Assume the query is executed on the following table employee where period1 and period2 are PERIOD(DATE) columns: ename period1 period2 ----- ---------------------------- ---------------------------- Adams ('2005-02-03', '2006-02-03') ('2005-02-03', '2006-02-03') Mary ('2005-04-02', '2006-01-03') ('2005-02-03', '2006-02-03') Jones ('2004-01-02', '2004-03-05') ('2002-03-05', '2004-10-07') Randy ('2006-01-02', '2007-03-05') ('2004-03-07', '2005-10-07') Simon ? ('2005-02-03', '2005-07-27') The result is as follows: ename (period2 LDIFF period1) ----- ---------------------------- Adams ? Mary ('2005-02-03', '2005-04-02') Jones ('2002-03-05', '2004-01-02') Randy ? Simon ? Chapter 9: Period Functions and Operators RDIFF 322 SQL Functions, Operators, Expressions, and Predicates RDIFF Purpose Operator that returns the portion of the first Period expression that exists from the end of the second Period expression when the Period expressions overlap. When the Period expressions overlap but there is no portion of the first Period expression from the end of the second Period expression or if the Period expressions do not overlap, RDIFF returns NULL. If either Period expression is NULL, RDIFF returns NULL. Syntax where: Return Value Assuming p1 and p2 are comparable Period expressions, p1 RDIFF p2 returns PERIOD(END(p2), END(p1)) if p1 OVERLAPS p2 is TRUE and END(p1) is greater than END(p2). If either Period expression is NULL, p1 OVERLAPS p2 is FALSE, or END(p1) is not greater than END(p2), the result is NULL. If the Period expressions have PERIOD(TIME[(n)] [WITH TIME ZONE]) or PERIOD(TIMESTAMP[(n)] [WITH TIME ZONE]) data types but have different precisions, the result has the higher of the two precisions. If one of the Period expressions contains time zones and the other does not, the result contains a time zone for each element. The result time zones are evaluated using the following rules: • If both Period expressions have a time zone, the time zone displacement of a result bound is obtained from the corresponding bound of the Period expressions as defined by the Period value constructor that follows. • If only one of the Period expressions has a time zone, the other Period expression is considered to be at the current session time zone and the result is computed as follows. Assuming p1 and p2 are Period expressions and the result element type as determined above is rt, the result of p1 RDIFF p2 is as follows if p1 OVERLAPS p2 is TRUE: Syntax element ... Specifies ... period_expression any expression that evaluates to a Period data type. Note: The Period expressions specified must be comparable. Implicit casting to a Period data type is not supported. period_expression RDIFF period_expression 1101A593 Chapter 9: Period Functions and Operators RDIFF SQL Functions, Operators, Expressions, and Predicates 323 PERIOD( CASE WHEN CAST(END(p1) AS rt) > CAST(END(p2) AS rt) THEN CAST(END(p2) AS rt) ELSE NULL END, CASE WHEN CAST(END(p1) AS rt) > CAST(END(p2) AS rt) THEN CAST(END(p1) AS rt) ELSE NULL END) Internally, Period values are saved in UTC and the OVERLAPS operator is evaluated using these UTC represented formats and the RDIFF operation is performed if they overlap. Format and Title The format is the default format for the resulting Period data type. The title is period_expression RDIFF period_expression. Error Conditions If either expression is not a Period expression, an error is reported. If the Period expressions are not comparable, an error is reported. Example In the following example, the RDIFF operator is used to find the right difference of the first Period expression with the second Period expression. SELECT ename, period2 RDIFF period1 FROM employee; Assume the query is executed on the following table employee where period1 and period2 are PERIOD(DATE) columns: ename period1 period2 ----- ---------------------------- ---------------------------- Adams ('2005-02-03', '2006-02-03') ('2005-02-03', '2006-02-03') Mary ('2005-04-02', '2006-01-03') ('2005-02-03', '2006-02-03') Jones ('2001-01-02', '2003-03-05') ('2002-03-05', '2004-10-07') Randy ('2006-01-02', '2007-03-05') ('2004-03-07', '2005-10-07') Simon ? ('2005-02-03', '2005-07-27') The result is as follows: ename (period2 RDIFF period1) ----- ---------------------------- Adams ? Mary ('2006-01-03', '2006-02-03') Jones ('2003-03-05', '2004-10-07') Randy ? Simon ? Chapter 9: Period Functions and Operators SUCCEEDS 324 SQL Functions, Operators, Expressions, and Predicates SUCCEEDS Purpose Predicate that operates on two Period expressions or one Period expression and one DateTime expression and evaluates to TRUE, FALSE, or UNKNOWN. If both expressions have a Period data type, returns TRUE if the beginning bound of the first expression is greater than or equal to the ending bound of the second expression; otherwise, returns FALSE. If the first expression is a Period expression and the second expression is a DateTime expression, returns TRUE if the beginning bound of the first expression is greater than the second expression; otherwise, returns FALSE. If the first expression is a DateTime expression and the second expression is a Period expression, returns TRUE if the DateTime expression is greater than or equal to the ending bound of the second expression; otherwise, returns FALSE. If either expression is NULL, the operator returns UNKNOWN. Syntax where: Error Conditions If either expression is other than a Period data type or a DateTime value expression, an error is reported. If the expressions are not comparable types, an error is reported. Example In the following example, the SUCCEEDS operator is used in the WHERE clause. SELECT * FROM employee WHERE period1 SUCCEEDS period2; Syntax element... Specifies... datetime_expression any expression that evaluates to a DATE, TIME, or TIMESTAMP data type. period_expression any expression that evaluates to a Period data type. Note: The Period expression specified must be comparable with the other expression. Implicit casting to a Period data type is not supported. period_expression period_expression datetime_expression SUCCEEDS NOT 1101A583 datetime_expression SUCCEEDS period_expression NOT Chapter 9: Period Functions and Operators SUCCEEDS SQL Functions, Operators, Expressions, and Predicates 325 Assume the query is executed on the following table employee where period1 and period2 are PERIOD(DATE) columns: The result is as follows: ename period1 period2 Adams ('2005-02-03', '2006-02-03') ('2005-02-03', '2006-02-03') Mary ('2005-04-02', '2006-01-03') ('2005-02-03', '2006-02-03') Jones ('2004-01-02', '2004-03-05') ('2004-03-05', '2004-10-07') Randy ('2004-01-02', '2004-03-05') ('2004-03-07', '2004-10-07') Simon ? ('2005-02-03', '2005-07-27') ename period1 period2 Jones ('2004-01-02','2004-03-05') ('2004-03-05', '2004-10-07') Randy ('2004-01-02','2004-03-05') ('2004-03-07', '2004-10-07') Chapter 9: Period Functions and Operators TD_NORMALIZE_OVERLAP 326 SQL Functions, Operators, Expressions, and Predicates TD_NORMALIZE_OVERLAP Purpose Combines the rows whose Period values overlap such that the resulting normalized row contains the earliest beginning bound and the latest ending bound from the Period values of all the rows involved. Syntax where: Invocation TD_NORMALIZE_OVERLAP is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Usage Notes TD_NORMALIZE_OVERLAP is a table function that takes two arguments. The arguments passed to the function are the specified columns in a subtable derived from using the WITH Request Modifier as follows: • The first argument is one or more grouping columns, not including the Period column. You must specify this argument as a dynamic UDT, where each column is an attribute of the UDT. For more information, see “NEW VARIANT_TYPE” on page 737. • The second argument is the Period column where you want to find the Period values that overlap. Input to the table function must be columns that are hash-redistributed on the grouping columns and sorted by the grouping columns and the Period values as follows: Syntax element ... Specifies ... grouping_column_list one or more grouping columns, not including the Period column. You must specify the input as a dynamic UDT. period_column a column with a data type of PERIOD(DATE), PERIOD(TIMESTAMP), or PERIOD(TIMESTAMP WITH TIME ZONE). 1101A739 TD_SYSFNLIB. TD_NORMALIZE_OVERLAP (grouping_column_list, period_column) Chapter 9: Period Functions and Operators TD_NORMALIZE_OVERLAP SQL Functions, Operators, Expressions, and Predicates 327 • You must specify a LOCAL ORDER BY clause that includes all of the grouping columns and the Period column in the same order that was specified in the input arguments. The sort order must be ascending. • You must include a HASH BY clause with at least one of the grouping columns. The HASH BY clause cannot include the Period column or any columns that are not part of the grouping columns. You must invoke the function with a RETURNS clause that specifies the output columns as follows: • You must specify the output columns to be the same as the columns specified in the input arguments, including the Period column. • You must specify the output columns with the same data types and in the same order as the corresponding input columns. • You can specify an optional INTEGER output column at the end of the RETURNS clause to contain a count of the rows that were normalized. Result TD_NORMALIZE_OVERLAP returns result rows with the columns specified in the RETURNS clause as follows: • The grouping columns specified in the input argument. • The Period column with normalized Period values. • An optional INTEGER column containing the count of the rows that were normalized because their Period values overlap. Example WITH subtbl(flight_id, duration) AS (SELECT flight_id, duration FROM FlightExp) SELECT * FROM TABLE (TD_SYSFNLIB.TD_NORMALIZE_OVERLAP(NEW VARIANT_TYPE(subtbl.flight_id), subtbl.duration) RETURNS (flight_id INT, duration PERIOD(TIMESTAMP(6) WITH TIME ZONE), NrmCount INT) HASH BY flight_id /* input data is redistributed on column, flight_id */ LOCAL ORDER BY flight_id, duration) /* input data is sorted on these columns */ AS DT(flight_id, duration, NrmCount) ORDER BY 1,2; Chapter 9: Period Functions and Operators TD_NORMALIZE_MEET 328 SQL Functions, Operators, Expressions, and Predicates TD_NORMALIZE_MEET Purpose Combines the rows whose Period values meet such that the resulting normalized row contains the earliest beginning bound and the latest ending bound from the Period values of all the rows involved. Syntax where: Invocation TD_NORMALIZE_MEET is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Usage Notes TD_NORMALIZE_MEET is a table function that takes two arguments. The arguments passed to the function are the specified columns in a subtable derived from using the WITH Request Modifier as follows: • The first argument is one or more grouping columns, not including the Period column. You must specify this argument as a dynamic UDT, where each column is an attribute of the UDT. For more information, see “NEW VARIANT_TYPE” on page 737. • The second argument is the Period column where you want to find the Period values that meet. Input to the table function must be columns that are hash-redistributed on the grouping columns and sorted by the grouping columns and the Period values as follows: Syntax element ... Specifies ... grouping_column_list one or more grouping columns, not including the Period column. You must specify the input as a dynamic UDT. period_column a column with a data type of PERIOD(DATE), PERIOD(TIMESTAMP), or PERIOD(TIMESTAMP WITH TIME ZONE). 1101A740 TD_SYSFNLIB. TD_NORMALIZE_MEET (grouping_column_list, period_column) Chapter 9: Period Functions and Operators TD_NORMALIZE_MEET SQL Functions, Operators, Expressions, and Predicates 329 • You must specify a LOCAL ORDER BY clause that includes all of the grouping columns and the Period column in the same order that was specified in the input arguments. The sort order must be ascending. • You must include a HASH BY clause with at least one of the grouping columns. The HASH BY clause cannot include the Period column or any columns that are not part of the grouping columns. You must invoke the function with a RETURNS clause that specifies the output columns as follows: • You must specify the output columns to be the same as the columns specified in the input arguments, including the Period column. • You must specify the output columns with the same data types and in the same order as the corresponding input columns. • You can specify an optional INTEGER output column at the end of the RETURNS clause to contain a count of the rows that were normalized. Result TD_NORMALIZE_MEET returns result rows with the columns specified in the RETURNS clause as follows: • The grouping columns specified in the input argument. • The Period column with normalized Period values. • An optional INTEGER column containing the count of the rows that were normalized because their Period values meet. Example WITH subtbl(flight_id, duration) AS (SELECT flight_id, duration FROM FlightExp) SELECT * FROM TABLE (TD_SYSFNLIB.TD_NORMALIZE_MEET(NEW VARIANT_TYPE(subtbl.flight_id), subtbl.duration) RETURNS (flight_id INT, duration PERIOD(TIMESTAMP(6) WITH TIME ZONE), NrmCount INT) HASH BY flight_id /* input data is redistributed on column, flight_id */ LOCAL ORDER BY flight_id, duration) /* input data is sorted on these columns */ AS DT(flight_id, duration, NrmCount) ORDER BY 1,2; Chapter 9: Period Functions and Operators TD_NORMALIZE_OVERLAP_MEET 330 SQL Functions, Operators, Expressions, and Predicates TD_NORMALIZE_OVERLAP_MEET Purpose Combines the rows whose Period values either meet or overlap such that the resulting normalized row contains the earliest beginning bound and the latest ending bound from the Period values of all the rows involved. Syntax where: Invocation TD_NORMALIZE_OVERLAP_MEET is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Usage Notes TD_NORMALIZE_OVERLAP_MEET is a table function that takes two arguments. The arguments passed to the function are the specified columns in a subtable derived from using the WITH Request Modifier as follows: • The first argument is one or more grouping columns, not including the Period column. You must specify this argument as a dynamic UDT, where each column is an attribute of the UDT. For more information, see “NEW VARIANT_TYPE” on page 737. • The second argument is the Period column where you want to find the Period values that overlap or meet. Input to the table function must be columns that are hash-redistributed on the grouping columns and sorted by the grouping columns and the Period values as follows: Syntax element ... Specifies ... grouping_column_list one or more grouping columns, not including the Period column. You must specify the input as a dynamic UDT. period_column a column with a data type of PERIOD(DATE), PERIOD(TIMESTAMP), or PERIOD(TIMESTAMP WITH TIME ZONE). 1101A741 TD_SYSFNLIB. TD_NORMALIZE_OVERLAP_MEET (grouping_column_list, period_column) Chapter 9: Period Functions and Operators TD_NORMALIZE_OVERLAP_MEET SQL Functions, Operators, Expressions, and Predicates 331 • You must specify a LOCAL ORDER BY clause that includes all of the grouping columns and the Period column in the same order that was specified in the input arguments. The sort order must be ascending. • You must include a HASH BY clause with at least one of the grouping columns. The HASH BY clause cannot include the Period column or any columns that are not part of the grouping columns. You must invoke the function with a RETURNS clause that specifies the output columns as follows: • You must specify the output columns to be the same as the columns specified in the input arguments, including the Period column. • You must specify the output columns with the same data types and in the same order as the corresponding input columns. • You can specify an optional INTEGER output column at the end of the RETURNS clause to contain a count of the rows that were normalized. Result TD_NORMALIZE_OVERLAP_MEET returns result rows with the columns specified in the RETURNS clause as follows: • The grouping columns specified in the input argument. • The Period column with normalized Period values. • An optional INTEGER column containing the count of the rows that were normalized because their Period values overlap or meet. Example WITH subtbl(flight_id, duration) AS (SELECT flight_id, duration FROM FlightExp) SELECT * FROM TABLE (TD_SYSFNLIB.TD_NORMALIZE_OVERLAP_MEET(NEW VARIANT_TYPE(subtbl.flight_id), subtbl.duration) RETURNS (flight_id INT, duration PERIOD(TIMESTAMP(6) WITH TIME ZONE), NrmCount INT) HASH BY flight_id /* input data is redistributed on column, flight_id */ LOCAL ORDER BY flight_id, duration) /* input data is sorted on these columns */ AS DT(flight_id, duration, NrmCount) ORDER BY 1,2; Chapter 9: Period Functions and Operators TD_SUM_NORMALIZE_OVERLAP 332 SQL Functions, Operators, Expressions, and Predicates TD_SUM_NORMALIZE_OVERLAP Purpose Finds the sum of a column for all the rows that were normalized because their Period values overlap. Syntax where: Invocation TD_SUM_NORMALIZE_OVERLAP is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Usage Notes TD_SUM_NORMALIZE_OVERLAP is a table function that takes three arguments. The arguments passed to the function are the specified columns in a subtable derived from using the WITH Request Modifier as follows: • The first argument is one or more grouping columns, not including the Period column. You must specify this argument as a dynamic UDT, where each column is an attribute of the UDT. For more information, see “NEW VARIANT_TYPE” on page 737. • The second argument is a numeric column on which SUM() is requested. All numeric data types are supported. You must specify this argument as a dynamic UDT where the column is an attribute of the UDT. 1101A742 TD_SYSFNLIB. TD_SUM_NORMALIZE_OVERLAP (grouping_column_list, numeric_column, period_column) Syntax element ... Specifies ... grouping_column_list one or more grouping columns, not including the Period column. You must specify the input as a dynamic UDT. numeric_column a numeric column on which SUM() is requested. You must specify the input as a dynamic UDT. period_column a column with a data type of PERIOD(DATE), PERIOD(TIMESTAMP), or PERIOD(TIMESTAMP WITH TIME ZONE). Chapter 9: Period Functions and Operators TD_SUM_NORMALIZE_OVERLAP SQL Functions, Operators, Expressions, and Predicates 333 • The third argument is the Period column where you want to find the Period values that overlap. Input to the table function must be columns that are hash-redistributed on the grouping columns and sorted by the grouping columns and the Period values as follows: • You must specify a LOCAL ORDER BY clause that includes all of the grouping columns and the Period column in the same order that was specified in the input arguments. The sort order must be ascending. • You must include a HASH BY clause with at least one of the grouping columns. The HASH BY clause cannot include the Period column or any columns that are not part of the grouping columns. You must invoke the function with a RETURNS clause that specifies the output columns as follows: • You must specify the output columns to be the same as the columns specified in the input arguments, including the Period column. • You must specify the output columns with the same data types and in the same order as the corresponding input columns. • You must include a numeric output column to contain the sum result value. The data type of this column should be the same data type as the corresponding input column. To prevent a possible overflow error, you can use the CAST function to convert the data type of the input column to a larger numeric data type. Result TD_SUM_NORMALIZE_OVERLAP returns result rows with the columns specified in the RETURNS clause as follows: • The grouping columns specified in the input argument. • A numeric column containing the requested sum. • The Period column with normalized Period values. Example WITH subtbl(flight_id, charges, duration) AS (SELECT flight_id, charges, duration FROM FlightExp) SELECT * FROM TABLE (TD_SYSFNLIB.TD_SUM_NORMALIZE_OVERLAP(NEW VARIANT_TYPE(subtbl.flight_id), NEW VARIANT_TYPE(subtbl.charges), subtbl.duration) RETURNS (flight_id INT, charges FLOAT, duration PERIOD(TIMESTAMP(6) WITH TIME ZONE)) HASH BY flight_id /* input data is redistributed on column, flight_id */ LOCAL ORDER BY flight_id, duration) /* input data is sorted on these columns */ AS DT(flight_id, charges, duration) ORDER BY 1,3; Chapter 9: Period Functions and Operators TD_SUM_NORMALIZE_MEET 334 SQL Functions, Operators, Expressions, and Predicates TD_SUM_NORMALIZE_MEET Purpose Finds the sum of a column for all the rows that were normalized because their Period values meet. Syntax where: Invocation TD_SUM_NORMALIZE_MEET is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Usage Notes TD_SUM_NORMALIZE_MEET is a table function that takes three arguments. The arguments passed to the function are the specified columns in a subtable derived from using the WITH Request Modifier as follows: • The first argument is one or more grouping columns, not including the Period column. You must specify this argument as a dynamic UDT, where each column is an attribute of the UDT. For more information, see “NEW VARIANT_TYPE” on page 737. • The second argument is a numeric column on which SUM() is requested. All numeric data types are supported. You must specify this argument as a dynamic UDT where the column is an attribute of the UDT. 1101A743 TD_SYSFNLIB. TD_SUM_NORMALIZE_MEET (grouping_column_list, numeric_column, period_column) Syntax element ... Specifies ... grouping_column_list one or more grouping columns, not including the Period column. You must specify the input as a dynamic UDT. numeric_column a numeric column on which SUM() is requested. You must specify the input as a dynamic UDT. period_column a column with a data type of PERIOD(DATE), PERIOD(TIMESTAMP), or PERIOD(TIMESTAMP WITH TIME ZONE). Chapter 9: Period Functions and Operators TD_SUM_NORMALIZE_MEET SQL Functions, Operators, Expressions, and Predicates 335 • The third argument is the Period column where you want to find the Period values that meet. Input to the table function must be columns that are hash-redistributed on the grouping columns and sorted by the grouping columns and the Period values as follows: • You must specify a LOCAL ORDER BY clause that includes all of the grouping columns and the Period column in the same order that was specified in the input arguments. The sort order must be ascending. • You must include a HASH BY clause with at least one of the grouping columns. The HASH BY clause cannot include the Period column or any columns that are not part of the grouping columns. You must invoke the function with a RETURNS clause that specifies the output columns as follows: • You must specify the output columns to be the same as the columns specified in the input arguments, including the Period column. • You must specify the output columns with the same data types and in the same order as the corresponding input columns. • You must include a numeric output column to contain the sum result value. The data type of this column should be the same data type as the corresponding input column. To prevent a possible overflow error, you can use the CAST function to convert the data type of the input column to a larger numeric data type. Result TD_SUM_NORMALIZE_MEET returns result rows with the columns specified in the RETURNS clause: • The grouping columns specified in the input argument. • A numeric column containing the requested sum. • The Period column with normalized Period values. Example WITH subtbl(flight_id, charges, duration) AS (SELECT flight_id, charges, duration FROM FlightExp) SELECT * FROM TABLE (TD_SYSFNLIB.TD_SUM_NORMALIZE_MEET(NEW VARIANT_TYPE(subtbl.flight_id), NEW VARIANT_TYPE(subtbl.charges), subtbl.duration) RETURNS (flight_id INT, charges FLOAT, duration PERIOD(TIMESTAMP(6) WITH TIME ZONE)) HASH BY flight_id /* input data is redistributed on column, flight_id */ LOCAL ORDER BY flight_id, duration) /* input data is sorted on these columns */ AS DT(flight_id, charges, duration) ORDER BY 1,3; Chapter 9: Period Functions and Operators TD_SUM_NORMALIZE_OVERLAP_MEET 336 SQL Functions, Operators, Expressions, and Predicates TD_SUM_NORMALIZE_OVERLAP_MEET Purpose Finds the sum of a column for all the rows that were normalized because their Period values either overlap or meet. Syntax where: Invocation TD_SUM_NORMALIZE_OVERLAP_MEET is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Usage Notes TD_SUM_NORMALIZE_OVERLAP_MEET is a table function that takes three arguments. The arguments passed to the function are the specified columns in a subtable derived from using the WITH Request Modifier as follows: • The first argument is one or more grouping columns, not including the Period column. You must specify this argument as a dynamic UDT, where each column is an attribute of the UDT. For more information, see “NEW VARIANT_TYPE” on page 737. • The second argument is a numeric column on which SUM() is requested. All numeric data types are supported. You must specify this argument as a dynamic UDT where the column is an attribute of the UDT. 1101A744 TD_SYSFNLIB. TD_SUM_NORMALIZE_OVERLAP_MEET (grouping_column_list, numeric_column, period_column) Syntax element ... Specifies ... grouping_column_list one or more grouping columns, not including the Period column. You must specify the input as a dynamic UDT. numeric_column a numeric column on which SUM() is requested. You must specify the input as a dynamic UDT. period_column a column with a data type of PERIOD(DATE), PERIOD(TIMESTAMP), or PERIOD(TIMESTAMP WITH TIME ZONE). Chapter 9: Period Functions and Operators TD_SUM_NORMALIZE_OVERLAP_MEET SQL Functions, Operators, Expressions, and Predicates 337 • The third argument is the Period column where you want to find the Period values that overlap or meet. Input to the table function must be columns that are hash-redistributed on the grouping columns and sorted by the grouping columns and the Period values as follows: • You must specify a LOCAL ORDER BY clause that includes all of the grouping columns and the Period column in the same order that was specified in the input arguments. The sort order must be ascending. • You must include a HASH BY clause with at least one of the grouping columns. The HASH BY clause cannot include the Period column or any columns that are not part of the grouping columns. You must invoke the function with a RETURNS clause that specifies the output columns as follows: • You must specify the output columns to be the same as the columns specified in the input arguments, including the Period column. • You must specify the output columns with the same data types and in the same order as the corresponding input columns. • You must include a numeric output column to contain the sum result value. The data type of this column should be the same data type as the corresponding input column. To prevent a possible overflow error, you can use the CAST function to convert the data type of the input column to a larger numeric data type. Result TD_SUM_NORMALIZE_OVERLAP_MEET returns result rows with the columns specified in the RETURNS clause: • The grouping columns specified in the input argument. • A numeric column containing the requested sum. • The Period column with normalized Period values. Example WITH subtbl(flight_id, charges, duration) AS (SELECT flight_id, charges, duration FROM FlightExp) SELECT * FROM TABLE ( TD_SYSFNLIB.TD_SUM_NORMALIZE_OVERLAP_MEET(NEW VARIANT_TYPE(subtbl.flight_id), NEW VARIANT_TYPE(subtbl.charges), subtbl.duration) RETURNS (flight_id INT, charges FLOAT, duration PERIOD(TIMESTAMP(6) WITH TIME ZONE)) HASH BY flight_id /* input data is redistributed on column, flight_id */ LOCAL ORDER BY flight_id, duration) /* input data is sorted on these columns */ AS DT(flight_id, charges, duration) ORDER BY 1,3; Chapter 9: Period Functions and Operators TD_SEQUENCED_SUM 338 SQL Functions, Operators, Expressions, and Predicates TD_SEQUENCED_SUM Purpose Finds the sum of a column for all adjacent periods in normalized rows whose Period values either meet or overlap. Syntax where: Invocation TD_SEQUENCED_SUM is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Usage Notes TD_SEQUENCED_SUM is a table function that takes three arguments. The arguments passed to the function are the specified columns in a subtable derived from using the WITH Request Modifier as follows: • The first argument is one or more grouping columns, not including the Period column. You must specify this argument as a dynamic UDT, where each column is an attribute of the UDT. For more information, see “NEW VARIANT_TYPE” on page 737. • The second argument is a numeric column on which SUM() is requested. All numeric data types are supported. You must specify this argument as a dynamic UDT where the column is an attribute of the UDT. 1101A745 TD_SYSFNLIB. TD_SEQUENCED_SUM (grouping_column_list, numeric_column, period_column) Syntax element ... Specifies ... grouping_column_list one or more grouping columns, not including the Period column. You must specify the input as a dynamic UDT. numeric_column a numeric column on which SUM() is requested. You must specify the input as a dynamic UDT. period_column a column with a data type of PERIOD(DATE), PERIOD(TIMESTAMP), or PERIOD(TIMESTAMP WITH TIME ZONE). Chapter 9: Period Functions and Operators TD_SEQUENCED_SUM SQL Functions, Operators, Expressions, and Predicates 339 • The third argument is the Period column where you want to find the Period values that overlap or meet. Input to the table function must be columns that are hash-redistributed on the grouping columns and sorted by the grouping columns and the Period values as follows: • You must specify a LOCAL ORDER BY clause that includes all of the grouping columns and the Period column in the same order that was specified in the input arguments. The sort order must be ascending. • You must include a HASH BY clause with at least one of the grouping columns. The HASH BY clause cannot include the Period column or any columns that are not part of the grouping columns. You must invoke the function with a RETURNS clause that specifies the output columns as follows: • The output columns must include all of the grouping columns with the same data type and in the same order as the input columns. • You must include a numeric output column to contain the sum result value. The data type of this column should be the same data type as the corresponding input column. To prevent a possible overflow error, you can use the CAST function to convert the data type of the input column to a larger numeric data type. • A Period column with the same Period data type as the input Period column. Result TD_SEQUENCED_SUM returns result rows with the columns specified in the RETURNS clause: • The grouping columns specified in the input argument. • A numeric column containing the requested sum result. • A Period column with the sequenced aggregation result. Example WITH subtbl(flight_id, charges, duration) AS (SELECT flight_id, charges, duration FROM FlightExp) SELECT * FROM TABLE ( TD_SYSFNLIB.TD_SEQUENCED_SUM(NEW VARIANT_TYPE(subtbl.flight_id), NEW VARIANT_TYPE(subtbl.charges), subtbl.duration) RETURNS (flight_id INT, charges FLOAT, duration PERIOD(TIMESTAMP(6) WITH TIME ZONE)) HASH BY flight_id /* input data is redistributed on column, flight_id */ LOCAL ORDER BY flight_id, duration) /* input data is sorted on these columns */ AS DT(flight_id, charges, duration) ORDER BY 1,3; Chapter 9: Period Functions and Operators TD_SEQUENCED_AVG 340 SQL Functions, Operators, Expressions, and Predicates TD_SEQUENCED_AVG Purpose Finds the average of a column for all adjacent periods in normalized rows whose Period values either meet or overlap. Syntax where: Invocation TD_SEQUENCED_AVG is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Usage Notes TD_SEQUENCED_AVG is a table function that takes three arguments. The arguments passed to the function are the specified columns in a subtable derived from using the WITH Request Modifier as follows: • The first argument is one or more grouping columns, not including the Period column. You must specify this argument as a dynamic UDT, where each column is an attribute of the UDT. For more information, see “NEW VARIANT_TYPE” on page 737. • The second argument is a numeric column on which AVG() is requested. All numeric data types are supported. You must specify this argument as a dynamic UDT where the column is an attribute of the UDT. 1101A746 TD_SYSFNLIB. TD_SEQUENCED_AVG (grouping_column_list, numeric_column, period_column) Syntax element ... Specifies ... grouping_column_list one or more grouping columns, not including the Period column. You must specify the input as a dynamic UDT. numeric_column a numeric column on which AVG() is requested. You must specify the input as a dynamic UDT. period_column a column with a data type of PERIOD(DATE), PERIOD(TIMESTAMP), or PERIOD(TIMESTAMP WITH TIME ZONE). Chapter 9: Period Functions and Operators TD_SEQUENCED_AVG SQL Functions, Operators, Expressions, and Predicates 341 • The third argument is the Period column where you want to find the Period values that overlap or meet. Input to the table function must be columns that are hash-redistributed on the grouping columns and sorted by the grouping columns and the Period values as follows: • You must specify a LOCAL ORDER BY clause that includes all of the grouping columns and the Period column in the same order that was specified in the input arguments. The sort order must be ascending. • You must include a HASH BY clause with at least one of the grouping columns. The HASH BY clause cannot include the Period column or any columns that are not part of the grouping columns. You must invoke the function with a RETURNS clause that specifies the output columns as follows: • The output columns must include all of the grouping columns with the same data type and in the same order as the input columns. • You must include a numeric output column to contain the average result value. The data type of this column can be FLOAT or the same data type as the corresponding input column; however, to avoid possible rounding of the result value, it is recommended that you use FLOAT. To prevent a possible overflow error, you can use the CAST function to convert the data type of the input column to a larger numeric data type. • A Period column with the same Period data type as the input Period column. Result TD_SEQUENCED_AVG returns result rows with the columns specified in the RETURNS clause: • The grouping columns specified in the input argument. • A numeric column containing the average result. • A Period column with the sequenced aggregation result. Example WITH subtbl(flight_id, charges, duration) AS (SELECT flight_id, charges, duration FROM FlightExp) SELECT * FROM TABLE ( TD_SYSFNLIB.TD_SEQUENCED_AVG(NEW VARIANT_TYPE(subtbl.flight_id), NEW VARIANT_TYPE(subtbl.charges), subtbl.duration) RETURNS (flight_id INT, charges FLOAT, duration PERIOD(TIMESTAMP(6) WITH TIME ZONE)) HASH BY flight_id /* input data is redistributed on column, flight_id */ LOCAL ORDER BY flight_id, duration) /* input data is sorted on these columns */ AS DT(flight_id, charges, duration) ORDER BY 1,3; Chapter 9: Period Functions and Operators TD_SEQUENCED_COUNT 342 SQL Functions, Operators, Expressions, and Predicates TD_SEQUENCED_COUNT Purpose Finds the count of a column for all adjacent periods in normalized rows whose Period values either meet or overlap. Syntax where: Invocation TD_SEQUENCED_COUNT is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Usage Notes TD_SEQUENCED_COUNT is a table function that takes two arguments. The arguments passed to the function are the specified columns in a subtable derived from using the WITH Request Modifier as follows: • The first argument is one or more grouping columns, not including the Period column. You must specify this argument as a dynamic UDT, where each column is an attribute of the UDT. For more information, see “NEW VARIANT_TYPE” on page 737. • The second argument is the Period column where you want to find the Period values that overlap or meet. Input to the table function must be columns that are hash-redistributed on the grouping columns and sorted by the grouping columns and the Period values as follows: 1101A747 TD_SYSFNLIB. TD_SEQUENCED_COUNT (grouping_column_list, period_column) Syntax element ... Specifies ... grouping_column_list one or more grouping columns, not including the Period column. You must specify the input as a dynamic UDT. period_column a column with a data type of PERIOD(DATE), PERIOD(TIMESTAMP), or PERIOD(TIMESTAMP WITH TIME ZONE). Chapter 9: Period Functions and Operators TD_SEQUENCED_COUNT SQL Functions, Operators, Expressions, and Predicates 343 • You must specify a LOCAL ORDER BY clause that includes all of the grouping columns and the Period column in the same order that was specified in the input arguments. The sort order must be ascending. • You must include a HASH BY clause with at least one of the grouping columns. The HASH BY clause cannot include the Period column or any columns that are not part of the grouping columns. You must invoke the function with a RETURNS clause that specifies the output columns as follows: • The output columns must include all of the grouping columns with the same data type and in the same order as the input columns. • You must include an INTEGER output column to contain the count result. • A Period column with the same Period data type as the input Period column. Result TD_SEQUENCED_COUNT returns result rows with the columns specified in the RETURNS clause: • The grouping columns specified in the input argument. • An INTEGER column containing the count result. • A Period column with the sequenced aggregation result. Example WITH subtbl(flight_id, duration) AS (SELECT flight_id, duration FROM FlightExp) SELECT * FROM TABLE ( TD_SYSFNLIB.TD_SEQUENCED_COUNT(NEW VARIANT_TYPE(subtbl.flight_id), subtbl.duration) RETURNS (flight_id INT, cnt INT, duration PERIOD(TIMESTAMP(6) WITH TIME ZONE)) HASH BY flight_id /* input data is redistributed on column, flight_id */ LOCAL ORDER BY flight_id, duration) /* input data is sorted on these columns */ AS DT(flight_id, cnt, duration) ORDER BY 1,3; Chapter 9: Period Functions and Operators TD_SEQUENCED_COUNT 344 SQL Functions, Operators, Expressions, and Predicates SQL Functions, Operators, Expressions, and Predicates 345 CHAPTER 10 Aggregate Functions This chapter describes SQL aggregate functions. For information on: • window aggregate functions and their Teradata-specific equivalents, see Chapter 11: “Ordered Analytical Functions.” • aggregate user-defined functions (UDFs), see “Aggregate UDF” on page 714. • window aggregate UDFs, see “Window Aggregate UDF” on page 717. Aggregate Functions Aggregate functions are typically used in arithmetic expressions. Aggregate functions operate on a group of rows and return a single numeric value in the result table for each group. In the following statement, the SUM aggregate function operates on the group of rows defined by the Sales_Table table: SELECT SUM(Total_Sales) FROM Sales_Table; Sum(Total_Sales) ---------------- 5192.40 You can use GROUP BY clauses to produce more complex, finer grained results in multiple result values. In the following statement, the SUM aggregate function operates on groups of rows defined by the Product_ID column in the Sales_Table table: SELECT Product_ID, SUM(Total_Sales) FROM Sales_Table GROUP BY Product_ID; Product_ID Sum(Total_Sales) ---------- ---------------- 101 2100.00 107 1000.40 102 2092.00 Aggregates in the Select List Aggregate functions are normally used in the expression list of a SELECT statement and in the summary list of a WITH clause. Chapter 10: Aggregate Functions Aggregate Functions 346 SQL Functions, Operators, Expressions, and Predicates Aggregates and GROUP BY If you use an aggregate function in the select list of an SQL statement, then either all other columns occurring in the select list must also be referenced by means of aggregate functions or their column name must appear in a GROUP BY clause. For example, the following statement uses an aggregate function and a column in the select list and references the column name in the GROUP BY clause: SELECT COUNT(*), Product_ID FROM Sales_Table GROUP BY Product_ID; The reason for this is that aggregates return only one value, while a non-GROUP BY column reference can return any number of values. Aggregates and Date It is valid to apply AVG, MIN, MAX, or COUNT to a date. It is not valid to specify SUM(date). Aggregates and Constant Expressions in the Select List Constant expressions in the select list may optionally appear in the GROUP BY clause. For example, the following statement uses an aggregate function and a constant expression in the select list, and does not use a GROUP BY clause: SELECT COUNT(*), SUBSTRING( CAST( CURRENT_TIME(0) AS CHAR(14) ) FROM 1 FOR 8 ) FROM Sales_Table; The results of such statements when the table has no rows depends on the type of constant expression. IF the constant expression … THEN the result of the constant expression in the query result is … does not contain a column reference the value of the constant expression. Functions such as RANDOM are computed in the immediate retrieve step of the request instead of in the aggregation step. Here is an example: SELECT COUNT(*), SUBSTRING(CAST(CURRENT_TIME(0) AS CHAR(14)) FROM 1 FOR 8) FROM Sales_Table; Count(*) Substring(Current Time(0) From 1 For 8) -------- --------------------------------------- 0 09:01:43 is a non-deterministic function, such as RANDOM Chapter 10: Aggregate Functions Aggregate Functions SQL Functions, Operators, Expressions, and Predicates 347 Nesting Aggregates Aggregate operations cannot be nested. The following aggregate is not valid and returns an error: AVG(MAXIMUM (Salary)) But aggregates can be nested in aggregate window functions. The following statement is valid and includes an aggregate SUM function nested in a RANK window function: SELECT region ,product ,SUM(amount) ,RANK() OVER (PARTITION BY region ORDER by SUM (amount)) FROM table; For details on aggregate window functions, see Chapter 11: “Ordered Analytical Functions.” Results of Aggregation on Zero Rows Aggregation on zero rows behaves as indicated by the following table. Aggregates and Nulls Aggregates (with the exception of COUNT(*)) ignore nulls1 in all computations. This behavior can result in apparent nontransitive anomalies. For example, if there are nulls in either column A or column B (or both), then the following expression is virtually always true. SUM(A) + SUM(B) <> SUM(A+B) contains a column reference NULL. Here is an example: SELECT COUNT(*), UDF_CALC(1,2) FROM Sales_Table; Count(*) UDF_CALC(1,2) ----------- ------------- 0 ? is a UDF IF the constant expression … THEN the result of the constant expression in the query result is … This form of aggregate function … Returns this result when there are zero rows … COUNT(expression) WHERE … 0 all other forms of aggregate_operator(expression) WHERE … Null aggregate_operator(expression) … GROUP BY … No Record Found aggregate_operator(expression) … HAVING … 1. A UDT column value is null only when you explicitly place a null value in the column, not when a UDT instance has an attribute that is set to null. Chapter 10: Aggregate Functions Aggregate Functions 348 SQL Functions, Operators, Expressions, and Predicates The only exception to this is the case in which the values for columns A and B are both null in the same rows, because in those cases the entire row is disregarded in the aggregation. This is a trivial case that does not violate the general rule. More formally stated, if and only if field A and field B are both null for every occurrence of a null in either field is the above inequality false. For examples that illustrate this behavior, see “Example 2” on page 358 and “Example 3” on page 358. Note that the aggregates are behaving exactly as they should—the results are not mathematically anomalous. There are several ways to work around this apparent nontransitivity issue if it presents a problem. Either solution provides the same consistent results. • Always define your numeric columns as NOT NULL DEFAULT 0 • Use the ZEROIFNULL function within the aggregate function to convert any nulls to zeros for the computation, for example SUM(ZEROIFNULL(x) + ZEROIFNULL(y)), which produces the same result as SUM(ZEROIFNULL(x) + ZEROIFNULL(y)). Aggregate Operations on Floating Point Data Operations involving floating point numbers are not always associative due to approximation and rounding errors: ((A + B) + C) is not always equal to (A + (B + C)). Although not readily apparent, the non-associativity of floating point arithmetic can also affect aggregate operations: you can get different results each time you use an aggregate function on a given set of floating point data. When Teradata Database performs an aggregation, it accumulates individual terms from each AMP involved in the computation and evaluates the terms in order of arrival to produce the final result. Because the order of evaluation can produce slightly different results, and because the order in which individual AMPs finish their part of the work is unpredictable, the results of an aggregate function on the same data on the same system can vary. For more information on potential problems associated with floating point values in computations, see SQL Data Types and Literals. Aggregates and LOBs Aggregates do not operate on CLOB or BLOB data types. Aggregates and Period Data Types Aggregates (with the exception of COUNT) do not operate on Period data types. Aggregates and SELECT AND CONSUME Statements Aggregates cannot appear in SELECT AND CONSUME statements. Chapter 10: Aggregate Functions Aggregate Functions SQL Functions, Operators, Expressions, and Predicates 349 Aggregates and Recursive Queries Aggregate functions cannot appear in a recursive statement of a recursive query. However, a non-recursive seed statement in a recursive query can specify an aggregate function. Aggregates in WHERE and HAVING Clauses Aggregates can appear in the following types of clauses: • The WHERE clause of an ABORT statement to specify an abort condition. But an aggregate function cannot appear in the WHERE clause of a SELECT statement. • A HAVING clause to specify a group condition. DISTINCT Option The DISTINCT option specifies that duplicate values are not to be used when an expression is processed. The following SELECT returns the number of unique job titles in a table. SELECT COUNT(DISTINCT JobTitle) FROM Employee; A query can have multiple aggregate functions that use DISTINCT with the same expression, as shown by the following example. SELECT SUM(DISTINCT x), AVG(DISTINCT x) FROM XTable; A query can also have multiple aggregate functions that use DISTINCT with different expressions, for example: SELECT SUM(DISTINCT x), SUM(DISTINCT y) FROM XYTable; Chapter 10: Aggregate Functions AVG 350 SQL Functions, Operators, Expressions, and Predicates AVG Purpose Returns the arithmetic average of all values in the specified expression for each row in the group. Syntax where: ANSI Compliance AVG is ANSI SQL:2008 compliant. AVERAGE and AVE are Teradata extensions to the ANSI standard. Result Type and Attributes The following table lists the default attributes for the result of AVG(x). Syntax element … Specifies … ALL that all non-null values specified by value_expression, including duplicates, are included in the average computation for the group. This is the default. DISTINCT that null and duplicate values specified by value_expression are eliminated from the average computation for the group. value_expression a constant or column expression for which an average is to be computed. The expression cannot contain any ordered analytical or aggregate functions. 1101B410 AVERAGE ( value_expression ) AVG DISTINCT AVE ALL Attribute Value Data Type REAL Title Average(x) Chapter 10: Aggregate Functions AVG SQL Functions, Operators, Expressions, and Predicates 351 For an explanation of the formatting characters in the format, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on a UDT argument that has an implicit cast that casts between the UDT and any of the following predefined types: • Numeric • Character • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including AVG, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” Computation of INTEGER or DECIMAL Values An AVG of a DECIMAL or INTEGER value may overflow if the individual values are very large or if there is a large number of values. If this occurs, change the AVG call to include a CAST function that converts the DECIMAL or INTEGER values to REAL as shown in the following example: AVG(CAST(value AS REAL) ) Casting the values as REAL before averaging causes a slight loss in precision. The type of the result is REAL in either case, so the only effect of the CAST is to accept a slight loss of precision where a result might not otherwise be available at all. Format IF the operand is … THEN the format is the … • numeric • date • interval same format as x. character default format for FLOAT. UDT format for the data type to which the UDT is implicitly cast. Attribute Value Chapter 10: Aggregate Functions AVG 352 SQL Functions, Operators, Expressions, and Predicates If x is an integer, AVG does not display a fractional value. A fractional value may be obtained by casting the value as DECIMAL, for example the following CAST to DECIMAL. CAST(AVG(value) AS DECIMAL(9,2)) Restrictions The value_expression must not be a column reference to a view column that is derived from a function. AVG is valid only for numeric data. Nulls are not included in the result computation. For more information, see SQL Fundamentals and “Aggregates and Nulls” on page 347. Example This example queries the sales table for average sales by region and returns the following results. SELECT Region, AVG(sales) FROM sales_tbl GROUP BY Region ORDER BY Region; Region Average (sales) ------ --------------- North 21840.17 East 55061.32 Midwest 15535.73 AVG Window Function For the AVG window function that computes a group, cumulative, or moving average, see “Window Aggregate Functions” on page 449. Chapter 10: Aggregate Functions CORR SQL Functions, Operators, Expressions, and Predicates 353 CORR Purpose Returns the Pearson product moment correlation coefficient of its arguments for all non-null data point pairs. Syntax where: ANSI Compliance CORR is ANSI SQL:2008 compliant. Definition The Pearson product-moment correlation coefficient is a measure of the linear association between variables. The boundary on the computed coefficient ranges from -1.00 to +1.00. Note that high correlation does not imply a causal relationship between the variables. The following table indicates the meaning of four extreme values for the coefficient of correlation between two variables. Syntax element … Specifies … value_expression_2 a numeric expression to be correlated with a second numeric expression. The expressions cannot contain an value_expression_1 y ordered analytical or aggregate functions. 1101B217 CORR ( value_expression_1, value_expression_2 ) IF the correlation coefficient has this value … THEN the association between the variables … -1.00 is perfectly linear, but inverse. As the value for y varies, the value for x varies identically in the opposite direction. 0 does not exist and they are said to be uncorrelated. +1.00 is perfectly linear. As the value for y varies, the value for x varies identically in the same direction. Chapter 10: Aggregate Functions CORR 354 SQL Functions, Operators, Expressions, and Predicates Computation The equation for computing CORR is defined as follows: where: Division by zero results in NULL rather than an error. Result Type and Attributes The data type, format, and title for CORR(y, x) are as follows. For an explanation of the formatting characters in the format, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on UDT arguments that have implicit casts that cast between the UDTs and any of the following predefined types: • Numeric • Character • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. NULL cannot be measured because there are no non-null data point pairs in the data used for the computation. IF the correlation coefficient has this value … THEN the association between the variables … This variable … Represents … x value_expression_2 y value_expression_1 CORR COVAR_SAMP(x,y) STDDEV_SAMP(x)STDDEV_SAMP(y) = ---------------------------------------------------------------------------------------------------- Data Type Format Title REAL the default format for DECIMAL(7,6) CORR(y,x) Chapter 10: Aggregate Functions CORR SQL Functions, Operators, Expressions, and Predicates 355 Implicit type conversion of UDTs for system operators and functions, including CORR, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” Combination With Other Functions CORR can be combined with ordered analytical functions in a SELECT list, QUALIFY clause, or ORDER BY clause. For information on ordered analytical functions, see Chapter 11: “Ordered Analytical Functions.” CORR cannot be combined with aggregate functions within the same SELECT list, QUALIFY clause, or ORDER BY clause. Example This example uses the data from the HomeSales table. SalesPrice NbrSold Area ---------- ------- --------- 160000 126 358711030 180000 103 358711030 200000 82 358711030 220000 75 358711030 240000 82 358711030 260000 40 358711030 280000 20 358711030 Consider the following query. SELECT CAST (CORR(NbrSold,SalesPrice) AS DECIMAL (6,4)) FROM HomeSales WHERE area = 358711030 AND SalesPrice Between 160000 AND 280000; CORR(NbrSold,SalesPrice) ------------------------ -.9543 The result -.9543 suggests an inverse relationship between the variables. That is, for the area and sales price range specified in the query, the value for NbrSold increases as sales price decreases and decreases as sales price increases. CORR Window Function For the CORR window function that performs a group, cumulative, or moving computation, see “Window Aggregate Functions” on page 449. Chapter 10: Aggregate Functions COUNT 356 SQL Functions, Operators, Expressions, and Predicates COUNT Purpose Returns a column value that is the total number of qualified rows in a group. Syntax where: Usage Notes For COUNT functions that return the group, cumulative, or moving count, see “Window Aggregate Functions” on page 449. COUNT is valid for any data type. With the exception of COUNT(*), the computation does not include nulls. For more information, see SQL Fundamentals and “Aggregates and Nulls” on page 347. Syntax element … Specifies … ALL that all non-null values of value_expression, including duplicates, are included in the total count. This is the default. DISTINCT that a value_expression that evaluates to a null value or to a duplicate value does not contribute to the total count. value_expression a constant or column expression for which the total count is computed. The expression cannot contain any ordered analytical or aggregate functions. * to count all rows in the group of rows on which COUNT operates. 1101A411 COUNT ( value_expression ) DISTINCT * ALL THIS syntax … Counts the total number of rows … COUNT(value_expression) i n the g roup for which value_expression is not null. COUNT (DISTINCT value_expression) in the group for which value_expression is unique and not null. COUNT(*) in the group of rows on which COUNT operates. Chapter 10: Aggregate Functions COUNT SQL Functions, Operators, Expressions, and Predicates 357 For an example that uses COUNT(*) and nulls, see “Example 2” on page 358. Result Type and Attributes The following table lists the data type for the result of COUNT. ANSI mode uses DECIMAL because tables frequently have a cardinality exceeding the range of INTEGER. Teradata mode uses INTEGER to avoid regression problems. When in Teradata mode, if the result of COUNT overflows and reports an error, you can cast the result to another data type, as illustrated by the following example. SELECT CAST(COUNT(*) AS BIGINT) FROM BIGTABLE; The following table lists the default format and title for the result of COUNT. For information on data type default formats, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Mode Data Type ANSI IF MaxDecimal in DBSControl is … THEN the result type is … 0, 15, or 18 DECIMAL(15,0) 38 DECIMAL(38,0) Teradata INTEGER Operation Format Title COUNT(x) Default format for result data type Count(x) COUNT(*) Default format for result data type Count(*) Chapter 10: Aggregate Functions COUNT 358 SQL Functions, Operators, Expressions, and Predicates Example 1 COUNT(*) reports the number of employees in each department because the GROUP BY clause groups results by department number. SELECT DeptNo, COUNT(*) FROM Employee GROUP BY DeptNo ORDER BY DeptNo; Without the GROUP BY clause, only the total number of employees represented in the Employee table is reported: SELECT COUNT(*) FROM Employee; Note that without the GROUP BY clause, the select list cannot include the DeptNo column because it returns any number of values and COUNT(*) returns only one value. Example 2 If any employees have been inserted but not yet assigned to a department, the return includes them as nulls in the DeptNo column. SELECT DeptNo, COUNT(*) FROM Employee GROUP BY DeptNo ORDER BY DeptNo; Assuming that two new employees are unassigned, the results table is: DeptNo Count(*) ------ -------- ? 2 100 4 300 3 500 7 600 4 700 3 Example 3 If you ran the report in Example 2 using SELECT... COUNT … without grouping the results by department number, the results table would have only registered non-null occurrences of DeptNo and would not have included the two employees not yet assigned to a department(nulls). The counts differ (23 in Example 2 as opposed to 21 using the statement documented in this example). Recall that in addition to the 21 employees in the Employee table who are assigned to a department, there are two new employees who are not yet assigned to a department (the row for each new employee has a null department number). SELECT COUNT(deptno) FROM employee ; The result of this SELECT is that COUNT returns a total of the non-null occurrences of department number. Chapter 10: Aggregate Functions COUNT SQL Functions, Operators, Expressions, and Predicates 359 Because aggregate functions ignore nulls, the two new employees are not reflected in the figure. Count(DeptNo) -------------- 21 Example 4 This example uses COUNT to provide the number of male employees in the Employee table of the database. SELECT COUNT(sex) FROM Employee WHERE sex = 'M' ; The result is as follows. Count(Sex) ---------- 12 Example 5 In this example COUNT provides, for each department, a total of the rows that have non-null department numbers. SELECT deptno, COUNT(deptno) FROM employee GROUP BY deptno ORDER BY deptno ; Notice once again that the two new employees are not included in the count. DeptNo Count(DeptNo) ------ ------------- 100 4 300 3 500 7 600 4 700 3 Example 6 To get the number of employees by department, use COUNT(*) with GROUP BY and ORDER BY clauses. SELECT deptno, COUNT(*) FROM employee GROUP BY deptno ORDER BY deptno ; Chapter 10: Aggregate Functions COUNT 360 SQL Functions, Operators, Expressions, and Predicates In this case, the nulls are included, indicated by QUESTION MARK. DeptNo Count(*) ------ -------- ? 2 100 4 300 3 500 7 600 4 700 3 Example 7 To determine the number of departments in the Employee table, use COUNT (DISTINCT) as illustrated in the following SELECT COUNT. SELECT COUNT (DISTINCT DeptNo) FROM Employee ; The system responds with the following report. Count(Distinct(DeptNo)) ----------------------- 5 Chapter 10: Aggregate Functions COVAR_POP SQL Functions, Operators, Expressions, and Predicates 361 COVAR_POP Purpose Returns the population covariance of its arguments for all non-null data point pairs. Syntax where: ANSI Compliance COVAR_POP is ANSI SQL:2008 compliant. Definition Covariance measures whether or not two random variables vary in the same way. It is the average of the products of deviations for each non-null data point pair. Note that high covariance does not imply a causal relationship between the variables. Combination With Other Functions COVAR_POP can be combined with ordered analytical functions in a SELECT list, QUALIFY clause, or ORDER BY clause. For more information on ordered analytical functions, see Chapter 11: “Ordered Analytical Functions.” COVAR_POP cannot be combined with aggregate functions within the same SELECT list, QUALIFY clause, or ORDER BY clause. Computation The equation for computing COVAR_POP is defined as follows: Syntax element … Specifies … value_expression_2 a numeric expression to be paired with a second numeric expression to determine their covariance. The expressions cannot contain any ordered analytical or aggregate functions. value_expression_1 1101B216 COVAR_POP ( value_expression_1, value_expression_2 ) COVAR_POP SUM((x – AVG(x))(y – AVG(y))) COUNT(x) = ------------------------------------------------------------------------------------ Chapter 10: Aggregate Functions COVAR_POP 362 SQL Functions, Operators, Expressions, and Predicates where: When there are no non-null data point pairs in the data used for the computation, then COVAR_POP returns NULL. Division by zero results in NULL rather than an error. Result Type and Attributes The data type, format, and title for COVAR_POP(y, x) are as follows. For information on the default format of data types and an explanation of the formatting characters in the format, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on UDT arguments that have implicit casts that cast between the UDTs and any of the following predefined types: • Numeric • Character • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. This variable … Represents … x value_expression_2 y value_expression_1 Data Type Format Title REAL COVAR_POP(y,x) IF the operand is … THEN the format is … character the default format for FLOAT. • numeric • date • interval the same format as x. UDT the format for the data type to which the UDT is implicitly cast. Chapter 10: Aggregate Functions COVAR_POP SQL Functions, Operators, Expressions, and Predicates 363 Implicit type conversion of UDTs for system operators and functions, including COVAR_POP, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” COVAR_POP Window Function For the COVAR_POP window function that performs a group, cumulative, or moving computation, see “Window Aggregate Functions” on page 449. Chapter 10: Aggregate Functions COVAR_SAMP 364 SQL Functions, Operators, Expressions, and Predicates COVAR_SAMP Purpose Returns the sample covariance of its arguments for all non-null data point pairs. Syntax where: ANSI Compliance COVAR_SAMP is ANSI SQL:2008 compliant. Definition Covariance measures whether or not two random variables vary in the same way. It is the sum of the products of deviations for each non-null data point pair. Note that high covariance does not imply a causal relationship between the variables. Combination With Other Functions COVAR_SAMP can be combined with ordered analytical functions in a SELECT list, QUALIFY clause, or ORDER BY clause. For more information on ordered analytical functions, see Chapter 11: “Ordered Analytical Functions.” COVAR_SAMP cannot be combined with aggregate functions within the same SELECT list, QUALIFY clause, or ORDER BY clause. Computation The equation for computing COVAR_SAMP is defined as follows: where: Syntax element … Specifies … value_expression_2 a numeric expression to be paired with a second numeric expression to determine their covariance. The expressions cannot contain any ordered analytical or aggregate functions. value_expression_1 1101A456 COVAR_SAMP ( value_expression_1, value_expression_2 ) COVAR_SAMP SUM((x – AVG(x))(y – AVG(y))) COUNT(x) – 1 = ------------------------------------------------------------------------------------ Chapter 10: Aggregate Functions COVAR_SAMP SQL Functions, Operators, Expressions, and Predicates 365 When there are no non-null data point pairs in the data used for the computation, then COVAR_SAMP returns NULL. Division by zero results in NULL rather than an error. Result Type and Attributes The data type, format, and title for COVAR_SAMP(y, x) are as follows. For information on the default format of data types and an explanation of the formatting characters in the format, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on UDT arguments that have implicit casts that cast between the UDTs and any of the following predefined types: • Numeric • Character • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including COVAR_SAMP, is a Teradata extension to the ANSI SQL standard. To disable this extension, This variable … Represents … x value_expression_2 y value_expression_1 Data Type Format Title REAL COVAR_SAMP(y,x) IF the operand is … THEN the format is … character the default format for FLOAT. • numeric • date • interval the same format as x. UDT the format for the data type to which the UDT is implicitly cast. Chapter 10: Aggregate Functions COVAR_SAMP 366 SQL Functions, Operators, Expressions, and Predicates set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” COVAR_SAMP Window Function For the COVAR_SAMP window function that performs a group, cumulative, or moving computation, see “Window Aggregate Functions” on page 449. Example This example is based the following regrtbl data. Nulls are indicated by the QUESTION MARK character. The following SELECT statement returns the sample covariance of weight and height where neither weight nor height is null. SELECT COVAR_SAMP(weight,height) FROM regrtbl; Covar_Samp(weight,height) ------------------------- 150 c1 height weight 1 60 84 2 62 95 3 64 140 4 66 155 5 68 119 6 70 175 7 72 145 8 74 197 9 76 150 10 76 ? 11 ? 150 12 ? ? Chapter 10: Aggregate Functions GROUPING SQL Functions, Operators, Expressions, and Predicates 367 GROUPING Purpose Returns a value that indicates whether a specified column in the result row was excluded from the grouping set of a GROUP BY clause. Syntax where: ANSI Compliance GROUPING is ANSI SQL:2008 compliant. Usage Notes A null in the result row of a grouped query containing CUBE, ROLLUP, or GROUPING SET can mean one of the following: • The actual data for the column is null. • The extended grouping specification aggregated over the column and excluded it from the particular grouping. A null in this case really represents all values for this column. Use GROUPING to distinguish between rows with nulls in actual data from rows with nulls generated from grouping sets. Result Type and Attributes The data type, format, and title for GROUPING(x) are as follows. Syntax element … Specifies … expression a column in the result row that might have been excluded from a grouped query containing CUBE, ROLLUP, or GROUPING SET. The argument must be an item of a GROUP BY clause. 1101A461 GROUPING expression ( ( Data Type Format Title INTEGER Default format of the INTEGER data type Grouping(x) Chapter 10: Aggregate Functions GROUPING 368 SQL Functions, Operators, Expressions, and Predicates For information on the default format of data types, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Result Value Example Suppose you have the following data in the sales_view table. To look at sales summaries by county and by city, use the following SELECT statement: SELECT county, city, sum(margin) FROM sale_view GROUP BY GROUPING SETS ((county),(city)); The query reports the following data: County City Sum(margin) ----------- ---------- ----------- Los Angeles ? 38700 San Diego ? 19500 ? Long Beach 24300 ? San Diego 19500 ? Avalon 14400 Notice that in this example, a null represents all values for a column because the column was excluded from the grouping set represented. To distinguish between rows with nulls in actual data from rows with nulls generated from grouping sets, use the GROUPING function: SELECT county, city, sum(margin), GROUPING(county) AS County_Grouping, GROUPING(city) AS City_Grouping FROM sale_view GROUP BY GROUPING SETS ((county),(city)); IF the value of the specified column in the result row is … THEN GROUPING returns … a null value generated when the extended grouping specification aggregated over the column and excluded it from the particular grouping 1 anything else 0 PID Cost Sale Margin State County City 1 38350 50150 11800 CA Los Angeles Long Beach 1 63375 82875 19500 CA San Diego San Diego 1 46800 61200 14400 CA Los Angeles Avalon 2 40625 53125 12500 CA Los Angeles Long Beach Chapter 10: Aggregate Functions GROUPING SQL Functions, Operators, Expressions, and Predicates 369 The results are: County City Sum(margin) County_Grouping City_Grouping ----------- ---------- ----------- --------------- ------------- Los Angeles ? 38700 0 1 San Diego ? 19500 0 1 ? Long Beach 24300 1 0 ? San Diego 19500 1 0 ? Avalon 14400 1 0 You can also use GROUPING to replace the nulls that appear in a result row because the extended grouping specification aggregated over a column and excluded it from the particular grouping. For example: SELECT CASE WHEN GROUPING(county) = 1 THEN '-All Counties-' ELSE county END AS County, CASE WHEN GROUPING(city) = 1 THEN '-All Cities-' ELSE city END AS City, SUM(margin) FROM sale_view GROUP BY GROUPING SETS (county,city); The query reports the following data: County City Sum(margin) -------------- ------------ ----------- Los Angeles -All Cities- 38700 San Diego -All Cities- 19500 -All Counties- Long Beach 24300 -All Counties- San Diego 19500 -All Counties- Avalon 14400 Related Topics For more information on GROUP BY, GROUPING SETS, ROLLUP, and CUBE, see SQL Data Manipulation Language. Chapter 10: Aggregate Functions KURTOSIS 370 SQL Functions, Operators, Expressions, and Predicates KURTOSIS Purpose Returns the kurtosis of the distribution of value_expression. Syntax where: ANSI Compliance KURTOSIS is a Teradata extension to the ANSI SQL:2008 standard. Definition Kurtosis is the fourth moment of a distribution. It is a measure of the relative peakedness or flatness compared with the normal, Gaussian distribution. The normal distribution has a kurtosis of 0. Positive kurtosis indicates a relative peakedness of the distribution, while negative kurtosis indicates a relative flatness. Result Type and Attributes The data type, format, and title for KURTOSIS(x) are as follows. Syntax element … Specifies … ALL to include all non-null values specified by value_expression, including duplicates, in the computation. This is the default. DISTINCT to exclude duplicates specified by value_expression from the computation. value_expression a constant or column expression for which the kurtosis of the distribution of its values is to be computed. The expression cannot contain any ordered analytical or aggregate functions. 1101B215 KURTOSIS ( value_expression ) DISTINCT ALL Data Type Format Title REAL Default format of the REAL data type Kurtosis(x) Chapter 10: Aggregate Functions KURTOSIS SQL Functions, Operators, Expressions, and Predicates 371 For information on the default format of data types, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on a UDT argument that has an implicit cast that casts between the UDT and any of the following predefined types: • Numeric • Character • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including KURTOSIS, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” Computation The equation for computing KURTOSIS is defined as follows: where: Conditions That Produce a NULL Return Value The following conditions produce a null return value: • Fewer than four non-null data points in the data used for the computation • STDDEV_SAMP(x) = 0 • Division by zero Kurtosis (COUNT(x))(COUNT(x) + 1) (COUNT(x) – 1)(COUNT(x) – 2)(COUNT(x) – 3) ----------------------------------------------------------------------------------------------------------------------------- ? ? ? ? SUM x – AVG(x) STDEV_SAMP(x) ? (---------------------------------------------**4)? ? ? (3)((COUNT(x) – 1)(**2)) (COUNT(x) – 2)(COUNT(x) – 3) ----------------------------------------------------------------------------------- ? ? = – ? ? This variable … Represents … x value_expression Chapter 10: Aggregate Functions MAX 372 SQL Functions, Operators, Expressions, and Predicates MAX Purpose Returns a column value that is the maximum value for value_expression for a group. Syntax where: ANSI Compliance MAX is ANSI SQL:2008 compliant. MAXIMUM is a Teradata extension to the ANSI SQL:2008 standard. Result Type and Attributes The following table lists the default attributes for the result of MAX(x). Syntax element … Specifies … ALL that all non-null values specified by value_expression, including duplicates, are included in the maximum value computation for the group. This is the default. DISTINCT that duplicate and non-null values specified by value_expression are eliminated from the maximum value computation for the group. value_expression a constant or column expression for which the maximum value is to be computed. The expression cannot contain any ordered analytical or aggregate functions. 1101B412 MAXIMUM ( value_expression ) DISTINCT ALL MAX Attribute Value Data Type IF operand x is … THEN the result data type is the data type … not a UDT of operand x. a UDT to which the UDT is implicitly cast. Chapter 10: Aggregate Functions MAX SQL Functions, Operators, Expressions, and Predicates 373 Support for UDTs By default, Teradata Database performs implicit type conversion on a UDT argument that has an implicit cast that casts between the UDT and any of the following predefined types: • Numeric • Character • Byte • DATE • TIME or TIMESTAMP • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including MAX, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” Usage Notes MAX is valid for character data as well as numeric data. When used with a character expression, MAX returns the highest sort order. Nulls are not included in the result computation. For more information, see SQL Fundamentals and “Aggregates and Nulls” on page 347. If value_expression is a column expression, the column must refer to at least one column in the table from which data is selected. The value_expression must not specify a column reference to a view column that is derived from a function. Format IF operand x is … THEN the result format is the format of … not a UDT operand x. a UDT the type to which the UDT is implicitly cast. Title Maximum(x) Attribute Value Chapter 10: Aggregate Functions MAX 374 SQL Functions, Operators, Expressions, and Predicates MAX Window Function For the MAX window function that computes a group, cumulative, or moving maximum value, see “Window Aggregate Functions” on page 449. Example 1: CHARACTER Data The following SELECT returns the immediately following result. SELECT MAX(Name) FROM Employee; Maximum(Name) ------------- Zorn J Example 2: Column Expressions You want to know which item in your warehouse stock has the maximum cost of sales. SELECT MAX(CostOfSales) AS m, ProdID FROM Inventory GROUP BY ProdID ORDER BY m DESC; Maximum(CostOfSales) ProdID -------------------- ------ 1295 3815 975 4400 950 4120 Chapter 10: Aggregate Functions MIN SQL Functions, Operators, Expressions, and Predicates 375 MIN Purpose Returns a column value that is the minimum value for value_expression for a group. Syntax where: ANSI Compliance MIN is ANSI SQL:2008 compliant. MINIMUM is a Teradata extension to the ANSI SQL:2008 standard. Result Type and Attributes The following table lists the default attributes for the result of MIN(x). Syntax element … Specifies … ALL that all non-null values specified by value_expression, including duplicates, are included in the minimum value computation for the group. This is the default. DISTINCT that duplicate and non-null values specified by value_expression are eliminated from the minimum value computation for the group. value_expression a constant or column expression for which the minimum value is to be computed. The expression cannot contain any ordered analytical or aggregate functions. 1101B413 MINIMUM ( value_expression ) MIN DISTINCT ALL Attribute Value Data Type IF operand x is … THEN the result data type is the data type … not a UDT of operand x. a UDT to which the UDT is implicitly cast. Title Minimum(x) Chapter 10: Aggregate Functions MIN 376 SQL Functions, Operators, Expressions, and Predicates Support for UDTs By default, Teradata Database performs implicit type conversion on a UDT argument that has an implicit cast that casts between the UDT and any of the following predefined types: • Numeric • Character • Byte • DATE • TIME or TIMESTAMP • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including MIN, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” Usage Notes MINIMUM is valid for character data as well as numeric data. MINIMUM returns the lowest sort order of a character expression. The computation does not include nulls. For more information, see “Manipulating Nulls” in SQL Fundamentals and “Aggregates and Nulls” on page 347. If value_expression specifies a column expression, the expression must refer to at least one column in the table from which data is selected. If value_expression specifies a column reference, the column must not be a view column that is derived from a function. Format IF operand x is … THEN the result format is the format of … not a UDT operand x. a UDT the type to which the UDT is implicitly cast. Attribute Value Chapter 10: Aggregate Functions MIN SQL Functions, Operators, Expressions, and Predicates 377 MIN Window Function For the MIN window function that computes a group, cumulative, or moving minimum value, see “Window Aggregate Functions” on page 449. Example 1: MINIMUM Used With CHARACTER Data The following SELECT returns the immediately following result. SELECT MINIMUM(Name) FROM Employee; Minimum(Name) ------------- Aarons A Example 2: JIT Inventory Your manufacturing shop has recently changed vendors and you know that you have no quantity of parts from that vendor that exceeds 20 items for the ProdID. You need to know how many of your other inventory items are low enough that you need to schedule a new shipment, where “low enough” is defined as fewer than 30 items in the QUANTITY column for the part. SELECT ProdID, MINIMUM(QUANTITY) FROM Inventory WHERE QUANTITY BETWEEN 20 AND 30 GROUP BY ProdID ORDER BY ProdID; The report is as follows: ProdID Minimum(Quantity) ----------- ----------------- 1124 24 1355 21 3215 25 4391 22 Chapter 10: Aggregate Functions REGR_AVGX 378 SQL Functions, Operators, Expressions, and Predicates REGR_AVGX Purpose Returns the mean of the independent_variable_expression for all non-null data pairs of the dependent and independent variable arguments. Syntax where: ANSI Compliance REGR_AVGX is ANSI SQL:2008 compliant. Setting Up Axes for Plotting If you export the data for plotting, define the y-axis (ordinate) as the dependent variable and the x-axis (abscissa) as the independent variable. Combination With Other Functions REGR_AVGX can be combined with ordered analytical functions in a SELECT list, QUALIFY clause, or ORDER BY clause. For more information on ordered analytical functions, see Chapter 11: “Ordered Analytical Functions.” REGR_AVGX cannot be combined with aggregate functions within the same SELECT list, QUALIFY clause, or ORDER BY clause. Syntax element … Specifies … dependent_variable_expression the dependent variable for the regression. A dependent variable is something that is measured in response to a treatment. The expression cannot contain any ordered analytical or aggregate functions. independent_variable_expression the independent variable for the regression. An independent variable is a treatment: something that is varied under your control to test the behavior of another variable. The expression cannot contain any ordered analytical or aggregate functions. 1101B414 REGR_AVGX ( dependent_variable_expression, independent_variable_expression ) Chapter 10: Aggregate Functions REGR_AVGX SQL Functions, Operators, Expressions, and Predicates 379 Computation The equation for computing REGR_AVGX is: where: When there are fewer than two non-null data point pairs in the data used for the computation, then REGR_AVGX returns NULL. Division by zero results in NULL rather than an error. Result Type and Attributes The data type, format, and title for REGR_AVGX(y, x) are as follows. For information on the default format of data types and an explanation of the formatting characters in the format, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on UDT arguments that have implicit casts that cast between the UDTs and any of the following predefined types: • Numeric • Character This variable … Represents … x independent_variable_expression x is the independent, or predictor, variable expression. n COUNT(x) REGR_AVGX SUM(x) n = ------------------- Data Type Format Title REAL REGR_AVGX(y,x) IF the operand is … THEN the format is … character the default format for FLOAT. • numeric • date • interval the same format as x. UDT the format for the data type to which the UDT is implicitly cast. Chapter 10: Aggregate Functions REGR_AVGX 380 SQL Functions, Operators, Expressions, and Predicates • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including REGR_AVGX, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” REGR_AVGX Window Function For the REGR_AVGX window function that performs a group, cumulative, or moving computation, see “Window Aggregate Functions” on page 449. Example This example is based the following regrtbl data. Nulls are indicated by the QUESTION MARK character. c1 height weight -- ------ ------ 1 60 84 2 62 95 3 64 140 4 66 155 5 68 119 6 70 175 7 72 145 8 74 197 9 76 150 10 76 ? 11 ? 150 12 ? ? The following SELECT statement returns the mean height for regrtbl where neither weight nor height is null. SELECT REGR_AVGX(weight,height) FROM regrtbl; Regr_Avgx(weight,height) ------------------------ 68 Chapter 10: Aggregate Functions REGR_AVGY SQL Functions, Operators, Expressions, and Predicates 381 REGR_AVGY Purpose Returns the mean of the dependent_variable_expression for all non-null data pairs of the dependent and independent variable arguments. Syntax where: ANSI Compliance REGR_AVGY is ANSI SQL:2008 compliant. Setting Up Axes for Plotting If you export the data for plotting, define the y-axis (ordinate) as the dependent variable and the x-axis (abscissa) as the independent variable. Combination With Other Functions REGR_AVGY can be combined with ordered analytical functions in a SELECT list, QUALIFY clause, or ORDER BY clause. For more information on ordered analytical functions, see Chapter 11: “Ordered Analytical Functions.” REGR_AVGY cannot be combined with aggregate functions within the same SELECT list, QUALIFY clause, or ORDER BY clause. Syntax element … Specifies … dependent_variable_expression the dependent variable for the regression. A dependent variable is something that is measured in response to a treatment. The expression cannot contain any ordered analytical or aggregate functions. independent_variable_expression the independent variable for the regression. An independent variable is a treatment: something that is varied under your control to test the behavior of another variable. The expression cannot contain any ordered analytical or aggregate functions. 1101B415 REGR_AVGY (dependent_variable_expression, independent_variable_expression ) Chapter 10: Aggregate Functions REGR_AVGY 382 SQL Functions, Operators, Expressions, and Predicates Computation The equation for computing REGR_AVGY is: where: When there are fewer than two non-null data point pairs in the data used for the computation, then REGR_AVGY returns NULL. Division by zero results in NULL rather than an error. Result Type and Attributes The data type, format, and title for REGR_AVGY(y, x) are as follows. For information on the default format of data types and an explanation of the formatting characters in the format, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on UDT arguments that have implicit casts that cast between the UDTs and any of the following predefined types: • Numeric • Character This variable … Represents … y dependent_variable_expression y is the dependent, or response, variable expression. n COUNT(y) REGR_AVGY SUM(y) n = ------------------- Data Type Format Title REAL REGR_AVGY(y,x) IF the operand is … THEN the format is … character the default format for FLOAT. • numeric • date • interval the same format as x. UDT the format for the data type to which the UDT is implicitly cast. Chapter 10: Aggregate Functions REGR_AVGY SQL Functions, Operators, Expressions, and Predicates 383 • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including REGR_AVGY, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” REGR_AVGY Window Function For the REGR_AVGY window function that performs a group, cumulative, or moving computation, see “Window Aggregate Functions” on page 449. Example This example is based the following regrtbl data. Nulls are indicated by the QUESTION MARK character. c1 height weight -- ------ ------ 1 60 84 2 62 95 3 64 140 4 66 155 5 68 119 6 70 175 7 72 145 8 74 197 9 76 150 10 76 ? 11 ? 150 12 ? ? The following SELECT statement returns the mean weight from regrtbl where neither height nor weight is null. SELECT REGR_AVGY(weight,height) FROM regrtbl; Regr_Avgy(weight,height) ------------------------ 140 Chapter 10: Aggregate Functions REGR_COUNT 384 SQL Functions, Operators, Expressions, and Predicates REGR_COUNT Purpose Returns the count of all non-null data pairs of the dependent and independent variable arguments. Syntax where: ANSI Compliance REGR_COUNT is ANSI SQL:2008 compliant. Setting Up Axes for Plotting If you export the data for plotting, define the y-axis (ordinate) as the dependent variable and the x-axis (abscissa) as the independent variable. Combination With Other Functions REGR_COUNT can be combined with ordered analytical functions in a SELECT list, QUALIFY clause, or ORDER BY clause. For more information on ordered analytical functions, see Chapter 11: “Ordered Analytical Functions.” REGR_COUNT cannot be combined with aggregate functions within the same SELECT list, QUALIFY clause, or ORDER BY clause. Syntax element … Specifies … dependent_variable_expression the dependent variable for the regression. A dependent variable is something that is measured in response to a treatment. The expression cannot contain any ordered analytical or aggregate functions. independent_variable_expression the independent variable for the regression. An independent variable is a treatment: something that is varied under your control to test the behavior of another variable. The expression cannot contain any ordered analytical or aggregate functions. 1101B416 REGR_COUNT (dependent_variable_expression, independent_variable_expression ) Chapter 10: Aggregate Functions REGR_COUNT SQL Functions, Operators, Expressions, and Predicates 385 Result Type and Attributes The following table lists the result type of REGR_COUNT(y,x). The result type of REGR_COUNT is consistent with the result type of COUNT for ANSI transaction mode and Teradata transaction mode. When in Teradata mode, if the result of REGR_COUNT overflows and reports an error, you can cast the result to another data type, as illustrated by the following example. SELECT CAST(REGR_COUNT(weight,height) AS BIGINT) FROM regrtbl; The following table lists the default format and title for the result of REGR_COUNT(y, x). For information on data type default formats, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on UDT arguments that have implicit casts that cast between the UDTs and any of the following predefined types: • Numeric • Character • DATE • Interval Mode Data Type ANSI IF MaxDecimal in DBSControl is … THEN the result type is … 0, 15, or 18 DECIMAL(15,0) 38 DECIMAL(38,0) Teradata INTEGER Format Title REGR_COUNT(y,x) IF operand y is … THEN the format is … character the default format for FLOAT. numeric the same format as y. UDT the format for the data type to which the UDT is implicitly cast. Chapter 10: Aggregate Functions REGR_COUNT 386 SQL Functions, Operators, Expressions, and Predicates To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including REGR_COUNT, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” REGR_COUNT Window Function For the REGR_COUNT window function that performs a group, cumulative, or moving computation, see “Window Aggregate Functions” on page 449. Example This example is based the following regrtbl data. Nulls are indicated by the QUESTION MARK character. The following SELECT statement returns the number of rows in regrtbl where neither height nor weight is null. SELECT REG_COUNT(weight,height) FROM regrtbl; Regr_Count(weight,height) c1 height weight 1 60 84 2 62 95 3 64 140 4 66 155 5 68 119 6 70 175 7 72 145 8 74 197 9 76 150 10 76 ? 11 ? 150 12 ? ? Chapter 10: Aggregate Functions REGR_COUNT SQL Functions, Operators, Expressions, and Predicates 387 ------------------------- 9 Chapter 10: Aggregate Functions REGR_INTERCEPT 388 SQL Functions, Operators, Expressions, and Predicates REGR_INTERCEPT Purpose Returns the intercept of the univariate linear regression line through all non-null data pairs of the dependent and independent variable arguments. Syntax where: ANSI Compliance REGR_INTERCEPT is ANSI SQL:2008 compliant. Definition The intercept is the point at which the regression line through the non-null data pairs in the sample intersects the ordinate, or y-axis, of the graph. The plot of the linear regression on the variables is used to predict the behavior of the dependent variable from the change in the independent variable. Note that this computation assumes a linear relationship between the variables. There can be a strong nonlinear relationship between independent and dependent variables, and the computation of the simple linear regression between such variable pairs does not reflect such a relationship. Independent and Dependent Variables An independent variable is a treatment: something that is varied under your control to test the behavior of another variable. A dependent variable is something that is measured in response to a treatment. Syntax element … Specifies … dependent_variable_expression the dependent variable for the regression. The expression cannot contain any ordered analytical or aggregate functions. independent_variable_expression the independent variable for the regression. The expression cannot contain any ordered analytical or aggregate functions. 1101B417 REGR_INTERCEPT ( dependent_variable_expression, independent_variable_expression ) Chapter 10: Aggregate Functions REGR_INTERCEPT SQL Functions, Operators, Expressions, and Predicates 389 For example, you might want to test the ability of various promotions to enhance sales of a particular item. In this case, the promotion is the independent variable and the sales of the item made as a result of the individual promotion is the dependent variable. The value of the linear regression intercept tells you the predicted value for sales when there is no promotion for the item selected for analysis. Setting Up Axes for Plotting If you export the data for plotting, define the y-axis (ordinate) as the dependent variable and the x-axis (abscissa) as the independent variable. Combination With Other Functions REGR_INTERCEPT can be combined with any of the ordered analytical functions in a SELECT list, QUALIFY clause, or ORDER BY clause. For more information on ordered analytical functions, see Chapter 11: “Ordered Analytical Functions.” REGR_INTERCEPT cannot be combined with aggregate functions within the same SELECT list, QUALIFY clause, or ORDER BY clause. Computation The equation for computing REGR_INTERCEPT is defined as follows: where: When there are fewer than two non-null data point pairs in the data used for the computation, then REGR_INTERCEPT returns NULL. Division by zero results in NULL rather than an error. Result Type and Attributes The data type, format, and title for REGR_INTERCEPT(y, x) are as follows. This variable … Represents … x independent_variable_expression y dependent_variable_expression REGR_INTERCEPT = AVG(y) – REGR_SLOPE(y,x)AVG(x) Data Type Format Title REAL Default format of the REAL data type REGR_INTERCEPT(y,x) Chapter 10: Aggregate Functions REGR_INTERCEPT 390 SQL Functions, Operators, Expressions, and Predicates For information on the default format of data types and an explanation of the formatting characters in the format, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on UDT arguments that have implicit casts that cast between the UDTs and any of the following predefined types: • Numeric • Character • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including REGR_INTERCEPT, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For details on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” REGR_INTERCEPT Window Function For the REGR_INTERCEPT window function that performs a group, cumulative, or moving computation, see “Window Aggregate Functions” on page 449. Example This example uses the data from the HomeSales table. The following query returns the intercept of the regression line for NbrSold and SalesPrice in the range of 160000 to 280000 in the 358711030 area. SalesPrice NbrSold Area 160000 126 358711030 180000 103 358711030 200000 82 358711030 220000 75 358711030 240000 82 358711030 260000 40 358711030 280000 20 358711030 Chapter 10: Aggregate Functions REGR_INTERCEPT SQL Functions, Operators, Expressions, and Predicates 391 SELECT CAST (REGR_INTERCEPT(NbrSold,SalesPrice) AS DECIMAL (5,1)) FROM HomeSales WHERE area = 358711030 AND SalesPrice BETWEEN 160000 AND 280000; Here is the result: REGR_INTERCEPT(NbrSold,SalesPrice) ---------------------------------- 249.9 Chapter 10: Aggregate Functions REGR_R2 392 SQL Functions, Operators, Expressions, and Predicates REGR_R2 Purpose Returns the coefficient of determination for all non-null data pairs of the dependent and independent variable arguments. Syntax where: ANSI Compliance REGR_R2 is ANSI SQL:2008 compliant. Setting Up Axes for Plotting If you export the data for plotting, define the y-axis (ordinate) as the dependent variable and the x-axis (abscissa) as the independent variable. Combination With Other Functions REGR_R2 can be combined with any of the ordered analytical functions in a SELECT list, QUALIFY clause, or ORDER BY clause. For more information on ordered analytical functions, see Chapter 11: “Ordered Analytical Functions.” REGR_R2 cannot be combined with aggregate functions within the same SELECT list, QUALIFY clause, or ORDER BY clause. Syntax element … Specifies … dependent_variable_expression the dependent variable for the regression. A dependent variable is something that is measured in response to a treatment. The expression cannot contain any ordered analytical or aggregate functions. independent_variable_expression the independent variable for the regression. An independent variable is a treatment: something that is varied under your control to test the behavior of another variable. The expression cannot contain any ordered analytical or aggregate functions. 1101B418 REGR_R2 ( dependent_variable_expression, independent_variable_expression ) Chapter 10: Aggregate Functions REGR_R2 SQL Functions, Operators, Expressions, and Predicates 393 Computation The coefficient of determination for two variables is the square of their Pearson productmoment correlation. The equation for computing REGR_R2 is defined as follows: where: When there are fewer than two non-null data point pairs in the data used for the computation, then REGR_R2 returns NULL. Division by zero results in NULL rather than an error. Result Type and Attributes The data type, format, and title for REGR_R2(y, x) are as follows. For information on the default format of data types and an explanation of the formatting characters in the format, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. REGR_R2 POWER(COUNT(xy) • SUM(xy) – SUM(x) • SUM(y) , 2) ((COUNT(xy) • SUM(x**2) – SUM(x) • SUM(x)) • (COUNT(xy) • SUM(y**2) – SUM(y) • SUM(y))) = ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- This variable … Represents … x independent_variable_expression x is the independent, or predictor, variable expression. y dependent_variable_expression y is the dependent, or response, variable expression. Data Type Format Title REAL REGR_R2(y,x) IF the operand is … THEN the format is … character the default format for FLOAT. numeric the same format as x. UDT the format for the data type to which the UDT is implicitly cast. Chapter 10: Aggregate Functions REGR_R2 394 SQL Functions, Operators, Expressions, and Predicates Support for UDTs By default, Teradata Database performs implicit type conversion on UDT arguments that have implicit casts that cast between the UDTs and any of the following predefined types: • Numeric • Character • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including REGR_R2, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” REGR_R2 Window Function For the REGR_R2 window function that performs a group, cumulative, or moving computation, see “Window Aggregate Functions” on page 449. Example This example is based the following regrtbl data. Nulls are indicated by the QUESTION MARK character. c1 height weight -- ------ ------ 1 60 84 2 62 95 3 64 140 4 66 155 5 68 119 6 70 175 7 72 145 8 74 197 9 76 150 10 76 ? 11 ? 150 12 ? ? The following SELECT statement returns the coefficient of determination for height and weight where neither height nor weight is null. SELECT CAST(REGR_R2(weight,height) AS DECIMAL(4,2)) FROM regrtbl; REGR_R2(weight,height) Chapter 10: Aggregate Functions REGR_R2 SQL Functions, Operators, Expressions, and Predicates 395 ---------------------- .58 Chapter 10: Aggregate Functions REGR_SLOPE 396 SQL Functions, Operators, Expressions, and Predicates REGR_SLOPE Purpose Returns the slope of the univariate linear regression line through all non-null data pairs of the dependent and independent variable arguments. Syntax where: ANSI Compliance REGR_SLOPE is ANSI SQL:2008 compliant. Definition The slope of the best fit linear regression is a measure of the rate of change of the regression of one independent variable on the dependent variable. The plot of the linear regression on the variables is used to predict the behavior of the dependent variable from the change in the independent variable. Note that this computation assumes a linear relationship between the variables. There can be a strong nonlinear relationship between independent and dependent variables, and the computation of the simple linear regression between such variable pairs does not reflect such a relationship. Independent and Dependent Variables An independent variable is a treatment: something that is varied under your control to test the behavior of another variable. Syntax element … Specifies … dependent_variable_expression the dependent variable for the regression. The expression cannot contain any ordered analytical or aggregate functions. independent_variable_expression the independent variable for the regression. The expression cannot contain any ordered analytical or aggregate functions. 1101B419 REGR_SLOPE ( dependent_variable_expression, independent_variable_expression ) Chapter 10: Aggregate Functions REGR_SLOPE SQL Functions, Operators, Expressions, and Predicates 397 A dependent variable is something that is measured in response to a treatment. For example, you might want to test the ability of various promotions to enhance sales of a particular item. In this case, the promotion is the independent variable and the sales of the item made as a result of the individual promotion is the dependent variable. Setting Up Axes for Plotting If you export the data for plotting, define the y-axis (ordinate) as the dependent variable and the x-axis (abscissa) as the independent variable. Combination With Other Functions REGR_SLOPE can be combined with ordered analytical functions in a SELECT list, QUALIFY clause, or ORDER BY clause. For more information on ordered analytical functions, see Chapter 11: “Ordered Analytical Functions.” REGR_SLOPE cannot be combined with aggregate functions within the same SELECT list, QUALIFY clause, or ORDER BY clause. Computation The equation for computing REGR_SLOPE is defined as follows: where: When there are fewer than two non-null data point pairs in the data used for the computation, then REGR_SLOPE returns NULL. Division by zero results in NULL rather than an error. Result Type and Attributes The data type, format, and title for REGR_SLOPE(y, x) are as follows. This variable … Represents … x independent_variable_expression y dependent_variable_expression REGR_SLOPE (COUNT(x)SUM(x*y)) – (SUM(x)SUM(y)) (COUNT(x)SUM(x**2)) – (SUM(x)**2) = ------------------------------------------------------------------------------------------------------------- Data Type Format Title REAL Default format of the REAL data type REGR_SLOPE(y,x) Chapter 10: Aggregate Functions REGR_SLOPE 398 SQL Functions, Operators, Expressions, and Predicates For information on the default format of data types and the formatting characters in the format, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on UDT arguments that have implicit casts that cast between the UDTs and any of the following predefined types: • Numeric • Character • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including REGR_SLOPE, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” REGR_SLOPE Window Function For the REGR_SLOPE window function that performs a group, cumulative, or moving computation, see “Window Aggregate Functions” on page 449. Example This example uses the data from the HomeSales table. SalesPrice NbrSold Area 160000 126 358711030 180000 103 358711030 200000 82 358711030 220000 75 358711030 240000 82 358711030 260000 40 358711030 280000 20 358711030 Chapter 10: Aggregate Functions REGR_SLOPE SQL Functions, Operators, Expressions, and Predicates 399 The following query returns the slope of the regression line for NbrSold and SalesPrice in the range of 160000 to 280000 in the 358711030 area. SELECT CAST (REGR_SLOPE(NbrSold,SalesPrice) AS FLOAT) FROM HomeSales WHERE area = 358711030 AND SalesPrice BETWEEN 160000 AND 280000; Here is the result: REGR_SLOPE(NbrSold,SalesPrice) ------------------------------ -7.92857142857143E-004 Chapter 10: Aggregate Functions REGR_SXX 400 SQL Functions, Operators, Expressions, and Predicates REGR_SXX Purpose Returns the sum of the squares of the independent_variable_expression for all non-null data pairs of the dependent and independent variable arguments. Syntax where: ANSI Compliance REGR_SXX is ANSI SQL:2008 compliant. Setting Up Axes for Plotting If you export the data for plotting, define the y-axis (ordinate) as the dependent variable and the x-axis (abscissa) as the independent variable. Combination With Other Functions REGR_SXX can be combined with any of the ordered analytical functions in a SELECT list, QUALIFY clause, or ORDER BY clause. For more information on ordered analytical functions, see Chapter 11: “Ordered Analytical Functions.” REGR_SXX cannot be combined with aggregate functions within the same SELECT list, QUALIFY clause, or ORDER BY clause. Syntax element … Specifies … dependent_variable_expression the dependent variable for the regression. A dependent variable is something that is measured in response to a treatment. The expression cannot contain any ordered analytical or aggregate functions. independent_variable_expression the independent variable for the regression. An independent variable is a treatment: something that is varied under your control to test the behavior of another variable. The expression cannot contain any ordered analytical or aggregate functions. 1101B420 REGR_SXX (dependent_variable_expression, independent_variable_expression ) Chapter 10: Aggregate Functions REGR_SXX SQL Functions, Operators, Expressions, and Predicates 401 Computation The equation for computing REGR_SXX is defined as follows: where: When there are fewer than two non-null data point pairs in the data used for the computation, then REGR_SXX returns NULL. Division by zero results in NULL rather than an error. Result Type and Attributes The data type, format, and title for REGR_SXX(y, x) are as follows. For information on the default format of data types and an explanation of the formatting characters in the format, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on UDT arguments that have implicit casts that cast between the UDTs and any of the following predefined types: • Numeric • Character This variable … Represents … x independent_variable_expression x is the independent, or predictor, variable expression. n COUNT(x) REGR_SXX (SUM(x**2)) SUM(x) SUM(x) n ------------------- ? ? ? ? • ? ? ? ? = – Data Type Format Title REAL REGR_SXX(y,x) IF the operand is … THEN the format is … character the default format for FLOAT. • numeric • date • interval the same format as x. UDT the format for the data type to which the UDT is implicitly cast. Chapter 10: Aggregate Functions REGR_SXX 402 SQL Functions, Operators, Expressions, and Predicates • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including REGR_SXX, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” REGR_SXX Window Function For the REGR_SXX window function that performs a group, cumulative, or moving computation, see “Window Aggregate Functions” on page 449. Example This example is based the following regrtbl data. Nulls are indicated by the QUESTION MARK character. c1 height weight -- ------ ------ 1 60 84 2 62 95 3 64 140 4 66 155 5 68 119 6 70 175 7 72 145 8 74 197 9 76 150 10 76 ? 11 ? 150 12 ? ? The following SELECT statement returns the sum of squares for height where neither height nor weight is null. SELECT REGR_SXX(weight,height) FROM regrtbl; Regr_Sxx(weight,height) ----------------------- 240 Chapter 10: Aggregate Functions REGR_SXY SQL Functions, Operators, Expressions, and Predicates 403 REGR_SXY Purpose Returns the sum of the products of the independent_variable_expression and the dependent_variable_expression for all non-null data pairs of the dependent and independent variable arguments. Syntax where: ANSI Compliance REGR_SXY is ANSI SQL:2008 compliant. Setting Up Axes for Plotting If you export the data for plotting, define the y-axis (ordinate) as the dependent variable and the x-axis (abscissa) as the independent variable. Combination With Other Functions REGR_SXY can be combined with any of the ordered analytical functions in a SELECT list, QUALIFY clause, or ORDER BY clause. For more information on ordered analytical functions, see Chapter 11: “Ordered Analytical Functions.” REGR_SXY cannot be combined with aggregate functions within the same SELECT list, QUALIFY clause, or ORDER BY clause. Syntax element … Specifies … dependent_variable_expression the dependent variable for the regression. A dependent variable is something that is measured in response to a treatment. The expression cannot contain any ordered analytical or aggregate functions. independent_variable_expression the independent variable for the regression. An independent variable is a treatment: something that is varied under your control to test the behavior of another variable. The expression cannot contain any ordered analytical or aggregate functions. 1101B421 REGR_SXY (dependent_variable_expression, independent_variable_expression ) Chapter 10: Aggregate Functions REGR_SXY 404 SQL Functions, Operators, Expressions, and Predicates Computation The equation for computing REGR_SXY is defined as follows: When there are fewer than two non-null data point pairs in the data used for the computation, then REGR_SXY returns NULL. Division by zero results in NULL rather than an error. Result Type and Attributes The data type, format, and title for REGR_SXY(y, x) are as follows. For information on the default format of data types, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on UDT arguments that have implicit casts that cast between the UDTs and any of the following predefined types: • Numeric • Character This variable … Represents … x independent_variable_expression x is the independent, or predictor, variable expression. y dependent_variable_expression y is the dependent, or response, variable expression. n COUNT(x,y) REGR_SXY (SUM(x*y)) (SUM(x)) SUM(y) n ------------------- ? ? • ? ? ? ? = – ? ? Data Type Format Title REAL REGR_SXY(y,x) IF the operand is … THEN the format is … character the default format for FLOAT. • numeric • date • interval the same format as x. UDT the format for the data type to which the UDT is implicitly cast. Chapter 10: Aggregate Functions REGR_SXY SQL Functions, Operators, Expressions, and Predicates 405 • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including REGR_SXY, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” REGR_SXY Window Function For the REGR_SXY window function that performs a group, cumulative, or moving computation, see “Window Aggregate Functions” on page 449. Example This example is based the following regrtbl data. Nulls are indicated by the QUESTION MARK character. c1 height weight -- ------ ------ 1 60 84 2 62 95 3 64 140 4 66 155 5 68 119 6 70 175 7 72 145 8 74 197 9 76 150 10 76 ? 11 ? 150 12 ? ? The following SELECT statement returns the sum of products of height and weight where neither height nor weight is null. SELECT REGR_SXY(weight,height) FROM regrtbl; Regr_Sxy(weight,height) ----------------------- 1200 Chapter 10: Aggregate Functions REGR_SYY 406 SQL Functions, Operators, Expressions, and Predicates REGR_SYY Purpose Returns the sum of the squares of the dependent_variable_expression for all non-null data pairs of the dependent and independent variable arguments. Syntax where: ANSI Compliance REGR_SYY is ANSI SQL:2008 compliant. Setting Up Axes for Plotting If you export the data for plotting, define the y-axis (ordinate) as the dependent variable and the x-axis (abscissa) as the independent variable. Combination With Other Functions REGR_SYY can be combined with any of the ordered analytical functions in a SELECT list, QUALIFY clause, or ORDER BY clause. For more information on ordered analytical functions, see Chapter 11: “Ordered Analytical Functions.” REGR_SYY cannot be combined with aggregate functions within the same SELECT list, QUALIFY clause, or ORDER BY clause. Syntax element … Specifies … dependent_variable_expression the dependent variable for the regression. A dependent variable is something that is measured in response to a treatment. The expression cannot contain any ordered analytical or aggregate functions. independent_variable_expression the independent variable for the regression. An independent variable is a treatment: something that is varied under your control to test the behavior of another variable. The expression cannot contain any ordered analytical or aggregate functions. 1101B422 REGR_SYY (dependent_variable_expression, independent_variable_expression ) Chapter 10: Aggregate Functions REGR_SYY SQL Functions, Operators, Expressions, and Predicates 407 Computation The equation for computing REGR_SYY is defined as follows: where: When there are fewer than two non-null data point pairs in the data used for the computation, then REGR_INTERCEPT returns NULL. Division by zero results in NULL rather than an error. Result Type and Attributes The data type, format, and title for REGR_SYY(y, x) are as follows. For information on the default format of data types, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on UDT arguments that have implicit casts that cast between the UDTs and any of the following predefined types: • Numeric • Character This variable … Represents … y dependent_variable_expression y is the dependent, or response, variable expression. n COUNT(y) REGR_SYY (SUM(y**2)) SUM(y) SUM(y) n ------------------- ? ? • ? ? ? ? = – ? ? Data Type Format Title REAL REGR_SYY(y,x) IF the operand is … THEN the format is … character the default format for FLOAT. • numeric • date • interval the same format as x. UDT the format for the data type to which the UDT is implicitly cast. Chapter 10: Aggregate Functions REGR_SYY 408 SQL Functions, Operators, Expressions, and Predicates • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including REGR_SYY, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” REGR_SYY Window Function For the REGR_SYY window function that performs a group, cumulative, or moving computation, see “Window Aggregate Functions” on page 449. Example This example is based the following regrtbl data. Nulls are indicated by the QUESTION MARK character. c1 height weight -- ------ ------ 1 60 84 2 62 95 3 64 140 4 66 155 5 68 119 6 70 175 7 72 145 8 74 197 9 76 150 10 76 ? 11 ? 150 12 ? ? The following SELECT statement returns the sum of squares for weight where neither height nor weight is null. SELECT REGR_SYY(weight,height) FROM regrtbl; Regr_Syy(weight,height) ----------------------- 10426 Chapter 10: Aggregate Functions SKEW SQL Functions, Operators, Expressions, and Predicates 409 SKEW Purpose Returns the skewness of the distribution of value_expression. Syntax where: ANSI Compliance SKEW is ANSI SQL:2008 compliant. Definition Skewness is the third moment of a distribution. It is a measure of the asymmetry of the distribution about its mean compared with the normal, Gaussian, distribution. The normal distribution has a skewness of 0. Positive skewness indicates a distribution having an asymmetric tail extending toward more positive values, while negative skewness indicates an asymmetric tail extending toward more negative values. Syntax element … Specifies … ALL that all non-null values specified by value_expression, including duplicates, are included in the computation for the group. This is the default. DISTINCT that null and duplicate values specified by value_expression are eliminated from the computation for the group. value_expression a constant or column expression for which the skewness of the distribution of its values is to be computed. The expression cannot contain any ordered analytical or aggregate functions. 1101B428 SKEW ( value_expression ) DISTINCT ALL Chapter 10: Aggregate Functions SKEW 410 SQL Functions, Operators, Expressions, and Predicates Result Type and Attributes The data type, format, and title for SKEW(x) are as follows. For information on the default format of data types, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on a UDT argument that has an implicit cast that casts between the UDT and any of the following predefined types: • Numeric • Character • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including SKEW, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” Computation The equation for computing SKEW is defined as follows: where: Data Type Format Title REAL Default format of the REAL data type SKEW(x) This variable … Represents … x value_expression SKEW COUNT(x) (COUNT(x) – 1)(COUNT(x) – 2) ----------------------------------------------------------------------------------- SUM x – AVG(x) (STDDEV_SAMP(x)**3) --------------------------------------------------------------- ? ? = • ? ? Chapter 10: Aggregate Functions SKEW SQL Functions, Operators, Expressions, and Predicates 411 Conditions That Produce a Null Result The following conditions product a null result: • Fewer than three non-null data points in the data used for the computation • STDDEV_SAMP(x) = 0 • Division by zero Chapter 10: Aggregate Functions STDDEV_POP 412 SQL Functions, Operators, Expressions, and Predicates STDDEV_POP Purpose Returns the population standard deviation for the non-null data points in value_expression. Syntax where: ANSI Compliance STDDEV_POP is ANSI SQL:2008 compliant. Definition The standard deviation is the second moment of a population. For a population, it is a measure of dispersion from the mean of that population. Do not use STDDEV_POP unless the data points you are processing are the complete population. Combination With Other Functions STDDEV_POP can be combined with ordered analytical functions in a SELECT list, QUALIFY clause, or ORDER BY clause. For more information on ordered analytical functions, see Chapter 11: “Ordered Analytical Functions.” STDDEV_POP cannot be combined with aggregate functions within the same SELECT list, QUALIFY clause, or ORDER BY clause. Syntax element … Specifies … ALL to include all non-null values specified by value_expression, including duplicates, in the computation. This is the default. DISTINCT to exclude duplicates of value_expression from the computation. value_expression a numeric constant or column expression whose population standard deviation is to be computed. The expression cannot contain any ordered analytical or aggregate functions. 1101B424 STDDEV_POP ( value_expression ) DISTINCT ALL Chapter 10: Aggregate Functions STDDEV_POP SQL Functions, Operators, Expressions, and Predicates 413 How GROUP BY Affects Report Breaks STDDEV_POP operates differently depending on whether there is a GROUP BY clause in the SELECT statement. Measuring the Standard Deviation of a Population If your data represents only a sample of the entire population for the variable, then use the STDDEV_SAMP function. For information, see “STDDEV_SAMP” on page 415. As the sample size increases, the values for STDDEV_SAMP and STDDEV_POP approach the same number, but you should always use the more conservative STDDEV_SAMP calculation unless you are absolutely certain that your data constitutes the entire population for the variable. Computation The equation for computing STDDEV_POP is as follows: where: When there are no non-null data points in the population, then STDDEV_POP returns NULL. Division by zero results in NULL rather than an error. IF the query … THEN STDDEV_POP is reported for … specifies a GROUP BY clause each individual group. does not specify a GROUP BY clause all the rows in the sample. This variable … Represents … x value_expression STDDEV_POP SQRT COUNT(x)SUM(x**2) – (SUM(x)**2) (COUNT(x)**2) = (-------------------------------------------------------------------------------------------------) Chapter 10: Aggregate Functions STDDEV_POP 414 SQL Functions, Operators, Expressions, and Predicates Result Type and Attributes The data type, format, and title for STDDEV_POP(x) are as follows. For information on the default format of data types, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on a UDT argument that has an implicit cast that casts between the UDT and any of the following predefined types: • Numeric • Character • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including STDDEV_POP, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” STDDEV_POP Window Function For the STDDEV_POP window function that performs a group, cumulative, or moving computation, see “Window Aggregate Functions” on page 449. Data Type Format Title REAL STDDEV_POP(x) IF the operand is … THEN the format is … character the default format for FLOAT. • numeric • date • interval the same format as x. UDT the format for the data type to which the UDT is implicitly cast. Chapter 10: Aggregate Functions STDDEV_SAMP SQL Functions, Operators, Expressions, and Predicates 415 STDDEV_SAMP Purpose Returns the sample standard deviation for the non-null data points in value_expression. Syntax where: ANSI Compliance STDDEV_SAMP is ANSI SQL:2008 compliant. Definition The standard deviation is the second moment of a distribution. For a sample, it is a measure of dispersion from the mean of that sample. The computation is more conservative for the population standard deviation to minimize the effect of outliers on the computed value. Computation The equation for computing STDDEV_SAMP is as follows: where: Syntax element … Specifies … ALL to include all non-null values specified by value_expression, including duplicates, in the computation. This is the default. DISTINCT to exclude duplicates of value_expression from the computation. value_expression a numeric constant or column expression whose sample standard deviation is to be computed. The expression cannot contain any ordered analytical or aggregate functions. 1101B425 STDDEV_SAMP ( value_expression ) DISTINCT ALL STDDEV_SAMP SQRT COUNT(x)SUM(x**2) – (SUM(x)**2) COUNT(x)(COUNT(x) – 1) = (------------------------------------------------------------------------------------------------) Chapter 10: Aggregate Functions STDDEV_SAMP 416 SQL Functions, Operators, Expressions, and Predicates Division by zero results in NULL rather than an error. When there are fewer than two non-null data points in the sample used for the computation, then STDDEV_SAMP returns NULL. Result Type and Attributes The data type, format, and title for STDDEV_SAMP(x) are as follows. For information on the default format of data types, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on a UDT argument that has an implicit cast that casts between the UDT and any of the following predefined types: • Numeric • Character • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including STDDEV_SAMP, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. This variable … Represents … x value_expression Data Type Format Title REAL STDDEV_SAMP(x) IF the operand is … THEN the format is … character the default format for FLOAT. • numeric • date • interval the same format as x. UDT the format for the data type to which the UDT is implicitly cast. Chapter 10: Aggregate Functions STDDEV_SAMP SQL Functions, Operators, Expressions, and Predicates 417 For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” Combination With Other Functions STDDEV_SAMP can be combined with ordered analytical functions in a SELECT list, QUALIFY clause, or ORDER BY clause. For more information on ordered analytical functions, see Chapter 11: “Ordered Analytical Functions.” STDDEV_SAMP cannot be combined with aggregate functions within the same SELECT list, QUALIFY clause, or ORDER BY clause. How GROUP BY Affects Report Breaks The GROUP BY clause affects the STDDEV_SAMP operation. Measuring the Standard Deviation of a Population If your data represents the entire population for the variable, then use the STDDEV_POP function. For information, see “STDDEV_POP” on page 412. As the sample size increases, the values for STDDEV_SAMP and STDDEV_POP approach the same number, but you should use the more conservative STDDEV_SAMP calculation unless you are absolutely certain that your data constitutes the entire population for the variable. STDDEV_SAMP Window Function For the STDDEV_SAMP window function that performs a group, cumulative, or moving computation, see “Window Aggregate Functions” on page 449. IF the query … THEN STDDEV_SAMP is reported for … specifies a GROUP BY clause each individual group. does not specify a GROUP BY clause all the rows in the sample. Chapter 10: Aggregate Functions SUM 418 SQL Functions, Operators, Expressions, and Predicates SUM Purpose Returns a column value that is the arithmetic sum for a specified expression for a group. Syntax where: ANSI Compliance SUM is ANSI SQL:2008 compliant. Result Type and Attributes The following table lists the default attributes for the result of SUM(x). Syntax element … Specifies … ALL that all non-null values specified by value_expression, including duplicates, are included in the sum computation for the group. This is the default. DISTINCT that duplicate and non-null values specified by value_expression are eliminated from the sum computation for the group. value_expression a constant or column expression for which the sum is to be computed. The expression cannot contain any ordered analytical or aggregate functions. 1101B423 SUM ( value_expression ) DISTINCT ALL Data Type of Operand Data Type of Result Format Title BYTEINT or SMALLINT Same as the operand Default format of the INTEGER data type Sum(x) character Same as the operand Default format for FLOAT UDT Same as the operand Format for the data type to which the UDT is implicitly cast Chapter 10: Aggregate Functions SUM SQL Functions, Operators, Expressions, and Predicates 419 For an explanation of the formatting characters in the format, and information on data type default formats, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on a UDT argument that has an implicit cast that casts between the UDT and either of the following predefined types: • Numeric • Character To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including SUM, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” DECIMAL(n,m) DECIMAL(p,m), where p is determined by the rules in the following table. Default format for the data type of the operand Sum(x) IF MaxDecimal in DBSControl is … AND … THEN p is … 0 or 15 n =15 15. 15 < n =18 18. n > 18 38. 18 n =18 18. n > 18 38. 38 n = any value 38. Other than UDT, SMALLINT, BYTEINT, DECIMAL, or character Same as the operand Default format for the data type of the operand Data Type of Operand Data Type of Result Format Title Chapter 10: Aggregate Functions SUM 420 SQL Functions, Operators, Expressions, and Predicates Usage Notes If value_expression is a column reference, the column must not be to a view column that is derived from a function. SUM is valid only for numeric data. Nulls are not included in the result computation. For details, see “Manipulating Nulls” in SQL Fundamentals and “Aggregates and Nulls” on page 347. The SUM function can result in a numeric overflow or the loss of data because of the default output format. If this occurs, a data type declaration may be used to override the default. For example, if QUANTITY comprises many rows of INTEGER values, it may be necessary to specify a data type declaration like the following for the SUM function: SUM(QUANTITY(FLOAT)) SUM Window Function For the SUM function that returns the cumulative, group, or moving sum, see “Window Aggregate Functions” on page 449. Example 1: Accounts Receivable You need to know how much cash you need to pay all vendors who billed you 30 or more days ago. SELECT SUM(Invoice) FROM AcctsRec WHERE (CURRENT_DATE - InvDate) >= 30; Example 2: Face Value of Inventory You need to know the total face value for all items in your inventory. SELECT SUM(QUANTITY * Price) FROM Inventory; Sum((QUANTITY * Price)) ----------------------- 38,525,151.91 Chapter 10: Aggregate Functions VAR_POP SQL Functions, Operators, Expressions, and Predicates 421 VAR_POP Purpose Returns the population variance for the data points in value_expression. Syntax where: ANSI Compliance VAR_POP is ANSI SQL:2008 compliant. Definition The variance of a population is a measure of dispersion from the mean of that population. Do not use VAR_POP unless the data points you are processing are the complete population. Computation The equation for computing VAR_POP is as follows: where: Syntax element … Specifies … ALL to include all non-null values specified by value_expression, including duplicates, in the computation. This is the default. DISTINCT to exclude duplicates of value_expression from the computation. value_expression a numeric constant or column expression whose population variance is to be computed. The expression cannot contain any ordered analytical or aggregate functions. 1101B426 VAR_POP ( value_expression ) DISTINCT ALL This variable … Represents … x value_expression VAR_POP COUNT(x)SUM(x**2) – (SUM(x)**2) (COUNT(x)**2) = ------------------------------------------------------------------------------------------------- Chapter 10: Aggregate Functions VAR_POP 422 SQL Functions, Operators, Expressions, and Predicates When the population has no non-null data points, VAR_POP returns NULL. Division by zero results in NULL rather than an error. Result Type and Attributes The data type, format, and title for VAR_POP(x) are as follows. For information on the default format of data types, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Support for UDTs By default, Teradata Database performs implicit type conversion on a UDT argument that has an implicit cast that casts between the UDT and any of the following predefined types: • Numeric • Character • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including VAR_POP, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” Data Type Format Title REAL VAR_POP(x) IF the operand is … THEN the format is … character the default format for FLOAT. • numeric • date • interval the same format as x. UDT the format for the data type to which the UDT is implicitly cast. Chapter 10: Aggregate Functions VAR_POP SQL Functions, Operators, Expressions, and Predicates 423 Combination With Other Functions VAR_POP can be combined with ordered analytical functions in a SELECT list, QUALIFY clause, or ORDER BY clause. For more information on ordered analytical functions, see Chapter 11: “Ordered Analytical Functions.” VAR_POP cannot be combined with aggregate functions within the same SELECT list, QUALIFY clause, or ORDER BY clause. GROUP BY Affects Report Breaks The GROUP BY clause affects the VAR_POP operation. Measuring the Standard Deviation of a Population If your data represents the only a sample of the entire population for the variable, then use the VAR_SAMP function. For information, see “VAR_SAMP” on page 424. As the sample size increases, the values for VAR_SAMP and VAR_POP approach the same number, but you should always use the more conservative STDDEV_SAMP calculation unless you are absolutely certain that your data constitutes the entire population for the variable. VAR_POP Window Function For the VAR_POP window function that performs a group, cumulative, or moving computation, see “Window Aggregate Functions” on page 449. IF the query … THEN VAR_POP is reported for … specifies a GROUP BY clause each individual group. does not specify a GROUP BY clause all the rows in the sample. Chapter 10: Aggregate Functions VAR_SAMP 424 SQL Functions, Operators, Expressions, and Predicates VAR_SAMP Purpose Returns the sample variance for the data points in value_expression. Syntax where: ANSI Compliance VAR_SAMP is ANSI SQL:2008 compliant. Definition The variance of a sample is a measure of dispersion from the mean of that sample. It is the square of the sample standard deviation. The computation is more conservative than that for the population standard deviation to minimize the effect of outliers on the computed value. Computation The equation for computing VAR_SAMP is as follows: where: Syntax element … Specifies … ALL to include all non-null values specified by value_expression, including duplicates, in the computation. This is the default. DISTINCT to exclude duplicates of value_expression from the computation. value_expression a numeric constant or column expression whose sample variance is to be computed. The expression cannot contain ordered analytical or aggregate functions. 1101B427 VAR_SAMP ( value_expression ) DISTINCT ALL VAR_SAMP COUNT(x)SUM(x**2) – (SUM(x)**2) (COUNT(x))(COUNT(x) – 1) = ------------------------------------------------------------------------------------------------ Chapter 10: Aggregate Functions VAR_SAMP SQL Functions, Operators, Expressions, and Predicates 425 When the sample used for the computation has fewer than two non-null data points, VAR_SAMP returns NULL. Division by zero results in NULL rather than an error. Combination With Other Functions VAR_SAMP can be combined with ordered analytical functions in a SELECT list, QUALIFY clause, or ORDER BY clause. For more information on ordered analytical functions, see Chapter 11: “Ordered Analytical Functions.” VAR_SAMP cannot be combined with aggregate functions within the same SELECT list, QUALIFY clause, or ORDER BY clause. GROUP BY Affects Report Breaks VAR_SAMP operates differently depending on whether or not there is a GROUP BY clause in the SELECT statement. Measuring the Variance of a Population If your data represents the entire population for the variable, then use the VAR_POP function. For information, see “VAR_POP” on page 421. As the sample size increases, the values for VAR_SAMP and VAR_POP approach the same number, but you should always use the more conservative VAR_SAMP calculation unless you are absolutely certain that your data constitutes the entire population for the variable. This variable … Represents … x value_expression IF the query … THEN VAR_SAMP is reported for … specifies a GROUP BY clause each individual group. does not specify a GROUP BY clause all the rows in the sample. Chapter 10: Aggregate Functions VAR_SAMP 426 SQL Functions, Operators, Expressions, and Predicates Result Type and Attributes The data type, format, and title for VAR_SAMP(x) are as follows. Support for UDTs By default, Teradata Database performs implicit type conversion on a UDT argument that has an implicit cast that casts between the UDT and any of the following predefined types: • Numeric • Character • DATE • Interval To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including VAR_SAMP, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” VAR_SAMP Window Function For the VAR_SAMP window function that performs a group, cumulative, or moving computation, see “Window Aggregate Functions” on page 449. Data Type Format Title REAL VAR_SAMP(x) IF the operand is … THEN the format is … character the default format for FLOAT. • numeric • date • interval the same format as x. UDT the format for the data type to which the UDT is implicitly cast. For details on the default format of data types, see SQL Data Types and Literals. SQL Functions, Operators, Expressions, and Predicates 427 CHAPTER 11 Ordered Analytical Functions This chapter describes ordered analytical functions that enable and expedite the processing of queries containing On Line Analytical Processing (OLAP) style decision support requests. Ordered analytical functions include ANSI SQL:2008 compliant window functions, as well as Teradata SQL-specific functions. Chapter 11: Ordered Analytical Functions Ordered Analytical Functions 428 SQL Functions, Operators, Expressions, and Predicates Ordered Analytical Functions Ordered analytical functions provide support for many common operations in analytical processing and data mining environments that require an ordered set of results rows or depend on values in a previous row. For example, computing a seven-day running sum requires: • First, that rows be ordered by date. • Then, the value for the running sum must be computed, • Adding the current row value to the value of the sum from the previous row, and • Subtracting the value from the row eight days ago. Ordered Analytical Functions Benefits Ordered analytical functions extend the Teradata Database query execution engine with the concept of an ordered set and with the ability to use the values from multiple rows in computing a new value. The result of an ordered analytical function is handled the same as any other SQL expression. It can be a result column or part of a more complex arithmetic expression within its SELECT. Each of the ordered analytical functions permit you to specify the sort ordering column or columns on which to sort the rows retrieved by the SELECT statement. The sort order and any other input parameters to the functions are specified the same as arguments to other SQL functions and can be any normal SQL expression. Ordered Analytical Calculations at the SQL Level Performing ordered analytical computations at the SQL level rather than through a higherlevel OLAP calculation engine provides four distinct advantages. • Reduced programming effort. • Elimination of the need for external sort routines. • Elimination of the need to export large data sets to external tools because ordered analytical functions enable you to target the specific data for analysis within the warehouse itself by specifying conditions in the query. • Marked enhancement of analysis performance over the slow, single-threaded operations that external tools perform on large data sets. Teradata Warehouse Miner You need not directly code SQL queries to take advantage of ordered analytical functions. Both Teradata Database and many third-party query management and analytical tools have full access to the Teradata SQL ordered analytical functions. Teradata Warehouse Miner, for Chapter 11: Ordered Analytical Functions Syntax Alternatives for Ordered Analytical Functions SQL Functions, Operators, Expressions, and Predicates 429 example, a tool that performs data mining preprocessing inside the database engine, relies on these features to perform functions in the database itself rather than requiring data extraction. Teradata Warehouse Miner includes approximately 40 predefined data mining functions in SQL based on the Teradata SQL-specific functions. For example, the Teradata Warehouse Miner FREQ function uses the Teradata SQL-specific functions CSUM, RANK, and QUALIFY to determine frequencies. Example The following example shows how the SQL query to calculate a frequency of gender to marital status would appear using Teradata Warehouse Miner. SELECT gender, marital_status, xcnt,xpct ,CSUM(xcnt, xcnt DESC, gender, marital_status) AS xcum_cnt ,CSUM(xpct, xcnt DESC, gender, marital_status) AS xcum_pct ,RANK(xcnt DESC, gender ASC, marital_status ASC) AS xrank FROM (SELECT gender, marital_status, COUNT(*) AS xcnt ,100.000 * xcnt / xall (FORMAT 'ZZ9.99') AS xpct FROM customer_table A, (SELECT COUNT(*) AS xall FROM customer_table) B GROUP BY gender, marital_status, xall HAVING xpct >= 1) T1 QUALIFY xrank <= 8 ORDER BY xcnt DESC, gender, marital_status The result for this query looks like the following table. Syntax Alternatives for Ordered Analytical Functions Teradata SQL supports two syntax alternatives for ordered analytical functions: • ANSI SQL:2008 compliant gender marital_status xcnt xpct xcum_cnt xcum_pct xrank F Married 3910093 36.71 3910093 36.71 1 M Married 2419511 22.71 6329604 59.42 2 F Divorced 1612130 15.13 7941734 74.55 3 M Divorced 1412624 3.26 9354358 87.81 4 F Single 491224 4.61 9845582 92.42 5 F Widowed 319881 3.01 10165463 95.43 6 M Single 319794 3.00 10485257 98.43 7 M Widowed 197131 1.57 10652388 100.00 8 Chapter 11: Ordered Analytical Functions Window Feature 430 SQL Functions, Operators, Expressions, and Predicates • Teradata Window aggregate, rank, distribution, and row number functions are ANSI SQL:2008 compliant, while Teradata-specific functions are not. The use of the Teradata-specific functions listed in the following table is strongly discouraged. These functions are retained only for backward compatibility with existing applications. Be sure to use the ANSI-compliant window functions for any new applications you develop. Relationship Between Teradata-Specific Functions and Window Functions The following table identifies equivalent ANSI SQL:2008 window functions for Teradataspecific functions: Window Feature The ANSI SQL:2008 window feature provides a way to dynamically define a subset of data, or window, in an ordered relational database table. A window is specified by the OVER() phrase, which can include the following clauses inside the parentheses: • PARTITION BY • ORDER BY • RESET WHEN • ROWS To see the syntax for the OVER() phrase and the associated clauses, refer to “Window Aggregate Functions” on page 449. The window feature can be applied to the following functions: Teradata-Specific Functions Equivalent ANSI SQL:2008 Window Functions CSUM SUM MAVG AVG MDIFF(x, w, y) composable from SUM MLINREG composable from SUM and COUNT QUANTILE composable from RANK and COUNT RANK RANK MSUM SUM Chapter 11: Ordered Analytical Functions Window Feature SQL Functions, Operators, Expressions, and Predicates 431 The window feature can also be applied to a user-defined aggregate function. For details, see “Window Aggregate UDF” on page 717. PARTITION BY Phrase PARTITION BY takes a column reference list and groups the rows based on the specified column reference list over which the ordered analytical function executes. Such a grouping is static. To define a group or partition based on a condition, use the RESET WHEN phrase. See “RESET WHEN Phrase” on page 433 for details. If there is no PARTITION BY phrase or RESET WHEN phrase, then the entire result set, delivered by the FROM clause, constitutes a single partition, over which the ordered analytical function executes. Consider the following table named sales_tbl. • AVG • CORR • COUNT • COVAR_POP • COVAR_SAMP • MAX • MIN • PERCENT_RANK • RANK • REGR_AVGX • REGR_AVGY • REGR_COUNT • REGR_INTERCEPT • REGR_R2 • REGR_SLOPE • REGR_SXX • REGR_SXY • REGR_SYY • ROW_NUMBER • STDDEV_POP • STDDEV_SAMP • SUM • VAR_POP • VAR_SAMP StoreID SMonth ProdID Sales 1001 1 C 35000.00 1001 2 C 25000.00 1001 3 C 40000.00 1001 4 C 25000.00 1001 5 C 30000.00 1001 6 C 30000.00 1002 1 C 40000.00 1002 2 C 35000.00 1002 3 C 110000.00 1002 4 C 60000.00 1002 5 C 35000.00 1002 6 C 100000.00 Chapter 11: Ordered Analytical Functions Window Feature 432 SQL Functions, Operators, Expressions, and Predicates The following SELECT statement, which does not include PARTITION BY, computes the average sales for all the stores in the table: SELECT StoreID, SMonth, ProdID, Sales, AVG(Sales) OVER () FROM sales_tbl; StoreID SMonth ProdID Sales Group Avg(Sales) ------- ------ ------ --------- ---------------- 1001 1 C 35000.00 47083.33 1001 2 C 25000.00 47083.33 1001 3 C 40000.00 47083.33 1001 4 C 25000.00 47083.33 1001 5 C 30000.00 47083.33 1001 6 C 30000.00 47083.33 1002 1 C 40000.00 47083.33 1002 2 C 35000.00 47083.33 1002 3 C 110000.00 47083.33 1002 4 C 60000.00 47083.33 1002 5 C 35000.00 47083.33 1002 6 C 100000.00 47083.33 To compute the average sales for each store, partition the data in sales_tbl by StoreID: SELECT StoreID, SMonth, ProdID, Sales, AVG(Sales) OVER (PARTITION BY StoreID) FROM sales_tbl; StoreID SMonth ProdID Sales Group Avg(Sales) ------- ------ ------ --------- ---------------- 1001 3 C 40000.00 30833.33 1001 5 C 30000.00 30833.33 1001 6 C 30000.00 30833.33 1001 4 C 25000.00 30833.33 1001 2 C 25000.00 30833.33 1001 1 C 35000.00 30833.33 1002 3 C 110000.00 63333.33 1002 5 C 35000.00 63333.33 1002 6 C 100000.00 63333.33 1002 4 C 60000.00 63333.33 1002 2 C 35000.00 63333.33 1002 1 C 40000.00 63333.33 ORDER BY Phrase ORDER BY specifies how the rows are ordered in a partition, which determines the sort order of the rows over which the function is applied. To add the monthly sales for a store in the sales_tbl table to the sales for previous months, compute the cumulative sales sum and order the rows in each partition by SMonth: SELECT StoreID, SMonth, ProdID, Sales, SUM(Sales) OVER (PARTITION BY StoreID ORDER BY SMonth ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM sales_tbl; StoreID SMonth ProdID Sales Cumulative Sum(Sales) Chapter 11: Ordered Analytical Functions Window Feature SQL Functions, Operators, Expressions, and Predicates 433 ------- ------ ------ --------- --------------------- 1001 1 C 35000.00 35000.00 1001 2 C 25000.00 60000.00 1001 3 C 40000.00 100000.00 1001 4 C 25000.00 125000.00 1001 5 C 30000.00 155000.00 1001 6 C 30000.00 185000.00 1002 1 C 40000.00 40000.00 1002 2 C 35000.00 75000.00 1002 3 C 110000.00 185000.00 1002 4 C 60000.00 245000.00 1002 5 C 35000.00 280000.00 1002 6 C 100000.00 380000.00 RESET WHEN Phrase RESET WHEN is a Teradata extension to the ANSI SQL standard. Depending on the evaluation of the specified condition, RESET WHEN determines the group or partition, over which the ordered analytical function operates. If the condition evaluates to TRUE, a new dynamic partition is created inside the specified window partition. To define a partition based on a column reference list, use the PARTITION BY phrase. See “PARTITION BY Phrase” on page 431 for details. If there is no RESET WHEN phrase or PARTITION BY phrase, then the entire result set, delivered by the FROM clause, constitutes a single partition, over which the ordered analytical function executes. You can have different RESET WHEN clauses in the same SELECT list. Note: A window specification that specifies a RESET WHEN clause must also specify an ORDER BY clause. RESET WHEN Condition Rules The condition in the RESET WHEN clause is equivalent in scope to the condition in a QUALIFY clause with the additional constraint that nested ordered analytical functions cannot specify conditional partitioning. The condition is applied to the rows in all designated window partitions to create subpartitions within the particular window partitions. The following rules apply for RESET WHEN conditions. A RESET WHEN condition can contain the following: • Ordered analytical functions that do not include the RESET WHEN clause • Scalar subqueries • Aggregate operators • DEFAULT functions However, DEFAULT without an explicit column specification is valid only if it is specified as a standalone condition in the predicate. See “Rules For Using a DEFAULT Function As Part of a RESET WHEN Condition” on page 434 for details. Chapter 11: Ordered Analytical Functions Window Feature 434 SQL Functions, Operators, Expressions, and Predicates A RESET WHEN condition cannot contain the following: • Ordered analytical functions that include the RESET WHEN clause • The SELECT statement • LOB columns • UDT expressions, including UDFs that return a UDT value However, a RESET WHEN condition can include an expression that contains UDTs as long as that expression returns a result that has a predefined data type. Rules For Using a DEFAULT Function As Part of a RESET WHEN Condition The following rules apply to the use of the DEFAULT function as part of a RESET WHEN condition: • You can specify a DEFAULT function with a column name argument within a predicate. The system evaluates the DEFAULT function to the default value of the column specified as its argument. Once the system has evaluated the DEFAULT function, it treats it like a constant in the predicate. • You can specify a DEFAULT function without a column name argument within a predicate only if there is one column specification and one DEFAULT function as the terms on each side of the comparison operator within the expression. • Following existing comparison rules, a condition with a DEFAULT function used with comparison operators other than IS [NOT] NULL is unknown if the DEFAULT function evaluates to null. A condition other than IS [NOT]NULL with a DEFAULT function compared with a null evaluates to unknown. See “DEFAULT” on page 621 for more information about the DEFAULT function. Example 1 This example finds cumulative sales for all periods of increasing sales for each region. SUM(sales) OVER ( PARTITION BY region ORDER BY day_of_calendar RESET WHEN sales < /* preceding row */ SUM(sales) OVER ( PARTITION BY region ORDER BY day_of_calendar ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) ROWS UNBOUNDED PRECEDING IF a DEFAULT function is used with... THEN the comparison is... IS NULL TRUE if the default is null, else it is FALSE. IS NOT NULL FALSE if the default is null, else it is TRUE. Chapter 11: Ordered Analytical Functions Window Feature SQL Functions, Operators, Expressions, and Predicates 435 ) Example 2 This example finds sequences of increasing balances. This implies that we reset whenever the current balance is less than or equal to the preceding balance. SELECT account_key, month, balance, ROW_NUMBER() over (PARTITION BY account_key ORDER BY month RESET WHEN balance /* current row balance */ <= SUM(balance) over (PARTITION BY account_key ORDER BY month ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) /* prev row */ ) - 1 /* to get the count started at 0 */ as balance_increase FROM accounts; The possible results of the preceding SELECT appear in the table below: account_key month balance balance_increase ----------- ----- ------- ---------------- 1 1 60 0 1 2 99 1 1 3 94 0 1 4 90 0 1 5 80 0 1 6 88 1 1 7 90 2 1 8 92 3 1 9 10 0 1 10 60 1 1 11 80 2 1 12 10 0 Example 3 The following example illustrates a window function with a nested aggregate. The query is processed as follows: 1 We use the SUM(balance) aggregate function to calculate the sum of all the balances for a given account in a given quarter. 2 We check to see if a balance in a given quarter (for a given account) is greater than the balance of the previous quarter. 3 If the balance increased, we track a cumulative count value. As long as the RESET WHEN condition evaluates to false, the balance is increasing over successive quarters, and we continue to increase the count. 4 We use the ROW_NUMBER() ordered analytical function to calculate the count value. When we reach a quarter whose balance is less than or equal to that of the previous quarter, the RESET WHEN condition evaluates to true, and we start a new partition and ROW_NUMBER() restarts the count from 1. We specify ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING to access the previous value. 5 Finally, we subtract 1 to ensure that the count values start with 0. The balance_increase column shows the number of successive quarters where the balance was increasing. In this example, we only have one quarter (1->2) where the balance has increased. Chapter 11: Ordered Analytical Functions Window Feature 436 SQL Functions, Operators, Expressions, and Predicates SELECT account_key, quarter, sum(balance), ROW_NUMBER() over (PARTITION BY account_key ORDER BY quarter RESET WHEN sum(balance) /* current row balance */ <= SUM(sum(balance)) over (PARTITION BY account_key ORDER BY quarter ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING)/* prev row */ ) - 1 /* to get the count started at 0 */ as balance_increase FROM accounts GROUP BY account_key, quarter; The possible results of the preceding SELECT appear in the table below: account_key quarter balance balance_increase ----------- ------- ------- ---------------- 1 1 253 0 1 2 258 1 1 3 192 0 1 4 150 0 Example 4 In the following example, the condition in the RESET WHEN clause contains SELECT as a nested subquery. This is not allowed and results in an error. SELECT SUM(a1) OVER (ORDER BY 1 RESET WHEN 1 in (SELECT 1)) FROM t1; $ *** Failure 3706 Syntax error: SELECT clause not supported in RESET...WHEN clause. ROWS Phrase ROWS defines the rows over which the aggregate function is computed for each row in the partition. If ROWS is specified, the computation of the aggregate function for each row in the partition includes only the subset of rows in the ROWS phrase. If there is no ROWS phrase, then the computation includes all the rows in the partition. ROWS can be specified with the ANSI SQL:2008 compliant window aggregate functions: • AVG • CORR • COUNT • COVAR_POP • COVAR_SAMP • MAX • MIN • REGR_AVGX • REGR_AVGY • REGR_COUNT • REGR_INTERCEPT • REGR_R2 • REGR_SLOPE • REGR_SXX • REGR_SXY • REGR_SYY • STDDEV_POP • STDDEV_SAMP • SUM • VAR_POP • VAR_SAMP Chapter 11: Ordered Analytical Functions Applying Windows to Aggregate Functions SQL Functions, Operators, Expressions, and Predicates 437 To compute the three-month moving average sales for each store in the sales_tbl table, partition by StoreID, order by SMonth, and perform the computation over the current row and the two preceding rows: SELECT StoreID, SMonth, ProdID, Sales, AVG(Sales) OVER (PARTITION BY StoreID ORDER BY SMonth ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) FROM sales_tbl; StoreID SMonth ProdID Sales Moving Avg(Sales) ------- ------ ------ --------- ----------------- 1001 1 C 35000.00 35000.00 1001 2 C 25000.00 30000.00 1001 3 C 40000.00 33333.33 1001 4 C 25000.00 30000.00 1001 5 C 30000.00 31666.67 1001 6 C 30000.00 28333.33 1002 1 C 40000.00 40000.00 1002 2 C 35000.00 37500.00 1002 3 C 110000.00 61666.67 1002 4 C 60000.00 68333.33 1002 5 C 35000.00 68333.33 1002 6 C 100000.00 65000.00 Multiple Window Specifications In an SQL statement using more than one window function, each window function can have a unique window specification. For example, SELECT StoreID, SMonth, ProdID, Sales, AVG(Sales) OVER (PARTITION BY StoreID ORDER BY SMonth ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), RANK() OVER (PARTITION BY StoreID ORDER BY Sales DESC) FROM sales_tbl; Applying Windows to Aggregate Functions A window specification can be applied to the following ANSI SQL:2008 compliant aggregate functions: Chapter 11: Ordered Analytical Functions Applying Windows to Aggregate Functions 438 SQL Functions, Operators, Expressions, and Predicates A window specification can also be applied to a user-defined aggregate function. For details, see “Window Aggregate UDF” on page 717. An aggregate function on which a window specification is applied is called a window aggregate function. Without a window specification, aggregate functions return one value for all qualified rows examined. Window aggregate functions return a new value for each of the qualifying rows participating in the query. Thus, the following SELECT statement, which includes the aggregate AVG, returns one value only: the average of sales. SELECT AVG(sale) FROM monthly_sales; Average(sale) ------------- 1368 The AVG window function retains each qualifying row. The following SELECT statement might return the results that follow. SELECT territory, smonth, sales, AVG(sales) OVER (PARTITION BY territory ORDER BY smonth ROWS 2 PRECEDING) FROM sales_history; territory smonth sales Moving Avg(sales) --------- ------- ----- ----------------- East 199810 10 10 East 199811 4 7 East 199812 10 8 East 199901 7 7 East 199902 10 9 West 199810 8 8 West 199811 12 10 West 199812 7 9 West 199901 11 10 West 199902 6 8 • AVG • CORR • COUNT • COVAR_POP • COVAR_SAMP • MAX • MIN • REGR_AVGX • REGR_AVGY • REGR_COUNT • REGR_INTERCEPT • REGR_R2 • REGR_SLOPE • REGR_SXX • REGR_SXY • REGR_SYY • STDDEV_POP • STDDEV_SAMP • SUM • VAR_POP • VAR_SAMP Chapter 11: Ordered Analytical Functions Characteristics of Ordered Analytical Functions SQL Functions, Operators, Expressions, and Predicates 439 Characteristics of Ordered Analytical Functions The Function Value The function value for a column in a row considers that row (and a subset of all other rows in the group) and produces a new value. The generic function describing this operation is as follows: new_column_value = FUNCTION(column_value,rows_defined_by_window) Use of QUALIFY Clause Rows can be eliminated by applying conditions on the new column value. The QUALIFY clause is analogous to the HAVING clause of aggregate functions. The QUALIFY clause eliminates rows based on the function value, returning a new value for each of the participating rows. For example: SELECT StoreID, SUM(profit) OVER (PARTITION BY StoreID) FROM facts QUALIFY SUM(profit) OVER (PARTITION BY StoreID) > 2; An SQL query that contains both ordered analytical functions and aggregate functions can have both a QUALIFY clause and a HAVING clause, as in the following example: SELECT StoreID, SUM(sale), SUM(profit) OVER (PARTITION BY StoreID) FROM facts GROUP BY StoreID, sale, profit HAVING SUM(sale) > 15 QUALIFY SUM(profit) OVER (PARTITION BY StoreID) > 2; For details on the QUALIFY clause, see SQL Data Manipulation Language. DISTINCT Clause Restriction The DISTINCT clause is not permitted in window aggregate functions. Permitted Query Objects Ordered analytical functions are permitted in the following database query objects: • Views • Macros • Derived tables • INSERT ... SELECT Where Ordered Analytical Functions are Not Permitted Ordered analytical functions are not permitted in: • Subqueries • WHERE clauses Chapter 11: Ordered Analytical Functions Characteristics of Ordered Analytical Functions 440 SQL Functions, Operators, Expressions, and Predicates • SELECT AND CONSUME statements Use of Standard SQL Features You can use standard SQL features within the same query to make your statements more sophisticated. For example, you can use ordered analytical functions in the following ways: Ordered analytical functions having different sort expressions are evaluated one after another, reusing the same spool file. Different functions having the same sort expression are evaluated simultaneously. Unsupported Data Types Ordered analytical functions do not operate on the following data types: • CLOB or BLOB data types • UDT data types Ordered Analytical Functions and Period Data Types Expressions that evaluate to Period data types can be specified for any expression within the following ordered analytical functions: QUANTILE, RANK (Teradata-specific function), and RANK(ANSI SQL Window function). Ordered Analytical Functions and Recursive Queries Ordered analytical functions cannot appear in a recursive statement of a recursive query. However, a non-recursive seed statement in a recursive query can specify an ordered analytical function. Ordered Analytical Functions and Hash or Join Indexes When a single table query specifies an ordered analytical function on columns that are also defined for a single table compressed hash or join index, the Optimizer does not select the hash or join index to process the query. Computation Sort Order and Result Order The sort order that you specify in the window specification defines the sort order of the rows over which the function is applied; it does not define the ordering of the results. Use an analytical function in this operation … To … INSERT … SELECT populate a new column. derived table create a new table to participate in a complex query. Chapter 11: Ordered Analytical Functions Characteristics of Ordered Analytical Functions SQL Functions, Operators, Expressions, and Predicates 441 For example, to compute the average sales for the months following the current month, order the rows by month: SELECT StoreID, SMonth, ProdID, Sales, AVG(Sales) OVER (PARTITION BY StoreID ORDER BY SMonth ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) FROM sales_tbl; StoreID SMonth ProdID Sales Remaining Avg(Sales) ------- ------ ------ --------- -------------------- 1001 6 C 30000.00 ? 1001 5 C 30000.00 30000.00 1001 4 C 25000.00 30000.00 1001 3 C 40000.00 28333.33 1001 2 C 25000.00 31250.00 1001 1 C 35000.00 30000.00 The default sort order is ASC for the computation. However, the results are returned in the reverse order. To order the results, use an ORDER BY phrase in the SELECT statement. For example: SELECT StoreID, SMonth, ProdID, Sales, AVG(Sales) OVER (PARTITION BY StoreID ORDER BY SMonth ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) FROM sales_tbl ORDER BY SMonth; StoreID SMonth ProdID Sales Remaining Avg(Sales) ------- ------ ------ --------- -------------------- 1001 1 C 35000.00 30000.00 1001 2 C 25000.00 31250.00 1001 3 C 40000.00 28333.33 1001 4 C 25000.00 30000.00 1001 5 C 30000.00 30000.00 1001 6 C 30000.00 ? Data in Partitioning Column of Window Specification and Resource Impact The columns specified in the PARTITION BY clause of a window specification determine the partitions over which the ordered analytical function executes. For example, the following query specifies the StoreID column in the PARTITION BY clause to compute the group sales sum for each store: SELECT StoreID, SMonth, ProdID, Sales, SUM(Sales) OVER (PARTITION BY StoreID) FROM sales_tbl; At execution time, Teradata Database moves all of the rows that fall into a partition to the same AMP. If a very large number of rows fall into the same partition, the AMP can run out of spool space. For example, if the sales_tbl table in the preceding query has millions or billions of rows, and the StoreID column contains only a few distinct values, an enormous number of rows are going to fall into the same partition, potentially resulting in out-of-spool errors. Chapter 11: Ordered Analytical Functions Nesting Aggregates in Ordered Analytical Functions 442 SQL Functions, Operators, Expressions, and Predicates To avoid this problem, examine the data in the columns of the PARTITION BY clause. If necessary, rewrite the query to include additional columns in the PARTITION BY clause to create smaller partitions that Teradata Database can distribute more evenly among the AMPs. For example, the preceding query can be rewritten to compute the group sales sum for each store for each month: SELECT StoreID, SMonth, ProdID, Sales, SUM(Sales) OVER (PARTITION BY StoreID, SMonth) FROM sales_tbl; Nesting Aggregates in Ordered Analytical Functions You can nest aggregates in window functions, including the select list, HAVING, QUALIFY, and ORDER BY clauses. However, the HAVING clause can only contain aggregate function references because HAVING cannot contain nested syntax like RANK() OVER (ORDER BY SUM(x)). Aggregate functions cannot be specified with Teradata-specific functions. Example The following query nests the SUM aggregate function within the RANK ordered analytical function in the select list: SELECT state, city, SUM(sale), RANK() OVER (PARTITION BY state ORDER BY SUM(sale)) FROM T1 WHERE T1.cityID = T2.cityID GROUP BY state, city HAVING MAX(sale) > 10; Alternative: Using Derived Tables Although only window functions allow aggregates specified together in the same SELECT list, window functions and Teradata-specific functions can be combined with aggregates using derived tables or views. Using derived tables or views also clarifies the semantics of the computation. Example The following example shows the sales rank of a particular product in a store and its percent contribution to the store sales for the top three products in each store. SELECT RT.storeid, RT.prodid, RT.sales, RT.rank_sales, RT.sales * 100.0/ST.sum_store_sales FROM (SELECT storeid, prodid, sales, RANK(sales) AS rank_sales FROM sales_tbl GROUP BY storeID QUALIFY RANK(sales) <=3) AS RT, (SELECT storeID, SUM(sales) AS sum_store_sales Chapter 11: Ordered Analytical Functions GROUP BY Clause SQL Functions, Operators, Expressions, and Predicates 443 FROM sales_tbl GROUP BY storeID) AS ST WHERE RT.storeID = ST.storeID ORDER BY RT.storeID, RT.sales; The results table might look something like the following: GROUP BY Clause GROUP BY and Window Functions For window functions, the GROUP BY clause must include all the columns specified in the: • Select list of the SELECT clause • Window functions in the select list of a SELECT clause • Window functions in the search condition of a QUALIFY clause • The condition in the RESET WHEN clause For example, the following SELECT statement specifies the column City in the select list and the column StoreID in the COUNT window function in the select list and QUALIFY clause. Both columns must also appear in the GROUP BY clause: SELECT City, StoreID, COUNT(StoreID) OVER () FROM sales_tbl GROUP BY City, StoreID QUALIFY COUNT(StoreID) >=3; For window functions, GROUP BY collapses all rows with the same value for the group-by columns into a single row. For example, the following statement uses the GROUP BY clause to collapse all rows with the same value for City and StoreID into a single row: storeID prodID sales rank_sales sales*100.0/sum_store_sales 1001 D 35000.00 3 17.949 1001 C 60000.00 2 30.769 1001 A 100000.00 1 51.282 1002 D 25000.00 3 25.000 1002 C 35000.00 2 35.000 1002 A 40000.00 1 40.000 1003 C 20000.00 3 20.000 1003 A 30000.00 2 30.000 1003 D 50000.00 1 50.000 ... ... ... ... Chapter 11: Ordered Analytical Functions GROUP BY Clause 444 SQL Functions, Operators, Expressions, and Predicates SELECT City, StoreID, COUNT(StoreID) OVER () FROM sales_tbl GROUP BY City, StoreID; The results look like this: City StoreID Group Count(StoreID) ----- ------- -------------------- Pecos 1001 3 Pecos 1002 3 Ozona 1003 3 Without the GROUP BY, the results look like this: City StoreID Group Count(StoreID) ----- ------- -------------------- Pecos 1001 9 Pecos 1001 9 Pecos 1001 9 Pecos 1001 9 Pecos 1002 9 Pecos 1002 9 Pecos 1002 9 Ozona 1003 9 Ozona 1003 9 GROUP BY and Teradata-Specific Functions For Teradata-specific functions, GROUP BY determines the partitions over which the function executes. The clause does not collapse all rows with the same value for the group-by columns into a single row. Thus, the GROUP BY clause in these cases need only specify the partitioning column for the function. For example, the following statement computes the running sales for each store by using the GROUP BY clause to partition the data in sales_tbl by StoreID: SELECT StoreID, Sales, CSUM(Sales, StoreID) FROM sales_tbl GROUP BY StoreID; The results look like this: StoreID Sales CSum(Sales,StoreID) ------- -------- ------------------- 1001 1100.00 1100.00 1001 400.00 1500.00 1001 1000.00 2500.00 1001 2000.00 4500.00 1002 500.00 500.00 1002 1500.00 2000.00 1002 2500.00 4500.00 1003 1000.00 1000.00 1003 3000.00 4000.00 Combining Window Functions, Teradata-Specific Functions, and GROUP BY The following table provides the semantics of the allowable combinations of window functions, Teradata-specific functions, aggregate functions, and the GROUP BY clause. Chapter 11: Ordered Analytical Functions GROUP BY Clause SQL Functions, Operators, Expressions, and Predicates 445 Combination Semantics Window Function Teradata-Specific Function Aggregate Function GROUP BY Clause X A value is computed for each row. X A value is computed for each row. The entire table constitutes a single group, or partition, over which the Teradata-specific function executes. X One aggregate value is computed for the entire table. X X GROUP BY collapses all rows with the same value for the group-by columns into a single row, and a value is computed for each resulting row. X X GROUP BY determines the partitions over which the Teradataspecific function executes. The clause does not collapse all rows with the same value for the group-by columns into a single row. X X An aggregation is performed for each group. X X Teradata-specific functions do not have partitions. The whole table is one partition. X X X GROUP BY determines partitions for Teradata-specific functions. GROUP BY does not collapse all rows with the same value for the group-by columns into a single row, and does not affect window function computation. X X X GROUP BY collapses all rows with the same value for the group-by columns into a single row. For window functions, a value is computed for each resulting row; for aggregate functions, an aggregation is performed for each group. Chapter 11: Ordered Analytical Functions Using Ordered Analytical Functions Examples 446 SQL Functions, Operators, Expressions, and Predicates Using Ordered Analytical Functions Examples Example 1: Using RANK and AVG Consider the result of the following SELECT statement using the following ordered analytical functions, RANK and AVG. SELECT item, smonth, sales, RANK() OVER (PARTITION BY item ORDER BY sales DESC), AVG(sales) OVER (PARTITION BY item ORDER BY smonth ROWS 3 PRECEDING) FROM sales_tbl ORDER BY item, smonth; The results table might look like the following: Example 2: Using QUALIFY With RANK Adding a QUALIFY clause to a query eliminates rows from an unqualified table. For example, if you wanted to see whether the high sales months were unusual, you could add a QUALIFY clause to the previous query. SELECT item, smonth, sales, RANK() OVER (PARTITION BY item ORDER BY sales DESC), Item SMonth Sales Rank(Sales) Moving Avg(Sales) A 1996-01 110 13 110 A 1996-02 130 10 120 A 1996-03 170 6 137 A 1996-04 210 3 155 A 1996-05 270 1 195 A 1996-06 250 2 225 A 1996-07 190 4 230 A 1996-08 180 5 222 A 1996-09 160 7 195 A 1996-10 140 9 168 A 1996-11 150 8 158 A 1996-12 120 11 142 A 1997-01 120 11 132 B 1996-02 30 5 30 ... ... ... ... ... Chapter 11: Ordered Analytical Functions Using Ordered Analytical Functions Examples SQL Functions, Operators, Expressions, and Predicates 447 AVG(sales) OVER (PARTITION BY item ORDER BY smonth ROWS 3 PRECEDING) FROM sales_tbl ORDER BY item, smonth QUALIFY RANK() OVER(PARTITION BY item ORDER BY sales DESC) <=5; This additional qualifier produces a results table that might look like the following: The result indicates that sales had probably been fairly low prior to the start of the current sales season. Example 3: Using QUALIFY With RANK Consider the following sales table named sales_tbl. Now perform the following simple SELECT statement against this table, qualifying answer rows by rank. SELECT store, prodID, sales, Item SMonth Sales Rank(Sales) Moving Avg(Sales) A 1996-04 210 3 155 A 1996-05 270 1 195 A 1996-06 250 2 225 A 1996-07 190 4 230 A 1996-08 180 5 222 B 1996-02 30 1 30 ... ... ... ... ... Store ProdID Sales 1003 C 20000.00 1003 D 50000.00 1003 A 30000.00 1002 C 35000.00 1002 D 25000.00 1002 A 40000.00 1001 C 60000.00 1001 D 35000.00 1001 A 100000.00 1001 B 10000.00 Chapter 11: Ordered Analytical Functions Using Ordered Analytical Functions Examples 448 SQL Functions, Operators, Expressions, and Predicates RANK() OVER (PARTITION BY store ORDER BY sales DESC) FROM sales_tbl QUALIFY RANK() OVER (PARTITION BY store ORDER BY sales DESC) <=3; The result appears in the following typical output table. Note that every row in the table is returned with the computed value for RANK except those that do not meet the QUALIFY clause (sales rank is less than third within the store). Store ProdID Sales Rank(Sales) 1001 A 100000.00 1 1001 C 60000.00 2 1001 D 35000.00 3 1002 A 40000.00 1 1002 C 35000.00 2 1002 D 25000.00 3 1003 D 50000.00 1 1003 A 30000.00 2 1003 C 20000.00 3 Chapter 11: Ordered Analytical Functions Window Aggregate Functions SQL Functions, Operators, Expressions, and Predicates 449 Window Aggregate Functions Purpose Cumulative, group, moving, or remaining computation of an aggregate function. A window specification can be applied to the following ANSI SQL:2008 compliant aggregate functions: A window specification can also be applied to a user-defined aggregate function. For details, see “Window Aggregate UDF” on page 717. Type ANSI SQL:2008 window aggregate function. • AVG • CORR • COUNT • COVAR_POP • COVAR_SAMP • MAX • MIN • REGR_AVGX • REGR_AVGY • REGR_COUNT • REGR_INTERCEPT • REGR_R2 • REGR_SLOPE • REGR_SXX • REGR_SXY • REGR_SYY • STDDEV_POP • STDDEV_SAMP • SUM • VAR_POP • VAR_SAMP Chapter 11: Ordered Analytical Functions Window Aggregate Functions 450 SQL Functions, Operators, Expressions, and Predicates Syntax 1101A465 window AVG A * ( value_expression ) COUNT ( value_expression ) COVAR_POP ( value_expression_1, value_expression_2 ) COVAR_SAMP ( value_expression_1, value_expression_2 ) CORR ( value_expression_1, value_expression_2 ) MAX ( value_expression ) MIN ( value_expression ) REGR_AVGX ( dependent_variable_expression, independent_variable_expression ) REGR_AVGY ( dependent_variable_expression, independent_variable_expression ) REGR_COUNT ( dependent_variable_expression, independent_variable_expression ) REGR_INTERCEPT ( dependent_variable_expression, independent_variable_expression ) REGR_R2 ( dependent_variable_expression, independent_variable_expression ) REGR_SLOPE ( dependent_variable_expression, independent_variable_expression ) REGR_SXX ( dependent_variable_expression, independent_variable_expression ) REGR_SXY ( dependent_variable_expression, independent_variable_expression ) REGR_SYY ( dependent_variable_expression, independent_variable_expression ) STDDEV_POP ( value_expression ) STDDEV_SAMP ( value_expression ) SUM ( value_expression ) VAR_POP ( value_expression ) VAR_SAMP ( value_expression ) A Chapter 11: Ordered Analytical Functions Window Aggregate Functions SQL Functions, Operators, Expressions, and Predicates 451 where: window OVER ( ROWS UNBOUNDED PRECEDING CURRENT ROW ROWS BETWEEN UNBOUNDED FOLLOWING CURRENT ROW B A PARTITION BY column_reference , value PRECEDING UNBOUNDED PRECEDING AND value PRECEDING value FOLLOWING UNBOUNDED FOLLOWING CURRENT ROW value PRECEDING value FOLLOWING value FOLLOWING value PRECEDING AND value FOLLOWING AND CURRENT ROW AND UNBOUNDED FOLLOWING CURRENT ROW value FOLLOWING UNBOUNDED FOLLOWING ORDER BY value_expression , ASC DESC A B 1101B464 RESET WHEN condition ) Chapter 11: Ordered Analytical Functions Window Aggregate Functions 452 SQL Functions, Operators, Expressions, and Predicates Syntax element … Specifies … AVG CORR COUNT COVAR_POP COVAR_SAMP MAX MIN REGR_AVGX REGR_AVGY REGR_COUNT REGR_INTERCEPT REGR_R2 REGR_SLOPE REGR_SXX REGR_SXY REGR_SYY STDDEV_POP STDDEV_SAMP SUM VAR_POP VAR_SAMP the aggregate function and arguments on which the window specification is applied. For descriptions of aggregate functions and arguments, see Chapter 10: “Aggregate Functions.” OVER how values are grouped, ordered, and considered when computing the cumulative, group, or moving function. Values are grouped according to the PARTITION BY and RESET WHEN clauses, sorted according to the ORDER BY clause, and considered according to the aggregation group within the partition. PARTITION BY in its column_reference, or comma-separated list of column references, the group, or groups, over which the function operates. PARTITION BY is optional. If there is no PARTITION BY or RESET WHEN clauses, then the entire result set, delivered by the FROM clause, constitutes a single group, or partition. PARTITION BY clause is also called the window partition clause. ORDER BY in its value_expression the order in which the values in a group, or partition, are sorted. ASC ascending sort order. The default is ASC. DESC descending sort order. RESET WHEN the group or partition, over which the function operates, depending on the evaluation of the specified condition. If the condition evaluates to TRUE, a new dynamic partition is created inside the specified window partition. RESET WHEN is optional. If there is no RESET WHEN or PARTITION BY clauses, then the entire result set, delivered by the FROM clause, constitutes a single partition. If RESET WHEN is specified, then the ORDER BY clause must be specified also. Chapter 11: Ordered Analytical Functions Window Aggregate Functions SQL Functions, Operators, Expressions, and Predicates 453 ANSI Compliance Window aggregate functions are partially ANSI SQL:2008 compliant. condition a conditional expression used to determine conditional partitioning. The condition in the RESET WHEN clause is equivalent in scope to the condition in a QUALIFY clause with the additional constraint that nested ordered analytical functions cannot specify a RESET WHEN clause. In addition, you cannot specify SELECT as a nested subquery within the condition. The condition is applied to the rows in all designated window partitions to create sub-partitions within the particular window partitions. For more information, see “RESET WHEN Condition Rules” on page 433 and the “QUALIFY Clause” in SQL Data Manipulation Language. ROWS the starting point for the aggregation group within the partition. The aggregation group end is the current row. The aggregation group of a row R is a set of rows, defined relative to R in the ordering of the rows within the partition. If there is no ROWS or ROWS BETWEEN clause, the default aggregation group is ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING. ROWS BETWEEN the aggregation group start and end, which defines a set of rows relative to the current row in the ordering of the rows within the partition. The row specified by the group start must precede the row specified by the group end. If there is no ROWS or ROWS BETWEEN clause, the default aggregation group is ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING. UNBOUNDED PRECEDING the entire partition preceding the current row. UNBOUNDED FOLLOWING the entire partition following the current row. CURRENT ROW the start or end of the aggregation group as the current row. value PRECEDING the number of rows preceding the current row. The value for value is always a positive integer constant. The maximum number of rows in an aggregation group is 4096 when value PRECEDING appears as the group start or group end. value FOLLOWING the number of rows following the current row. The value for value is always a positive integer constant. The maximum number of rows in an aggregation group is 4096 when value FOLLOWING appears as the group start or group end. Syntax element … Specifies … Chapter 11: Ordered Analytical Functions Window Aggregate Functions 454 SQL Functions, Operators, Expressions, and Predicates In the presence of an ORDER BY clause and the absence of a ROWS or ROWS BETWEEN clause, ANSI SQL:2008 window aggregate functions use ROWS UNBOUNDED PRECEDING as the default aggregation group, whereas Teradata SQL window aggregate functions use ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING. The RESET WHEN clause is a Teradata extension to the ANSI SQL standard. Type of Computation Arguments to Window Aggregate Functions Window aggregate functions can take constants, constant expressions, column names (sales, for example), or column expressions (sales + profit) as arguments. Window aggregates can also take regular aggregates as input parameters to the PARTITION BY and ORDER BY clauses. The RESET WHEN clause can take an aggregate as part of the RESET WHEN condition clause. COUNT can take “*” as an input argument, as in the following SQL query: SELECT city, kind, sales, profit, COUNT(*) OVER (PARTITION BY city, kind ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) To compute this type of function … Use this aggregation group … Cumulative • ROWS UNBOUNDED PRECEDING • ROWS BETWEEN UNBOUNDED PRECEDING AND value PRECEDING • ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW • ROWS BETWEEN UNBOUNDED PRECEDING AND value FOLLOWING Group ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING Moving • ROWS value PRECEDING • ROWS CURRENT ROW • ROWS BETWEEN value PRECEDING AND value PRECEDING • ROWS BETWEEN value PRECEDING AND CURRENT ROW • ROWS BETWEEN value PRECEDING AND value FOLLOWING • ROWS BETWEEN CURRENT ROW AND CURRENT ROW • ROWS BETWEEN CURRENT ROW AND value FOLLOWING • ROWS BETWEEN value FOLLOWING AND value FOLLOWING Remaining • ROWS BETWEEN value PRECEDING AND UNBOUNDED FOLLOWING • ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING • ROWS BETWEEN value FOLLOWING AND UNBOUNDED FOLLOWING Chapter 11: Ordered Analytical Functions Window Aggregate Functions SQL Functions, Operators, Expressions, and Predicates 455 FROM activity_month; Result Type and Format The result data type and format for window aggregate functions are as follows. Function Result Type Format AVG(x) where x is a character type FLOAT Default format for FLOAT AVG(x) where x is a numeric, DATE, or INTERVAL type FLOAT Same format as operand x CORR(x,y) COVAR_POP(x,y) COVAR_SAMP(x,y) REGR_AVGX(x,y) REGR_AVGY(x,y) REGR_COUNT(x,y) REGR_INTERCEPT(x,y) REGR_R2(x,y) REGR_SLOPE(x,y) REGR_SXX(x,y) REGR_SXY(x,y) REGR_SYY(x,y) STDDEV_POP(x) STDDEV_SAMP(x) VAR_POP(x) VAR_SAMP(x) where x is a character type FLOAT Default format for FLOAT Chapter 11: Ordered Analytical Functions Window Aggregate Functions 456 SQL Functions, Operators, Expressions, and Predicates CORR(x,y) COVAR_POP(x,y) COVAR_SAMP(x,y) REGR_AVGX(x,y) REGR_AVGY(x,y) REGR_INTERCEPT(x,y) REGR_R2(x,y) REGR_SLOPE(x,y) REGR_SXX(x,y) REGR_SXY(x,y) REGR_SYY(x,y) STDDEV_POP(x) STDDEV_SAMP(x) VAR_POP(x) VAR_SAMP(x) where x is one of the following types: • Numeric • DATE • Interval Same data type as operand x. Default format for the data type of operand x COUNT(x) COUNT(*) REGR_COUNT(x,y) where the transaction mode is ANSI DECIMAL(p,0) Default format for resulting data type IF MaxDecimal in DBSControl is … THEN p is … 0, 15, or 18 15. 38 38. ANSI transaction mode uses DECIMAL because tables frequently have a cardinality exceeding the range of INTEGER. COUNT(x) COUNT(*) REGR_COUNT(x,y) where the transaction mode is Teradata INTEGER Teradata transaction mode uses INTEGER to avoid regression problems. Default format for resulting data type MAX(x), MIN(x) Same data type as operand x. Same format as operand x SUM(x) where x is a character type Same as the operand x. Default format for FLOAT Function Result Type Format Chapter 11: Ordered Analytical Functions Window Aggregate Functions SQL Functions, Operators, Expressions, and Predicates 457 For information on the default format of data types and an explanation of the formatting characters in the format, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Result Title The default title that appears in the heading for displayed or printed results depends on the type of computation performed. SUM(x) where x is a DECIMAL(n,m) type DECIMAL(p,m), where p is determined by the rules in the following table. Default format for DECIMAL IF MaxDecimal in DBSControl is … AND … THEN p is … 0 or 15 n =15 15. 15 < n =18 18. n > 18 38. 18 n =18 18. n > 18 38. 38 n = any value 38. SUM(x) where x is any numeric type other than DECIMAL Same as the operand x. Default format for the data type of the operand Function Result Type Format IF the type of computation is … THEN the result title is … cumulative Cumulative Function_name (argument_list) For example, consider the following computation: SELECT AVG(sales) OVER (PARTITION BY region ORDER BY smonth ROWS UNBOUNDED PRECEDING) FROM sales_history; The title that appears in the result heading is: Cumulative Avg(sales) --------------------- Chapter 11: Ordered Analytical Functions Window Aggregate Functions 458 SQL Functions, Operators, Expressions, and Predicates Problems With Missing Data Ensure that data you analyze has no missing data points. Computing a moving function over data with missing points produces unexpected and incorrect results because the computation considers n physical rows of data rather than n logical data points. Using Window Aggregate Functions Instead of Teradata Functions Be sure to use the ANSI-compliant window functions for any new applications you develop. Avoid using Teradata-specific functions such as MAVG, CSUM, and MSUM for applications intended to be ANSI-compliant and portable. group Group Function_name (argument_list) For example, consider the following computation: SELECT AVG(sales) OVER (PARTITION BY region ORDER BY smonth ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM sales_history; The title that appears in the result heading is: Group Avg(sales) ---------------- moving Moving Function_name (argument_list) For example, consider the following computation: SELECT AVG(sales) OVER (PARTITION BY region ORDER BY smonth ROWS 2 PRECEDING) FROM sales_history; The title that appears in the result heading is: Moving Avg(sales) ----------------- remaining Remaining Function_name (argument_list) For example, consider the following computation: SELECT AVG(sales) OVER (PARTITION BY region ORDER BY smonth ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM sales_history; The title that appears in the result heading is: Remaining Avg(sales) -------------------- IF the type of computation is … THEN the result title is … Chapter 11: Ordered Analytical Functions Window Aggregate Functions SQL Functions, Operators, Expressions, and Predicates 459 ANSI Function Teradata Function Relationship AVG MAVG The form of the AVG window function that specifies an aggregation group of ROWS value PRECEDING is the ANSI equivalent of the MAVG Teradataspecific function. Note that the ROWS value PRECEDING phrase specifies the number of rows preceding the current row that are used, together with the current row, to compute the moving average. The total number of rows in the aggregation group is value + 1. For the MAVG function, the total number of rows in the aggregation group is the value of width. For AVG window function, an aggregation group of ROWS 5 PRECEDING, for example, means that the 5 rows preceding the current row, plus the current row, are used to compute the moving average. Thus the moving average for the 6th row of a partition would have considered row 6, plus rows 5, 4, 3, 2, and 1 (that is, 6 rows in all). For the MAVG function, a width of 5 means that the current row, plus 4 preceding rows, are used to compute the moving average. The moving average for the 6th row would have considered row 6, plus rows 4, 5, 3, and 2 (that is, 5 rows in all). SUM CSUM MSUM Be sure to use the ANSI-compliant SUM window function for any new applications you develop. Avoid using CSUM and MSUM for applications intended to be ANSI-compliant and portable. The following defines the relationship between the SUM window function and the CSUM and MSUM Teradata-specific functions, respectively: • The SUM window function that uses the ORDER BY clause and specifies ROWS UNBOUNDED PRECEDING is the ANSI equivalent of CSUM. • The SUM window function that uses the ORDER BY clause and specifies ROWS value PRECEDING is the ANSI equivalent of MSUM. Note that the ROWS value PRECEDING phrase specifies the number of rows preceding the current row that are used, together with the current row, to compute the moving average. The total number of rows in the aggregation group is value + 1. For the MSUM function, the total number of rows in the aggregation group is the value of width. Thus for the SUM window function that computes a moving sum, an aggregation group of ROWS 5 PRECEDING means that the 5 rows preceding the current row, plus the current row, are used to compute the moving sum. The moving sum for the 6th row of a partition, for example, would have considered row 6, plus rows 5, 4, 3, 2, and 1 (that is, 6 rows in all). For the MSUM function, a width of 5 means that the current row, plus 4 preceding rows, are used to compute the moving sum. The moving sum for the 6th row, for example, would have considered row 6, plus rows 5, 4, 3, and 2 (that is, 5 rows in all). Moreover, for data having fewer than width rows, MSUM computes the sum using all the preceding rows. MSUM returns the current sum rather than nulls when the number of rows in the sample is fewer than width. Chapter 11: Ordered Analytical Functions Window Aggregate Functions 460 SQL Functions, Operators, Expressions, and Predicates Example 1: Moving Average Determine, for a business with several sales territories, the sales in each territory averaged over the current month and the preceding 2 months. The following query might return the results found in the table that follows it. SELECT territory, smonth, sales, AVG(sales) OVER (PARTITION BY territory ORDER BY smonth ROWS 2 PRECEDING) FROM sales_history; territory smonth sales Moving Avg(sales) --------- ------ ----- ----------------- East 199810 10 10 East 199811 4 7 East 199812 10 8 East 199901 7 7 East 199902 10 9 West 199810 8 8 West 199811 12 10 West 199812 7 9 West 199901 11 10 West 199902 6 8 The meanings of the phrases in the example query are as follows: Thus, the moving average for the first row of the partition East (199810), which has no preceding rows, is 10. That is, the value of the first row, the current row (10)/ the number of rows (1) = 10. The moving average for the second row of the partition East (199811), which has only 1 preceding row, is 7. That is, the value of the second row, the current row, and the preceding row (10 + 4) / the number of rows (2) = 7. The moving average for the third row of the partition East (199812), which has 2 preceding rows, is 8. That is, the value of the third row, the current row, and the 2 preceding rows (10 + 4 + 10) / the number of rows (3) = 8. And so on. Month is specified as a six-digit numeric in the YYYYMM format. Phrase Meaning PARTITION BY Indicates that the rows delivered by the FROM clause, the rows of sales_history, should be assigned to groups, or partitions, based on their territory. If no PARTITION clause is specified, then the entire result set constitutes a single group, or partition. ORDER BY Indicates that rows are sorted in ascending order of month within each group, or partition. Ascending is the default sort order. ROWS 2 PRECEDING Defines the number of rows used to compute the moving average. In this case, the computation uses the current row and the 2 preceding rows of the group, or partition, as available. Chapter 11: Ordered Analytical Functions Window Aggregate Functions SQL Functions, Operators, Expressions, and Predicates 461 Example 2: Group Count The following SQL query might yield the results that follow it, where the group count for sales is returned for each of the four partitions defined by city and kind. Notice that rows that have no sales are not counted. SELECT city, kind, sales, profit, COUNT(sales) OVER (PARTITION BY city, kind ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM activity_month; city kind sales profit Group Count(sales) ------- -------- ----- ------ ------------------ LA Canvas 45 320 4 LA Canvas 125 190 4 LA Canvas 125 400 4 LA Canvas 20 120 4 LA Leather 20 40 1 LA Leather ? ? 1 Seattle Canvas 15 30 3 Seattle Canvas 20 30 3 Seattle Canvas 20 100 3 Seattle Leather 35 50 1 Seattle Leather ? ? 1 Example 3: Remaining Count To count all the rows, including rows that have no sales, use COUNT(*). Here is an example that counts the number of rows remaining in the partition after the current row: SELECT city, kind, sales, profit, COUNT(*) OVER (PARTITION BY city, kind ORDER BY profit DESC ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) FROM activity_month; city kind sales profit Remaining Count(*) ------- -------- ----- ------ ------------------ LA Canvas 20 120 ? LA Canvas 125 190 1 LA Canvas 45 320 2 LA Canvas 125 400 3 LA Leather ? ? ? LA Leather 20 40 1 Seattle Canvas 15 30 ? Seattle Canvas 20 30 1 Seattle Canvas 20 100 2 Seattle Leather ? ? ? Seattle Leather 35 50 1 Note that the sort order that you specify in the window specification defines the sort order of the rows over which the function is applied; it does not define the ordering of the results. In the example, the DESC sort order is specified for the computation, but the results are returned in the reverse order. Chapter 11: Ordered Analytical Functions Window Aggregate Functions 462 SQL Functions, Operators, Expressions, and Predicates To order the results, use the ORDER BY phrase in the SELECT statement: SELECT city, kind, sales, profit, COUNT(*) OVER (PARTITION BY city, kind ORDER BY profit DESC ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) FROM activity_month ORDER BY city, kind, profit DESC; city kind sales profit Remaining Count(*) ------- -------- ----- ------ ------------------ LA Canvas 125 400 3 LA Canvas 45 320 2 LA Canvas 125 190 1 LA Canvas 20 120 ? LA Leather 20 40 1 LA Leather ? ? ? Seattle Canvas 20 100 2 Seattle Canvas 20 30 1 Seattle Canvas 15 30 ? Seattle Leather 35 50 1 Seattle Leather ? ? ? Example 4: Cumulative Maximum The following SQL query might yield the results that follow it, where the cumulative maximum value for sales is returned for each partition defined by city and kind. SELECT city, kind, sales, week, MAX(sales) OVER (PARTITION BY city, kind ORDER BY week ROWS UNBOUNDED PRECEDING) FROM activity_month; city kind sales week Cumulative Max(sales) ------- -------- ----- ---- --------------------- LA Canvas 263 16 263 LA Canvas 294 17 294 LA Canvas 321 18 321 LA Canvas 274 20 321 LA Leather 144 16 144 LA Leather 826 17 826 LA Leather 489 20 826 LA Leather 555 21 826 Seattle Canvas 100 16 100 Seattle Canvas 182 17 182 Seattle Canvas 94 18 182 Seattle Leather 933 16 933 Seattle Leather 840 17 933 Seattle Leather 899 18 933 Seattle Leather 915 19 933 Seattle Leather 462 20 933 Chapter 11: Ordered Analytical Functions Window Aggregate Functions SQL Functions, Operators, Expressions, and Predicates 463 Example 5: Cumulative Minimum The following SQL query might yield the results that follow it, where the cumulative minimum value for sales is returned for each partition defined by city and kind. SELECT city, kind, sales, week, MIN(sales) OVER (PARTITION BY city, kind ORDER BY week ROWS UNBOUNDED PRECEDING) FROM activity_month; city kind sales week Cumulative Min(sales) ------- -------- ----- ---- --------------------- LA Canvas 263 16 263 LA Canvas 294 17 263 LA Canvas 321 18 263 LA Canvas 274 20 263 LA Leather 144 16 144 LA Leather 826 17 144 LA Leather 489 20 144 LA Leather 555 21 144 Seattle Canvas 100 16 100 Seattle Canvas 182 17 100 Seattle Canvas 94 18 94 Seattle Leather 933 16 933 Seattle Leather 840 17 840 Seattle Leather 899 18 840 Seattle Leather 915 19 840 Seattle Leather 462 20 462 Example 6: Cumulative Sum The following query returns the cumulative balance per account ordered by transaction date: SELECT acct_number, trans_date, trans_amount, SUM(trans_amount) OVER (PARTITION BY acct_number ORDER BY trans_date ROWS UNBOUNDED PRECEDING) as balance FROM ledger ORDER BY acct_number, trans_date; Here are the possible results of the preceding SELECT: acct_number trans_date trans_amount balance 73829 1998-11-01 113.45 113.45 73829 1988-11-05 -52.01 61.44 73929 1998-11-13 36.25 97.69 82930 1998-11-01 10.56 10.56 82930 1998-11-21 32.55 43.11 82930 1998-11-29 -5.02 38.09 Chapter 11: Ordered Analytical Functions Window Aggregate Functions 464 SQL Functions, Operators, Expressions, and Predicates Example 7: Group Sum The query below finds the total sum of meat sales for each city. SELECT city, kind, sales, SUM(sales) OVER (PARTITION BY city ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM monthly; The possible results of the preceding SELECT appear in the following table: Example 8: Group Sum The following query returns the total sum of meat sales for all cities. Note there is no PARTITION BY clause in the SUM function, so all cities are included in the group sum. SELECT city, kind, sales, SUM(sales) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM monthly; The possible results of the preceding SELECT appear in the table below: city kind sales Group Sum (sales) Omaha pure pork 45 220 Omaha pure pork 125 220 Omaha pure pork 25 220 Omaha variety pack 25 220 Chicago variety pack 55 175 Chicago variety pack 45 175 Chicago pure pork 50 175 Chicago variety pack 25 175 city kind sales Group Sum (sales) Omaha pure pork 45 395 Omaha pure pork 125 395 Omaha pure pork 25 395 Omaha variety pack 25 395 Chicago variety pack 55 395 Chicago variety pack 45 395 Chicago pure pork 50 395 Chicago variety pack 25 395 Chapter 11: Ordered Analytical Functions Window Aggregate Functions SQL Functions, Operators, Expressions, and Predicates 465 Example 9: Moving Sum The following query returns the moving sum of meat sales by city. Notice that the query returns the moving sum of sales by city (the partition) for the current row (of the partition) and three preceding rows where possible. The order in which each meat variety is returned is the default ascending order according to profit. Where no sales figures are available, no moving sum of sales is possible. In this case, there is a null in the sum(sales) column. SELECT city, kind, sales, profit, SUM(sales) OVER (PARTITION BY city, kind ORDER BY profit ROWS 3 PRECEDING) FROM monthly; city kind sales profit Moving sum (sales) Omaha pure pork 25 40 25 Omaha pure pork 25 120 50 Omaha pure pork 45 140 95 Omaha pure pork 125 190 220 Omaha pure pork 45 320 240 Omaha pure pork 1255 400 340 Omaha variety pack ? ? ? Omaha variety pack 25 40 25 Omaha variety pack 25 120 50 Chicago pure pork ? ? ? Chicago pure pork 15 10 15 Chicago pure pork 54 12 69 Chicago pure pork 14 20 83 Chicago pure pork 54 24 137 Chicago pure pork 14 34 136 Chicago pure pork 95 80 177 Chicago pure pork 95 140 258 Chicago pure pork 15 220 219 Chicago variety pack 23 39 23 Chicago variety pack 25 40 48 Chicago variety pack 125 70 173 Chicago variety pack 125 100 298 Chicago variety pack 23 100 298 Chicago variety pack 25 120 298 Chapter 11: Ordered Analytical Functions Window Aggregate Functions 466 SQL Functions, Operators, Expressions, and Predicates Example 10: Remaining Sum The following query returns the remaining sum of meat sales for all cities. Note there is no PARTITION BY clause in the SUM function, so all cities are included in the remaining sum. SELECT city, kind, sales, SUM(sales) OVER (ORDER BY city, kind ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) FROM monthly; The possible results of the preceding SELECT appear in the table below: city kind sales Remaining Sum(sales) ------- ------------- ------- -------------------- Omaha variety pack 25 ? Omaha pure pork 125 25 Omaha pure pork 25 150 Omaha pure pork 45 175 Chicago variety pack 55 220 Chicago variety pack 25 275 Chicago variety pack 45 300 Chicago pure pork 50 345 Note that the sort order for the computation is alphabetical by city, and then by kind. The results, however, appear in the reverse order. The sort order that you specify in the window specification defines the sort order of the rows over which the function is applied; it does not define the ordering of the results. To order the results, use an ORDER BY phrase in the SELECT statement. For example: SELECT city, kind, sales, SUM(sales) OVER (ORDER BY city, kind ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) FROM monthly ORDER BY city, kind; The possible results of the preceding SELECT appear in the table below: city kind sales Remaining Sum(sales) ------- ------------- ------- -------------------- Chicago pure pork 50 345 Chicago variety pack 55 265 Chicago variety pack 25 320 Chicago variety pack 45 220 Omaha pure pork 25 70 Omaha pure pork 125 95 Omaha pure pork 45 25 Omaha variety pack 25 ? Chapter 11: Ordered Analytical Functions CSUM SQL Functions, Operators, Expressions, and Predicates 467 CSUM Purpose Returns the cumulative (or running) sum of a value expression for each row in a partition, assuming the rows in the partition are sorted by the sort_expression list. Type Teradata-specific function. Syntax where: ANSI Compliance CSUM is a Teradata extension to the ANSI SQL:2008 standard. Syntax element … Specifies … value_expression a numeric constant or column expression for which a running sum is to be computed. By default, CSUM uses the default data type of value_expression. Larger numeric values are supported by casting it to a higher data type. The expression cannot contain any ordered analytical or aggregate functions. sort_expression a constant or column expression or comma-separated list of constant or column expressions to be used to sort the values. For example, CSUM(Sale, Region ASC, Store DESC), where Sale is the value_expression, and Region ASC, Store DESC is the sort_expression list. The expression cannot contain any ordered analytical or aggregate functions. ASC ascending sort order. The default sort direction is ASC. DESC descending sort order. 1101A398 CSUM , ASC DESC ( ( value_expression, sort_expression Chapter 11: Ordered Analytical Functions CSUM 468 SQL Functions, Operators, Expressions, and Predicates Using SUM Instead of CSUM The use of CSUM is strongly discouraged. It is a Teradata extension to the ANSI SQL:2008 standard, and is equivalent to the ANSI-compliant SUM window function that specifies ROWS UNBOUNDED PRECEDING as its aggregation group. CSUM is retained only for backward compatibility with existing applications. For more information on the SUM window function, see “Window Aggregate Functions” on page 449. Meaning of Cumulative Sums CSUM accumulates a sum over an ordered set of rows, providing the current value of the SUM on each row. Result Type and Attributes The data type, format, and title for CSUM(x, y direction) are as follows: For information on the default format of data types and an explanation of the formatting characters in the format, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Example 1 Report the daily running sales total for product code 10 for each month of 1998. SELECT cmonth, CSUM(sumPrice, cdate) FROM (SELECT a2.month_of_year, a2.calendar_date,a1.itemID, SUM(a1.price) FROM Sales a1, SYS_CALENDAR.Calendar a2 WHERE a1.calendar_date=a2.calendar_date AND a2.calendar_date=1998 AND a1.itemID=10 GROUP BY a2.month_of_year, a1.calendar_date, a1.itemID) AS T1(cmonth, cdate, sumPrice) GROUP BY cmonth; Grouping by month allows the total to accumulate until the end of each month, when it is then set to zero for the next month. This permits the calculation of cumulative totals for each item in the same query. Data Type Format Title Same as operand x CSum(x, y direction) IF operand x is … THEN the format is … character the default format for FLOAT. numeric the same format as x. Chapter 11: Ordered Analytical Functions CSUM SQL Functions, Operators, Expressions, and Predicates 469 Example 2 Provide a running total for sales of each item in store 5 in January and generate output that is ready to export into a graphing program. SELECT Item, SalesDate, CSUM(Revenue,Item,SalesDate) AS CumulativeSales FROM (SELECT Item, SalesDate, SUM(Sales) AS Revenue FROM DailySales WHERE StoreId=5 AND SalesDate BETWEEN '1/1/1999' AND '1/31/1999' GROUP BY Item, SalesDate) AS ItemSales ORDER BY SalesDate; The result might like something like the following table: Item SalesDate CumulativeSales InstaWoof dog food 01/01/1999 972.99 InstaWoof dog food 01/02/1999 2361.99 InstaWoof dog food 01/03/1999 5110.97 InstaWoof dog food 01/04/1999 7793.91 Chapter 11: Ordered Analytical Functions MAVG 470 SQL Functions, Operators, Expressions, and Predicates MAVG Purpose Computes the moving average of a value expression for each row in a partition using the specified value expression for the current row and the preceding width-1 rows. Type Teradata-specific function. Syntax where: ANSI Compliance MAVG is a Teradata extension to the ANSI SQL:2008 standard. Syntax element … Specifies … value_expression a numeric constant or column expression for which a moving average is to be computed. The expression cannot contain any ordered analytical or aggregate functions. width number of previous rows to be used in computing the moving average. The value is always a positive integer constant. The maximum is 4096. sort_expression a constant or column expression or comma-separated list of constant or column expressions to be used to sort the values. For example, MAVG(Sale, 6, Region ASC, Store DESC), where Sale is the value_expression, 6 is the width, and Region ASC, Store DESC is the sort_expression list. The expression cannot contain any ordered analytical or aggregate functions. ASC ascending sort order. The default sort direction is ASC. DESC descending sort order. 1101A399 MAVG , ASC DESC ( ( value_expression, width, sort_expression Chapter 11: Ordered Analytical Functions MAVG SQL Functions, Operators, Expressions, and Predicates 471 Using AVG Instead of MAVG The use of MAVG is strongly discouraged. It is a Teradata extension to the ANSI SQL:2008 standard, and is equivalent to the ANSI-compliant AVG window function that specifies ROWS value PRECEDING as its aggregation group. MAVG is retained only for backward compatibility with existing applications. For more information on the AVG window function, see “Window Aggregate Functions” on page 449. Result Type and Attributes The data type, format, and title for MAVG(x, w, y direction) are as follows: For information on the default format of data types, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Problems With Missing Data Ensure that data you analyze using MAVG has no missing data points. Computing a moving average over data with missing points produces unexpected and incorrect results because the computation considers n physical rows of data rather than n logical data points. Computing the Moving Average When Number of Rows < width For the (possibly grouped) resulting relation, the moving average considering width rows is computed where the rows are sorted by the sort_expression list. When there are fewer than width rows, the average is computed using the current row and all preceding rows. Example 1 Compute the 7-day moving average of sales for product code 10 for each day in the month of October, 1996. SELECT cdate, itemID, MAVG(sumPrice, 7, date) FROM (SELECT a1.calendar_date, a1.itemID, SUM(a1.price) FROM Sales a1 Data Type Format Title Same as operand x MAvg(x, w, y direction) IF operand x is … THEN the format is … character the default format for FLOAT. • numeric • date • interval the same format as x. Chapter 11: Ordered Analytical Functions MAVG 472 SQL Functions, Operators, Expressions, and Predicates WHERE a1.itemID=10 AND a1.calendar_date BETWEEN 96-10-01 AND 96-10-31 GROUP BY a1.calendar_date, a1.itemID) AS T1(cdate, itemID, sumPrice); Example 2 The following example calculates the 50-day moving average of the closing price of the stock for Zemlinsky Bros. Corporation. The ticker name for the company is ZBC. SELECT MarketDay, ClosingPrice, MAVG(ClosingPrice,50, MarketDay) AS ZBCAverage FROM MarketDailyClosing WHERE Ticker = 'ZBC' ORDER BY MarketDay; The results for the query might look something like the following table: MarketDay ClosingPrice ZBCAverage 12/27/1999 89 1/16 85 1/2 12/28/1999 91 1/8 86 1/16 12/29/1999 92 3/4 86 1/2 12/30/1999 94 1/2 87 Chapter 11: Ordered Analytical Functions MDIFF SQL Functions, Operators, Expressions, and Predicates 473 MDIFF Purpose Returns the moving difference between the specified value expression for the current row and the preceding width rows for each row in the partition. Type Teradata-specific function. Syntax where: ANSI Compliance MDIFF is a Teradata extension to the ANSI SQL:2008 standard. Syntax element … Specifies … value_expression a numeric column or constant expression for which a moving difference is to be computed. The expression cannot contain any ordered analytical or aggregate functions. width the number of previous rows to be used in computing the moving difference. The value is always a positive integer constant. The maximum is 4096. sort_expression a constant or column expression or comma-separated list of constant or column expressions to be used to sort the values. For example, MDIFF(Sale, 6, Region ASC, Store DESC), where Sale is the value_expression, 6 is the width, and Region ASC, Store DESC is the sort_expression list. The expression cannot contain any ordered analytical or aggregate functions. ASC ascending sort order. The default sort direction is ASC. DESC descending sort order. 1101A400 MDIFF , ASC DESC ( ( value_expression, width, sort_expression Chapter 11: Ordered Analytical Functions MDIFF 474 SQL Functions, Operators, Expressions, and Predicates Meaning of Moving Difference A common business metric is to compare activity for some variable in a current time period to the activity for the same variable in another time period a fixed distance in the past. For example, you might want to compare current sales volume against sales volume for preceding quarters. This is a moving difference calculation where value_expression would be the quarterly sales volume, width is 4, and sort_expression might be the quarter_of_calendar column from the SYS_CALENDAR.Calendar system view. Using SUM Instead of MDIFF The use of MDIFF is strongly discouraged. It is a Teradata extension to the ANSI SQL:2008 standard, and is retained only for backward compatibility with existing applications. MDIFF(x, w, y) is equivalent to: x - SUM(x) OVER (ORDER BY y ROWS BETWEEN w PRECEDING AND w PRECEDING) For more information on the SUM window function, see “Window Aggregate Functions” on page 449. Result Type and Attributes The data type, format, and title for MDIFF(x, w, y direction) are as follows: For information on the default format of data types, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Problems With Missing Data Ensure that rows you analyze using MDIFF have no missing data points. Computing a moving difference over data with missing points produces unexpected and incorrect results because the computation considers n physical rows of data rather than n logical data points. Data Type and Format Title MDiff(x, w, y direction) IF operand x is … THEN the data type is … AND the format is … character the same as x. the default format for FLOAT. numeric the same as x. the same format as x. date INTEGER the default format for INTEGER. Chapter 11: Ordered Analytical Functions MDIFF SQL Functions, Operators, Expressions, and Predicates 475 Computing the Moving Difference When No Preceding Row Exists When the number of preceding rows to use in a moving difference computation is fewer than the specified width, the result is null. Example 1 Display the difference between each quarter and the same quarter sales for last year for product code 10. SELECT year_of_calendar, quarter_of_calendar, MDIFF(sumPrice, 4, year_of_calendar, quarter_of_calendar) FROM (SELECT a2.year_of_calendar, a2.quarter_of_calendar, SUM(a2.Price) AS sumPrice FROM Sales a1, SYS_CALENDAR.Calendar a2 WHERE a1.itemID=10 and a1.calendar_date=a2.calendar_date GROUP BY a2.year_of_calendar, a2.quarter_of_calendar) AS T1 ORDER BY year_of_calendar, quarter_of_year; Example 2 The following example computes the changing market volume week over week for the stock of company Horatio Parker Imports. The ticker name for the company is HPI. SELECT MarketWeek, WeekVolume, MDIFF(WeekVolume,1,MarketWeek) AS HPIVolumeDiff FROM (SELECT MarketWeek, SUM(Volume) AS WeekVolume FROM MarketDailyClosing WHERE Ticker = 'HPI' GROUP BY MarketWeek) ORDER BY MarketWeek; The result might look like the following table. Note that the first row is null for column HPIVolume Diff, indicating no previous row from which to compute a difference. MarketWeek WeekVolume HPIVolumeDiff 11/29/1999 9817671 ? 12/06/1999 9945671 128000 12/13/1999 10099459 153788 12/20/1999 10490732 391273 12/27/1999 11045331 554599 Chapter 11: Ordered Analytical Functions MLINREG 476 SQL Functions, Operators, Expressions, and Predicates MLINREG Purpose Returns a predicted value for an expression based on a least squares moving linear regression of the previous width-1 (based on sort_expression) column values. Type Teradata-specific function. Syntax where: Syntax element … Specifies … value_expression a numeric constant or column expression for which a predicted value is to be computed. The expression cannot contain any ordered analytical or aggregate functions. The data type of the expression must be numeric or a data type that Teradata Database can successfully convert implicitly to numeric. width the number of rows to use to compute the function. width-1 previous rows are used to compute the linear regression and the row value itself is used for calculating the predicted value. The value is always a positive integer constant greater than 2. The maximum is 4096. sort_expression a column expression that defines the independent variable for calculating the linear regression. For example, MLINREG(Sales, 6, Fiscal_Year_Month ASC), where Sales is the value_expression, 6 is the width, and Fiscal_Year_Month ASC is the sort_expression. The data type of the column reference must be numeric or a data type that Teradata Database can successfully convert implicitly to numeric. ASC ascending sort order. The default sort direction is ASC. DESC descending sort order. 1101A401 MLINREG ASC DESC ( ( value_expression, width, sort_expression Chapter 11: Ordered Analytical Functions MLINREG SQL Functions, Operators, Expressions, and Predicates 477 ANSI Compliance MLINREG is Teradata extension to the ANSI SQL:2008 standard. Using ANSI-Compliant Window Functions Instead of MLINREG Using ANSI-compliant window functions instead of MLINREG is strongly encouraged. MLINREG is a Teradata extension to the ANSI SQL:2008 standard, and is retained only for backward compatibility with existing applications. Result Type and Attributes The data type, format, and title for MLINREG(x, w, y direction) are as follows: For information on the default format of data types and an explanation of the formatting characters in the format, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Default Independent Variable MLINREG assumes that the independent variable is described by sort_expression. Computing MLINREG When Preceding Rows < width - 1 When there are fewer than width-1 preceding rows, MLINREG computes the regression using all the preceding rows. MLINREG Report Structure All rows in the results table except the first two, which are always null, display the predicted value. Data Type Format Title Same as operand x MLinReg(x, w, y direction) IF operand x is … THEN the format is … character the default format for FLOAT. • numeric • date • interval the same format as x. Chapter 11: Ordered Analytical Functions MLINREG 478 SQL Functions, Operators, Expressions, and Predicates Example Consider the itemID, smonth, and sales columns from sales_table: SELECT itemID, smonth, sales FROM fiscal_year_sales_table ORDER BY itemID, smonth; itemID smonth sales ------ -------- ----- A 1 100 A 2 110 A 3 120 A 4 130 A 5 140 A 6 150 A 7 170 A 8 190 A 9 210 A 10 230 A 11 250 A 12 ? B 1 20 B 2 30 ... Assume that the null value in the sales column is because in this example the month of December (month 12) is a future date and the value is unknown. The following statement uses MLINREG to display the expected sales using past trends for each month for each product using the sales data for the previous six months. SELECT itemID, smonth, sales, MLINREG(sales,7,smonth) FROM fiscal_year_sales_table; GROUP BY itemID; itemID smonth sales MLinReg(sales,7,smonth) ------ -------- ----- ----------------------- A 1 100 ? A 2 110 ? A 3 120 120 A 4 130 130 A 5 140 140 A 6 150 150 A 7 170 160 A 8 190 177 A 9 210 198 A 10 230 222 A 11 250 247 A 12 ? 270 B 1 20 ? B 2 30 ? ... Chapter 11: Ordered Analytical Functions MSUM SQL Functions, Operators, Expressions, and Predicates 479 MSUM Purpose Computes the moving sum specified by a value expression for the current row and the preceding n-1 rows. This function is very similar to the MAVG function. Type Teradata-specific function. Syntax where: ANSI Compliance MSUM is a Teradata extension to the ANSI SQL:2008 standard. Syntax element … Specifies … value_expression a numeric constant or column expression for which a moving sum is to be computed. The expression cannot contain any ordered analytical or aggregate functions. width the number of previous rows to be used in computing the moving sum. The value is always a positive integer constant. The maximum is 4096. sort_expression a constant or column expression or comma-separated list of constant or column expressions to be used to sort the values. For example, MSUM(Sale, 6, Region ASC, Store DESC), where Sale is the value_expression, 6 is the width, and Region ASC, Store DESC is the sort_expression list. ASC ascending sort order. The default sort direction is ASC. DESC descending sort order. 1101A402 MSUM , ASC DESC ( ( value_expression, width, sort_expression Chapter 11: Ordered Analytical Functions MSUM 480 SQL Functions, Operators, Expressions, and Predicates Using SUM Instead of MSUM The use of MSUM is strongly discouraged. It is a Teradata extension to the ANSI SQL:2008 standard, and is equivalent to the ANSI-compliant SUM window function. MSUM is retained only for backward compatibility with existing applications. For more information on the SUM window function, see “Window Aggregate Functions” on page 449. Result Type and Attributes The data type, format, and title for MSUM(x, w, y direction) are as follows: For information on the default format of data types, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Problems With Missing Data Ensure that data you analyze using MSUM has no missing data points. Computing a moving average over data with missing points produces unexpected and incorrect results because the computation considers n physical rows of data rather than n logical data points. Computing MSUM When Number of Rows < width For data having fewer than width rows, MSUM computes the sum using all the preceding rows. MSUM returns the current sum rather than nulls when the number of rows in the sample is fewer than width. Data Type Format Title Same as operand x MSum(x, w, y direction) IF operand x is … THEN the format is … character the default format for FLOAT. numeric the same format as x. Chapter 11: Ordered Analytical Functions PERCENT_RANK SQL Functions, Operators, Expressions, and Predicates 481 PERCENT_RANK Purpose Returns the relative rank of rows for a value_expression. Type ANSI SQL:2008 window function. Syntax where: Syntax element … Specifies … OVER how the values, grouped according to the PARTITION BY and RESET WHEN clauses and named by value_expression in the ORDER BY clause, are ranked. PARTITION BY in its column_reference the column, or columns, according to which ranking resets. PARTITION BY is optional. If there is no PARTITION BY or RESET WHEN clauses, then the entire result set, specified by the ORDER BY clause, constitutes a single group or partition. PARTITION BY clause is also called the window partition clause. ORDER BY in its value_expression the column, or columns, being ranked. ASC ascending sort order. The default order is ASC. DESC descending sort order. 1101A567 PERCENT_RANK() PARTITION BY column_reference , OVER ( ASC A ORDER BY value_expression ) A DESC , RESET WHEN condition Chapter 11: Ordered Analytical Functions PERCENT_RANK 482 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance The PERCENT_RANK window function, which uses ANSI-specific syntax, is ANSI SQL:2008 compliant. The RESET WHEN clause is a Teradata extension to the ANSI SQL standard. Computation The formula for PERCENT_RANK is: where: The assigned rank of a row is defined as 1 (one) plus the number of rows that precede the row and are not peers of it. PERCENT_RANK is expressed as an approximate numeric ratio between 0.0 and 1.0. RESET WHEN the group or partition, over which the function operates, depending on the evaluation of the specified condition. If the condition evaluates to TRUE, a new dynamic partition is created inside the specified window partition. RESET WHEN is optional. If there is no RESET WHEN or PARTITION BY clauses, then the entire result set constitutes a single partition. If RESET WHEN is specified, then the ORDER BY clause must be specified also. condition a conditional expression used to determine conditional partitioning. The condition in the RESET WHEN clause is equivalent in scope to the condition in a QUALIFY clause with the additional constraint that nested ordered analytical functions cannot specify a RESET WHEN clause. In addition, you cannot specify SELECT as a nested subquery within the condition. The condition is applied to the rows in all designated window partitions to create sub-partitions within the particular window partitions. For more information, see “RESET WHEN Condition Rules” on page 433and the “QUALIFY Clause” in SQL Data Manipulation Language. Syntax element … Specifies … This variable … Represents the … RK rank of the row NR number of rows in the window partition PERCENT_RANK has this value … FOR the result row assigned this rank … 0.0 1. PERCENT_RANK (RK - 1) (NR - 1) = -------------------- Chapter 11: Ordered Analytical Functions PERCENT_RANK SQL Functions, Operators, Expressions, and Predicates 483 Result Type and Attributes For PERCENT_RANK() OVER (PARTITION BY x ORDER BY y direction), the data type, format, and title are as follows: For an explanation of the formatting characters in the format, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Example 1 Determine the relative rank, called the percent_rank, of Christmas sales. The following query: SELECT sales_amt, PERCENT_RANK() OVER (ORDER BY sales_amt) FROM xsales; might return the following results. Note that the relative rank is returned in ascending order, the default when no sort order is specified and that the currency is not reported explicitly. Example 2 Determine the rank and the relative rank of Christmas sales. 1.0 highest in the result. PERCENT_RANK has this value … FOR the result row assigned this rank … Data Type Format Title REAL the default format for DECIMAL(7,6). Percent_Rank(y direction) sales_amt Percent_Rank 100.00 0.000000 120.00 0.125000 130.00 0.250000 140.00 0.375000 143.00 0.500000 147.00 0.625000 150.00 0.750000 155.00 0.875000 160.00 1.000000 Chapter 11: Ordered Analytical Functions PERCENT_RANK 484 SQL Functions, Operators, Expressions, and Predicates SELECT sales_amt, RANK() OVER (ORDER BY sales_amt), PERCENT_RANK () OVER (ORDER BY sales_amt) FROM xsales; sales_amt Rank Percent_Rank 100.00 1 0.000000 120.00 2 0.125000 130.00 3 0.250000 140.00 4 0.375000 143.00 5 0.500000 147.00 6 0.625000 150.00 7 0.750000 155.00 8 0.875000 160.00 9 1.000000 Chapter 11: Ordered Analytical Functions QUANTILE SQL Functions, Operators, Expressions, and Predicates 485 QUANTILE Purpose Computes the quantile scores for the values in a group. Type Teradata-specific function. Syntax where: ANSI Compliance QUANTILE is a Teradata extension to the ANSI SQL:2008 standard. Definition A quantile is a generic interval of user-defined width. For example, percentiles divide data among 100 evenly spaced intervals, deciles among 10 evenly spaced intervals, quartiles among 4, and so on. A quantile score indicates the fraction of rows having a sort_expression value lower than the current value. For example, a percentile score of 98 means that 98 percent of the rows in the list have a sort_expression value lower than the current value. Syntax element … Specifies … quantile_constant a positive integer constant used to define the number of quantile partitions to be used. sort_expression a constant or column expression or comma-separated list of constant or column expressions to be used to sort the values. For example, QUANTILE(10, Region ASC, Store DESC), where 10 is the quantile_constant and Region ASC, Store DESC is the sort_expression list. ASC ascending sort order. DESC descending sort order. The default sort direction is DESC. 1101A403 QUANTILE , ASC DESC ( ( quantile_constant, sort_expression Chapter 11: Ordered Analytical Functions QUANTILE 486 SQL Functions, Operators, Expressions, and Predicates Using ANSI Window Functions Instead of QUANTILE The use of QUANTILE is strongly discouraged. It is a Teradata extension to the ANSI SQL:2008 standard and is retained only for backward compatibility with existing applications. To compute QUANTILE(q, s) using ANSI window functions, use the following: (RANK() OVER (ORDER BY s) - 1) * q / COUNT(*) OVER() QUANTILE Report For each row in the group, QUANTILE returns an integer value that represents the quantile of the sort_expression value for that row relative to the sort_expression value for all the rows in the group. Quantile Value Range Quantile values range from 0 through (Q-1), where Q is the number of quantile partitions specified by quantile_constant. Result Type and Attributes The data type, format, and title for QUANTILE(Q, list) are as follows: For information on the default format of data types, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Example 1 Display each item and its total sales in the ninth (top) decile according to the total sales. SELECT itemID, sumPrice FROM (SELECT a1.itemID, SUM(price) FROM Sales a1 GROUP BY a1.itemID) AS T1(itemID, sumPrice) QUALIFY QUANTILE(10,sumPrice)=9; Example 2 The following example groups all items into deciles by profitability. SELECT Item, Profit, QUANTILE(10, Profit) AS Decile FROM (SELECT Item, Sum(Sales) — (Count(Sales) * ItemCost) AS Profit FROM DailySales, Items WHERE DailySales.Item = Items.Item GROUP BY Item) AS Item; Data Type Format Title INTEGER the default format for the INTEGER data type Quantile(Q, list) Chapter 11: Ordered Analytical Functions QUANTILE SQL Functions, Operators, Expressions, and Predicates 487 The result might look like the following table: Example 3 Because QUANTILE uses equal-width histograms to partition the specified data, it does not partition the data equally using equal-height histograms. In other words, do not expect equal row counts per specified quantile. Expect empty quantile histograms when, for example, duplicate values for sort_expression are found in the data. For example, consider the following simple SELECT statement. SELECT itemNo, quantity, QUANTILE(10,quantity) FROM inventory; The report might look like this. Because the quantile sort is on quantity, and there are only two quantity scores in the inventory table, there are no scores in the report for deciles 1 through 8. Item Profit Decile High Tops 97112 9 Low Tops 74699 7 Running 69712 6 Casual 28912 3 Xtrain 100129 9 itemNo quantity Quantile(10, quantity) 13 1 0 9 1 0 7 1 0 2 1 0 5 1 0 3 1 0 1 1 0 6 1 0 4 1 0 10 1 0 8 1 0 11 1 0 12 9 9 Chapter 11: Ordered Analytical Functions RANK 488 SQL Functions, Operators, Expressions, and Predicates RANK Purpose Returns the rank (1 … n) of all the rows in the group by the value of sort_expression list, with the same sort_expression values receiving the same rank. Type Teradata-specific function. Syntax where: ANSI Compliance RANK is a Teradata extension to the ANSI SQL:2008 standard. Using ANSI RANK Instead of Teradata RANK The use of Teradata RANK is strongly discouraged. It is a Teradata extension to the ANSI SQL:2008 standard, and is equivalent to the ANSI-compliant RANK window function. Teradata RANK is retained only for backward compatibility with existing applications. For more information on the RANK window function, see “RANK” on page 491. Syntax element … Specifies … sort_expression a constant or column expression or comma-separated list of constant or column expressions to be used to sort the values. For example, RANK(Region ASC, Store DESC), where Region ASC, Store DESC is the sort_expression list. The expression cannot contain any ordered analytical or aggregate functions. ASC ascending sort order. DESC descending sort order. The default sort direction is DESC. 1101A404 RANK , ASC DESC ( ( sort_expression Chapter 11: Ordered Analytical Functions RANK SQL Functions, Operators, Expressions, and Predicates 489 Meaning of Rank A rank r implies the existence of exactly r-1 rows with sort_expression value preceding it. All rows having the same sort_expression value are assigned the same rank. For example, if n rows have the same sort_expression values, then they are assigned the same rank—call it rank r. The next distinct value receives rank r+n. Less formally, RANK sorts a result set and identifies the numeric rank of each row in the result. The only argument for RANK is the sort column or columns, and the function returns an integer that represents the rank of each row in the result. Computing Top and Bottom Values You can use RANK to compute top and bottom values as shown in the following examples. Top(n, column) is computed as QUALIFY RANK(column DESC) <=n. Bottom(n, column) is computed as QUALIFY RANK(column ASC) <=n. Result Type and Attributes The data type, format, and title for RANK(x) are as follows: For information on the default format of data types, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Example 1 Display each item, its total sales, and its sales rank for the top 100 selling items. SELECT itemID, sumPrice, RANK(sumPrice) FROM (SELECT a1.itemID, SUM(a1.Price) FROM Sales a1 GROUP BY a1.itemID AS T1(itemID, sumPrice) QUALIFY RANK(sumPrice) <=100; Example 2 Sort employees alphabetically and identify their level of seniority in the company. SELECT EmployeeName, (HireDate - CURRENT_DATE) AS ServiceDays, RANK(ServiceDays) AS Seniority FROM Employee ORDER BY EmployeeName; Data Type Format Title INTEGER the default format for the INTEGER data type Rank(x) Chapter 11: Ordered Analytical Functions RANK 490 SQL Functions, Operators, Expressions, and Predicates The result might look like the following table: Example 3 Sort items by category and report them in order of descending revenue rank. SELECT Category, Item, Revenue, RANK(Revenue) AS ItemRank FROM ItemCategory, (SELECT Item, SUM(sales) AS Revenue FROM DailySales GROUP BY Item) AS ItemSales WHERE ItemCategory.Item = ItemSales.Item ORDER BY Category, ItemRank DESC; The result might look like the following table. EmployeeName Service Days Seniority Ferneyhough 9931 2 Lucier 9409 4 Revueltas 9408 5 Ung 9931 2 Wagner 10248 1 Category Item Revenue ItemRank Hot Cereal Regular Oatmeal 39112.00 4 Hot Cereal Instant Oatmeal 44918.00 3 Hot Cereal Regular COW 59813.00 2 Hot Cereal Instant COW 75411.00 1 Chapter 11: Ordered Analytical Functions RANK SQL Functions, Operators, Expressions, and Predicates 491 RANK Purpose Returns an ordered ranking of rows based on the value_expression in the ORDER BY clause. Type ANSI SQL:2008 window function. Syntax where: Syntax element … Specifies … OVER how the values, grouped according to the PARTITION BY and RESET WHEN clauses and named by value_expression in the ORDER BY clause, are ranked. PARTITION BY in its column_reference the column, or columns, according to which ranking resets. PARTITION BY is optional. If there is no PARTITION BY or RESET WHEN clauses, then the entire result set, specified by the ORDER BY clause, constitutes a single group, or partition. PARTITION BY clause is also called the window partition clause. ORDER BY in its value_expression the column, or columns, being ranked. ASC ascending rank, or sort order. The default order is ASC. DESC descending rank, or sort order. 1101A566 RANK() PARTITION BY column_reference , OVER ( ASC A ORDER BY value_expression ) A DESC , RESET WHEN condition Chapter 11: Ordered Analytical Functions RANK 492 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance The RANK window function is ANSI SQL:2008 compliant. The RESET WHEN clause is a Teradata extension to the ANSI SQL standard. Meaning of Rank RANK returns an ordered ranking of rows based on the value_expression in the ORDER BY clause. All rows having the same value_expression value are assigned the same rank. If n rows have the same value_expression values, then they are assigned the same rank—call it rank r. The next distinct value receives rank r+n. And so on. Less formally, RANK sorts a result set and identifies the numeric rank of each row in the result. RANK returns an integer that represents the rank of each row in the result. Result Type and Attributes For RANK() OVER (PARTITION BY x ORDER BY y direction), the data type, format, and title are as follows: For an explanation of the formatting characters in the format, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. RESET WHEN the group or partition, over which the function operates, depending on the evaluation of the specified condition. If the condition evaluates to TRUE, a new dynamic partition is created inside the specified window partition. RESET WHEN is optional. If there is no RESET WHEN or PARTITION BY clauses, then the entire result set constitutes a single partition. If RESET WHEN is specified, then the ORDER BY clause must be specified also. condition a conditional expression used to determine conditional partitioning. The condition in the RESET WHEN clause is equivalent in scope to the condition in a QUALIFY clause with the additional constraint that nested ordered analytical functions cannot specify a RESET WHEN clause. In addition, you cannot specify SELECT as a nested subquery within the condition. The condition is applied to the rows in all designated window partitions to create sub-partitions within the particular window partitions. For more information, see “RESET WHEN Condition Rules” on page 433 and the “QUALIFY Clause” in SQL Data Manipulation Language. Syntax element … Specifies … Data Type Format Title INTEGER the default format for the INTEGER data type Rank(y direction) Chapter 11: Ordered Analytical Functions RANK SQL Functions, Operators, Expressions, and Predicates 493 Example This example ranks salespersons by sales region based on their sales. SELECT sales_person, sales_region, sales_amount, RANK() OVER (PARTITION BY sales_region ORDER BY sales_amount DESC) FROM sales_table; Notice that the rank column in the preceding table lists salespersons in declining sales order according to the column specified in the PARTITION BY clause (sales_region) and that the rank of their sales (sales_amount) is reset when the sales_region changes. sales_person sales_region sales_amount Rank(sales_amount) Garabaldi East 100 1 Baker East 99 2 Fine East 89 3 Adams East 75 4 Edwards West 100 1 Connors West 99 2 Davis West 99 2 Chapter 11: Ordered Analytical Functions ROW_NUMBER 494 SQL Functions, Operators, Expressions, and Predicates ROW_NUMBER Purpose Returns the sequential row number, where the first row is number one, of the row within its window partition according to the window ordering of the window. Type ANSI SQL:2008 window function. Syntax where: Syntax element … Specifies … OVER the window partition and ordering. PARTITION BY the column, or columns, according to which the result set is partitioned. PARTITION BY is optional. If there is no PARTITION BY or RESET WHEN clauses, then the entire result set, specified by the ORDER BY clause, constitutes a partition. PARTITION BY clause is also called the window partition clause. ORDER BY in its value_expression the order in which to sort the values in the partition. ASC ascending sort order. The default order is ASC. DESC descending sort order. 1101C108 ROW_NUMBER() PARTITION BY column_reference , OVER ( ASC A ORDER BY value_expression ) A DESC , RESET WHEN condition Chapter 11: Ordered Analytical Functions ROW_NUMBER SQL Functions, Operators, Expressions, and Predicates 495 ANSI Compliance The ROW_NUMBER window function is ANSI SQL:2008 compliant. The RESET WHEN clause is a Teradata extension to the ANSI SQL standard. Window Aggregate Equivalent ROW_NUMBER() OVER (PARTITION BY column ORDER BY value) is equivalent to COUNT(*) OVER (PARTITION BY column ORDER BY value ROWS UNBOUNDED PRECEDING). For more information on COUNT, see “Window Aggregate Functions” on page 449. Example To order salespersons based on sales within a sales region, the following SQL query might yield the following results. SELECT ROW_NUMBER() OVER (PARTITION BY sales_region ORDER BY sales_amount DESC), sales_person, sales_region, sales_amount FROM sales_table; Row_Number() sales_person sales_region sales_amount ------------ ------------ ------------ ------------ 1 Baker East 100 2 Edwards East 99 3 Davis East 89 4 Adams East 75 1 Garabaldi West 100 2 Connors West 99 RESET WHEN the group or partition, over which the function operates, depending on the evaluation of the specified condition. If the condition evaluates to TRUE, a new dynamic partition is created inside the specified window partition. RESET WHEN is optional. If there is no RESET WHEN or PARTITION BY clauses, then the entire result set constitutes a single partition. If RESET WHEN is specified, then the ORDER BY clause must be specified also. condition a conditional expression used to determine conditional partitioning. The condition in the RESET WHEN clause is equivalent in scope to the condition in a QUALIFY clause with the additional constraint that nested ordered analytical functions cannot specify a RESET WHEN clause. In addition, you cannot specify SELECT as a nested subquery within the condition. The condition is applied to the rows in all designated window partitions to create sub-partitions within the particular window partitions. For more information, see “RESET WHEN Condition Rules” on page 433 and the “QUALIFY Clause” in SQL Data Manipulation Language. Syntax element … Specifies … Chapter 11: Ordered Analytical Functions ROW_NUMBER 496 SQL Functions, Operators, Expressions, and Predicates 3 Fine West 99 SQL Functions, Operators, Expressions, and Predicates 497 CHAPTER 12 String Operator and Functions This chapter describes the concatenation operator and functions that operate on character, byte, and numeric strings. String Functions SQL provides a concatenation operator and string functions to translate, concatenate, and perform other operations on strings. String Definition The functions documented in this chapter are designed primarily to work with strings of characters. Because many of them can also process byte and numeric constant and literal data strings, the term string is frequently used here to refer to all three of these data type families. IF you want to … THEN use … concatenate strings concatenation operator convert a character string to hexadecimal representation CHAR2HEXINT get the starting position of a substring within another string • INDEX • POSITION convert a character string to lowercase LOWER get the Soundex code for a character string SOUNDEX extract a substring from another string • SUBSTRING • SUBSTR translate a character string to another server character set TRANSLATE determine if TRANSLATE can successfully translate a character string to a specified server character set TRANSLATE_CHK trim specified pad characters or bytes from a character or byte string TRIM convert a character string to uppercase UPPER convert a character string to VARGRAPHIC representation VARGRAPHIC Chapter 12: String Operator and Functions String Functions 498 SQL Functions, Operators, Expressions, and Predicates Data Types on Which String Functions can Operate The following table lists all the data types that can be processed as strings. Note that not all types are acceptable to all functions. See the individual functions for the types they can process. ANSI Equivalence of Teradata SQL String Functions Several of the Teradata SQL string functions are extensions to the ANSI SQL:2008 standard. To maintain ANSI compatibility, use the ANSI equivalent functions instead of Teradata SQL string functions, when available. The following Teradata functions have no ANSI equivalents: • CHAR2HEXINT • SOUNDEX • TRANSLATE_CHK • UPPER • VARGRAPHIC Additional Functions That Operate on Strings SQL provides other string functions and operators that are not discussed in this chapter. Data Type Grouping Character Byte Numeric • CHARACTER • VARCHAR • CLOB • BYTE • VARBYTE • BLOB • BYTEINT • DECIMAL • FLOAT • INTEGER • NUMERIC • SMALLINT Change this Teradata string function … To this ANSI string function in new applications … INDEX POSITION MINDEX† SUBSTR SUBSTRING MSUBSTR† † These functions are no longer documented because their use is deprecated and they will no longer be supported after support for KANJI1 is dropped. Chapter 12: String Operator and Functions String Functions SQL Functions, Operators, Expressions, and Predicates 499 FOR more information on … SEE … attribute functions that return descriptive information about strings, such as: • BYTE • CHARACTER_LENGTH/ CHAR_LENGTH • OCTET_LENGTH Chapter 14: “Attribute Functions.” comparison operators Chapter 5: “Comparison Operators.” the LIKE predicate Chapter 13: “Logical Predicates.” Chapter 12: String Operator and Functions Effects of Server Character Sets on Character String Functions 500 SQL Functions, Operators, Expressions, and Predicates Effects of Server Character Sets on Character String Functions String functions that operate on character data follow the rules listed below. Uppercase Character Conversion for LATIN For the LATIN server character set, the method of converting to uppercase characters is based on ISO 8859 Latin1. Logical Characters vs. Physical Characters For UNICODE, GRAPHIC and KANJISJIS server character sets, the functions operate on a logical character basis, except for the functions that are sensitive to the ANSI mode vs. Teradata mode switch. Although the storage space for KANJISJIS is allocated on a physical basis and is not ANSI compatible, all string operations on this type operate on a character basis as dictated by ANSI. Untranslatable KANJI1 Characters Character string functions do not work on all characters in the KANJI1 server character set when the session character set is UTF8 or UTF16, because the KANJI1 server character set is ambiguous with regards to multibyte characters and some single-byte characters. Recommendation: Unless the KANJI1 server character set is required, use the UNICODE server character set with the UTF8 and UTF16 session character sets for best results. The following single-byte characters in KanjiEBCDIC to KANJI1 translations are mapped to the following Unicode character names. However, with a KanjiSJIS character set, these hexadecimal values map to control characters. Implicit Server Character Set Translation For functions that operate on more than one argument, if the arguments have different server character sets, implicit translation rules take effect. Hexadecimal Value Character Unicode Character Name 0x10 ¢ CENT SIGN 0x11 £ POUND SIGN 0x12 ¬ NOT SIGN 0x13 \ REVERSE SOLIDUS 0x14 ~ TILDE Chapter 12: String Operator and Functions Effects of Server Character Sets on Character String Functions SQL Functions, Operators, Expressions, and Predicates 501 For details, see “Implicit Character-to-Character Translation” on page 765. Chapter 12: String Operator and Functions Concatenation Operator 502 SQL Functions, Operators, Expressions, and Predicates Concatenation Operator Purpose Concatenates string expressions. Syntax where: ANSI Compliance EXCLAMATION POINT character pairs (!!) are Teradata extensions to the ANSI SQL:2008 standard. Do not use them as concatenation operators. Solid and broken VERTICAL LINE character pairs (||) are ANSI SQL:2008 compliant forms of the concatenation operator. Argument Types and Rules Use the concatenation operator on strings and string expressions of type: • Byte If any argument is a byte type, all other arguments must also be byte types. • Numeric A numeric argument is converted to a character string using the format for the numeric value. For details about implicit numeric to character data type conversion, see “Implicit Numeric-to-Character Conversion” on page 828 • Character When the arguments are both character types, but have different server character sets, then implicit string conversion occurs. For details, see “Implicit Character-to-Character Translation” on page 765. Syntax element … Specifies … string_expression_1 a byte, numeric, or character string or string expression. string_expression_2 string_expression_n FF07D195 string_expression_1 string_expression_2 string_expression_n Chapter 12: String Operator and Functions Concatenation Operator SQL Functions, Operators, Expressions, and Predicates 503 • UDTs that have implicit casts to a predefined character type. To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including the concatenation operator, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” Result Type and Attributes The result of a concatenation operation is a string formed by concatenating the arguments in a left-to-right direction. Here are the default result type and attributes for arg1 || arg2: If either argument is null, the result is null. The data types and attributes of the arguments determine whether the result type of a concatenation operation is a fixed length or varying length string. Result types appear in the following table, where n is the sum of the lengths of all arguments: Data Type Heading (arg1||arg2) IF the arguments are … THEN the result is a … byte strings byte string. numeric or character strings or UDTs that are implicitly cast to character strings character string. IF this argument … Is this data type or attribute … THEN the result is this data type or attribute … either VARBYTE VARBYTE(n) VARCHAR VARCHAR(n) numeric UDT that is implicitly cast to VARCHAR CLOB CLOB(n) BLOB BLOB(n) Chapter 12: String Operator and Functions Concatenation Operator 504 SQL Functions, Operators, Expressions, and Predicates When either argument is a character string that specifies the CASESPECIFIC attribute, the result also specifies the CASESPECIFIC attribute. Example 1: Using Concatenation to Create More Readable Results Constants, spaces, and the TITLE phrase can be included in the operation definition to format the result heading and improve readability. For example, the following definition returns side titles, evenly spaced result strings, and a blank heading. SELECT ('Sex ' || sex ||', Marital Status ' || mstat)(TITLE ' ') FROM Employee ; Sex M, Marital Status S Sex F, Marital Status M Sex M, Marital Status M Sex F, Marital Status M Sex F, Marital Status M Sex M, Marital Status M Sex F, Marital Status W ... Example 2: Concatenating First Name With Last Name Consider a table called names that contains last and first names columns, defined as VARCHAR, as listed here: lname fname ------------ ------------ Ryan Loretta Villegas Arnando Kanieski Carol Brown Alan Use string concatenation and a space separator to combine first and last names: SELECT fname ||' '|| lname FROM names ORDER BY lname ; both BYTE BYTE(n) CHARACTER (with same server character set) CHARACTER(n) UDT that is implicitly cast to CHARACTER (with the same server character set) CHARACTER (with different server character sets) VARCHAR(n) UDT that is implicitly cast to CHARACTER (with different server character sets) numeric IF this argument … Is this data type or attribute … THEN the result is this data type or attribute … Chapter 12: String Operator and Functions Concatenation Operator SQL Functions, Operators, Expressions, and Predicates 505 The result is: ((fname||' ')||lname) --------------------- Alan Brown Carol Kanieski Loretta Ryan Arnando Villegas Example 3: Concatenating Last Name With First Name Change the SELECT and the separator to obtain last and first names: SELECT lname||', '||fname FROM names ORDER BY lname; The result is: ((lname||', ')||fname) ---------------------- Brown, Alan Kanieski, Carol Ryan, Loretta Villegas, Arnando Example 4: Concatenating Byte Strings This example shows how to concatenate byte strings. Consider the following table definition: CREATE TABLE tsttbla (column_1 BYTE(2) ,column_2 VARBYTE(10) ,column_3 BLOB(128K) ); The following values are inserted into table tsttbla: INSERT tsttbla ('4142'XB, '7A7B7C'XB, '1A1B1C2B2C'XB); The following SELECT statement concatenates column_2 and column_1 and column_3: SELECT (column_2 || column_1 || column_3) (FORMAT 'X(20)') FROM tsttbla ; The result is: ((column_2||column_1)||column_3) -------------------------------- 7A7B7C41421A1B1C2B2C The resulting data type is BLOB. Concatenating Character Strings Having Different Server Character Sets There are special considerations for the concatenation of character strings that specify different server character sets in the CHARACTER SET attribute. Implicit translation rules apply. For details, see “Implicit Character-to-Character Translation” on page 765. Chapter 12: String Operator and Functions Concatenation Operator 506 SQL Functions, Operators, Expressions, and Predicates If the strings are fixed strings, then the result is varying with length equal to the sum of the lengths of the strings being concatenated. This is true regardless of whether the string lengths are defined in terms of bytes or characters. So, a fixed n-byte KANJISJIS character string concatenated with a fixed m-character UNICODE string produces a VARCHAR(m+n) CHARACTER SET UNICODE result. Consider the following table definition: CREATE TABLE tab1 (cunicode CHARACTER(4) CHARACTER SET UNICODE ,clatin CHARACTER(3) CHARACTER SET LATIN ,csjis CHARACTER(3) CHARACTER SET KANJISJIS); The following values are inserted into table tab1: INSERT tab1 ('abc', 'abc', 'abc'); The following table illustrates these concatenation properties. With the exception of KanjiEBCDIC, concatenation of KANJI1 character strings acts as described above. Under KanjiEBCDIC, any adjacent shift-out (<) and shift-in (>) characters within the resulting expression are removed. In this case, the result string is padded as necessary with trailing characters. Examples for Japanese Character Sets The following tables show the results of concatenating string expressions under each of the Kanji character sets supported by Teradata Database. These examples assume that the string expressions follow the rules defined in the chapter “SQL Data Definition” in SQL Data Types and Literals. For an explanation of symbols and other notation in the examples, see “Character Shorthand Notation Used In This Book” on page 954. Example 1: KanjiEBCDIC string_expression_1 || string_expression_2 Concatenation Result Type of Result cunicode || clatin 'abc?abc' VARCHAR(7) CHARACTER SET UNICODE clatin || csjis 'abcabc' VARCHAR(6) CHARACTER SET UNICODE cunicode || csjis 'abc?abc' VARCHAR(7) CHARACTER SET UNICODE string_expression_1 string_expression_2 Result G G <> Chapter 12: String Operator and Functions Concatenation Operator SQL Functions, Operators, Expressions, and Predicates 507 Example 2: KanjiEUC string_expression_1 || string_expression_2 Example 3: KanjiShift-JIS string_expression_1 || string_expression_2 a a string_expression_1 string_expression_2 Result string_expression_1 string_expression_2 Result ABCm DEFg ABCmDEFg ss3A ss2B m ss3C ss3A ss2B m ss3C string_expression_1 string_expression_2 Result mnABCX B mnABCXB mnABCX g mnABCXg Chapter 12: String Operator and Functions CHAR2HEXINT 508 SQL Functions, Operators, Expressions, and Predicates CHAR2HEXINT Purpose Returns the hexadecimal representation for a character string. Syntax where: ANSI Compliance CHAR2HEXINT is a Teradata extension to the ANSI SQL:2008 standard. Argument Types Use CHAR2HEXINT on character strings or character string expressions. By default, Teradata Database performs implicit type conversion on a UDT argument that has an implicit cast that casts between the UDT and a predefined character type. To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including CHAR2HEXINT, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” CHAR2HEXINT is not supported for CLOBs. Syntax element … Specifies … character_string_expression a character string or character string expression for which the hexadecimal representation is to be returned. 1101E173 CHAR2HEXINT ( character_string_expression ) Chapter 12: String Operator and Functions CHAR2HEXINT SQL Functions, Operators, Expressions, and Predicates 509 Result Type and Attributes Here are the default attributes for CHAR2HEXINT(character_string_expression): The length of the result is twice the length of character_string_expression. The server character set of the result depends on whether Japanese language support was enabled during sysinit. CHAR2HEXINT and Constant Strings You can apply CHAR2HEXINT to a string literal to determine its hexadecimal equivalent. Character constants are treated as VARCHAR(n) CHARACTER SET UNICODE, where n is the length of the constant. The following statement and results illustrate how CHAR2HEXINT operates on constant strings: SELECT CHAR2HEXINT('123'); Char2HexInt('123') ----------------------- 003100320033 Example 1 Assume that the system was enabled with Japanese language support during sysinit. CREATE TABLE tab1 (clatin CHAR(3) CHARACTER SET LATIN ,cunicode CHAR(3) CHARACTER SET UNICODE ,csjis CHAR(3) CHARACTER SET KANJISJIS ,cgraphic CHAR(3) CHARACTER SET GRAPHIC ,ckanji1 CHAR(3) CHARACTER SET KANJI1); INSERT INTO tab1('abc','abc','abc',_GRAPHIC 'ABC','abc'); The bold uppercase LATIN characters in the example represent full width LATIN characters. Data Type Heading CHARACTER Char2HexInt(character_string_expression) IF the system uses this type of language support … THEN the result specifies this server character set … standard LATIN Japanese KANJI1 Chapter 12: String Operator and Functions CHAR2HEXINT 510 SQL Functions, Operators, Expressions, and Predicates CHAR2HEXINT returns the following results for the character strings inserted into tab1. Example 2 To find the internal hexadecimal representation of all table names, submit the following SELECT statement using CHAR2HEXINT. SELECT CHAR2HEXINT(TRIM(t.tablename))(FORMAT 'X(30)') (TITLE 'Internal Hex Representation of TableName') ,t.tablename (TITLE 'TableName') FROM dbc.tables T WHERE t.tablekind = 'T' ORDER BY t.tablename; Partial output from this SELECT statement is similar to the following report: Internal Hex Representation of TableName TableName ---------------------------------------- ---------------- 416363657373526967687473 AccessRights 4163634C6F6752756C6554626C AccLogRuleTbl 4163634C6F6754626C AccLogTbl 4163636F756E7473 Accounts 4163637467 Acctg 416C6C All 436F70496E666F54626C CopInfoTbl This function … Returns this result … CHAR2HEXINT(clatin) 616263 CHAR2HEXINT(cunicode) 006100620063' CHAR2HEXINT(csjis) 616263 CHAR2HEXINT(cgraphic) FF41FF42FF43 CHAR2HEXINT(ckanji1) 616263 Chapter 12: String Operator and Functions INDEX SQL Functions, Operators, Expressions, and Predicates 511 INDEX Purpose Returns the position in string_expression_1 where string_expression_2 starts. Syntax where: ANSI Compliance INDEX is a Teradata extension to the ANSI SQL:2008 standard. Use POSITION instead of INDEX for ANSI SQL:2008 compliance. Argument Types and Rules INDEX operates on the following types of arguments: • Character • Byte If one string expression is of type BYTE, then both string expressions must be of type BYTE. • Numeric If any string expression is numeric, then it is converted implicitly to CHARACTER type. • UDTs that have implicit casts that cast between the UDT and any of the following predefined types: • Numeric • Character • DATE • Byte To define an implicit cast for a UDT, use CREATE CAST and specify AS ASSIGNMENT. For details on CREATE CAST, see SQL Data Definition Language. Syntax element … Specifies … string_expression_1 a full string to be searched. string_expression_2 a substring to be searched for its position within the full string. FF07D253 INDEX ( string_expression_1 ,string_expression_2 ) Chapter 12: String Operator and Functions INDEX 512 SQL Functions, Operators, Expressions, and Predicates Implicit type conversion of UDTs for system operators and functions, including INDEX, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. INDEX does not support CLOBs or BLOBs. For more information on implicit type conversion, see Chapter 20: “Data Type Conversions.” Result Type and Attributes Here are the default result type and attributes for INDEX(arg1, arg2): Expected Values The following rules apply to the value that INDEX returns: • If string_expression_2 is not found in string_expression_1, then the result is zero. • If string_expression_2 is null, then the result is null. • If the arguments are character types, INDEX returns a logical character position, not a byte position, except when the server character set of the arguments is KANJI1 and the session client character set is KanjiEBCDIC. For details, see “Rules for KANJI1 Server Character Set” on page 513. Rules for Character Type Arguments If the arguments are character types, matching is in terms of logical characters. Single byte characters are matched against single byte characters, and multibyte characters are matched against multibyte characters. For a match to occur, representation of the logical character must be identical in both expressions. If the server character sets of the arguments are not the same, INDEX performs an implicit character translation. For a description of implicit character translation rules, see “Implicit Character-to-Character Translation” on page 765. The CASESPECIFIC attribute affects whether characters are considered to be a match. Data Type Heading INTEGER Index(arg1, arg2) IF the session mode is … THEN the default case specification for character columns and literals is … ANSI CASESPECIFIC. Teradata NOT CASESPECIFIC. The exception is character data of type GRAPHIC, which is always CASESPECIFIC. Chapter 12: String Operator and Functions INDEX SQL Functions, Operators, Expressions, and Predicates 513 To override the default case specification, you can apply the CASESPECIFIC or NOT CASESPECIFIC phrase to a character column in CREATE TABLE or ALTER TABLE. Or, you can apply the CASESPECIFIC or NOT CASESPECIFIC phrase to the INDEX character string arguments. Using the rules for character type arguments, if you want INDEX to match letters only if they are the same letters in the same case, specify the CASESPECIFIC phrase with at least one of the arguments. For example: SELECT Name FROM Employee WHERE INDEX(Name, 'X' (CASESPECIFIC)) = 1; If you want INDEX to match letters without considering the case, specify the NOT CASESPECIFIC phrase with both of the arguments. Rules for KANJI1 Server Character Set When the server character set is KANJI1 and the client character set is KanjiEBCDIC, the offset count includes Shift-Out/Shift-In characters, but they are not matched. They are treated only as an indication of a transition from a single byte character and an multibyte character. The nonzero position of the result is reported as follows: IF … THEN … either argument has a CASESPECIFIC attribute (either by default or specified explicitly) simple Latin letters are considered to be matching only if they are the same letters and the same case. both arguments have a NOT CASESPECIFIC attribute (either by default or specified explicitly) before the operation begins, some characters are converted to uppercase. IF the character is a … THEN the character is … lowercase simple Latin letter converted to uppercase before the operation begins. non-Latin single byte character not converted to uppercase. multibyte character byte indicating a transition between single-byte and multibyte character data IF the character set is … THEN the result is the … KanjiEBCDIC position of the first byte of the logical character offset (including Shift- Out/Shift-In in the offset count) within string_expression_1. other than KanjiEBCDIC logical character offset within string_expression_1. Chapter 12: String Operator and Functions INDEX 514 SQL Functions, Operators, Expressions, and Predicates Relationship Between INDEX and POSITION INDEX and POSITION behave identically, except on character type arguments when the client character set is KanjiEBCDIC, the server character set is KANJI1, and an argument contains a multibyte character. For an example of when the two functions return different results for the same data, see “How POSITION and INDEX Differ” on page 521. Example 1 The following table shows examples of simple INDEX expressions and their results. Example 2 The following examples show how INDEX(string_1, string_2) operates when the server character set for string_1 and the server character set for string_2 differ. In these cases, both arguments are converted to UNICODE (if needed) and the characters are matched logically. Example 3 The following examples show how INDEX(string_1, string_2) operates when the server character set for both arguments is KANJI1 and the client character set is KanjiEBCDIC. Note that for KanjiEBCDIC, results are returned in terms of physical units, making INDEX DB2-compliant in that environment. Expression Result INDEX('catalog','log') 5 INDEX('catalog','dog') 0 INDEX('41424344'XB,'43'XB) 3 IF string_1 is … AND string_2 is … THEN the result is … Character Set Data Character Set Data UNICODE 92 abc LATIN abc 4 UNICODE abc UNICODE c 3 KANJISJIS 92 04 UNICODE 0 4 IF string_1 contains … AND string_2 contains … THEN the result is … MN 6 MN 4 Chapter 12: String Operator and Functions INDEX SQL Functions, Operators, Expressions, and Predicates 515 Example 4 The following examples show how INDEX(string_1, string_2) operates when the server character set for both arguments is KANJI1 and the client character set is KanjiEUC. Example 5 The following examples show how INDEX(string_1, string_2) operates when the server character set for both arguments is KANJI1 and the client character set is KanjiShift-JIS. Example 6 In this example, INDEX is applied to ’ ’ (the SPACE character) in the value strings in the Name column of the Employee table. SELECT name FROM employee WHERE INDEX(name, ' ') > 6 ; INDEX examines the Name field and returns all names where a space appears in a character position beyond the sixth (character position seven or higher). MNP P 9 MXNP 7 IF string_1 contains … AND string_2 contains … THEN the result is … IF string_1 contains … AND string_2 contains … THEN the result is … a b ss3A ss3A 3 a b ss2B ss2B 3 CS1_DATA A 6 a b ss2D ss3E ss2F ss2F 5 a b C ss2D ss3E ss2F ss2F 6 CS1_DmATA A 7 IF string_1 contains … AND string_2 contains … THEN the result is … mnABCX B 4 mnABCX X 6 Chapter 12: String Operator and Functions INDEX 516 SQL Functions, Operators, Expressions, and Predicates Example 7 The following example displays a list of projects in which the word Batch appears in the project description, and lists the starting position of the word. SELECT proj_id, INDEX(description, 'Batch') FROM project WHERE INDEX(description, 'Batch') > 0 ; The system returns the following report. proj_id Index (description, 'Batch') ------------- ---------------------------- OE2-0003 5 AP2-0003 13 OE1-0003 5 AP1-0003 13 AR1-0003 10 AR2-0003 10 Example 8 A somewhat more complex construction employing concatenation, SUBSTRING, and INDEX might be more instructive. Suppose the employee table contains the following values. empno name ---------- ----------- 10021 Smith T 10007 Aguilar J 10018 Russell S 10011 Chin M 10019 Newman P You can transpose the form of the names from the name column selected from the employee table and change the punctuation in the report using the following query: SELECT empno, SUBSTRING(name FROM INDEX(name,' ')+1 FOR 1)| | '. '| | SUBSTRING(name FROM 1 FOR INDEX(name, ' ')-1) (TITLE 'Emp Name') FROM employee ; The system returns the following report. empno Emp Name ---------- -------------- 10021 T. Smith 10007 J. Aguilar 10018 S. Russell 10011 M. Chin 10019 P. Newman Chapter 12: String Operator and Functions LOWER SQL Functions, Operators, Expressions, and Predicates 517 LOWER Purpose Returns a character string identical to character_string_expression, except that all uppercase letters are replaced by their lowercase equivalents. Syntax where: ANSI Compliance LOWER is ANSI SQL:2008 compliant. Argument Types Use LOWER on character strings or character string expressions, except for CLOBs. By default, Teradata Database performs implicit type conversion on a UDT argument that has an implicit cast that casts between the UDT and a predefined character type, except for CLOB. To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including LOWER, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” Syntax element … Specifies … character_string_expression a character string or character string expression for which all uppercase characters are to be replaced by their lowercase equivalents. FF07D091 LOWER (character_string_expression) Chapter 12: String Operator and Functions LOWER 518 SQL Functions, Operators, Expressions, and Predicates Result Type and Attributes Here are the default result type and attributes for LOWER(arg): Usage Notes The LOWER function allows users who want ANSI portability to have case blind comparisons with ANSI-compliant syntax. You can also replace characters with uppercase equivalents. For more information, see “UPPER” on page 553. Restrictions The LOWER function operates with the LATIN server character set. If the type of argument for LOWER is anything other than LATIN, LOWER attempts to translate the non-LATIN string to LATIN before evaluation. If the string cannot be converted successfully, an error is returned. Note that a constant string is an acceptable argument because it is implicitly converted from UNICODE to LATIN before it is evaluated. Examples In the following examples, columns charfield_1 and charfield_2 have CASESPECIFIC comparison attributes. Teradata SQL has the type attribute NOT CASESPECIFIC that allows case blind comparisons, but the type attributes CASESPECIFIC and NOT CASESPECIFIC are Teradata extensions to the ANSI standard. Example 1 The following example compares the strings on a case blind basis. SELECT id FROM names WHERE LOWER(charfield_1) = LOWER(charfield_2); Example 2 The use of LOWER to return and store values is shown in the following example. SELECT LOWER (last_name) FROM names; INSERT INTO names SELECT LOWER(last_name),LOWER(first_name) FROM newnames; Data Type Heading Same type as arg Lower(arg) Chapter 12: String Operator and Functions LOWER SQL Functions, Operators, Expressions, and Predicates 519 The identical result is achieved with a USING phrase. USING (last_name CHAR(20),first_name CHAR(20)) INSERT INTO names (LOWER(:last_name), LOWER(:first_name)); Chapter 12: String Operator and Functions POSITION 520 SQL Functions, Operators, Expressions, and Predicates POSITION Purpose Returns the position in string_expression_2 where string_expression_1 starts. Syntax where: ANSI Compliance POSITION is ANSI SQL:2008 compliant. Use POSITION instead of INDEX for ANSI SQL:2008 conformance. POSITION and INDEX behave identically except when the client character set is KanjiEBCDIC and the server character for an argument is KANJI1 and contains multibyte characters. Use POSITION in place of MINDEX. (MINDEX no longer appears in this book because its use is deprecated and it will not be supported after support for KANJI1 is dropped.) Argument Types and Rules POSITION operates on the following types of arguments: • Character, except for CLOB • Byte, except for BLOB If one string expression is of type BYTE, then both expressions must be of type BYTE. • Numeric Numeric string expressions are converted implicitly to CHARACTER type. • UDTs that have implicit casts that cast between the UDT and any of the following predefined types: • Numeric • Character Syntax element … Specifies … string_expression_1 a substring to be searched for its position within the full string. string_expression_2 a full string to be searched. FF07D090 POSITION (string_expression_1 I N string_expression_2) Chapter 12: String Operator and Functions POSITION SQL Functions, Operators, Expressions, and Predicates 521 • DATE • Byte To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including POSITION, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion, see Chapter 20: “Data Type Conversions.” Result Type and Attributes Here are the default result type and attributes for POSITION(arg1 IN arg2): Expected Values POSITION returns a value according to the following rules. If the arguments are character types, then regardless of the server character set, the value for POSITION represents the position of a logical character, not a byte position. How POSITION and INDEX Differ INDEX and POSITION behave identically except when the session client character set is KanjiEBCDIC, the server character set is KANJI1, and the parent string contains a multibyte character. This is the only case for which the results of these two functions differ when performed on the same data. Data Type Heading INTEGER Position(arg1 in arg2) IF … THEN the result is … either argument is null null. string_expression_1 has length zero one. string_expression_1 is a substring within string_expression_2 the position in string_expression_2 where string_expression_1 starts. none of the preceding is true zero. Chapter 12: String Operator and Functions POSITION 522 SQL Functions, Operators, Expressions, and Predicates Suppose we create the following table. CREATE TABLE iptest ( column_1 VARCHAR(30) CHARACTER SET Kanji1 column_2 VARCHAR(30) CHARACTER SET Kanji1); We then insert the following set of values for the columns. The client session character set is KanjiEBCDIC5026_0I. Now we perform a query that demonstrates how INDEX and POSITION return different results in this condition. SELECT column_1, column_2, INDEX(column_1,column_2) FROM iptest; The result of this query looks like the following: column_1 column_2 Index(column_1,column_2) ----------- ----------- ------------------------ MN 6 MNP 4 MNP P 9 MNP 6 With the same session characteristics in place, perform the semantically identical query on the table using POSITION instead of INDEX. SELECT column_1, column_2, POSITION(column_2 IN column_1) FROM iptest; The result of this query looks like the following: column_1 column_2 Position(column_2 in column_1) ----------- ----------- ------------------------------ MN 4 MNP 3 MNP P 5 MNP 4 The different results are accounted for by the following differences in how INDEX and POSITION operate in this particular case. • INDEX counts Shift-Out and Shift-In characters; POSITION does not. • INDEX counts bytes; POSITION counts logical characters. As a result, an A, for example, counts as two bytes (two physical characters) for INDEX, but only one logical character for POSITION. column_1 column_2 MN MNP MNP P MNP Chapter 12: String Operator and Functions SOUNDEX SQL Functions, Operators, Expressions, and Predicates 523 SOUNDEX Purpose Returns a character string that represents the Soundex code for string_expression. Syntax where: ANSI Compliance SOUNDEX is a Teradata extension to the ANSI SQL:2008 standard. Argument Types Use SOUNDEX on character strings or character string expressions that use the LATIN or UNICODE server character set. SOUNDEX does not accept CLOB types. By default, Teradata Database performs implicit type conversion on UDT arguments that have implicit casts to predefined character types. To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including SOUNDEX, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” Syntax element … Specifies … string_expression a character string or expression that contains a surname to be evaluated in simple Latin characters. Soundex is case insensitive. Embedded or trailing pad characters within character_string return an error to the requestor. KO01A060 SOUNDEX ( string_expression ) Chapter 12: String Operator and Functions SOUNDEX 524 SQL Functions, Operators, Expressions, and Predicates Definition: Simple Latin Characters A simple Latin character is one that does not have diacritical marks such as tilde (~) or acute accent (´). There are 26 uppercase simple Latin characters and 26 lowercase simple Latin characters. Definition: Soundex Soundex is a system that codes surnames having the same or similar sounds, but variant spellings. The Soundex system was first used by the National Archives in 1880 to index the United States census. Soundex codes begin with the first letter of the surname followed by a three-digit code. Zeros are added to names that do not have enough letters. Soundex Coding Guide The following process outlines the Soundex coding guide: 1 Retain the first letter of the name. 2 Drop all occurrences of the following letters: A, E, I, O, U, Y, H, W in other positions. 3 Assign the following number to the remaining letters after the first letter: 1 = B, F, P, V 2 = C, G, J, K, Q, S X, Z 3 = D, T 4 = L 5 = M, N 6 = R 4 If two or more letters with the same code are adjacent in the original name or adjacent except for any intervening H or W, omit all but the first. 5 Convert the form “letter, digit, digit, digit,” by adding trailing zeros if less than three digits. 6 Drop the rightmost digits if more than three digits. 7 Names with adjacent letters having the same equivalent number are coded as one letter with a single number Surname prefixes are generally not used. Chapter 12: String Operator and Functions SOUNDEX SQL Functions, Operators, Expressions, and Predicates 525 Example 1 The following SELECT statement returns the result that follows. SELECT SOUNDEX ('ashcraft'); Soundex('ashcraft') ------------------- a261 The surname “ashcraft” initially evaluates to “a2h2613,” but the following Soundex rules convert the result to a261. • “h” is dropped because it occurs in the third position. Soundex drops all occurrences of the following characters in any position other than the first. A, E, I, O, U, Y, H, W • “2” is dropped because it represents the second occurrence of one of the following characters: C, G, J, K, Q, S X, Z If two or more characters with the same code are adjacent in the original name, or adjacent except for any intervening H or W, Soundex omits all but the code for the first occurrence of the character in the returned code. • “3” is dropped because Soundex drops the rightmost digits if character_string evaluates to more than three digits following the initial simple Latin character. Example 2 “Example 2” and “Example 3” on page 526 use the following table data: SELECT family_name FROM family; family_name ----------- John Joan Joey joanne michael Bob Here are the results of the SOUNDEX function on the data in the family_name column: SELECT SOUNDEX(TRIM(family.family_name)); Soundex(TRIM(BOTH FROM family_name)) ------------------------------------ J500 J500 B100 J000 m240 j500 Chapter 12: String Operator and Functions SOUNDEX 526 SQL Functions, Operators, Expressions, and Predicates Example 3 Find all family names in Family that sound like “Joan”. SELECT family_name FROM family WHERE SOUNDEX(TRIM(family.family_name)) = SOUNDEX('Joan'); family_name ----------- John Joan Joanne Examples of Invalid Usage The following SOUNDEX examples are not valid for the reasons given in the table. Statement Why the Statement is Not Valid SELECT SOUNDEX(12345); 12345 is a numeric string, not a character string. SELECT SOUNDEX('ábç'); The characters á and ç are not simple Latin characters. Chapter 12: String Operator and Functions STRING_CS SQL Functions, Operators, Expressions, and Predicates 527 STRING_CS Purpose Returns a heuristically derived integer value that you can use to help determine which KANJI1-compatible client character set was used to encode string_expression. The result is not guaranteed correct, but should work for most strings likely to be encountered. Syntax where: ANSI Compliance STRING_CS is a Teradata extension to the ANSI SQL:2008 standard. Argument Types Use STRING_CS on character strings or character string expressions that use the KANJI1 server character set. (Non-KANJI1 character strings will be coerced to KANJI1, but the results are unlikely to be useful.) STRING_CS does not accept CLOB or UDT types. Result Value STRING_CS returns a heuristically derived INTEGER value that you can use to help determine the client character set that was used to encode the KANJI1 character string or expression. The result value can also help determine which client character set to use to interpret the character data. Syntax element … Specifies … string_expression a CHAR or VARCHAR character string or expression. 1101A515 STRING_CS ( string_expression ) IF the result value is … THEN the heuristic found that string_expression … -1 most likely uses a single-byte client character set encoding, but it may also contain a mix of encodings. Chapter 12: String Operator and Functions STRING_CS 528 SQL Functions, Operators, Expressions, and Predicates Usage Notes STRING_CS helps determine which encoding to use when using the TRANSLATE function to translate a string from the KANJI1 server character set to the UNICODE server character set. For more information on TRANSLATE, see “TRANSLATE” on page 536. 0 does not contain anything distinguishable from any particular character set, so any character set that you use to interpret string_expression provides the same result. Not all translations use the same interpretation for the characters represented by 0x5C and 0x7E, however. IF string_expression contains … AND you want it to be interpreted as … THEN use … 0x5C REVERSE SOLIDUS a single-byte character set. 0x7E TILDE 0x5C YEN SIGN any of the following: • KANJISJIS_0S • KANJIEBCDIC5026_0I • KANJIEBCDIC5035_0I • KATAKANAEBCDIC • KANJIEUC_0U 0x7E OVERLINE 1 uses the encoding of one of the following: • KANJIEBCDIC5026_0I • KANJIEBCDIC5035_0I • KATAKANAEBCDIC 2 uses the encoding of KANJIEUC_0U. 3 uses the encoding of KANJISJIS_0S. IF the result value is … THEN the heuristic found that string_expression … IF the result value is … THEN substitute the following value for source_TO_target in TRANSLATE(string_expression USING source_to_target) … -1 KANJI1_SBC_TO_UNICODE. 0 KANJI1_SBC_TO_UNICODE. 1 KANJI1_KANJIEBCDIC_TO_UNICODE. 2 KANJI1_KANJIEUC_TO_UNICODE. 3 KANJI1_KANJISJIS_TO_UNICODE. Chapter 12: String Operator and Functions STRING_CS SQL Functions, Operators, Expressions, and Predicates 529 Example 1: Using STRING_CS to Determine the Client Character Set Consider the following table definition: CREATE TABLE SysNames (SysID INTEGER ,SysName VARCHAR(30) CHARACTER SET KANJI1); Suppose the session character set is KANJIEBCDIC5026_0I. The following statement inserts the mixed single-byte/multibyte character string 'Q' into the SysName column of the SysNames table: INSERT SysNames (101, '0E42E342C542E242E30FD8'XC); Using STRING_CS to determine the client character set that was used to encode the string produces the results that follow: SELECT STRING_CS(SysName) FROM SysNames WHERE SysID = 101; String_CS(SysName) ------------------ 1 Example 2: Using STRING_CS to Translate a KANJI1 String to UNICODE Consider the SysNames table from the preceding example, “Example 1: Using STRING_CS to Determine the Client Character Set.” The following statement uses STRING_CS to determine which encoding to use to translate strings in the SysName column from the KANJI1 server character set to the UNICODE server character set: SELECT CASE STRING_CS(SysName) WHEN 0 THEN TRANSLATE(SysName USING KANJI1_SBC_TO_UNICODE) WHEN 1 THEN TRANSLATE(SysName USING KANJI1_KANJIEBCDIC_TO_UNICODE) WHEN 2 THEN TRANSLATE(SysName USING KANJI1_KANJIEUC_TO_UNICODE) WHEN 3 THEN TRANSLATE(SysName USING KANJI1_KANJISJIS_TO_UNICODE) ELSE TRANSLATE(SysName USING KANJI1_SBC_TO_UNICODE) END FROM SysNames; Chapter 12: String Operator and Functions SUBSTRING/SUBSTR 530 SQL Functions, Operators, Expressions, and Predicates SUBSTRING/SUBSTR Purpose Extracts a substring from a named string based on position. ANSI Syntax where: Teradata Syntax where: Syntax Element … Specifies … string_expression a string expression from which the substring is to be extracted. n1 the starting position of the substring to extract from string_expression. FOR a keyword indicating that the searched substring is bounded on the right by the value n2. If you omit FOR n2, then you extract the entire right hand portion of the named string or string expression, beginning at the position named by n1. If string_expression is a BYTE or CHAR type and you omit FOR n2, trailing binary zeros or pad characters are trimmed. n2 the length of the substring to extract from string_expression. If n2 < 0, the function returns an error. SUBSTRING FOR n2 (string_expression FROM n1 ) FF07D256 Syntax Element … Specifies … string_expression a string expression from which the substring is to be extracted. n1 the starting position of the substring to extract from string_expression. n2 the length of the substring to be extracted from string_expression. If string_expression is a BYTE or CHAR type and you omit n2, trailing binary zeros or pad characters are trimmed. If n2 < 0, the function returns an error. FF07D257 SUBSTR (string_expression,n1 ) ,n2 Chapter 12: String Operator and Functions SUBSTRING/SUBSTR SQL Functions, Operators, Expressions, and Predicates 531 ANSI Compliance SUBSTRING is ANSI SQL:2008 compliant. SUBSTR is a Teradata extension to the ANSI SQL:2008 standard. Argument Types and Rules SUBSTRING and SUBSTR operate on the following types of arguments: • Character • Byte • Numeric If the string_expression argument is numeric, it is implicitly converted to CHARACTER type. • UDTs that have implicit casts to any of the following predefined types: • Character • Numeric • Byte • DATE To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including SUBSTRING and SUBSTR, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion, see Chapter 20: “Data Type Conversions.” Result Type and Attributes Here are the default result type and attributes for SUBSTR(string, n1, n2) and SUBSTRING(string FROM n1 FOR n2): Data Type Heading Substring(string From n1 For n2) IF the string argument is a … THEN the result type is … Substr(string, n1, n2) BLOB BLOB(n). byte string other than BLOB VARBYTE(n). CLOB CLOB(n). numeric, or character string other than CLOB VARCHAR(n). Chapter 12: String Operator and Functions SUBSTRING/SUBSTR 532 SQL Functions, Operators, Expressions, and Predicates In ANSI mode, the value of n for the resulting BLOB(n), VARBYTE(n), CLOB(n), or VARCHAR(n) is the same as the original string. In Teradata mode, the value of n for the result type depends on the number of characters or bytes in the resulting string. To get the data type of the resulting string, use the TYPE function. Result Value SUBSTRING/SUBSTR extracts n2 characters or bytes from string_expression starting at position n1. To get the number of characters or bytes in the resulting string, use the BYTE function for byte strings and the CHARACTER_LENGTH function for character strings. If either of the following conditions are true, SUBSTRING/SUBSTR returns a zero length string: • (n1 > string_length) AND (0 = n2) • (n1 < 1) AND (0 = n2) AND ((n2 + n1 - 1) = 0) Usage Rules for SUBSTRING and SUBSTR SUBSTRING is the ANSI SQL:2008 syntax. Teradata syntax using SUBSTR is supported for backward compatibility. Use SUBSTRING in place of SUBSTR for ANSI compliance. Use SUBSTRING in place of MSUBSTR. (MSUBSTR no longer appears in this book because its use is deprecated and it will not be supported after support for KANJI1 is dropped.) Difference Between SUBSTRING and SUBSTR SUBSTRING and SUBSTR perform identically except when they operate on character strings in Teradata mode where the server character set is KANJI1 and the client character set is KanjiEBCDIC. In this case, SUBSTR interprets n1 and n2 as physical units, making the DB2-compliant SUBSTR operate on a byte-by-byte basis. Shift-Out and Shift-In bytes are significant because the result might be formatted incorrectly. For example, the result string might not contain either the opening Shift-Out character or the closing Shift-In character. Otherwise, if string_expression is character data, then SUBSTRING expects mixed single byte and multibyte character strings and operates on logical characters that are valid for the character set of the session. In this case, n1 is a positive integer pointing to the first character of the result and n2 is in terms of logical characters. Example 1 Suppose sn is a CHARACTER(15) field of Serial IDs for Automobiles and positions 3 to 5 represent the country of origin as three letters. For example: 12JAP3764-35421 37USA9873-26189 11KOR1221-13145 Chapter 12: String Operator and Functions SUBSTRING/SUBSTR SQL Functions, Operators, Expressions, and Predicates 533 To search for serial IDs of cars made in the USA: SELECT make, sn FROM autos WHERE SUBSTRING (sn FROM 3 FOR 3) = 'USA'; Example 2 If we want the last five characters of the serial ID, which represent manufacturing sequence number, another substring can be accessed. SELECT make, SUBSTRING (sn FROM 11) AS sequence FROM autos WHERE SUBSTRING (sn FROM 3 FOR 3) = 'USA'; Example 3 Suppose nameaddress is a VARCHAR(120) field, and the application used positions 1 to 30 for name, starting address at position 31. To return address only, but limit the number of characters returned to 50 use: ... SUBSTRING (nameaddress FROM 31 FOR 50) This returns an address of up to 50 characters. Example 4 The following example shows a SELECT statement requesting substrings from a character field in positions 1 through 4 for every row: SELECT SUBSTRING (jobtitle FROM 1 FOR 4) FROM employee ; The result is as follows. Substring(jobtitle From 1 For 4) -------------------------------- Tech Cont Sale Secr Test ... Example 5 Consider the following table: CREATE TABLE cstr (c1 CHAR(3) CHARACTER SET LATIN ,c2 CHAR(10) CHARACTER SET KANJI1); INSERT cstr ('abc', '92 abc'); Chapter 12: String Operator and Functions SUBSTRING/SUBSTR 534 SQL Functions, Operators, Expressions, and Predicates Here are some examples of how to use SUBSTR to extract substrings from the KanjiEUC client character set. Example 6 Consider the following table: CREATE TABLE ctable1 (c1 VARCHAR(11) CHARACTER SET KANJI1); The following table shows the difference between SUBSTR and SUBSTRING in Teradata mode for KANJI1 strings from KanjiEBCDIC client character set. Example 7 The following table shows examples for the KanjiEUC client character set, where ctable1 is the table defined in Example 6. Function Result SELECT SUBSTR(c2, 2, 3) FROM cstr; '2 a' SELECT SUBSTR(c1, 2, 2) FROM cstr; 'bc' IF c1 contains … THEN this query … Returns … MNP SELECT SUBSTR(c1,2) FROM ctable1; NP SELECT SUBSTR(c1,3,8) FROM ctable1; SELECT SUBSTR(c1,4) FROM ctable1; ABC>P Note: The client application might not be able to properly interpret the resulting multibyte characters because the shift out (<) is missing. SELECT SUBSTRING(c1 FROM 2) FROM ctable1; NP SELECT SUBSTRING(c1 FROM 3 FOR 8) FROM ctable1; P SELECT SUBSTRING(c1 FROM 4) FROM ctable1; P IF c1 contains … THEN this query … Returns … A ss2B CD SELECT SUBSTR(c1,2) FROM ctable1; ss2B CD ss3A ss2B ss3C ss2D SELECT SUBSTR(c1,2,2) FROM ctable1; ss2B ss3C Chapter 12: String Operator and Functions SUBSTRING/SUBSTR SQL Functions, Operators, Expressions, and Predicates 535 Example 8 The following table shows examples for KanjiShift-JIS client character set, where ctable1 is the table defined in Example 6. Example 9 The following statement applies the SUBSTRING function to a CLOB column in table full_text and stores the result in a CLOB column in table sub_text. INSERT sub_text (text) SELECT SUBSTRING (text FROM 9 FOR 128000) FROM full_text; IF c1 contains … THEN this query … Returns … mnABCX SELECT SUBSTR(c1, 6, 1) FROM ctable1; X SELECT SUBSTR(c1,4) FROM ctable1; BCX Chapter 12: String Operator and Functions TRANSLATE 536 SQL Functions, Operators, Expressions, and Predicates TRANSLATE Purpose Converts a character string or character string expression from one server character set to another server character set. Syntax where: Syntax element … Specifies … character_string_expression a character string to translate to another server character set. If the string or string expression is not a character type, an error is returned. source_repertoire_name the source character set of the string to translate. For supported values, see “Supported Translations Between Character Sets” on page 539. A value of LOCALE can be specified for source_repertoire_name to translate a character string from LATIN or KANJI1 to UNICODE using a source repertoire determined by the language support mode of the system and the client character set of the session. For details, see “Supported Translations Between Character Sets” on page 539. _encoding an optional literal for translating from KANJI1 to UNICODE that indicates a specific encoding of KANJI1. The _encoding option is not allowed if LOCALE is specified for source_repertoire_name or target_repertoire_name. 1101E198 TRANSLATE character_string_expression _encoding ( USING source_repertoire_name A _TO_target_repertoire_name A _suffix WITH ERROR ) Chapter 12: String Operator and Functions TRANSLATE SQL Functions, Operators, Expressions, and Predicates 537 ANSI Compliance TRANSLATE is ANSI SQL:2008 compliant. _encoding (continued) IF the translation is from this character set … THEN use this value for _encoding … • KatakanaEBCDIC • KanjiEBCDIC5026_0I • KanjiEBCDIC5038_0I _KanjiEBCDIC KanjiEUC_0U _KanjiEUC KanjiShiftJIS_0S _KANJISJIS ASCII or EBCDIC _SBC target_repertoire_name the target character set of the string to translate. For supported values, see “Supported Translations Between Character Sets” on page 539. A value of LOCALE can be specified for target_repertoire_name to translate a character string from UNICODE to LATIN or KANJI1 using a target repertoire determined by the language support mode of the system and the client character set of the session. For details, see “Supported Translations Between Character Sets” on page 539. _suffix that the translation maps some source characters to semantically different characters. For example, a translation that specifies the _Halfwidth suffix maps any character with a halfwidth variant to that variant, and all fullwidth variants to their non-fullwidth counterparts. The _suffix option also indicates the form of character data translated from UNICODE to the KANJI1 server character set, for example, _KanjiEUC. Valid values are: • _KanjiEBCDIC • _KanjiEUC • _KANJISJIS • _SBC • _PadSpace • _PadGraphic • _Fullwidth • _Halfwidth • _FoldSpace • _VarGraphic The _suffix option is not allowed if LOCALE is specified for source_repertoire_name or target_repertoire_name. WITH ERROR that the translation replaces offending characters in the string with a designated error character, instead of reporting an error. For details, see “Error Characters Assigned by the WITH ERROR Option” on page 542). Syntax element … Specifies … Chapter 12: String Operator and Functions TRANSLATE 538 SQL Functions, Operators, Expressions, and Predicates Argument Types Use TRANSLATE on character strings or character string expressions. By default, Teradata Database performs implicit type conversion on UDT arguments that have implicit casts to predefined character types. To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including TRANSLATE, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” Result Type and Attributes The default attributes for TRANSLATE (string USING source_TO_target) are as follows. Supported Translations for CLOB Strings The following translations are supported for CLOB strings: • LATIN_TO_UNICODE • UNICODE_TO_LATIN Data Type Heading Translate(string USING source_to_target) IF the argument is … THEN the result is … • CHAR • VARCHAR VARCHAR(n) CHARACTER SET target CLOB CLOB(n) CHARACTER SET target where source_TO_target determines the character set value of target, according to the supported translations in “Supported Translations Between Character Sets” on page 539. Chapter 12: String Operator and Functions TRANSLATE SQL Functions, Operators, Expressions, and Predicates 539 Supported Translations Between Character Sets The following table lists the supported values that you can use for source_repertoire_name_TO_target_repertoire_name to translate between server character sets. Value of source_TO_target Source Character Set Target Character Set GRAPHIC_TO_KANJISJIS GRAPHIC KANJISJIS GRAPHIC_TO_LATIN GRAPHIC LATIN GRAPHIC_TO_UNICODE GRAPHIC UNICODE GRAPHIC_TO_UNICODE_PadSpace GRAPHIC UNICODE KANJI1_KanjiEBCDIC_TO_UNICODE KANJI1 UNICODE KANJI1_KanjiEUC_TO_UNICODE KANJI1 UNICODE KANJI1_KANJISJIS_TO_UNICODE KANJI1 UNICODE KANJI1_SBC_TO_UNICODE KANJI1 UNICODE KANJISJIS_TO_GRAPHIC KANJISJIS GRAPHIC KANJISJIS_TO_LATIN KANJISJIS LATIN KANJISJIS_TO_UNICODE KANJISJIS UNICODE LATIN_TO_GRAPHIC LATIN GRAPHIC LATIN_TO_KANJISJIS LATIN KANJISJIS LATIN_TO_UNICODE LATIN UNICODE LOCALE_TO_UNICODE KANJI1 UNICODE LATIN UNICODE_TO_GRAPHIC UNICODE GRAPHIC UNICODE_TO_GRAPHIC_PadGraphic UNICODE GRAPHIC UNICODE_TO_GRAPHIC_VarGraphic UNICODE GRAPHIC UNICODE_TO_KANJI1_KanjiEBCDIC UNICODE KANJI1 UNICODE_TO_KANJI1_KanjiEUC UNICODE KANJI1 UNICODE_TO_KANJI1_KANJISJIS UNICODE KANJI1 UNICODE_TO_KANJI1_SBC UNICODE KANJI1 UNICODE_TO_KANJISJIS UNICODE KANJISJIS UNICODE_TO_LATIN UNICODE LATIN UNICODE_TO_LOCALE UNICODE KANJI1 LATIN UNICODE_TO_UNICODE_FoldSpace UNICODE UNICODE Chapter 12: String Operator and Functions TRANSLATE 540 SQL Functions, Operators, Expressions, and Predicates If the value specified for source_repertoire_name_TO_target_repertoire_name is UNICODE_TO_LOCALE or LOCALE_TO_UNICODE, the repertoire that the translation uses for LOCALE is determined by the language support mode for the system and the client character set for the session. UNICODE_TO_UNICODE_Fullwidth UNICODE UNICODE UNICODE_TO_UNICODE_Halfwidth UNICODE UNICODE IF the language support mode is … AND the session character set is … THEN the repertoire that the translation uses for LOCALE is … standard any LATIN Japanese • ASCII • LATIN1252_0A • LATIN1_0A • LATIN9_0A • EBCDIC • EBCDIC037_0E • EBCDIC273_0E • EBCDIC277_0E KANJI1_SBC • any other client character set with a name that has a suffix of _0A or _0E • a single-byte, extended site-defined client character set • KANJIEBCDIC5026_0I • KANJIEBCDIC5035_0I • KATAKANAEBCDIC • any other client character set with a name that has a suffix of _0I KANJI1_KANJIEBCDIC • UTF8 • UTF16 • KanjiShiftJIS_0S • any other client character set with a name that has a suffix of _0S • a multibyte extended site-defined client character set KANJI1_KANJISJIS • KanjiEUC_0U • any other client character set with a name that has a suffix of _0U KANJI1_KanjiEUC Value of source_TO_target Source Character Set Target Character Set Chapter 12: String Operator and Functions TRANSLATE SQL Functions, Operators, Expressions, and Predicates 541 Source Characters That Generate Errors The following table lists the characters that generate errors for specific source_repertoire_name_TO_target_repertoire_name translations. For supported translations that do not appear in the table, only the error character generates errors. Value of source_TO_target Source Characters That Generate Errors • LATIN_TO_GRAPHIC • KANJISJIS_TO_GRAPHIC • UNICODE_TO_GRAPHIC non-GRAPHIC • LATIN_TO_KANJISJIS • KANJI1_KANJISJIS_TO_UNICODE • GRAPHIC_TO_KANJISJIS • UNICODE_TO_KANJI1_KANJISJIS • UNICODE_TO_KANJISJIS • LOCALE_TO_UNICODE or UNICODE_TO_LOCALE where the repertoire that the translation uses for LOCALE is KANJI1_KANJISJIS non-KANJISJIS • KANJI1_KanjiEBCDIC_TO_UNICODE • UNICODE_TO_KANJI1_KanjiEBCDIC • LOCALE_TO_UNICODE or UNICODE_TO_LOCALE where the repertoire that the translation uses for LOCALE is KANJI1_KanjiEBCDIC non-KanjiEBCDIC KANJI1 is very permissive, so there may be characters outside the defined region of the encoding as well as illegal form-of-use errors. • KANJI1_KanjiEUC_TO_UNICODE • UNICODE_TO_KANJI1_KanjiEUC • LOCALE_TO_UNICODE or UNICODE_TO_LOCALE where the repertoire that the translation uses for LOCALE is KANJI1_KanjiEUC non-KanjiEUC • KANJISJIS_TO_LATIN • GRAPHIC_TO_LATIN • UNICODE_TO_LATIN • UNICODE_TO_KANJI1_SBC • UNICODE_TO_LOCALE where the repertoire that the translation uses for LOCALE is LATIN or KANJI1_SBC non-LATIN Chapter 12: String Operator and Functions TRANSLATE 542 SQL Functions, Operators, Expressions, and Predicates Error Characters Assigned by the WITH ERROR Option The error characters substituted for offending characters that cannot be translated to a designated target character set are defined in the following table. Suffixes The _suffix variable is used for translations that map source characters to semantically different characters. They indicate the nature of the semantic transformation. The translations perform minor, yet essential, semantic changes to the data, such as halfwidth/ fullwidth conversions, and Space folding modification. The _suffix variable also indicates the form of character data translated from UNICODE to the KANJI1 server character set in one of the four possible encodings, for example Unicode_TO_Kanji1_KanjiEBCDIC. For a list of the encodings, see the definition of _encoding in “Syntax” on page 536. This form of translation is also useful for migrating object names. For information, see “Migration” on page 544. Translations Between Fullwidth and Halfwidth Character Data UNICODE has an area known as the compatibility zone. Among other things, this zone includes halfwidth and fullwidth variants of characters that exist elsewhere in the standard. Translations between fullwidth and halfwidth are provided by the following source_repertoire_name_TO_target_repertoire_name values. Target Character Set Error Character LATIN 0x1A KANJI1 0x1A KANJISJIS 0x1A UNICODE U+FFFD GRAPHIC U+FFFD source_TO_target Meaning UNICODE_TO_UNICODE_Fullwidth This translation maps any character with a fullwidth variant to that variant. At the same time, it maps any character defined by the standard as a halfwidth variant to its non-halfwidth counterpart outside the compatibility zone. Other characters remain unchanged by the translation. Chapter 12: String Operator and Functions TRANSLATE SQL Functions, Operators, Expressions, and Predicates 543 Note that these translations are useful for maintaining more information as a step in translating GRAPHIC to LATIN and vice versa. For details on the mappings, see International Character Set Support. Space Folding Space folding is performed via UNICODE_TO_UNICODE_FoldSpace. All characters defined as space are converted to U+0020. All other characters are left unchanged. For details on which characters are converted to U+0020, see International Character Set Support. Pad Character Translation The following translations do not translate the pad character. If you require pad character translation, use one of the following translations. Other characters are not affected. Note that the position of a character does not affect the translation, so not only trailing pad characters are modified. UNICODE_TO_UNICODE_Halfwidth This translation maps any character with a halfwidth variant to that variant, and all fullwidth variants to their non-fullwidth counterparts. Other characters remain unchanged by the translation. UNICODE_TO_GRAPHIC_VarGraphic This translation is an ANSI equivalent to the VARGRAPHIC function. source_TO_target Meaning source_TO_target Pad Character Translation GRAPHIC_TO_UNICODE A GRAPHIC string that includes an Ideographic Space is translated to a UNICODE string with an Ideographic Space. UNICODE_TO_GRAPHIC A UNICODE string with a Space character generates an error when translated to GRAPHIC. source_TO_target Pad Character Translation GRAPHIC_TO_UNICODE_PadSpace Converts all occurrences of Ideographic Space (U+3000) to Space (U+0020). UNICODE_TO_GRAPHIC_PadGraphic Converts all occurrences of Space to Ideographic Space. Chapter 12: String Operator and Functions TRANSLATE 544 SQL Functions, Operators, Expressions, and Predicates Migration During the migration process, any GRAPHIC data in the old form must be translated to the new canonical form. Note that this involves converting the pad characters from Null (U+0000) to Ideographic Space (U+3000). Implicit Character Data Type Conversion TRANSLATE performs implicit conversion if the string server character set does not match the type implied by source_repertoire_name. An implicit conversion generates an error if a character from character_string_expression has no corresponding character in the source_repertoire_name type. This holds regardless of whether you specify the WITH ERROR option. For example, the following function first translates the string from UNICODE to LATIN, because Teradata Database treats constants as UNICODE, and then translates the string from LATIN to KANJISJIS. However, the translation generates an error because the last character is not in the LATIN repertoire. ... TRANSLATE('abc ' USING LATIN_TO_KanjiSJIS WITH ERROR) ... To circumvent the problem if error character substitution is acceptable, specify two levels of translation, as used in the following example. ... TRANSLATE((TRANSLATE(_UNICODE 'abc ' USING UNICODE_TO_LATIN WITH ERROR)) USING LATIN_TO_KanjiSJIS WITH ERROR) ... Examples Related Topics For details on the mappings that Teradata Database uses for the TRANSLATE function, see International Character Set Support. Function Result Type of the Result TRANSLATE('abc' USING UNICODE_TO_LATIN) 'abc' VARCHAR(3) CHARACTER SET LATIN TRANSLATE('abc' USING UNICODE_TO_UNICODE_Fullwidth) 'abc' VARCHAR(3) CHARACTER SET UNICODE TRANSLATE('abc ' USING UNICODE_TO_LATIN WITH ERROR) where e represents the designated error character for LATIN (0x1A). 'abce' VARCHAR(4) CHARACTER SET LATIN Chapter 12: String Operator and Functions TRANSLATE_CHK SQL Functions, Operators, Expressions, and Predicates 545 TRANSLATE_CHK Purpose Determines if a TRANSLATE conversion can be performed without producing errors; returns an integer test result. Use TRANSLATE_CHK to filter untranslatable strings. You can choose to select translatable strings only, or untranslatable strings only, depending on how you form your SELECT statement. Syntax where: Syntax element … Specifies … character_string_expression a character string to be translated to another server character set. If the string or string expression is not a character type, an error is returned. source_repertoire_name the source character set of the string to be translated. For supported values, see “Supported Translations Between Character Sets” on page 539. A value of LOCALE can be specified for source_repertoire_name to translate a character string from LATIN or KANJI1 to UNICODE using a source repertoire determined by the language support mode of the system and the client character set of the session. For details, see “Supported Translations Between Character Sets” on page 539. _encoding an optional literal for translating from KANJI1 to UNICODE that indicates a specific encoding of KANJI1. The _encoding option is not allowed if LOCALE is specified for source_repertoire_name or target_repertoire_name. 1101E199 TRANSLATE_CHK character_string_expression _encoding ( USING source_repertoire_name A _TO_target_repertoire_name A _suffix ) Chapter 12: String Operator and Functions TRANSLATE_CHK 546 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance TRANSLATE_CHK is a Teradata extension to the ANSI SQL:2008 standard. _encoding (continued) IF the translation is from this character set … THEN use this value for _encoding … • KatakanaEBCDIC • KanjiEBCDIC5026_0I • KanjiEBCDIC5038_0I _KanjiEBCDIC KanjiEUC_0U _KanjiEUC KanjiShiftJIS_0S _KANJISJIS ASCII or EBCDIC _SBC target_repertoire_name the target character set of the string to translate. For supported values, see “Supported Translations Between Character Sets” on page 539. A value of LOCALE can be specified for target_repertoire_name to translate a character string from UNICODE to LATIN or KANJI1 using a target repertoire determined by the language support mode of the system and the client character set of the session. For details, see “Supported Translations Between Character Sets” on page 539. _suffix that the translation maps some source characters to semantically different characters. For example, a translation that specifies the _Halfwidth suffix maps any character with a halfwidth variant to that variant, and all fullwidth variants to their non-fullwidth counterparts. The _suffix option also indicates the form of character data translated from UNICODE to the KANJI1 server character set, for example, _KanjiEUC. Valid values are: • _KanjiEBCDIC • _KanjiEUC • _KANJISJIS • _SBC • _PadSpace • _PadGraphic • _Fullwidth • _Halfwidth • _FoldSpace • _VarGraphic The _suffix option is not allowed if LOCALE is specified for source_repertoire_name or target_repertoire_name. Syntax element … Specifies … Chapter 12: String Operator and Functions TRANSLATE_CHK SQL Functions, Operators, Expressions, and Predicates 547 Argument Types Use TRANSLATE_CHK on character strings and character string expressions. By default, Teradata Database performs implicit type conversion on UDT arguments that have implicit casts to predefined character types. To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including TRANSLATE_CHK, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” Result Type and Attributes Default attributes for TRANSLATE_CHK (string USING source_TO_target) are: Result Values Example 1 Data Type Heading INTEGER Translate_Chk(string using source_to_target) Value Meaning 0 The string can be translated without error. anything else The position of the first character in the string causing a translation error. The value is a logical position for arguments of type LATIN, UNICODE, KANJISJIS, and GRAPHIC. The value is a physical position for arguments of type KANJI1. Function Result TRANSLATE_CHK(‘abc’ USING UNICODE_TO_LATIN) 0 TRANSLATE_CHK(‘abc ’ USING UNICODE_TO_LATIN) 4 Chapter 12: String Operator and Functions TRANSLATE_CHK 548 SQL Functions, Operators, Expressions, and Predicates Example 2 Consider the following table definition: CREATE TABLE table_1 (cunicode CHARACTER(64) CHARACTER SET UNICODE); To find all values in cunicode that can be translated to LATIN, use the following statement: SELECT cunicode FROM table_1 WHERE TRANSLATE_CHK(cunicode USING Unicode_TO_Latin) = 0; Example 3 Consider the following table definitions: CREATE TABLE table_1 (ckanji1 VARCHAR(20) CHARACTER SET KANJI1); CREATE TABLE table_2 (cunicode CHARACTER(20) CHARACTER SET UNICODE); Assume table_1 is populated from the KanjiEUC client character set. To translate the data in ckanji1 in table_1 to UNICODE, and populate table_2 with translations that have no errors, use the following statement: INSERT INTO table_2 SELECT TRANSLATE(ckanji1 USING Kanji1_KanjiEUC_TO_Unicode) FROM table_1 WHERE TRANSLATE_CHK(ckanji1 USING Kanji_KanjiEUC_TO_Unicode) = 0; Example 4 After converting column ckanji1 in table_1 to column cunicode in table_2, you want to find all the fields in table_1 that could not be translated. SELECT ckanji1 FROM table_1 WHERE TRANSLATE_CHK(ckanji1 USING Kanji1_KanjiEUC_TO_Unicode) <> 0; Chapter 12: String Operator and Functions TRIM SQL Functions, Operators, Expressions, and Predicates 549 TRIM Purpose Takes a character or byte string_expression argument, trims the specified pad characters or bytes, and returns the trimmed string_expression. Syntax where: Syntax Element … Specifies … BOTH TRAILING LEADING how to trim the specified trim character or byte from string_expression. The keywords and their meanings appear in the following table. Keyword Meaning BOTH Trim both trailing and leading characters or bytes. TRAILING Trim only trailing characters or bytes. LEADING Trim only leading characters or bytes. If you omit this option, the default is BOTH, and the default trim character is a null byte for byte types and a pad character for character types. trim_expression the character or byte to trim from the head, tail, or both, of string_expression. The expression must evaluate to a single character. You cannot specify trim_expression without also specifying BOTH, TRAILING, or LEADING. You cannot specify a trim_expression of type KANJI1, nor can you apply a trim_expression to a string_expression of type KANJI1. FROM a keyword required when BOTH, TRAILING, or LEADING are specified. character_set the name of the server character set to associate with the string expression. 1101F200 TRIM ( string_expression ) FROM trim_expression BOTH character_set TRAILING LEADING Chapter 12: String Operator and Functions TRIM 550 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance TRIM is ANSI SQL:2008 compliant. Argument Types and Rules The trim_expression argument must evaluate to a single byte that has a byte data type or single character that has a character data type. TRIM operates on the following types of string_expression arguments: • Character, except for CLOB • Byte, except for BLOB • Numeric If a numeric expression is used as the string_expression argument, it is converted implicitly to CHARACTER type. • UDTs that have implicit casts to any of the following predefined types: • Character • Numeric • Byte • DATE To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including TRIM, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion, see Chapter 20: “Data Type Conversions.” character_set (continued) Possible values appear in the following table. Value Server Character Set _Latin LATIN _Unicode UNICODE _KanjiSJIS KANJISJIS _Graphic GRAPHIC string_expression a byte or character string or string expression to be trimmed. Syntax Element … Specifies … Chapter 12: String Operator and Functions TRIM SQL Functions, Operators, Expressions, and Predicates 551 Result Type and Attributes Here are the default result type and attributes for TRIM(string_expression): It is possible for the length of the result to be zero. The server character set of the result is the same as the argument. If the string_expression argument is null, the result is null. Concatenation With TRIM The TRIM function is typically used with the concatenation operator to remove trailing pad characters or trailing bytes containing binary 00 from the concatenated string. If the TRIM function is specified for character data types, leading, trailing, or leading and trailing pad characters are suppressed in the concatenated string, according to which syntax is used. Example 1 If the Names table includes the columns first_name and last_name, which contain the following information: first_name (CHAR(12)) has a value of 'Mary ' last_name (CHAR(12)) has a value of 'Jones ' then this statement: SELECT TRIM (BOTH FROM last_name) || ', ' || TRIM(BOTH FROM first_name) FROM names ; returns the following string (note that the seven trailing blanks at the end of string Jones, and the eight trailing blanks at the end of string Mary are not included in the result): 'Jones, Mary' If the TRIM function is removed, the statement: SELECT last_name || ', ' || first_name FROM names; returns trailing blanks in the string: 'Jones , Mary ' Data Type Heading Trim(BOTH FROM string_expression) IF string_expression is … THEN the result type is … a byte string VARBYTE. a numeric expression or character string VARCHAR. Chapter 12: String Operator and Functions TRIM 552 SQL Functions, Operators, Expressions, and Predicates Example 2 Assume column a is BYTE(4) and column b is VARBYTE(10). If these columns contained the following values: a b ------------ --------- 78790000 43440000 68690000 3200 12550000 332200 then this function: SELECT TRIM (TRAILING FROM a) || TRIM (TRAILING FROM b) FROM ... returns: 78794344 686932 12553322 Example 3 The following statement trims trailing SEMICOLON characters from the specified string. SELECT TRIM( TRAILING ';' FROM textfield) FROM texttable; Example 4 The following table illustrates several more complicated TRIM functions: Function Result SELECT TRIM(LEADING 'a' FROM 'aaabcd'); 'bcd' CREATE TABLE t2 (i1 INTEGER, c1 CHAR(6), c2 CHAR(1)); INSERT t2 (1, 'aaabcd', 'a'); SELECT TRIM(LEADING c2 FROM c1) FROM t2; 'bcd' CREATE TABLE t3 (i1 INTEGER, c1 CHAR(6) CHAR SET UNICODE); INSERT t3 (1, _Unicode '006100610061006200630064'XC); SELECT TRIM(LEADING _Unicode '0061'XC FROM t3.c1); 'bcd' SELECT TRIM(_Unicode '??abc ???'); 'abc ' SELECT TRIM(_Unicode '??abc ???'); 'abc ??' ? (GRAPHIC pad) is not removed. CREATE TABLE t1 (c1 CHARACTER(6) CHARACTER SET GRAPHIC); INSERT t1 (_Graphic 'abc ??'); SELECT TRIM(c1) from t1; 'abc ' ? (GRAPHIC pad) is removed because the operand of the TRIM function is of type GRAPHIC. Chapter 12: String Operator and Functions UPPER SQL Functions, Operators, Expressions, and Predicates 553 UPPER Purpose Returns a character string identical to character_string_expression, except that all lowercase letters are replaced by their uppercase equivalents. Syntax where: ANSI Compliance UPPER is ANSI SQL:2008 compliant. Argument Types UPPER is valid only for character strings and character string expressions, except for CLOBs. By default, Teradata Database performs implicit type conversion on UDT arguments that have implicit casts to predefined character types. To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including UPPER, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” Syntax element … Specifies … character_string_expression a character string or character string expression for which all lowercase characters are to be replaced by their uppercase equivalents. FF07D258 UPPER ( character_string_expression) Chapter 12: String Operator and Functions UPPER 554 SQL Functions, Operators, Expressions, and Predicates Result Type and Attributes Here are the default result type and attributes for UPPER(arg): Usage Notes The UPPER function allows users who want ANSI portability to have case blind comparisons with ANSI-compliant syntax. This function is treated the same as the following obsolete form: expression (UPPERCASE) You can also replace characters with lowercase equivalents. For more information, see “LOWER” on page 517. Restrictions UPPER does not convert multibyte characters to uppercase in the KANJI1 server character set. Example 1 Consider the following table definition where the character columns have CASESPECIFIC attributes: CREATE TABLE employee (last_name CHAR(32) CASESPECIFIC ,city CHAR(32) CASESPECIFIC ,emp_id CHAR(9) CASESPECIFIC ,emp_ssn CHAR(9) CASESPECIFIC); To compare on a case blind basis: SELECT emp_id FROM employee WHERE UPPER(emp_id) = UPPER(emp_ssn); To compare with a string literal: SELECT emp_id FROM employee WHERE UPPER(city) = 'MINNEAPOLIS'; Teradata SQL also has the data type attribute NOT CASESPECIFIC, which allows case blind comparisons. Note that the data type attributes CASESPECIFIC and NOT CASESPECIFIC are Teradata extensions to the ANSI standard. Data Type Heading Same type as arg Upper(arg) Chapter 12: String Operator and Functions UPPER SQL Functions, Operators, Expressions, and Predicates 555 Example 2 The use of UPPER to store values is shown in the following examples: INSERT INTO names SELECT UPPER(last_name),UPPER(first_name) FROM newnames; or USING (last_name CHAR(20),first_name CHAR(20)) INSERT INTO names (UPPER(:last_name), UPPER(:first_name)); Example 3 This example shows that in the KANJI1 server character set, only single byte characters are converted to uppercase. SELECT UPPER('abcd '); The result is 'ABCD '. Chapter 12: String Operator and Functions VARGRAPHIC 556 SQL Functions, Operators, Expressions, and Predicates VARGRAPHIC Purpose Returns the VARGRAPHIC representation of the character data in character_string_expression. Syntax where: ANSI Compliance VARGRAPHIC is a Teradata extension to the ANSI SQL:2008 standard. Argument Types VARGRAPHIC operates on the following types of arguments: • Character, except for CLOB • Numeric If the argument is numeric, it is implicitly converted to a character type. • UDTs that have implicit casts to any of the following predefined types: • Character • Numeric • DATE To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including VARGRAPHIC, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion, see Chapter 20: “Data Type Conversions.” Syntax element … Specifies … character_string_expression a character string or character string expression for which the VARGRAPHIC representation is to be returned. 1101E197 VARGRAPHIC ( character_string_expression ) Chapter 12: String Operator and Functions VARGRAPHIC SQL Functions, Operators, Expressions, and Predicates 557 Result Type and Attributes Here are the default result type and attributes for VARGRAPHIC(arg): Rules VARGRAPHIC reports an error if the session character set is UTF8 or a single-byte character set, such as ASCII. If the argument is of type KANJI1, the only valid session character set is KanjiEBCDIC. All characters in the string are converted into one or more graphics that are valid for the character set of the current session. For details, see “VARGRAPHIC Function Conversion Tables” on page 559. The argument cannot be of type GRAPHIC. A result that exceeds the maximum length of a VARCHAR CHARACTER SET GRAPHIC data type generates an error. VARGRAPHIC cannot appear as the first argument in a user-defined method invocation. Specific rules apply to the server character set of character_string_expression. Data Type Heading VARCHAR(n) CHARACTER SET GRAPHIC Vargraphic(arg) IF the string specifies this server character set … THEN VARGRAPHIC operates as follows … KANJI1 Shift-Out/Shift-In characters in the character_string_expression do not appear in the result string. They are required only to indicate the transition between single byte characters and multibyte characters. Improperly placed Shift-Out/Shift-Ins are replaced by the illegal character for the character set of the session. The SPACE CHARACTER translates to the IDEOGRAPHIC SPACE CHARACTER. UNICODE • Characters with fullwidth representation in the UNICODE compatibility zone translate to that fullwidth representation. • Halfwidth characters from the compatibility zone translate to the corresponding characters outside the compatibility zone. • The SPACE CHARACTER translates to the IDEOGRAPHIC SPACE CHARACTER. • The control characters U+0000 - U+001F and character U+007F are converted to the VARGRAPHIC error character. • Other characters are left untranslated. anything else The result is as if string were first converted to UNICODE and then translated according to the rules listed for UNICODE above. Chapter 12: String Operator and Functions VARGRAPHIC 558 SQL Functions, Operators, Expressions, and Predicates Example 1 The following table shows examples of converting strings that use the UNICODE and LATIN server character sets to GRAPHIC data: Example 2 Consider the following table definition with two character columns that use the KANJI1 server character set: CREATE TABLE t1 (c1 VARCHAR(12) CHARACTER SET KANJI1 ,c2 VARCHAR(12) CHARACTER SET KANJI1); Use the KanjiEBCDIC client character set and insert the following strings: INSERT t1 ('def', 'gHX'); Convert the strings to GRAPHIC data: Function Result VARGRAPHIC('92 abc?') '92 abc?' VARGRAPHIC('abc') 'abc' Function Result SELECT VARGRAPHIC (c1) FROM t1; 'def' SELECT VARGRAPHIC (c2) FROM t1; 'gHABCX' (The single byte Hankaku Katakana X is converted to double byte X.) Chapter 12: String Operator and Functions VARGRAPHIC Function Conversion Tables SQL Functions, Operators, Expressions, and Predicates 559 VARGRAPHIC Function Conversion Tables The following table shows the translation of a single byte character to its double byte equivalent by the VARGRAPHIC function. Values in columns 2, 3, and 4 are hexadecimal. (Also see the notes following the table.) Single Byte Character Double Byte Equivalent JIS Internal Code JIS X 0201 Printable Character KanjiEBCDIC 5026/ 5035 Katakana EBCDIC 00 FEFE FEFE 01 FEFE FEFE 02 FEFE FEFE 03 FEFE FEFE 04 FEFE FEFE 05 FEFE FEFE 06 FEFE FEFE 07 FEFE FEFE 08 FEFE FEFE 09 FEFE FEFE 0A FEFE FEFE 0B FEFE FEFE 0C FEFE FEFE 0D FEFE FEFE 0Ea N/A N/A 0Fb FEFE FEFE 10 FEFE FEFE 11c £ 424A 424A 12d ¬ 425F FEFE 13 \ 43E0 FEFE 14 ~ 43A1 FEFE 15 FEFE FEFE Chapter 12: String Operator and Functions VARGRAPHIC Function Conversion Tables 560 SQL Functions, Operators, Expressions, and Predicates 16 FEFE FEFE 17 FEFE FEFE 18 FEFE FEFE 19 FEFE FEFE 1A FEFE FEFE 1B FEFE FEFE 1C FEFE FEFE 1D FEFE FEFE 1E FEFE FEFE 1F FEFE FEFE 20 4040 4040 21 ! 425A 425A 22 " 4472 4472 23 # 427B 427B 24 $ 42E0 42E0 25 % 426C 426C 26 & 4250 4250 27 ' 4471 4471 28 ( 424D 424D 29 ) 425D 425D 2A * 425C 425C 2B + 424E 424E 2C , 426B 426B 2D - 4260 4260 2E . 424B 424B 2F / 4261 4261 30 0 42F0 42F0 31 1 42F1 42F1 32 2 42F2 42F2 Single Byte Character Double Byte Equivalent JIS Internal Code JIS X 0201 Printable Character KanjiEBCDIC 5026/ 5035 Katakana EBCDIC Chapter 12: String Operator and Functions VARGRAPHIC Function Conversion Tables SQL Functions, Operators, Expressions, and Predicates 561 33 3 42F3 43F3 34 4 42F4 42F4 35 5 42F5 42F5 36 6 42F6 42F6 37 7 42F7 42F7 38 8 42F8 42F8 39 9 42F9 42F9 3A : 427A 427A 3B ; 425E 425E 3C < 424C 424C 3D = 427E 427E 3E > 426E 426E 3F ? 426F 426F 40 @ 427C 427C 41 A 42C1 42C1 42 B 42C2 42C2 43 C 42C3 42C3 44 D 42C4 42C4 45 E 42C5 42C5 46 F 42C6 42C6 47 G 42C7 42C7 48 H 42C8 42C8 49 I 42C9 42C9 4A J 42D1 42D1 4B K 42D2 42D2 4C L 42D3 42D3 4D M 42D4 42D4 4E N 42D5 42D5 4F O 42D6 42D6 Single Byte Character Double Byte Equivalent JIS Internal Code JIS X 0201 Printable Character KanjiEBCDIC 5026/ 5035 Katakana EBCDIC Chapter 12: String Operator and Functions VARGRAPHIC Function Conversion Tables 562 SQL Functions, Operators, Expressions, and Predicates 50 P 42D7 42D7 51 Q 42D8 42D8 52 R 42D9 42D9 53 S 42E2 42E2 54 T 42E3 42E3 55 U 42E4 42E4 56 V 42E5 42E5 57 W 42E6 42E6 58 X 42E7 42E7 59 Y 42E8 42E8 5A Z 42E9 42E9 5B [ 4444 FEFE 5C \ 425B 425B 5D ] 4445 FEFE 5E ^ 4470 425F 5F _ 426D 426D 60 ` 4279 FEFE 61 a 4281 FEFE 62 b 4282 FEFE 63 c 4283 FEFE 64 d 4284 FEFE 65 e 4285 FEFE 66 f 4286 FEFE 67 g 4287 FEFE 68 h 4288 FEFE 69 i 4289 FEFE 6A j 4291 FEFE 6B k 4292 FEFE 6C l 4293 FEFE Single Byte Character Double Byte Equivalent JIS Internal Code JIS X 0201 Printable Character KanjiEBCDIC 5026/ 5035 Katakana EBCDIC Chapter 12: String Operator and Functions VARGRAPHIC Function Conversion Tables SQL Functions, Operators, Expressions, and Predicates 563 6D m 4294 FEFE 6E n 4295 FEFE 6F o 4296 FEFE 70 p 4297 FEFE 71 q 4298 FEFE 72 r 4299 FEFE 73 s 42A2 FEFE 74 t 42A3 FEFE 75 u 42A4 FEFE 76 v 42A5 FEFE 77 w 42A6 FEFE 78 x 42A7 FEFE 79 y 42A8 FEFE 7A z 42A9 FEFE 7B { 42C0 FEFE 7C | 424F 424F 7D } 42D0 FEFE 7E -e 42A1 42A1 7F FEFE FEFE 80 FEFE FEFE 81 FEFE FEFE 82 FEFE FEFE 83 FEFE FEFE 84 FEFE FEFE 85 FEFE FEFE 86 FEFE FEFE 87 FEFE FEFE 88 FEFE FEFE 89 FEFE FEFE Single Byte Character Double Byte Equivalent JIS Internal Code JIS X 0201 Printable Character KanjiEBCDIC 5026/ 5035 Katakana EBCDIC Chapter 12: String Operator and Functions VARGRAPHIC Function Conversion Tables 564 SQL Functions, Operators, Expressions, and Predicates 8A FEFE FEFE 8B FEFE FEFE 8C FEFE FEFE 8D FEFE FEFE 8E FEFE FEFE 8F FEFE FEFE 90 FEFE FEFE 91 FEFE FEFE 92 FEFE FEFE 93 FEFE FEFE 94 FEFE FEFE 95 FEFE FEFE 96 FEFE FEFE 97 FEFE FEFE 98 FEFE FEFE 99 FEFE FEFE 9A FEFE FEFE 9B FEFE FEFE 9C FEFE FEFE 9D FEFE FEFE 9E FEFE FEFE 9F FEFE FEFE A0 FEFE FEFE A1 f 4341 4341 A2 g 4342 4342 A3 h 4343 4343 A4 i 4344 4344 A5 j 4345 4345 A6 k 4346 4346 Single Byte Character Double Byte Equivalent JIS Internal Code JIS X 0201 Printable Character KanjiEBCDIC 5026/ 5035 Katakana EBCDIC Chapter 12: String Operator and Functions VARGRAPHIC Function Conversion Tables SQL Functions, Operators, Expressions, and Predicates 565 A7 l 4347 4347 A8 m 4348 4348 A9 n 4349 4349 AA o 4351 4351 AB p 4352 4352 AC q 4353 4353 AD r 5454 4354 AE s 4355 4355 AF t 4356 4356 B0 u 4358 4358 B1 A 4381 4381 B2 I 4382 4382 B3 U 4383 4383 B4 E 4384 4384 B5 O 4385 4385 B6 KA 4386 4386 B7 KI 4387 4387 B8 KU 4388 4388 B9 KE 4389 4389 BA KO 438A 438A BB SA 438C 438C BC SHI 438D 438D BD SU 438E 438E BE SEE 438F 438F BF SO 4390 4390 C0 TAI 4391 4391 C1 CHI 4392 4392 C2 TSU 4393 4393 C3 TE 4394 4394 Single Byte Character Double Byte Equivalent JIS Internal Code JIS X 0201 Printable Character KanjiEBCDIC 5026/ 5035 Katakana EBCDIC Chapter 12: String Operator and Functions VARGRAPHIC Function Conversion Tables 566 SQL Functions, Operators, Expressions, and Predicates C4 TO 4395 4395 C5 NA 4396 4396 C6 NI 4397 4397 C7 NU 4398 4398 C8 NE 4399 4399 C9 NO 439A 439A CA HA 439D 439D CB HI 439E 439E CC FU 439F 439F CD HE 43A2 43A2 CE HO 43A3 43A3 CF MA 43A4 43A4 D0 MI 43A5 43A5 D1 MU 43A6 43A6 D2 ME 43A7 43A7 D3 MO 43A8 43A8 D4 YA 43A9 43A9 D5 YU 43AA 43AA D6 YO 43AC 43AC D7 RA 43AD 43AD D8 RI 43AE 43AE D9 RU 43AF 43AF DA RE 43BA 43BA DB RO 43BB 43BB DC WA 43BC 43BC DD N 43BD 43BD DE v 43BE 43BE DF w 43BF 43BF E0 FEFE FEFE Single Byte Character Double Byte Equivalent JIS Internal Code JIS X 0201 Printable Character KanjiEBCDIC 5026/ 5035 Katakana EBCDIC Chapter 12: String Operator and Functions VARGRAPHIC Function Conversion Tables SQL Functions, Operators, Expressions, and Predicates 567 E1 FEFE FEFE E2 FEFE FEFE E3 FEFE FEFE E4 FEFE FEFE E5 FEFE FEFE E6 FEFE FEFE E7 FEFE FEFE E8 FEFE FEFE E9 FEFE FEFE EA FEFE FEFE EB FEFE FEFE EC FEFE FEFE ED FEFE FEFE EE FEFE FEFE EF FEFE FEFE F0 FEFE FEFE F1 FEFE FEFE F2 FEFE FEFE F3 FEFE FEFE F4 FEFE FEFE F5 FEFE FEFE F6 FEFE FEFE F7 FEFE FEFE F8 FEFE FEFE F9 FEFE FEFE FA FEFE FEFE FB FEFE FEFE BC FEFE FEFE FD FEFE FEFE Single Byte Character Double Byte Equivalent JIS Internal Code JIS X 0201 Printable Character KanjiEBCDIC 5026/ 5035 Katakana EBCDIC Chapter 12: String Operator and Functions VARGRAPHIC Function Conversion Tables 568 SQL Functions, Operators, Expressions, and Predicates FE FEFE FEFE FF FEFE FEFE a. For KanjiEBCDIC, the SO/SI is not placed in the output of vargraphic function. In particular, a single SO character will not generate any output, or strictly speaking will generate a string with 0 length b. For KanjiEBCDIC, the SO/SI is not placed in the output of vargraphic function. However, if the SI character appears in the input without matching SO, we will generate FEFE for that SI. c. Pound Sterling sign d. Logical NOT e. Overline f. Ideographic period g. Left corner bracket h. Right corner bracket i. Ideographic comma j. Katakana middle dot k. Katakana letter WO l. Katakana letter A m. Katakana letter small I n. Katakana letter small U o. Katakana letter small E p. Katakana letter small O q. Katakana letter small YA r. Katakana letter small YU s. Katakana letter small YO t. Katakana letter small WO u. Katakana-Hiragana prolonged sound mark v. Katakana-Hiragana voiced sound mark w. Katakana-Hiragana semi-voice sound mark Single Byte Character Double Byte Equivalent JIS Internal Code JIS X 0201 Printable Character KanjiEBCDIC 5026/ 5035 Katakana EBCDIC SQL Functions, Operators, Expressions, and Predicates 569 CHAPTER 13 Logical Predicates This chapter describes SQL logical predicates. For information on comparison operators, see Chapter 5: “Comparison Operators.” Logical Predicates A logical predicate tests an operand against one or more other operands to evaluate to a logical (Boolean TRUE, FALSE, or UNKNOWN) result. The tested operand can be one of the following: • A column name • A constant • An arithmetic expression • A Period value expression • The DEFAULT function • A built-in function such as CURRENT_DATE or USER that evaluates to a system variable Logical predicates are also referred to as conditional expressions. The ANSI SQL standard refers to them as search conditions. Where Logical Predicates Are Used Logical predicates are typically used in a WHERE, ON, or HAVING clause to qualify or disqualify rows as a table expression is evaluated in a SELECT statement. Logical predicates can be used in a WHEN clause search condition in a searched CASE expression. The type of test performed is a function of the predicate. Conditional Expressions as a Collection of Logical Primitives You can think of a conditional expression as a collection of logical predicate primitives where the order of evaluation is controlled by the use of the logical operators AND, OR, and NOT and by the placement of parentheses. Superficially similar conditional expressions can produce radically different results depending on how you group their component primitives, so use caution in planning the logic of any conditional expressions. Chapter 13: Logical Predicates Logical Predicates 570 SQL Functions, Operators, Expressions, and Predicates SQL supports the logical predicate primitives listed in the following table. Note that Match and Unique conditions are not supported. Restrictions on the Data Types Involved in Predicates The restrictions in the following table apply to operations involving predicates and CLOB, BLOB, Period, and UDT types. Logical Predicate Primitive Condition SQL Logical Predicate Function Comparison For a complete list of SQL comparison operators, see “Supported Comparison Operators” on page 162. Tests for equality, inequality, or magnitude difference between two data values. Range BETWEEN NOT BETWEEN Tests whether a data value is included within (or excluded from) a specified range of column data values. Like LIKE Tests for a pattern match between a specified character string and a column data value. In IN NOT IN Tests whether a data value is (or is not) a member of a specified set of column values. IN is equivalent to = ANY. NOT IN is equivalent to <> ALL. All ALL Tests whether a data value compares TRUE to all column values in a specified set. Any ANY SOME Tests whether a data value compares TRUE to any column value in a specified set. Exists EXISTS NOT EXISTS Tests whether a specified table contains at least one row. Overlaps OVERLAPS Tests whether two time periods overlap. Period predicates CONTAINS MEETS PRECEDES SUCCEEDS Operates on two Period expressions or one Period expression and one DateTime expression and evaluates to TRUE, FALSE, or UNKNOWN. IS UNTIL_CHANGED IS NOT UNTIL_CHANGED Tests whether the ending bound of a Period value expression is (or is not) UNTIL_CHANGED. Data Type Restrictions BLOB Predicates do not support BLOB or CLOB data types. You can explicitly cast BLOBs to BYTE and VARBYTE types and CLOBs to CHARACTER and VARCHAR types, and use the results in a predicate. CLOB Chapter 13: Logical Predicates Logical Predicates SQL Functions, Operators, Expressions, and Predicates 571 PERIOD Predicates are only supported for CONTAINS, MEETS, PRECEDES, SUCCEEDS, and IS [NOT] UNTIL_CHANGED. UDT Predicate Restrictions LIKE The LIKE and OVERLAPS logical predicates do not support UDTs. OVERLAPS EXISTS/ NOT EXISTS Multiple UDTs involved as predicate operands must be identical types because Teradata Database does not perform implicit type conversion on UDTs involved as predicate operands. A workaround for this restriction is to use CREATE CAST to define casts that cast between the UDTs and then explicitly invoke the CAST function within the operation involving predicates. For more information on CREATE CAST, see SQL Data Definition Language. BETWEEN/ NOT BETWEEN • Multiple UDTs involved as predicate operands must be identical types because Teradata Database does not perform implicit type conversion on UDTs involved as predicate operands. A workaround for this restriction is to use CREATE CAST to define casts that cast between the UDTs and then explicitly invoke the CAST function within the operation involving predicates. • UDTs involved as predicate operands must have ordering definitions. Teradata Database generates ordering functionality for distinct UDTs where the source types are not LOBs. To create an ordering definition for structured UDTs or distinct UDTs where the source types are LOBs, or to replace systemgenerated ordering functionality, use CREATE ORDERING. For more information on CREATE CAST and CREATE ORDERING, see SQL Data Definition Language. IN/NOT IN Data Type Restrictions Chapter 13: Logical Predicates Logical Predicates 572 SQL Functions, Operators, Expressions, and Predicates Restrictions on the DEFAULT Function in a Predicate The DEFAULT function returns the default value of a column. It has two forms: one that specifies a column name and one that omits the column name. Predicates support both forms of the DEFAULT function, but the following conditions must be true when the DEFAULT function omits the column name: • The predicate uses a comparison operator • The comparison involves a single column reference • The DEFAULT function is not part of an expression For example, the following statement uses DEFAULT to compare the values of the Dept_No column with the default value of the Dept_No column. Because the comparison operation involves a single column reference, Teradata Database can derive the column context of the DEFAULT function even though the column name is omitted. SELECT * FROM Employee WHERE Dept_No < DEFAULT; Note that if the DEFAULT function evaluates to null, the predicate is unknown and the WHERE condition is false. Chapter 13: Logical Predicates ANY/ALL/SOME Quantifiers SQL Functions, Operators, Expressions, and Predicates 573 ANY/ALL/SOME Quantifiers Purpose Enables quantification in a comparison operation or IN/NOT IN predicate. Syntax where: Syntax element … Specifies … expression an expression that specifies a value. comparison_operator a comparison operator that compares the expression or list of expressions and the constants in the list (Constants syntax) or the subquery (Subquery syntax) to produce a TRUE, FALSE or UNKNOWN result. For more information on comparison operators, see Chapter 5: “Comparison Operators.” [NOT] IN a predicate that tests the existence of the expression or list of expressions in the list of constants (Constants syntax) or the subquery (Subquery syntax) to produce a TRUE, FALSE, or UNKNOWN result. For more information on IN/NOT IN, see “IN/NOT IN” on page 585. constant a literal value. subquery a subquery that selects the same number of expressions as are specified in the expression or list of expressions. The subquery cannot specify a SELECT AND CONSUME statement. 1101B090 expression comparison_operator ALL ( constant ) IN OR NOT ANY SOME , , Constants syntax Subquery syntax expression comparison_operator ALL (subquery ) IN NOT ANY SOME ( expression ) comparison_operator ALL (subquery ) IN NOT ANY SOME Chapter 13: Logical Predicates ANY/ALL/SOME Quantifiers 574 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance ANY, SOME, and ALL are ANSI SQL:2008 compliant quantifiers. ANY/ALL/SOME Quantifiers and Constant Syntax When a list of constants is used with quantifiers and comparison operations or IN/NOT IN predicates, the results are determined as follows. For comparison operations, implicit conversion rules are the same as for the comparison operators. If expression evaluates to NULL, the result is considered to be unknown. ANY/ALL/SOME Quantifiers and Subquery Syntax When subqueries are used with quantifiers and comparison operations or IN/NOT IN predicates, the results are determined as follows. IF the predicate is … AND specifies … THEN the result is true when … a comparison operation ALL the comparison of expression and every constant in the list produces true results. ANY the comparison of expression and any constant in the list is true. SOME IN ALL expression is equal to every constant in the list. ANY expression is equal to any constant in the list. SOME NOT IN ALL expression is not equal to any constant in the list. ANY expression is not equal to every constant in the list. SOME IF this quantifier is specified … AND the predicate is … THEN the result is … WHEN … ALL a comparison operation TRUE the comparison of expression and every value in the set of values returned by subquery produces true results. IN TRUE expression is equal to every value in the set of values returned by subquery. NOT IN TRUE expression is not equal to any value in the set of values returned by subquery. Chapter 13: Logical Predicates ANY/ALL/SOME Quantifiers SQL Functions, Operators, Expressions, and Predicates 575 Equivalences Using ANY/ALL/SOME and Comparison Operators The following table provides equivalences for the ANY/ALL/SOME quantifiers, where op is a comparison operator: Here are some examples: ALL a comparison operation TRUE subquery returns no values. IN NOT IN ANY SOME a comparison operation TRUE the comparison of expression and at least one value in the set of values returned by subquery is true. IN TRUE expression is equal to at least one value in the set of values returned by subquery. NOT IN TRUE expression is not equal to at least one value in the set of values returned by subquery. a comparison operation FALSE subquery returns no values. IN NOT IN IF this quantifier is specified … AND the predicate is … THEN the result is … WHEN … This … Is equivalent to … x op ALL (:a, :b, :c) (x op :a) AND (x op :b) AND (x op :c) x op ANY (:a, :b, :c) (x op :a) OR (x op :b) OR (x op :c) x op SOME (:a, :b, :c) This expression … Is equivalent to … x < ALL (:a, :b, :c) (x < :a) AND (x < :b) AND (x < :c) x > ANY (:a, :b, :c) (x > :a) OR (x > :b) OR (x > :c) x > SOME (:a, :b, :c) Chapter 13: Logical Predicates ANY/ALL/SOME Quantifiers 576 SQL Functions, Operators, Expressions, and Predicates Equivalences Using ANY/ALL/SOME and IN/NOT IN The following table provides equivalences for the ANY/ALL/SOME quantifiers, where op is IN or NOT IN: Here are some examples: Example 1 The following statement uses a comparison operator with the ANY quantifier to select the employee number, name, and department number of anyone in departments 100, 300, and 500: This … Is equivalent to …a a. If op is NOT IN, then NOT op is IN, not NOT NOT IN. NOT (x op ALL (:a, :b, :c)) x NOT op ANY (:a, :b, :c) x NOT op SOME (:a, :b, :c) NOT (x op ANY (:a, :b, :c)) x NOT op ALL (:a, :b, :c) NOT (x op SOME (:a, :b, :c)) This expression … Is equivalent to … NOT (x IN ANY (:a, :b, :c)) x NOT IN ALL (:a, :b, :c) NOT (x IN ALL (:a, :b, :c)) x NOT IN ANY (:a, :b, :c) NOT (x NOT IN ANY (:a, :b, :c)) x IN ALL (:a, :b, :c) NOT (x NOT IN ALL (:a, :b, :c)) x IN ANY (:a, :b, :c) This Expression … Is Equivalent to this expression… SELECT EmpNo, Name, DeptNo FROM Employee WHERE DeptNo = ANY (100,300,500) ; SELECT EmpNo, Name, DeptNo FROM Employee WHERE (DeptNo = 100) OR (DeptNo = 300) OR (DeptNo = 500) ; and SELECT EmpNo, Name, DeptNo FROM Employee WHERE DeptNo IN (100,300,500) ; Chapter 13: Logical Predicates ANY/ALL/SOME Quantifiers SQL Functions, Operators, Expressions, and Predicates 577 Example 2 Here is an example that uses a subquery in a comparison operation that specifies the ALL quantifier: SELECT EmpNo, Name, JobTitle, Salary, YrsExp FROM Employee WHERE (Salary, YrsExp) >= ALL (SELECT Salary, YrsExp FROM Employee) ; Example 3 This example shows the behavior of ANY/ALL/SOME. Consider the following table definition and contents: CREATE TABLE t (x INTEGER); INSERT t (1); INSERT t (2); INSERT t (3); INSERT t (4); INSERT t (5); IF you use this query … THEN the result is … SELECT * FROM t WHERE x IN ANY (1,2) 1, 2 SELECT * FROM t WHERE x = SOME (1,2) 1, 2 SELECT * FROM t WHERE x NOT IN ALL (1,2) 3, 4, 5 SELECT * FROM t WHERE NOT (x IN ANY (1,2)) 3, 4, 5 SELECT * FROM t WHERE NOT (x = SOME (1,2)) 3, 4, 5 SELECT * FROM t WHERE x NOT IN SOME (1, 2) 1, 2, 3, 4, 5 SELECT * FROM t WHERE x NOT = ANY (1, 2) 1, 2, 3, 4, 5 SELECT * FROM t WHERE x IN ALL (1,2) no rows SELECT * FROM t WHERE NOT (x NOT IN SOME (1,2)) no rows SELECT * FROM t WHERE x = ALL (1,2) no rows SELECT * FROM t WHERE NOT (x NOT = ANY (1,2)) no rows Chapter 13: Logical Predicates BETWEEN/NOT BETWEEN 578 SQL Functions, Operators, Expressions, and Predicates BETWEEN/NOT BETWEEN Purpose Tests whether an expression value is between two other expression values. Syntax ANSI Compliance BETWEEN and NOT BETWEEN are ANSI SQL:2008 compliant. Usage Notes The BETWEEN test is satisfied if the following condition is true. expression_2 <= expression_1 <= expression_3 If the BETWEEN test fails, no rows are returned. The BETWEEN test is treated as two separate logical comparisons. expression_1 >= expression_2 AND expression_1 <= expression_3. Note that because expression_1 is actually evaluated twice, using a nondeterministic function, such as RANDOM, can produce unexpected results. Example The following example uses a search condition in a HAVING clause to select from the Employee table those departments with the number 100, 300, 500, or 600, and with a salary average of at least $35,000 but not more than $55,000: SELECT AVG(Salary) FROM Employee WHERE DeptNo IN (100,300,500,600) GROUP BY DeptNo HAVING AVG(Salary) BETWEEN 35000 AND 55000 ; HH01A038 NOT expr1 BETWEEN expr2 AND expr3 This expression … Is equivalent to … x BETWEEN y AND z ((x >= y) AND (x <=z)) Chapter 13: Logical Predicates EXISTS/NOT EXISTS SQL Functions, Operators, Expressions, and Predicates 579 EXISTS/NOT EXISTS Purpose Tests a specified table (normally a derived table) for the existence of at least one row (that is, it tests whether the table in question is non-empty). EXISTS is supported as the predicate of the search condition in a WHERE clause. Syntax ANSI Compliance EXISTS and NOT EXISTS are ANSI SQL:2008 compliant. Usage Notes The function of the EXISTS predicate is to test the result of subquery. If execution of the subquery returns response rows then the where condition is considered satisfied. Note that use of the NOT qualifier for the EXISTS predicate reverses the sense of the test. Execution of the subquery does not, in fact, return any response rows. Instead, it returns a boolean result to indicate whether responses would or would not have been returned had they been requested. Subquery Restrictions The subquery cannot specify a SELECT AND CONSUME statement. Relationship Between EXISTS/NOT EXISTS and IN/NOT IN EXISTS predicate tests the existence of specified rows of a subquery. In general, EXISTS can be used to replace comparisons with IN and NOT EXISTS can be used to replace comparisons with NOT IN. However, the reverse is not true. Some problems can be solved only by using EXISTS and/or NOT EXISTS predicate. For an example, see “For ALL” on page 581. For information on IN and NOT IN, see “IN/NOT IN” on page 585. HH01A047 subquery NOT EXISTS Chapter 13: Logical Predicates EXISTS/NOT EXISTS 580 SQL Functions, Operators, Expressions, and Predicates Example To select rows of t1 whose values in column x1 are equal to the value in column x2 of t2, one of the following queries can be used: SELECT * FROM t1 WHERE x1 IN (SELECT x2 FROM t2); SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t2 WHERE t1.x1=t2.x2); To select rows of t1 whose values in column x1 are not equal to any value in column x2 of t2, you can use any one of the following queries: SELECT * FROM t1 WHERE x1 NOT IN (SELECT x2 FROM t2); SELECT * FROM t1 WHERE NOT EXISTS (SELECT * FROM t2 WHERE t1.x1=t2.x2); SELECT 'T1 is not empty' WHERE EXISTS (SELECT * FROM t1); SELECT 'T1 is empty' WHERE NOT EXISTS (SELECT * FROM t1); EXISTS Predicate Versus NOT IN and Nulls Use the NOT EXISTS predicate instead of NOT IN if the following conditions are true: • Some column of the NOT IN condition is defined as nullable. • Any rows from the main query with a null in any column of the NOT IN condition should always be returned. • Any nulls returned in the select list of the subquery should not prevent any rows from the main query from being returned. Chapter 13: Logical Predicates EXISTS/NOT EXISTS SQL Functions, Operators, Expressions, and Predicates 581 For example, if all of the previous conditions are true for the following query, use NOT EXISTS instead of NOT IN: SELECT dept, DeptName FROM Department WHERE Dept NOT IN (SELECT Dept FROM Course); The NOT EXISTS version looks like this: SELECT dept, DeptName FROM Department WHERE NOT EXISTS (SELECT Dept FROM Course WHERE Course.Dept=Department.Dept); That is, either Course.Dept or Department.Dept is nullable and a row from Department with a null for Dept should be returned and a null in Course.Dept should not prevent rows from Department from being returned. For ALL Two nested NOT EXISTS can be used to express a SELECT statement that embodies the notion of “for all (logical ?) the values in a column, there exists (logical ? ) …” For example the query to select a ‘true’ value if the library has at least one book for all the publishers can be expressed as follows: SELECT 'TRUE' WHERE NOT EXISTS (SELECT * FROM publisher pb WHERE NOT EXISTS (SELECT * FROM book bk WHERE pb.PubNum=bk.PubNum); [NOT] EXISTS Clauses and Stored Procedures You cannot specify a [NOT] EXISTS clause in a stored procedure conditional expression if that expression also references an alias for a local variable, parameter, or cursor. NOT EXISTS and Recursive Queries NOT EXISTS cannot appear in a recursive statement of a recursive query. However, a nonrecursive seed statement in a recursive query can specify the NOT EXISTS predicate. Example 1: EXISTS with Correlated Subqueries Select all student names who have registered in at least one class offered by some department. SELECT SName, SNo FROM student s WHERE EXISTS (SELECT * Chapter 13: Logical Predicates EXISTS/NOT EXISTS 582 SQL Functions, Operators, Expressions, and Predicates FROM department d WHERE EXISTS (SELECT * FROM course c, registration r, class cl WHERE c.Dept=d.Dept AND c.CNo=r.CNo AND s.SNo=r.SNo AND r.CNo=cl.CNo AND r.Sec=cl.Sec)); The content of the student table is as follows: The content of the department table is as follows: The content of course table is as follows: Sname SNo Helen Chu 1 Alice Clark 2 Kathy Kim 3 Tom Brown 4 Dept DeptName 100 Computer Science 200 Physic 300 Math 400 Science CNo Dept 10 100 11 100 12 200 13 200 14 300 Chapter 13: Logical Predicates EXISTS/NOT EXISTS SQL Functions, Operators, Expressions, and Predicates 583 The content of the class table is as follows: The content of the registration table is as follows: The following rows are returned: SName SNo ----------- --- Helen Chu1 * Alice Clark 2 Kathy Kim 3 For a full explanation of correlated subqueries, see “Correlated Subqueries” in SQL Data Manipulation Language. Example 2: NOT EXISTS with Correlated Subqueries Select the names of all students who have registered in at least one class offered by each department that offers a course. SELECT SName, SNo FROM student s WHERE NOT EXISTS (SELECT * FROM department d WHERE d.Dept IN (SELECT Dept FROM course) AND NOT EXISTS (SELECT * CNo Sec 10 1 11 1 12 1 13 1 14 1 CNo SNo Sec 10 1 1 10 2 1 11 3 1 12 1 1 13 2 1 14 1 1 Chapter 13: Logical Predicates EXISTS/NOT EXISTS 584 SQL Functions, Operators, Expressions, and Predicates FROM course c, registration r, class cl WHERE c.Dept=d.Dept AND c.CNo=r.CNo AND s.SNo=r.SNo AND r.CNo=cl.CNo AND r.Sec=cl.Sec))); With the contents of the tables as in “Example 1: EXISTS with Correlated Subqueries” on page 581, the following rows are returned: SName SNo ----- --- Helen Chu 1 Chapter 13: Logical Predicates IN/NOT IN SQL Functions, Operators, Expressions, and Predicates 585 IN/NOT IN Purpose Tests the existence of the value of an expression or expression list in a comparable set in one of two ways: • Compares the value of an expression with values in an explicit list of constants. • Compares values in a list of expressions with values and in a set of corresponding expressions in a subquery. ANSI Compliance IN and NOT IN are ANSI SQL:2008 compliant. Using TO in a list of constants is a Teradata extension to the ANSI standard. Syntax 1: expression IN and NOT IN expression or constants where: Syntax element … Specifies … expression_1 the value of the expression whose existence is to be tested in expression_2 or in an explicit list of constants named by constant, signed_constant TO signed_constant, or datetime_literal. 1101A309 NOT expression_1 IN OR , expression_2 ( constant signed_constant_1 TO signed_constant_2 ) datetime_literal Chapter 13: Logical Predicates IN/NOT IN 586 SQL Functions, Operators, Expressions, and Predicates Result If IN is used with a list of constants, the result is true if the value of expression_1 is: • equal to any constant in the list, • between signed_constant_1 and signed_constant_2, inclusively, when signed_constant_1 is less than or equal to signed_constant_2, or • between signed_constant_2 and signed_constant_1, inclusively, when signed_constant_2 is less than signed_constant_1 If the value of expression_1 is null, then the result is considered to be unknown. If the value of expression_1 is not null, and none of the conditions are satisfied for the result to be true, then the result is false. Using this form, the IN search condition is satisfied if the expression is equal to any of the values in the list of constants; the NOT IN condition is satisfied if none of the values in the list of constants are equal to the expression. IN NOT IN whether the test is inclusive or exclusive. You can substitute … FOR … • IN ANY • IN SOME • = ANY • = SOME IN, unless a list of constants is specified and includes signed_constant_1 TO signed_constant_2 • <> ALL • NOT IN ALL NOT IN, unless a list of constants is specified and includes signed_constant_1 TO signed_constant_2 expression_2 the value in which the existence of expression_1 is to be tested. constant • constant • macro parameter • built-in value such as TIME or DATE signed_constant_1 TO signed_constant_2 a range of constants. datetime_literal an ANSI DateTime literal. Syntax element … Specifies … THE condition is true for this form … WHEN … expression_1 IN expression_2 expression_1 = expression_2 expression_1 NOT IN expression_2 expression_1 <> expression_2 Chapter 13: Logical Predicates IN/NOT IN SQL Functions, Operators, Expressions, and Predicates 587 Here are some examples: Usage Notes If IN is used with a single-term operator, that operator can be a constant or an expression. If a multiple-term operator is used, that operator must consist of constants; expressions are not allowed. The expression_1 data type and the constant values must be compatible. Implicit conversion rules are the same as for the comparison operators. Relationship Between IN/NOT IN and EXISTS/NOT EXISTS In general, you can use EXISTS to replace comparisons with IN, and NOT EXISTS to replace comparisons with NOT IN. However, the reverse is not true. The solutions to some problems require using the EXISTS or NOT EXISTS predicate. For information on EXISTS and NOT EXISTS, see “EXISTS/NOT EXISTS” on page 579. expression_1 IN (const_1, const_2) (expression_1 = const_1) OR (expression_1 = const_2) expression_1 NOT IN (const_1, const_2) (expression_1 <> const_1) AND (expression_1 <> const_2) expression_1 IN (signed_const_1 TO signed_const_2) where signed_const_1 <= signed_const_2 (signed_const_1 <= expression_1) AND (expression_1 <= signed_const_2) expression_1 IN (signed_const_1 TO signed_const_2) where signed_const_2 < signed_const_1 (signed_const_2 <= expression_1) AND (expression_1 <= signed_const_1) expression_1 NOT IN (signed_const_1 TO signed_const_2) where signed_const_1 <= signed_const_2 (expression_1 < signed_const_1) OR (expression_1 > signed_const_2) expression_1 NOT IN (signed_const_1 TO signed_const_2) where signed_const_2 < signed_const_1 (expression_1 < signed_const_2) OR (expression_1 > signed_const_1) This statement … Is equivalent to this statement … SELECT DeptNo FROM Department WHERE DeptNo IN (500, 600); SELECT DeptNo FROM Department WHERE (DeptNo = 500) OR (DeptNo = 600); UPDATE Employee SET Salary=Salary + 200 WHERE DeptNo NOT IN (100, 700); UPDATE Employee SET Salary=Salary + 200 WHERE (DeptNo ^= 100) AND (DeptNo ^= 700); THE condition is true for this form … WHEN … Chapter 13: Logical Predicates IN/NOT IN 588 SQL Functions, Operators, Expressions, and Predicates Equivalences Using IN/NOT IN, NOT, and ANY/ALL/SOME The following table provides equivalences for the ANY/ALL/SOME quantifiers, where op is IN or NOT IN: Here are some examples: Syntax 2: expression IN and NOT IN subquery This syntax for IN and NOT IN is correct in either of the following two forms: where: This usage … Is equivalent to …a a. In the equivalences, if op is NOT IN, then NOT op is IN, not NOT NOT IN. NOT (x op ALL (:a, :b, :c)) x NOT op ANY (:a, :b, :c) x NOT op SOME (:a, :b, :c) NOT (x op ANY (:a, :b, :c)) x NOT op ALL (:a, :b, :c) NOT (x op SOME (:a, :b, :c)) NOT (x op (:a, :b, :c)) x NOT op (:a, :b, :c) This expression … Is equivalent to … NOT (x IN ANY (:a, :b, :c)) x NOT IN ALL (:a, :b, :c) NOT (x IN ALL (:a, :b, :c)) x NOT IN ANY (:a, :b, :c) NOT (x NOT IN ANY (:a, :b, :c)) x IN ALL (:a, :b, :c) NOT (x NOT IN ALL (:a, :b, :c)) x IN ANY (:a, :b, :c) NOT (x IN (:a, :b, :c)) x NOT IN (:a, :b, :c) NOT (x NOT IN (:a, :b, :c)) x IN (:a, :b, :c) Syntax element … Specifies … expression the value of the expression whose existence is to be tested in subquery. HH01B002 NOT IN subquery , ( expression ) ( ) NOT expression IN ( subquery ) Chapter 13: Logical Predicates IN/NOT IN SQL Functions, Operators, Expressions, and Predicates 589 Behavior of Nulls for IN A statement result does not include column nulls when IN is used with a subquery. Behavior of Nulls for NOT IN The following table explains the behavior of nulls for NOT IN for queries of various forms: [NOT] IN Clauses and Stored Procedures You cannot specify a [NOT] IN clause in a stored procedure conditional expression if that expression also references an alias for a local variable, parameter, or cursor. subquery a SELECT statement that returns values that satisfy the stated search criterion. The subquery must: • Be enclosed in parentheses. • Not end with a semicolon. • Select the same number of expressions as are defined in the expression list. • Not specify a SELECT AND CONSUME statement. Syntax element … Specifies … FOR a query of the following form … IF … THEN … SELECT ... FROM T1 WHERE x NOT IN (SELECT y FROM T2); one of the y values is null no T1 rows are returned for the entire query. some rows are returned by the subquery, and if x contains some nulls those T1 rows that contain a null in x are not returned. SELECT ... FROM T1 WHERE expression_list_1 NOT IN (SELECT expression_list_2 FROM T2); a null is the first field in expression_list_2 no rows from T1 are returned. a null is in a field other than the first field of expression_list_2 some rows may be returned the subquery returns some rows, and if a null is in the first field in expression_list_1 the T1 rows containing a null in the first field of expression_list_1 are not returned. SELECT ... FROM T1 WHERE expression_list_1 NOT IN (SELECT expression_list_2 FROM T2 WHERE search_condition); the search_condition on T2 returns no rows all T1 rows, including those containing a null value in the first field of expression_list_1, are returned. Chapter 13: Logical Predicates IN/NOT IN 590 SQL Functions, Operators, Expressions, and Predicates NOT IN and Recursive Queries NOT IN cannot appear in a recursive statement of a recursive query. However, a non-recursive seed statement in a recursive query can specify the NOT IN predicate. Queries With Large [NOT] IN Clauses Can Fail Queries that contain thousands of arguments within an IN or NOT IN clause sometimes fail. For example, suppose you ran the following query with 16000 IN clause arguments, and it failed. SELECT MAX(emp_num) FROM employee WHERE emp_num IN(1,2,7,8,...,121347); A workaround when this problem occurs is to rewrite the query using a temporary or volatile table to contain the arguments within the IN clause. The following statements allow you to make the same selection, but without failure. CREATE VOLATILE TABLE temp_IN_values ( in_value INTEGER) ON COMMIT PRESERVE ROWS; INSERT INTO temp_IN_values SELECT emp_num FROM table_with_emp_num_values; The new query is as follows: SELECT MAX(emp_num) FROM employee AS e JOIN temp_IN_values AS en ON (e.emp_num = en.in_value); Example 1 The following statement searches for the names of all employees who work in Atlanta. SELECT Name FROM Employee WHERE DeptNo IN (SELECT DeptNo FROM Department WHERE Loc = 'ATL'); Example 2 Using a similar example but assuming that the DeptNo is divided into two columns, the following statement could be used: SELECT Name FROM Employee WHERE (DeptNoA, DeptNoB) IN (SELECT DeptNoA, DeptNoB FROM Department WHERE Loc = 'LAX') ; Chapter 13: Logical Predicates IN/NOT IN SQL Functions, Operators, Expressions, and Predicates 591 Example 3 This example shows the behavior of IN/NOT IN with a list of constants. Consider the following table definition and contents: CREATE TABLE t (x INTEGER); INSERT t (1); INSERT t (2); INSERT t (3); INSERT t (4); INSERT t (5); IF you use this query … THEN the result is … SELECT * FROM t WHERE x IN (1,2) 1, 2 SELECT * FROM t WHERE x IN ANY (1,2) 1, 2 SELECT * FROM t WHERE NOT (x NOT IN (1,2)) 1, 2 SELECT * FROM t WHERE x NOT IN (1,2) 3, 4, 5 SELECT * FROM t WHERE x NOT IN ALL (1,2) 3, 4, 5 SELECT * FROM t WHERE NOT (x IN (1, 2)) 3, 4, 5 SELECT * FROM t WHERE NOT (x IN ANY (1,2)) 3, 4, 5 SELECT * FROM t WHERE x IN (3 TO 5) 3, 4, 5 SELECT * FROM t WHERE x NOT IN SOME (1, 2) 1, 2, 3, 4, 5 SELECT * FROM t WHERE x IN (1, 2 TO 4, 5) 1, 2, 3, 4, 5 SELECT * FROM t WHERE x IN ALL (1,2) no rows SELECT * FROM t WHERE NOT (x NOT IN SOME (1,2)) no rows SELECT * FROM t WHERE x NOT IN (1 TO 5) no rows Chapter 13: Logical Predicates IS NULL/IS NOT NULL 592 SQL Functions, Operators, Expressions, and Predicates IS NULL/IS NOT NULL Purpose Searches for or excludes nulls in an expression. Syntax where: ANSI Compliance IS NULL and IS NOT NULL are ANSI SQL:2008 compliant. Example 1 To search for the names of all employees who have not been assigned to a department, enter the following statement: SELECT Name FROM Employee WHERE DeptNo IS NULL; The result of this query is the names of all employees with a null in the DeptNo field. Example 2 Conversely, to search for the names of all employees who have been assigned to a department, you could enter the following statement: SELECT Name FROM Employee WHERE DeptNo IS NOT NULL; This query returns the names of all employees with a non-null value in the DeptNo field. Example 3: Searching for NULL and NOT-NULL in the Same Statement If you are searching for nulls and non-null values in the same statement, the search condition for null values must appear separately. Syntax element … Specifies … expression an expression that specifies a value that is tested for nulls. HH01A042 NOT expression IS NULL Chapter 13: Logical Predicates IS NULL/IS NOT NULL SQL Functions, Operators, Expressions, and Predicates 593 For example, to select the names of all employees without the job title of “Manager” or “Vice Pres”, plus the names of all employees with a null in the JobTitle column, you must enter the statement as follows: SELECT Name, JobTitle FROM Employee WHERE (JobTitle NOT IN ('Manager' OR 'Vice Pres')) OR (JobTitle IS NULL) ; Example 4: Searching a Table That Might Contain Nulls You must be careful when searching a table that might contain nulls. For example, if the EdLev column contains nulls and you submit the following query, the result contains only the names of employees with an education level of less than 16 years. SELECT Name, EdLev FROM Employee WHERE (EdLev < 16) ; To ensure that the result of a statement contains nulls, you must structure it as follows. SELECT Name, EdLev FROM Employee WHERE (EdLev < 16) OR (EdLev IS NULL) ; Chapter 13: Logical Predicates LIKE 594 SQL Functions, Operators, Expressions, and Predicates LIKE Purpose Searches for a character string pattern within another character string or character string expression. Syntax where: Syntax Element … Specifies … expression a character string or character string expression argument to be searched for the substring pattern_expression. pattern_expression a character expression for which expression is to be searched. ANY ALL SOME a quantifier that allows one or more expressions to be searched for one or more patterns or for one or more values returned by a subquery. SOME is a synonym for ANY. subquery a SELECT statement argument. A subquery cannot specify a SELECT AND CONSUME statement. ESCAPE escape_character keyword/variable combination specifying a single escape character (single or multibyte). FF07D196 NOT LIKE ESCAPE escape_character ( pattern_expression ) , NOT expression LIKE ESCAPE escape_character ( subquery ) , NOT expression LIKE ESCAPE escape_character ( ) ( subquery ) NOT expression LIKE pattern_expression ESCAPE escape_character , ( expression ) ALL ANY SOME ALL ANY SOME ALL ANY SOME Chapter 13: Logical Predicates LIKE SQL Functions, Operators, Expressions, and Predicates 595 ANSI Compliance LIKE is ANSI SQL:2008 compliant. Optimized Performance Using a NUSI If it is cost-effective, the Optimizer may choose to evaluate a LIKE expression by scanning a NUSI with or without accessing the base table. The cost of using a NUSI depends on the selectivity of the LIKE expression, the size of the NUSI subtable, and if the NUSI is a covering index or a partially covering index. For a partially covering index, the cost of sorting the RowID spool is also included. For details on NUSIs and query covering, see Database Design. The Optimizer can perform a better cost comparison between using a NUSI and using an allrows scan if the following are true: • There are statistics collected for both the base table primary index and for the NUSI columns against which the expression string is evaluated. • The expression string is either the mode or max value in at least one interval in the base table statistics histogram. You cannot use a NUSI with a VARCHAR field for processing a LIKE expression when: • the NUSI contains a VARCHAR field, and the VARCHAR field is used in a NOT LIKE operation. • the NUSI contains a VARCHAR field, and the VARCHAR field is used in a string function. For example, the following is not allowed if d1 is a NUSI column of VARCHAR type. d1||‘ab’ LIKE ‘b ab’ In addition, a NUSI with a VARCHAR field cannot be used as a partially covering index for an unconstrained aggregate query. Null Expressions If any expression in a comparison is null, the result of the comparison is unknown. For a LIKE operation to provide a true result when searching fields that may contain nulls, the statement must include the IS [NOT] NULL operator. Case Specification If neither pattern_expression nor expression has been designated CASESPECIFIC, any lowercase letters in pattern_expression and expression are converted to uppercase before the comparison operation occurs. If ESCAPE is specified and the escape character is a lowercase character, it is also converted to uppercase before the comparison operation occurs. If either expression or pattern_expression has been designated CASESPECIFIC, two letters match only if they are the same letters and the same case. Wildcard Characters The % and _ characters may be used in any combination in pattern_expression. Chapter 13: Logical Predicates LIKE 596 SQL Functions, Operators, Expressions, and Predicates The underscore and percent characters cannot be used in a pattern. To get around this, specify a single escape character in addition to pattern_expression. For details, see “ESCAPE Feature of LIKE” on page 597. The following table describes how the metacharacters % and _ (and their fullwidth equivalents) behave when matching strings for various server character sets. Note that ANSI only defines the single byte spacing underscore and percent sign metacharacters. Teradata SQL extends the permissible metacharacter set for the LIKE predicate to include the fullwidth underscore and the fullwidth percent sign. Character Description % (PERCENT SIGN) Represents any string of zero or more arbitrary characters. Any string of characters is acceptable as a replacement for the percent. _ (LOW LINE) Represents exactly one arbitrary character. Any single character is acceptable in the position in which the underscore character appears. FOR this server character set … USE this metacharacter … TO match this character or characters … ANSI Mode Teradata Mode KANJI1 spacing underscore any one single- or multibyte character. any one single byte character. fullwidth spacing underscore any one single byte character or multibyte character. any one single byte character or multibyte character. percent sign any sequence of single or multibyte characters. any sequence of single byte characters or multibyte characters. fullwidth percent sign any sequence of single or multibyte characters. any sequence of single byte characters or multibyte characters. UNICODE LATIN KANJISJIS fullwidth spacing underscore none. These characters are not treated as metacharacters in order to maintain compliance with the ANSI SQL standard. fullwidth percent GRAPHIC fullwidth spacing underscore any one single GRAPHIC character. fullwidth percent sign any sequence of GRAPHIC characters. Chapter 13: Logical Predicates LIKE SQL Functions, Operators, Expressions, and Predicates 597 ESCAPE Feature of LIKE When the defined ESCAPE character is in the pattern string, it must be immediately followed by an underscore, percent sign, or another ESCAPE character. In a left-to-right scan of the pattern string the following rules apply when ESCAPE is specified: • Until an instance of the ESCAPE character occurs, characters in the pattern are interpreted at face value. • When an ESCAPE character immediately follows another ESCAPE character, the two character sequence is treated as though it were a single instance of the ESCAPE character, considered as a normal character. • When an underscore metacharacter immediately follows an ESCAPE character, the sequence is treated as a single underscore character (not a wildcard character). • When a percent metacharacter immediately follows an ESCAPE character, the sequence is treated as a single percent character (not a wildcard character). • When an ESCAPE character is not immediately followed by an underscore metacharacter, a percent metacharacter, or another instance of itself, the scan stops and an error is reported. Example The following example illustrates the use of ESCAPE: To look for the pattern ‘95%’ in a string such as ‘Result is 95% effective’, if Result is the field to be checked, use: WHERE Result LIKE '%95Z%%' ESCAPE 'Z' This clause finds the value ‘95%’. Pad Characters The following notes apply to pad characters and how they are treated in strings: • Pad characters are significant in both the character expression, and in the pattern string. • When using pattern matching, be aware that both leading and trailing pad characters in the field or expression must match exactly with the pattern. For example, ‘A%BC’ matches ‘AxxBC’, but not ‘AxxBC?’, and ‘A%BC?’ matches ‘AxxBC?’, but not ‘AxxBC’ or ‘AxxBC??’ (? indicates a pad character). • To retrieve the row in all cases, consider using the TRIM function, which removes both leading and trailing pad characters from the source string before doing the pattern match. For example, to remove trailing pad characters: TRIM (TRAILING FROM expression) LIKE pattern-string To remove leading and trailing pad characters: TRIM (BOTH FROM expression) LIKE pattern-string Chapter 13: Logical Predicates LIKE 598 SQL Functions, Operators, Expressions, and Predicates • If pattern_expression is forced to a fixed length, trailing pad characters might be appended. In such cases, the field must contain the same number of trailing pad characters in order to match. For example, the following statement appends trailing pad characters to pattern strings shorter than 5 characters long. CREATE MACRO (pattern (CHAR(5)) AS field LIKE :pattern… • To retrieve the row in all cases, apply the TRIM function to the pattern string (TRIM (TRAILING FROM :pattern) ), or the macro parameter can be defined as VARCHAR. These two methods do not always return the same results.TRIM removes pad characters, while the VARCHAR method maintains the data pattern exactly as entered. Example 1 The following example uses the LIKE predicate to select a list of employees whose job title contains the string “Pres”: SELECT Name, DeptNo, JobTitle FROM Employee WHERE JobTitle LIKE '%Pres%' ; The form %string% requires Teradata Database to examine much of each string x. If x is long and there are many rows in the table, the search for qualifying rows may take a long time. The result returned is: Example 2 This example selects a list of all employees whose last name begins with the letter P. SELECT Name FROM Employee WHERE Name LIKE 'P%'; The result returned is: Name ---------- Phan A Peterson J Example 3 This example uses the % and _ characters to select a list of employees with the letter A as the second letter in the last name. The length of the return string may be two or more characters. Name DeptNo JobTitle Watson L 500 Vice President Phan A 300 Vice President Russel S 300 President Chapter 13: Logical Predicates LIKE SQL Functions, Operators, Expressions, and Predicates 599 SELECT Name FROM Employee WHERE Name LIKE '_a%'; returns the result: Name ---------- Marston A Watson L Carter J Replacing _a% with _a_ changes the search to a three-character string with the letter a as the second character. Because none of the names in the Employee table fit this description, the query returns no rows. Both leading and trailing pad characters in a pattern are significant to the matching rules. Example 4 LIKE ’??Z%’ locates only those fields that start with two pad characters followed by Z. ANY/ALL/SOME Quantifiers SQL recognizes the quantifiers ANY (or SOME) and ALL. A quantifier allows one or more expressions to be compared with one or more values such as shown by the following generic example. The ALL quantifier is the logical statement FOR ?. The ANY quantifier is the logical statement FOR ? . The following table restates this. IF you specify this quantifier … THEN the search condition is satisfied if expression LIKE pattern_string … is true for … ALL every string in the list. ANY any string in the list. THIS expression … IS equivalent to this expression … x LIKE ALL ('A%','%B','%C%') x LIKE 'A%' AND x LIKE '%B' AND x LIKE '%C%' x LIKE ANY ('A%','%B','%C%') x LIKE 'A%' OR x LIKE '%B' OR x LIKE '%C%' FF07D273 expression LIKE quantifier ( pattern_string ) , Chapter 13: Logical Predicates LIKE 600 SQL Functions, Operators, Expressions, and Predicates The following statement selects from the employee table the row of any employee whose job title includes the characters “Pres” or begins with the characters “Man”: SELECT * FROM Employee WHERE JobTitle LIKE ANY ('%Pres%', 'Man%'); The result of this statement is: For the following forms, if you specify the ALL or ANY/SOME quantifier, then the subquery may return none, one, or several rows. If, however, a quantifier is not used, then the subquery must return either no value or a single value as described in the following table. Example The following statement uses the ANY quantifier to retrieve every row from the Project table, which contains either the Accounts Payable or the Accounts Receivable project code: SELECT * FROM Project WHERE Proj_Id LIKE ANY (SELECT Proj_Id FROM Charges WHERE Proj_Id LIKE ANY ('A%')) ; EmpNo Name DeptNo JobTitle Salary 10021 Smith T 700 Manager 45, 000.00 10008 Phan A 300 Vice Pres 55, 000.00 10007 Aguilar J 600 Manager 45, 000.00 10018 Russell S 300 President 65, 000.00 10012 Watson L 500 Vice Pres 56, 000.00 This expression … Is TRUE when expression matches … expression LIKE (subquery) the single value returned by subquery. expression LIKE ANY (subquery) at least one value of the set of values returned by subquery; is false if subquery returns no values. expression LIKE ALL (subquery) each individual value in the set of values returned by subquery, and is true if subquery returns no values. FF07D274 NOT expression LIKE ( subquery ) quantifier , NOT ( expression ) LIKE ( subquery ) quantifier Chapter 13: Logical Predicates LIKE SQL Functions, Operators, Expressions, and Predicates 601 subquery If the following form is used, the subquery might return none, one, or several values. The following example shows how you can match using patterns selected from another table. There are two base tables. Department_Proj has two columns: Proj_pattern and Department. The rows in this table look like the following. The following query uses LIKE to match patterns selected from the Department_Proj table to select all rows in the Project table that have a Proj_Id that matches project patterns associated with the Finance department as defined in the Department_Proj table. SELECT * FROM Project WHERE Proj_Id LIKE ANY (SELECT Proj_Pattern FROM Department_Proj WHERE Department = 'Finance'); When this syntax is used, the subquery must select the same number of expressions as are in the expression list. This table … Defines these things … Project • Unique project ID • Project description Department_Proj The association between project ID patterns and departments. Proj_pattern Department AP% Finance AR% Finance Nut% R&D Screw% R&D HH01A045 NOT expr LIKE quantifier ( subquery ) HH01A046 NOT ( expr ) LIKE ( subquery ) , quantifier Chapter 13: Logical Predicates LIKE 602 SQL Functions, Operators, Expressions, and Predicates For example: (x,y) LIKE ALL (SELECT a,b FROM c) is equivalent to: (x LIKE c.a) AND (y LIKE c.b) Behavior of the ESCAPE Character When escape_character is used in (generic) string_2, it must be followed immediately by a metacharacter of the appropriate server character set or another escape_character. The resultant two-character sequence matches a single character in string_1 if and only if the character in string_1 collates identically to the character following the escape_character in string_2. In other words, escape_character is ignored for matching purposes and the character following escape_character is matched for a single occurrence of itself. When string_1 and string_2 do not share a common server character set, then the valid metacharacters are SPACING UNDERSCORE and PERCENT SIGN because the arguments are translated to UNICODE automatically when mismatched. Their behavior then follows the rules described in “Implicit Character-to-Character Translation” on page 765. Miscellaneous Examples KanjiEBCDIC Examples The following examples indicate the behavior of LIKE with KanjiEBCDIC strings using the function (expression LIKE pattern_expression). Function Result _KanjiSJIS ‘92 abc’ LIKE _Unicode ‘%abc’ TRUE _KanjiSJIS ‘92 abc’ LIKE _Unicode ‘%abc’ FALSEa a. % (FULLWIDTH PERCENT SIGN) is not a metacharacter in either KanjiSJIS or Unicode. ‘c%’ LIKE ‘c%%’ ESCAPE ‘%’ TRUE ‘c%’ LIKE ‘c%%’ ESCAPE ‘%’ FALSEb b. % (FULLWIDTH PERCENT SIGN) does not match % (PERCENT SIGN). expression pattern_expression Server Character Set Result MN % KANJI1 TRUE MNP <%B>% KANJI1 TRUE MNP %P KANJI1 TRUE Chapter 13: Logical Predicates LIKE SQL Functions, Operators, Expressions, and Predicates 603 KanjiEUC Examples The following examples indicate the behavior of LIKE with KanjiEUC strings using the function (expression LIKE pattern_expression). KanjiShift-JIS Examples The following examples indicate the behavior of LIKE with KanjiShift-JIS strings using the function (expression LIKE pattern_expression). MNP %<__C>% KANJI1 FALSE __ represents a FULLWIDTH UNDERSCORE. expression pattern_expression Server Character Set Result expression pattern_expression Server Character Set Result ss3A ss2B ss3C ss2D % ss2B% UNICODE TRUE M ss2B N ss2D M __% GRAPHIC TRUE ss3A ss2B ss3C ss2D __% KANJISJIS TRUE ss3A ss2B ss3C ss2D _ % KANJISJIS TRUE __ represents a FULLWIDTH UNDERSCORE. _ represents a SPACING UNDERSCORE. expression pattern_expression Server Character Set ANSI Mode Result Teradata Mode Result ABCD __B% GRAPHIC TRUE TRUE mnABCI %B% UNICODE TRUE TRUE mnABCI %I UNICODE TRUE TRUE mnABCI mn_%I KANJI1 TRUE The underscore in pattern_expression matches a single byteor multibyte character in ANSI mode. FALSE The underscore in pattern_expression matches a single byte character in Teradata mode. mnABCI mn__%I KANJI1 TRUE TRUE __ represents a FULLWIDTH UNDERSCORE. _ represents a SPACING UNDERSCORE. Chapter 13: Logical Predicates OVERLAPS 604 SQL Functions, Operators, Expressions, and Predicates OVERLAPS Purpose Tests whether two time periods overlap one another. Syntax where: ANSI Compliance OVERLAPS is ANSI SQL:2008 compliant. Time Periods Each time period to the left and right of the OVERLAPS keyword is one of the following expression types: • DateTime, DateTime • DateTime, Interval • Row subquery • Period Each time period represents a start and end DateTime, using an explicit Period value, DateTime values or a DateTime and an Interval. Syntax element … Specifies … datetime_expression a start and end DateTime. interval_expression an end DateTime. row_subquery an element of a row subquery in a SELECT statement. The subquery cannot specify a SELECT AND CONSUME statement. period_expression any expression that evaluates to a Period data type. 1101A612 ( datetime_expression, datetime_expression OVERLAPS datetime_expression, datetime_expression datetime_expression, interval_expression ) ( ) period_expression period_expression row_subquery datetime_expression, interval_expression row_subquery Chapter 13: Logical Predicates OVERLAPS SQL Functions, Operators, Expressions, and Predicates 605 If the start and end DateTime values in a time period are not ordered chronologically, they are manipulated to make them so prior to making the comparison, using the rule that end_DateTime >= start_DateTime for all cases. If a time period contains a null start_DateTime and a non-null end_DateTime, then the values are switched to indicate a non-null start_DateTime and a null end_DateTime. If both time periods have a Period data type, the data types must be comparable. If only one time period is a Period type, the other time period must evaluate to a DateTime type that is comparable to the element type of the Period. Note: Implicit casting to a Period data type is not supported. Results Consider the general case of an OVERLAPS comparison, stated as follows. (S1, E1) OVERLAPS (S2, E2) The result of OVERLAPS is as follows. (S1 > S2 AND NOT (S1 >= E2 AND E1 >= E2)) OR (S2 > S1 AND NOT (S2 >= E1 AND E2 >= E1)) OR (S1 = S2 AND (E1 = E2 OR E1 <> E2)) For Period data types, where p1 is the first Period expression and p2 is the second Period expression, the values of S1, E1, S2, and E2 are as follows: S1 = BEGIN(p1) E1 = END(p1) S2 = BEGIN(p2) E2 = END(p2) Rules The following rules apply to the OVERLAPS comparison. • When you specify two DateTime types, they must be comparable. • When you specify two Period types, they must be comparable. • If the first columns of each left and right time periods are DateTime types, they must have the same data type: both DATE, both TIME, or both TIMESTAMP. • If only one time period is a Period type, the first column of the other time period must have the same data type as the element type of the Period. • If neither time period is a Period type, then the second column of each left and right time period must either be the same DateTime type as its corresponding first column (that is, the two types must be compatible) or it must be an Interval type that involves only DateTime fields where the precision is such that its value can be added to that of the corresponding DateTime type. Chapter 13: Logical Predicates OVERLAPS 606 SQL Functions, Operators, Expressions, and Predicates Example 1 The following example compares two time spans that share a single common point, CURRENT_TIME. The result returned is FALSE because when two time spans share a single point, they do not overlap by definition. SELECT 'OVERLAPS' WHERE (CURRENT_TIME(0), INTERVAL '1' HOUR) OVERLAPS (CURRENT_TIME(0), INTERVAL -'1' HOUR); Example 2 The following example is nearly identical to the previous one, except that the arguments have been adjusted to overlap by one second. The result is TRUE and the value ‘OVERLAPS’ is returned. SELECT 'OVERLAPS' WHERE (CURRENT_TIME(0), INTERVAL '1' HOUR) OVERLAPS (CURRENT_TIME(0) + INTERVAL '1' SECOND,INTERVAL -'1' HOUR); Example 3 Here is an example that uses the datetime_expression, datetime_expression form of OVERLAPS. The two DATE periods overlap each other, so the result is TRUE. SELECT 'OVERLAPS' WHERE (DATE '2000-01-15',DATE '2002-12-15') OVERLAPS (DATE '2001-06-15',DATE '2005-06-15'); Example 4 The following example is the same as the previous one, but in row_subquery form: SELECT 'OVERLAPS' WHERE (SELECT DATE '2000-01-15', DATE '2002-12-15') OVERLAPS (SELECT DATE '2001-06-15', DATE '2005-06-15'); Example 5 The null value in the following example means the second datetime_expression has a start time of 2001-06-13 15:00:00 and a null end time. SELECT 'OVERLAPS' WHERE (TIMESTAMP '2001-06-12 10:00:00', TIMESTAMP '2001-06-15 08:00:00') OVERLAPS (TIMESTAMP '2001-06-13 15:00:00', NULL); Because the start time for the second expression falls within the TIMESTAMP interval defined by the first expression, the result is TRUE. Example 6 In the following example, the OVERLAPS predicate operates on PERIOD(DATE) columns. SELECT * FROM employee WHERE period2 OVERLAPS period1; Chapter 13: Logical Predicates OVERLAPS SQL Functions, Operators, Expressions, and Predicates 607 Assume the query is executed on the following table employee; where period1 and period2 are PERIOD(DATE) columns: The result is as follows: Ename period1 period2 Adams ('2005-02-03', '2006-02-03') ('2005-02-03', '2006-02-03') Mary ('2005-04-02', '2006-01-03') ('2005-02-03', '2006-02-03') Jones ('2004-01-02', '2004-03-05') ('2004-03-05', '2004-10-07') Randy ('2004-01-02', '2004-03-05') ('2004-03-07', '2004-10-07') Simon ? ('2005-02-03', '2005-07-27') Ename period1 period2 Adams ('2005-02-03', '2006-02-03') ('2005-02-03', '2006-02-03') Mary ('2005-04-02', '2006-01-03') ('2005-02-03', '2006-02-03') Chapter 13: Logical Predicates Logical Operators and Search Conditions 608 SQL Functions, Operators, Expressions, and Predicates Logical Operators and Search Conditions Purpose Specify the criteria for logically producing the result of a search condition. Definition: Logical Operator An operator applied to the result of a predicate to determine the result of a search condition. The logical operators are: • AND • NOT • OR For example: Use NOT to negate an expression, for example: Definition: Search Condition A search condition, or conditional expression, consists of one or more conditional terms connected by one or more of the following logical predicates: • Comparison operators • [NOT] BETWEEN • LIKE • [NOT] IN • ALL or ANY/SOME • [NOT] EXISTS • OVERLAPS • IS [NOT] NULL Where To Use Search Conditions A search condition can be used in various SQL clauses such as WHERE, ON, QUALIFY, RESET WHEN, or HAVING. FF07D220 expression_1 OR expression_2 OR expression_3 FF07D221 expression_1 AND NOT expression_2 Chapter 13: Logical Predicates Logical Operators and Search Conditions SQL Functions, Operators, Expressions, and Predicates 609 When used in a HAVING clause, a logical expression can be used with an aggregate operator. For example, the following query uses a search condition in a HAVING clause to select from the Employee table those departments with the number 100, 300, 500, or 600, and with a salary average of at least $35,000 but not more than $55,000: SELECT AVG(Salary) FROM Employee WHERE DeptNo IN (100,300,500,600) GROUP BY DeptNo HAVING AVG(Salary) BETWEEN 35000 AND 55000 ; Rules for Order of Evaluation The following rules apply to evaluation order for conditional expressions: • If an expression contains more than one of the same operator, the evaluation precedence is left to right. • If an expression contains a combination of logical operators, the order of evaluation is as follows: • Parentheses can be used to establish the desired evaluation precedence. • The logical expressions in a conditional expression are not always evaluated left to right. Avoid using a conditional expression if its accuracy depends on the order in which its logical expressions are evaluated. For example, compare the following two expressions: F2/(NULLIF(F1,0)) > 500 F1 <> 0 AND F2/F1 > 500 The first expression guarantees exclusion of division by zero. The second allows the possibility of error, because the order of its evaluation determines the exclusion of zeros. Evaluation Results Each logical expression in a conditional expression evaluates to one of three results: • TRUE • FALSE • UNKNOWN 1 NOT 2 AND 3 OR Chapter 13: Logical Predicates Logical Operators and Search Conditions 610 SQL Functions, Operators, Expressions, and Predicates AND Truth Table The following table illustrates the AND logic used in evaluating search conditions. OR Truth Table The following table illustrates the OR logic used in evaluating search conditions. NOT Truth Table The following table illustrates the NOT logic used in evaluating search conditions. Subquery Restrictions Predicates in search conditions cannot specify SELECT AND CONSUME statements in subqueries. Examples of Logical Operators in Search Conditions The following examples illustrate the use of logical operators in search conditions. x FALSE x UNKNOWN x TRUE y FALSE FALSE FALSE FALSE y UNKNOWN FALSE UNKNOWN UNKNOWN y TRUE FALSE UNKNOWN TRUE x FALSE x UNKNOWN x TRUE y FALSE FALSE UNKNOWN TRUE y UNKNOWN UNKNOWN UNKNOWN TRUE y TRUE TRUE TRUE TRUE Result x FALSE TRUE x UNKNOWN UNKNOWN x TRUE FALSE Chapter 13: Logical Predicates Logical Operators and Search Conditions SQL Functions, Operators, Expressions, and Predicates 611 Example 1 The following example uses a search condition to select from a user table named Profile the names of applicants who have either more than two years of experience or at least twelve years of schooling with a high school diploma: SELECT Name FROM Profile WHERE YrsExp > 2 OR (EdLev >= 12 AND Grad = 'Y') ; Example 2 The following statement requests a list of all the employees who report to manager number 10007 or manager number 10012. The manager information is contained in the Department table, while the employee information is contained in the Employee table. The request is processed by joining the tables on DeptNo, their common column. DeptNo must be fully qualified in every reference to avoid ambiguity and an extra set of parentheses is needed to group the ORed IN conditions. Without them, the result is a Cartesian product. SELECT EmpNo,Name,JobTitle,Employee.DeptNo,Loc FROM Employee,Department WHERE (Employee.DeptNo=Department.DeptNo) AND ((Employee.DeptNo IN (SELECT Department.DeptNo FROM Department WHERE MgrNo=10007)) OR (Employee.DeptNo IN (SELECT Department.DeptNo FROM Department WHERE MgrNo=10012))) ; Assuming that the Department table contains the following rows: DeptNo Department Loc MgrNo 100 Administration NYC 10005 600 Manufacturing CHI 10007 500 Engineering ATL 10012 300 Exec Office NYC 10018 700 Marketing NYC 10021 Chapter 13: Logical Predicates Logical Operators and Search Conditions 612 SQL Functions, Operators, Expressions, and Predicates The join statement returns: EmpNo Name JobTitle DeptNo Loc 10012 Watson L Vice Pres 500 ATL 10004 Smith T Engineer 500 ATL 10014 Inglis C Tech Writer 500 ATL 10009 Marston A Secretary 500 ATL 10006 Kemper R Assembler 600 CHI 10015 Omura H Programmer 500 ATL 10007 Aguilar J Manager 600 CHI 10010 Reed C Technician 500 ATL 10013 Regan R Purchaser 600 CHI 10016 Carter J Engineer 500 ATL 10019 Newman P Test Tech 600 CHI SQL Functions, Operators, Expressions, and Predicates 613 CHAPTER 14 Attribute Functions This chapter describes SQL attribute functions. Attribute Functions Attribute functions return descriptive information about their operand. Except for the DEFAULT function, the operand need not be a column reference; it can be a general expression that is not evaluated mathematically. When an attribute function is used in a request, the response returns one row for every data row that meets the search condition. Some of these functions are extensions to ANSI SQL. For a list of data type attributes, see “Data Type Phrases” in SQL Data Types and Literals. Each attribute function is described individually in the following topics. ANSI Equivalence of Teradata Attribute Functions Several of the Teradata attribute functions are extensions to the ANSI SQL:2008 standard. To maintain ANSI compatibility, use the ANSI equivalent functions instead of Teradata attribute functions, when available. The following Teradata functions have no ANSI equivalents: • BYTES • FORMAT • TYPE Change this Teradata function … To this ANSI function in new applications … CHARACTERS CHARS CHAR CHARACTER_LENGTH MCHARACTERS† † This function is no longer documented because its use is deprecated and it will no longer be supported after support for KANJI1 is dropped. Chapter 14: Attribute Functions BYTES 614 SQL Functions, Operators, Expressions, and Predicates BYTES Purpose Returns the number of bytes contained in the specified byte string. Syntax where: ANSI Compliance BYTES is a Teradata extension to the ANSI SQL:2008 standard. Argument Types The data types of byte_expression are restricted to the following: • BYTE, VARBYTE and BLOB • UDT that has an implicit cast to a predefined byte type To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including BYTES, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” Length Includes Trailing Zeros Because trailing double zero bytes are considered bytes, the length of the value in a fixed length column is always equal to the length defined for the column. The length of the value in a variable length column is always equal to the number of bytes, including any trailing double zero bytes, contained in that value. Syntax element … Specifies … byte_expression the byte string for which the number of bytes is to be returned. 1101F174 BYTE ( byte_expression BYTES ( Chapter 14: Attribute Functions BYTES SQL Functions, Operators, Expressions, and Predicates 615 If you do not want trailing blanks included in the byte count for a data value, use the TRIM function on the argument to BYTES. For example: SELECT BYTES( TRIM( TRAILING FROM byte_col ) ) FROM table1; For more information on TRIM, see “TRIM” on page 549. Example The following statement applies the BYTES function to the BadgePic column, which is type VARBYTE(32000), to obtain the number of bytes in each badge picture. SELECT BadgePic, BYTES(BadgePic) FROM Employee; The result is as follows: BadgePic Bytes(BadgePic) -------------- --------------- 20003BA0 4 9A3243F805 5 EEFF08C3441900 7 Chapter 14: Attribute Functions CHARACTER_LENGTH 616 SQL Functions, Operators, Expressions, and Predicates CHARACTER_LENGTH Purpose Returns the length of a string either in logical characters or in bytes. Syntax where: ANSI Compliance CHARACTER_LENGTH is ANSI SQL:2008 compliant. Usage Notes CHARACTER_LENGTH is the ANSI form of the Teradata CHARACTERS function. Use CHARACTER_LENGTH instead of CHARACTERS for ANSI SQL:2008 conformance. Use CHARACTER_LENGTH in place of MCHARACTERS. (MCHARACTERS no longer appears in this book because its use is deprecated and it will not be supported after support for KANJI1 is dropped.) Argument Types The type of string_expression must be CHARACTER, VARCHAR, or CLOB. For noncharacter data types, the function returns an error. By default, Teradata Database performs implicit type conversion on a UDT argument that has an implicit cast that casts between the UDT and a predefined character type. To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including CHARACTER_LENGTH, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. Syntax element … Specifies … string_expression the string expression for which the length is to be returned. FF07D088 CHARACTER_LENGTH (string_expression) CHAR_LENGTH Chapter 14: Attribute Functions CHARACTER_LENGTH SQL Functions, Operators, Expressions, and Predicates 617 For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” Result For all server character sets except KANJI1, CHARACTER_LENGTH returns the length of string_expression in characters. For KANJI1, the following results are obtained. Because trailing pad characters are considered characters, the length of the value in a CHARACTER column is always equal to the length defined for the column. The length of the value in a VARCHAR or CLOB column is always equal to the number of characters, including any trailing pad characters, contained in that value. Suppressing Trailing Pad Characters To suppress trailing pad characters from the character count for a data value, use the TRIM function on the argument to CHARACTER_LENGTH. For example: SELECT CHARACTER_LENGTH( TRIM( TRAILING FROM Name ) ) FROM Employee; Example The following statement applies the CHARACTER_LENGTH function to the Name column, which is type VARCHAR(30) CHARACTER SET LATIN, to obtain the number of characters in each employee name: SELECT Name, CHARACTER_LENGTH(Name) FROM Employee; FOR this client character set … CHARACTER_LENGTH returns … KanjiEBCDIC the length of string_expression as the number of bytes. A mix of single and multibyte characters is expected. If any Shift-Out/Shift-In characters are present, they are included in the result count. KanjiEUC KanjiShift-JIS the length of string_expression as the number of logical characters, based on the client session character set. A mix of single and multibyte characters is expected. ASCII EBCDIC the length of string_expression as the number of bytes. Chapter 14: Attribute Functions CHARACTER_LENGTH 618 SQL Functions, Operators, Expressions, and Predicates The result is as follows (note that separator blanks are considered characters): Name Character_Length(Name) -------- ---------------------- Smith T 7 Newman P 8 Omura H 7 . . Example Set 1: KanjiEBCDIC Example Set 2: KanjiShift-JIS Example Set 3: KanjiEUC FOR this server character set … AND example … CHARACTER_LENGTH returns … GRAPHIC ABC 3 KANJI1 De 10 <><> 4 FOR this server character set … AND example … CHARACTER_LENGTH returns … KANJI1 <><> 10 DeF 3 UNICODE ABC 3 GRAPHIC ABC 3 FOR this server character set … AND example … CHARACTER_LENGTH returns … KANJI1 ss3Css3D 2 GRAPHIC 2 UNICODE <><> 0 dA ss2B ss3E 4 LATIN ABC 3 Chapter 14: Attribute Functions CHARACTERS SQL Functions, Operators, Expressions, and Predicates 619 CHARACTERS Purpose Returns an integer value representing the number of logical characters or bytes contained in the specified operand string. Syntax where: ANSI Compliance CHARACTERS is a Teradata extension to the ANSI SQL-99standard. Value Returned by CHARACTERS and Server Character Set Because CHARACTERS returns the number of logical characters or bytes in string_expression, the value differs depending on the server character set of string_expression. The following table illustrates the differences among the various character sets for a CHARACTER(12) column. Syntax element … Specifies … string_expression a character (single byte, multibyte, mixed single byte and multibyte) string for which the number of characters is to be returned. The data types for string_expression are restricted to CHARACTER, VARCHAR, and CLOB. 1101A488 CHARACTERS ( string_expression ) CHARS CHAR FOR this server character set … The length of string_expression … • UNICODE • LATIN • GRAPHIC is always 12. Unicode, Latin, and Graphic are fixed width character types. • KANJISJIS • KANJI1 varies depending on the mix of characters (multibyte and single byte) in the string. KanjiSJIS and KANJI1 are variable width character sets. Chapter 14: Attribute Functions CHARACTERS 620 SQL Functions, Operators, Expressions, and Predicates CHARACTER_LENGTH versus CHARACTERS Use of the CHARACTERS function is deprecated. Instead, use the ANSI-equivalent “CHARACTER_LENGTH.” Chapter 14: Attribute Functions DEFAULT SQL Functions, Operators, Expressions, and Predicates 621 DEFAULT Purpose Returns the current default value for the specified or derived column. Syntax where: ANSI Compliance DEFAULT is partially ANSI SQL:2008 compliant. The form of DEFAULT that specifies a column name is a Teradata extension. Using DEFAULT in a predicate is also a Teradata extension. Result Type and Attributes The result type, format, and title for DEFAULT(x) appear in the following table. For information on data type default formats, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Result Value The DEFAULT function returns the default value of the specified column or derived column (if the column name is omitted). If the specified or derived column is a view column or derived table column, the DEFAULT function returns the default value of the underlying table column. Syntax element … Specifies … column_name the name of a column in a base table, view, queue table, or derived table. The column name can be qualified or unqualified. 1101A394 DEFAULT ( column_name ) Data Type Format Title Data type of the specified column Format of the specified column Default(x) Chapter 14: Attribute Functions DEFAULT 622 SQL Functions, Operators, Expressions, and Predicates If the default value of a column evaluates to a system variable, for example when the default value is CURRENT_TIME or USER, the DEFAULT function returns the value of the system variable at the time the statement is executed. DEFAULT returns null when any of the following conditions are true: • The specified or derived column was defined with a DEFAULT NULL phrase • The specified or derived column has no explicit default value • The data type of the specified or derived column is UDT • The specified or derived column is the name of a view column that is derived from a single underlying table column that has no explicit default value For an example, see “Example 3: Specifying a View Column Name” on page 624. • The specified or derived column is the name of a view column that is not derived from a single underlying table column, for example, the view column is derived from a constant expression Omitting the Column Name You can use the form of DEFAULT that omits the column name under certain conditions in an INSERT, UPDATE, or MERGE statement or in a predicate clause that involves a comparison operation. The form of DEFAULT that omits the column name cannot be part of an expression. When the DEFAULT function does not specify a column name, Teradata Database derives the column based on context. For example, consider the following table definition: CREATE TABLE Manager (Emp_ID INTEGER ,Dept_No INTEGER DEFAULT 99 ); The following INSERT statement uses DEFAULT without a column name to insert the default value into the Dept_No column: INSERT INTO Manager VALUES (103499, DEFAULT); Using the DEFAULT function without specifying a column name can produce an error if Teradata Database cannot derive the column context. For an example that omits the column name when using the DEFAULT function in a predicate clause that involves a comparison operation, see “Example 2: Using DEFAULT in a Predicate” on page 623. For details on using the DEFAULT function in INSERT, UPDATE, and MERGE statements, see SQL Data Manipulation Language. Using a Qualified Column Name If you specify a qualified column name that includes the name of the table, you can use DEFAULT in a SELECT statement that has no FROM clause. For example, you can use the following statement to get the default value of the Dept_No column in the Manager table: SELECT DEFAULT(Manager.Dept_No); Chapter 14: Attribute Functions DEFAULT SQL Functions, Operators, Expressions, and Predicates 623 Restrictions The DEFAULT function cannot be used as a partitioning expression for defining PPIs. Error Conditions Using the DEFAULT function can result in an error when any of the following conditions are true: • The column name is omitted and Teradata Database cannot derive the column context • The DEFAULT function appears in a partitioning expression for defining PPIs • The column name is omitted and the DEFAULT function appears in an expression that does not support the DEFAULT function without a column name • The DEFAULT function appears in an expression for which the result type is incompatible For example, consider the following table definition: CREATE TABLE Parts_Table (Part_Code INTEGER DEFAULT 9999 ,Part_Name CHAR(20) ); The following statement results in an error because the result type of the DEFAULT function is not compatible with the column to which the result is being compared: SELECT * FROM Parts_Table WHERE Part_Name = DEFAULT(Part_Code); Example 1: Inserting the Default Value Under Certain Conditions Consider the following Employee table definition: CREATE TABLE Employee (Emp_ID INTEGER ,Last_Name VARCHAR(30) ,First_Name VARCHAR(30) ,Dept_No INTEGER DEFAULT 99 ); The following statement uses DEFAULT to insert the default value of the Dept_No column when the supplied value is negative. USING (id INTEGER, n1 VARCHAR(30), n2 VARCHAR(30), dept INTEGER) INSERT INTO Employee VALUES (:id ,:n1 ,:n2 ,CASE WHEN (:dept < 0) THEN DEFAULT(Dept_No) ELSE :dept END ); Example 2: Using DEFAULT in a Predicate The following statement uses DEFAULT to compare the values of the Dept_No column with the default value of the Dept_No column. Because the comparison operation involves a single column reference, Teradata Database can derive the column context of the DEFAULT function even though the column name is omitted. SELECT * FROM Employee WHERE Dept_No < DEFAULT; Chapter 14: Attribute Functions DEFAULT 624 SQL Functions, Operators, Expressions, and Predicates Note that if the DEFAULT function evaluates to null, the predicate is unknown and the WHERE condition is false. Example 3: Specifying a View Column Name Consider the DBC.HostsInfo system view, which has the following definition: REPLACE VIEW DBC.HostsInfo (LogicalHostId, HostName, DefaultCharSet) AS SELECT LogicalHostId ,HostName ,DefaultCharSet FROM DBC.Hosts WITH CHECK OPTION; The underlying table, DBC.Hosts, has the following definition: CREATE SET TABLE DBC.Hosts, FALLBACK, NO BEFORE JOURNAL, NO AFTER JOURNAL, CHECKSUM = DEFAULT (LogicalHostId SMALLINT FORMAT 'ZZZ9' NOT NULL ,HostName VARCHAR(128) CHARACTER SET UNICODE NOT CASESPECIFIC NOT NULL ,DefaultCharSet VARCHAR(128) CHARACTER SET UNICODE NOT CASESPECIFIC NOT NULL) UNIQUE PRIMARY INDEX (LogicalHostId) UNIQUE INDEX (HostName); The following statement uses the DEFAULT function with the DBC.HostsInfo.HostName view column name: SELECT DISTINCT DEFAULT(HostName) FROM DBC.HostsInfo; The result of the DEFAULT function is null because the HostName view column is derived from a table column that has no explicit default value. Related Topics For information on … See … using predicates Chapter 13: “Logical Predicates.” comparison operations in predicates Chapter 5: “Comparison Operators.” the DEFAULT value control phrase SQL Data Types and Literals. INSERT, UPDATE, and MERGE statements SQL Data Manipulation Language. Chapter 14: Attribute Functions FORMAT SQL Functions, Operators, Expressions, and Predicates 625 FORMAT Purpose Returns the declared format for the named expression. Syntax where: ANSI Compliance FORMAT is a Teradata extension to the ANSI SQL:2008 standard. Result Type FORMAT returns a CHAR(n) character string of up to 30 characters. Example The following statement requests the format of the Salary column in the Employee table. SELECT FORMAT(Employee.Salary); The result is the following. Format(Salary) ------------------------------ ZZZ,ZZ9.99 Syntax element … Specifies … expression the expression for which the FORMAT is to be reported. 1101A489 FORMAT (column_name ) Chapter 14: Attribute Functions OCTET_LENGTH 626 SQL Functions, Operators, Expressions, and Predicates OCTET_LENGTH Purpose Returns the length of string_expression in octets when it is converted to the named character set (taking the export width value into consideration). Syntax where: ANSI Compliance OCTET_LENGTH is ANSI SQL:2008 compliant. Argument Types The data type of string_expression must be one of the following: • CHARACTER or VARCHAR • UDT that has an implicit cast to a predefined character type To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including OCTET_LENGTH, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion of UDTs, see Chapter 20: “Data Type Conversions.” Syntax element … Specifies … string_expression the character string for which the number of octets is required. character_set_name the character set in which the result is to be returned. If character_set_name is not provided, the session character set is assumed. See the list of Teradata-provided character sets in the table on “Usage Notes” on page 627. 1101A513 OCTET_LENGTH ( string_expression ) , character_set_name Chapter 14: Attribute Functions OCTET_LENGTH SQL Functions, Operators, Expressions, and Predicates 627 Usage Notes Any Shift-Out/Shift-In and trailing GRAPHIC pad characters are included in the result count. OCTET_LENGTH operates in the same manner in both Teradata and ANSI modes. The following table lists the client character sets shipped with Teradata. Although these character sets are shipped with the system, your system administrator must install them individually to become available for use. Your site might also have site-defined character sets. Check with your system administrator for a complete list of character sets available at your site. IF string_expression is … THEN … of type KANJI1 the result is independent of character_set_name. not CHARACTER data an error is generated. Character Sets Where Found • ASCII • EBCDIC • UTF8 • UTF16 Built-in • ARABIC1256_6A0 a • CYRILLIC1251_2A0 a • EBCDIC037_0E • EBCDIC273_0E • EBCDIC277_0E • HANGUL949_7R0 a • HANGULEBCDIC933_1II • HANGULKSC5601_2R4 • HEBREW1255_5A0 a • KANJI932_1S0 a • KANJIEBCDIC5026_0I • KANJIEBCDIC5035_0I • KANJIEUC_0U • KANJISJIS_0S • KATAKANAEBCDIC a. Windows code page compatible session character set • LATIN1250_1A0 a • LATIN1252_0A • LATIN1252_3A0 a • LATIN1254_7A0 a • LATIN1258_8A0 a • LATIN1_0A • LATIN9_0A • SCHEBCDIC935_2IJ • SCHGB2312_1T0 • SCHINESE936_6R0 a • TCHBIG5_1R0 • TCHEBCDIC937_3IB • TCHINESE950_8R0 a • THAI874_4A0 a DBC.CharTranslationsV Chapter 14: Attribute Functions OCTET_LENGTH 628 SQL Functions, Operators, Expressions, and Predicates Examples Examples of output from OCTET_LENGTH appear in the following table. Client Character Set Server Character Set string_expression Result EBCDIC LATIN abcdefgh 8 ASCII KANJI1 abcdefgh 8 KanjiEBCDIC KANJI1 ABP 11 KanjiEBCDIC GRAPHIC MNOP 8 (record mode) 10 (field mode) KanjiEUC KANJISJIS dA ss2B ss3E 8 KanjiShift-JIS KANJISJIS DeF 5 KanjiShift-JIS UNICODE ABC 6 Chapter 14: Attribute Functions TITLE SQL Functions, Operators, Expressions, and Predicates 629 TITLE Purpose Returns the title of an expression as it would appear in the heading for displayed or printed results. Syntax where: ANSI Compliance TITLE is a Teradata extension to the ANSI SQL:2008 standard. Result Type TITLE returns a CHAR(n) character string of up to 60 characters. Usage Notes Use the TITLE phrase to change the heading for displayed or printed results that is different from the column name, which is the default heading. For more information, see SQL Data Types and Literals. Example The following statement requests the title of the Salary column in the Employee table. SELECT TITLE(Employee.Salary); The result is the following. Title(Salary) ------------------------------------------------------------ Salary Syntax element … Specifies … expression the expression for which the title is to be returned. 1101B039 TITLE ( expression ) Chapter 14: Attribute Functions TYPE 630 SQL Functions, Operators, Expressions, and Predicates TYPE Purpose Returns the data type defined for an expression. Syntax where: ANSI Compliance TYPE is a Teradata extension to the ANSI SQL:2008 standard. Result Type and Value TYPE returns a CHAR(n) character string that contains the name of the data type of the expression. For a list of the supported data types, see SQL Data Types and Literals. For information on geospatial types, see SQL Geospatial Types. When the argument is a function or operation, TYPE returns a character string that contains the result type of the function or operation. For rules on the result type for an operation or function, refer to the documentation for the specific function or operation. Character Type Arguments If the server character set for a character type argument is different from the user default server character set, then the resulting character string also contains the CHARACTER SET phrase and the name of the server character set for the argument. For examples, see “Example 1” and “Example 2” on page 631. Example 1 Consider the Name column in the following table definition: CREATE TABLE Employee (EmployeeID INTEGER ,Name CHARACTER(30) CHARACTER SET LATIN Syntax element … Specifies … expression the expression for which the data type is to be returned. 1101A491 TYPE ( expression ) Chapter 14: Attribute Functions TYPE SQL Functions, Operators, Expressions, and Predicates 631 ,Salary DECIMAL(8,2)); If the user default server character set is LATIN, then the character string that TYPE returns for the Name column does not contain the CHARACTER SET phrase. SELECT TYPE(Employee.Name); Type(Name) ---------- CHAR(30) Example 2 If the user default server character set is LATIN, but the server character set for the Name column is UNICODE, then the result string contains the CHARACTER SET phrase. CREATE TABLE Employee (EmployeeID INTEGER ,Name VARCHAR(30) CHARACTER SET UNICODE ,Salary DECIMAL(8,2)); SELECT TYPE(Employee.Name); Type(Name) --------------------------------- VARCHAR(30) CHARACTER SET UNICODE Example 3 The following statement returns the types of the Name and Salary columns: SELECT TYPE(Employee.Name), TYPE(Employee.Salary); Type(Name) Type(Salary) ----------- ------------ VARCHAR(30) DECIMAL(8,2) Example 4 If TYPE is used to request the data type of two columns, defined as GRAPHIC and LONG VARGRAPHIC, respectively, the result is as follows. TYPE(GColName) TYPE(LVGColName) ----------------------------- ------------------------------------ CHAR(4) CHARACTER SET GRAPHIC VARCHAR(32000) CHARACTER SET GRAPHIC In the case of a LONG VARGRAPHIC column, the length returned is the maximum length of 32000. Example 5 Consider the following TYPE function. SELECT TYPE(SUBSTR(Employee.Name,3,2)); The result type of SUBSTR depends on the session mode. Chapter 14: Attribute Functions TYPE 632 SQL Functions, Operators, Expressions, and Predicates If the session is set to ANSI mode, the returned result is as follows: Type(Substr(Name,3,2)) ---------------------- VARCHAR(30) If the session is set to Teradata mode, the returned result is as follows: Type(Substr(Name,3,2)) ---------------------- VARCHAR(2) Example 6 Consider the following table definition: CREATE TABLE images (imageid INTEGER ,imagedesc VARCHAR(50) ,image BLOB(2K)) UNIQUE PRIMARY INDEX (imageid); The following statement applies the TYPE function to the BLOB column: SELECT TYPE(images.image) FROM images; The result is: Type(image) ----------- BLOB(2048) Note that the result is a normal integer length, and does not use the K option that was used to define the BLOB column the CREATE TABLE statement. SQL Functions, Operators, Expressions, and Predicates 633 CHAPTER 15 Hash-Related Functions Hash-related functions return information about the: • Primary or fallback AMP that corresponds to a given hash bucket number • Hash bucket number that corresponds to a given row hash value • Row hash value for the primary index of a row • Highest AMP number • Highest hash bucket number • Maximum value that can be generated by applying the hash function to an unsigned integer Features Use the hash-related functions to identify the statistical properties of the current primary index or secondary index, or to evaluate these properties for other columns to determine their suitability as a future primary index or secondary index. The statistics can help you to minimize hash synonyms and enhance the uniformity of data distribution. Chapter 15: Hash-Related Functions HASHAMP 634 SQL Functions, Operators, Expressions, and Predicates HASHAMP Purpose Returns the identification number of the primary AMP corresponding to the specified hash bucket number. If no hash bucket number is specified, HASHAMP returns one less than the maximum number of AMPs in the system. Syntax where: ANSI Compliance HASHAMP is a Teradata extension to the ANSI SQL:2008 standard. Argument Type and Value The expression argument must evaluate to INTEGER data type where the valid range of values depends on the system setting for the hash bucket size. For information on how to specify the system setting for the hash bucket size, see “DBS Control utility” in Utilities. If expression cannot be implicitly converted to an INTEGER, an error is reported. Syntax element … Specifies … expression an optional expression that evaluates to a valid hash bucket number. For information on obtaining a hash bucket number that you can use for expression, see “HASHBUCKET” on page 640. HH01A027 HASHAMP ( expression ) IF the hash bucket size is … THEN the range of values for expression is … 16 bits 0 to 65535. 20 bits 0 to 1048575. Chapter 15: Hash-Related Functions HASHAMP SQL Functions, Operators, Expressions, and Predicates 635 If expression results in a UDT, Teradata Database performs implicit type conversion on the UDT, provided that the UDT has an implicit cast that casts between the UDT and any of the following predefined types: • Numeric • Character • DATE To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including HASHAMP, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion, see Chapter 20: “Data Type Conversions.” Result For information on the hash map that defines the relationship between hash buckets and primary AMPs, see “Reconfiguration utility” in the Utilities book. Examples The following examples assume a table T with columns column_1, column_2, and an INTEGER column B populated with integer numbers from zero to the maximum number of hash buckets on the system. CREATE TABLE T (column_1 INTEGER ,column_2 INTEGER ,B INTEGER) UNIQUE PRIMARY INDEX (column_1, column_2); IF expression … THEN … evaluates to a valid hash bucket number HASHAMP determines the primary AMP corresponding to the hash bucket and returns the AMP identification number. The result is an INTEGER value that is greater than or equal to zero and less than the maximum number of AMPs in the configuration. does not appear in the argument list HASHAMP returns an INTEGER value that is one less than the maximum number of AMPs in the system. evaluates to NULL HASHAMP returns NULL. Chapter 15: Hash-Related Functions HASHAMP 636 SQL Functions, Operators, Expressions, and Predicates Example 1 If you call HASHAMP without an argument, it returns one less than the maximum number of AMPs on the system. SELECT HASHAMP(); Example 2 If you call HASHAMP with an argument of NULL, it returns NULL. SELECT HASHAMP(NULL); Example 3 The following query returns the distribution of the hash buckets among the primary AMPs. SELECT B, HASHAMP (B) FROM T ORDER BY 1; Example 4 The following query returns the number of rows on each primary AMP where column_1 and column_2 are to be the primary index of table T. SELECT HASHAMP (HASHBUCKET (HASHROW (column_1,column_2))), COUNT (*) FROM T GROUP BY 1 ORDER BY 1; Chapter 15: Hash-Related Functions HASHBAKAMP SQL Functions, Operators, Expressions, and Predicates 637 HASHBAKAMP Purpose Returns the identification number of the fallback AMP corresponding to the specified hash bucket. If no hash bucket is specified, HASHBAKAMP returns one less than the maximum number of AMPs in the system. Syntax where: ANSI Compliance HASHBAKAMP is a Teradata extension to the ANSI SQL:2008 standard. Argument Type and Value The expression argument must evaluate to INTEGER data type where the valid range of values depends on the system setting for the hash bucket size. For information on how to specify the system setting for the hash bucket size, see “DBS Control utility” in Utilities. If expression cannot be implicitly converted to an INTEGER, an error is reported. Syntax element … Specifies … expression an optional expression that evaluates to a valid hash bucket number. For information on obtaining a hash bucket number that you can use for expression, see “HASHBUCKET” on page 640. HH01A028 HASHBAKAMP ( expression ) IF the hash bucket size is … THEN the range of values for expression is … 16 bits 0 to 65535. 20 bits 0 to 1048575. Chapter 15: Hash-Related Functions HASHBAKAMP 638 SQL Functions, Operators, Expressions, and Predicates If expression results in a UDT, Teradata Database performs implicit type conversion on the UDT, provided that the UDT has an implicit cast that casts between the UDT and any of the following predefined types: • Numeric • Character • DATE To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including HASHBAKAMP, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion, see Chapter 20: “Data Type Conversions.” Result For information on the hash map that defines the relationship between hash buckets and fallback AMPs, see “Reconfiguration utility” in the Utilities book. Examples The following examples assume a table T with an INTEGER column B populated with integer numbers from zero to the maximum number of hash buckets on the system. Example 1 If you call HASHBAKAMP without an argument, it returns one less than the maximum number of AMPs on the system. SELECT HASHBAKAMP(); IF expression … THEN … does not appear in the argument list HASHBAKAMP returns an INTEGER value that is one less than the maximum number of AMPs in the system. evaluates to NULL HASHBAKAMP returns NULL. evaluates to a valid hash bucket number HASHBAKAMP determines the fallback AMP corresponding to the hash bucket and returns the identification number of the AMP. The result is an INTEGER value that is greater than or equal to zero and less than the maximum number of AMPs in the configuration. Chapter 15: Hash-Related Functions HASHBAKAMP SQL Functions, Operators, Expressions, and Predicates 639 Example 2 If you call a HASHBAKAMP function with an argument of NULL, the function returns NULL. SELECT HASHBAKAMP(NULL); Example 3 This query returns the distribution of the hash buckets among the fallback AMPs. SELECT B, HASHBAKAMP (B) FROM T ORDER BY 1; Chapter 15: Hash-Related Functions HASHBUCKET 640 SQL Functions, Operators, Expressions, and Predicates HASHBUCKET Purpose Returns the hash bucket number that corresponds to a specified row hash value. If no row hash value is specified, HASHBUCKET returns the highest hash bucket number. Syntax where: ANSI Compliance HASHBUCKET is a Teradata extension to the ANSI SQL:2008 standard. Result HASHBUCKET returns an INTEGER data type. Syntax element … Specifies … expression an optional expression that evaluates to a valid BYTE(4) row hash value. If expression results in a UDT, Teradata Database performs implicit type conversion on the UDT, provided that the UDT has an implicit cast to a predefined byte type. To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. Implicit type conversion of UDTs for system operators and functions, including HASHBUCKET, is a Teradata extension to the ANSI SQL standard. To disable this extension, set the DisableUDTImplCastForSysFuncOp field of the DBS Control Record to TRUE. For details, see Utilities. For more information on implicit type conversion, see Chapter 20: “Data Type Conversions.” For information on obtaining a row hash value that you can use for expression, see “HASHROW” on page 643. HH01A026 HASHBUCKET ( expression ) Chapter 15: Hash-Related Functions HASHBUCKET SQL Functions, Operators, Expressions, and Predicates 641 Using HASHBUCKET to Convert a BYTE Type to an INTEGER Type When a byte data type is the source type of a conversion using CAST syntax or Teradata Conversion syntax, the target data type must also be a byte type. To convert a BYTE(1) or BYTE(2) data type to INTEGER, you can use the HASHBUCKET function. Consider the following table definition: CREATE TABLE ByteData(b1 BYTE(1), b2 BYTE(2)); To convert column b1 to INTEGER regardless of the system setting of the hash bucket size, use the following: SELECT HASHBUCKET('00'XB || b1 (BYTE(4))) / ((HASHBUCKET()+1)/65536) FROM ByteData; To convert column b2 to INTEGER regardless of the system setting of the hash bucket size, use the following: SELECT HASHBUCKET(b2 (BYTE(4))) / ((HASHBUCKET()+1)/65536) FROM ByteData; Examples The following examples assume a table T with columns C1 and C2 and possibly other columns. Example 1 If you call HASHBUCKET without an argument, it returns the maximum hash bucket. SELECT HASHBUCKET(); IF expression … THEN … does not appear in the argument list HASHBUCKET returns an INTEGER value that is the highest hash bucket number. evaluates to NULL HASHBUCKET returns NULL. evaluates to a valid BYTE(4) row hash value HASHBUCKET returns the hash bucket number corresponding to the row hash value. The range of values for hash bucket numbers depends on the system setting of the hash bucket size. IF the hash bucket size is … THEN hash bucket numbers can have a value from … 16 bits 0 to 65535. 20 bits 0 to 1048575. Chapter 15: Hash-Related Functions HASHBUCKET 642 SQL Functions, Operators, Expressions, and Predicates Example 2 If you call a HASHBUCKET function with an argument of NULL, the function returns NULL. SELECT HASHBUCKET(NULL); Example 3 Building on the previous example, you can nest a call to HASHROW within a HASHBUCKET call. Calling HASHBUCKET (HASHROW (NULL)) returns the 0 hash bucket. SELECT HASHBUCKET(HASHROW(NULL)); Example 4 The following example returns the number of rows in each hash bucket where C1 and C2 are to be the primary index of T. SELECT HASHBUCKET (HASHROW (C1,C2)), COUNT (*) FROM T GROUP BY 1 ORDER BY 1; Example 5 The results of the following example lists each hash bucket that has one or more rows and its corresponding primary AMP. SELECT HASHAMP (HASHBUCKET (HASHROW (C1, C2))), HASHBUCKET (HASHROW (C1,C2)) FROM T GROUP BY 1,2 ORDER BY 1,2 ; Chapter 15: Hash-Related Functions HASHROW SQL Functions, Operators, Expressions, and Predicates 643 HASHROW Purpose Returns the hexadecimal row hash value for an expression or sequence of expressions. If no expression is specified, HASHROW returns the maximum hash code value. Syntax where: ANSI Compliance HASHROW is a Teradata extension to the ANSI SQL:2008 standard. Result The resulting row hash value is typed BYTE(4). Syntax element … Specifies … expression an optional expression or comma-separated list of expressions that can appear in the expression list of the select clause of a SELECT statement; typically a comma-separated list of column names that make up a (potential) index. HASHROW does not support expressions that result in UDT data types. 1101B026 HASHROW , ( expression ) IF the argument list is … THEN HASHROW … empty returns the maximum hash code value. an expression that evaluates to NULL returns '00000000'XB. a list of expressions where all the expressions evaluate to NULL an expression that evaluates to 0, '', ' ', or a similar value a valid, non-NULL expression that can appear in the select list of a SELECT statement evaluates expression or the list of expressions and applies the hash function on the result. HASHROW returns the resulting a list of expressions that can appear in the select list of a row hash value. SELECT statement, where some expressions can evaluate to NULL Chapter 15: Hash-Related Functions HASHROW 644 SQL Functions, Operators, Expressions, and Predicates Usage Notes HASHROW is particularly useful for identifying the statistical properties of the current primary index, or to evaluate these properties for other columns to determine their suitability as a future primary index. You can also use these statistics to help minimize hash synonyms and enhance the uniformity of data distribution. There are a maximum of 4,294,967,295 hash codes available in the system, ranging from '00000000'XB to 'FFFFFFFF'XB. You can embed a HASHROW call within a HASHBUCKET call. For information on HASHBUCKET, see “HASHBUCKET” on page 640. Example 1 If you call HASHROW without an argument, it returns 'FFFFFFFF'XB, which is the maximum hash code in the system. SELECT HASHROW(); Example 2 The following example returns the average number of rows per row hash, where columns date_field and time_field constitute the primary index of the table eventlog. SELECT COUNT(*) / COUNT(DISTINCT HASHROW (date_field,time_field)) FROM eventlog; If columns date_field and time_field qualify for a unique index, this example returns the average number of rows with the same hash synonym. Example 3 The following example evaluates the efficiency of changing the decimal format of a numeric field to eliminate synonyms. Assume that column_1 and column_2 are declared as DECIMAL(2,2). You can determine the effect of reformatting the columns to DECIMAL(8,6) and DECIMAL(8,4) on hash collisions by submitting these two queries. SELECT COUNT (DISTINCT column_1(DECIMAL(8,6)) || column_2(DECIMAL(8,4)) FROM T; SELECT COUNT (DISTINCT HASHROW (column_1(DECIMAL(8,6)), column_2 (DECIMAL(8,4))) FROM T; If the result of the second query is significantly less than the result of the first query, there are a significant number of hash collisions. That is, the closer the second result is to the first value indicates elimination of more hash synonyms. SQL Functions, Operators, Expressions, and Predicates 645 CHAPTER 16 Compression/Decompression Functions This chapter describes the functions that you can use with Algorithmic Compression (ALC) to compress and decompress column data of character or byte type. Compression of data reduces space usage and may improve performance by reducing the amount of I/O required. For a detailed comparison between the compression functions and guidelines for choosing a compression function, see “Reducing Space Usage with Data Compression” in Database Administration. If the compression and decompression functions described in this chapter are not optimal for your data, you can write your own user-defined functions (UDFs) to compress and decompress table columns. Prerequisites The functions in this chapter are domain-specific functions; therefore, before you can use these functions, you must run the Database Initialization Program (DIP) utility and execute the DIPALL or DIPUDT script. For details, see “Activating Domain-specific Functions” on page 20. Related Topics FOR more information on... SEE... ALC • “COMPRESS and DECOMPRESS Phrases” in SQL Data Types and Literals. • “CREATE TABLE” in SQL Data Definition Language. writing UDFs for ALC • “Defining Functions for Algorithmic Compression” in SQL External Routine Programming. • “CREATE TABLE” in SQL Data Definition Language. compression methods supported by Teradata Database and a comparison of the various methods “Reducing Space Usage with Data Compression” in Database Administration. Chapter 16: Compression/Decompression Functions CAMSET 646 SQL Functions, Operators, Expressions, and Predicates CAMSET Purpose Compresses the specified Unicode character data into the following possible values using a proprietary Teradata algorithm: • partial byte values (for example, 4-bit digits or 5-bit alphabetic letters) • one byte values (for example, other Latin characters) • two byte values (for example, other Unicode characters) Syntax where: ANSI Compliance CAMSET is a Teradata extension to the ANSI SQL:2008 standard. Invocation CAMSET is a domain-specific function. For information on activating and invoking domainspecific functions, see “Domain-specific Functions” on page 20. Argument Type and Rules Expressions passed to this function must have a data type of VARCHAR(n) CHARACTER SET UNICODE, where the maximum supported size (n) is 32000. You can also pass arguments with data types that can be converted to VARCHAR(32000) CHARACTER SET UNICODE using the implicit data type conversion rules that apply to UDFs. For example, CAMSET(CHAR) is allowed because it can be implicitly converted to CAMSET(VARCHAR). Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If an argument cannot be converted to VARCHAR following the UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” in SQL External Routine Programming. Syntax element… Specifies… Unicode_string a Unicode character string or string expression. 1101A781 TD_SYSFNLIB. CAMSET (Unicode_string) Chapter 16: Compression/Decompression Functions CAMSET SQL Functions, Operators, Expressions, and Predicates 647 The input to this function must be Unicode character data. If you specify NULL as input, the function returns NULL. Result Type The result data type is VARBYTE(64000). Usage Notes Uncompressed character data in Teradata Database requires two bytes per character when storing Unicode data. CAMSET takes Unicode character input, compresses it into partial byte, one byte, or two byte values, and returns the compressed result. CAMSET provides best results for short or medium Unicode strings that: • contain mainly digits and English alphabet letters. • do not frequently switch between: • lowercase and uppercase letters. • digits and letters. • Latin and non-Latin characters. For a detailed comparison between the Teradata-supplied compression functions and guidelines for choosing a compression function, see Database Administration. Although you can call the function directly, CAMSET is normally used with Algorithmic Compression (ALC) to compress table columns. If CAMSET is used with ALC, nulls are also compressed if those columns are nullable. For more information about ALC, see “COMPRESS and DECOMPRESS Phrases” in SQL Data Types and Literals. Restrictions CAMSET currently can only compress Unicode characters from U+0000 to U+00FF. Decompressing Data Compressed with CAMSET To decompress Unicode data that was compressed using CAMSET, use the DECAMSET function. See “DECAMSET” on page 652. Example 1 In this example, the Unicode values in the Description column are compressed using the CAMSET function with ALC. The DECAMSET function decompresses the previously compressed values. CREATE MULTISET TABLE Pendants (ItemNo INTEGER, Gem CHAR(10) UPPERCASE CHARACTER SET UNICODE, Description VARCHAR(1000) CHARACTER SET UNICODE COMPRESS USING TD_SYSFNLIB.CAMSET Chapter 16: Compression/Decompression Functions CAMSET 648 SQL Functions, Operators, Expressions, and Predicates DECOMPRESS USING TD_SYSFNLIB.DECAMSET); Example 2 Given the following table definition: CREATE TABLE Pendants (ItemNo INTEGER, Description VARCHAR(100) CHARACTER SET UNICODE); The following query returns the compressed values of the Description column. SELECT TD_SYSFNLIB.CAMSET(Pendants.Description); Chapter 16: Compression/Decompression Functions CAMSET_L SQL Functions, Operators, Expressions, and Predicates 649 CAMSET_L Purpose Compresses the specified Latin character data into the following possible values using a proprietary Teradata algorithm: • partial byte values (for example, 4-bit digits or 5-bit alphabetic letters) • one byte values (for example, other Latin characters) Syntax where: ANSI Compliance CAMSET_L is a Teradata extension to the ANSI SQL:2008 standard. Invocation CAMSET_L is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Argument Type and Rules Expressions passed to this function must have a data type of VARCHAR(n) CHARACTER SET LATIN, where the maximum supported size (n) is 64000. You can also pass arguments with data types that can be converted to VARCHAR(64000) CHARACTER SET LATIN using the implicit data type conversion rules that apply to UDFs. For example, CAMSET_L(CHAR) is allowed because it can be implicitly converted to CAMSET_L(VARCHAR). Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If an argument cannot be converted to VARCHAR following the UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” in SQL External Routine Programming. Syntax element… Specifies… Latin_string a Latin character string or string expression. 1101A782 TD_SYSFNLIB. CAMSET_L (Latin_string) Chapter 16: Compression/Decompression Functions CAMSET_L 650 SQL Functions, Operators, Expressions, and Predicates The input to this function must be Latin character data. If you specify NULL as input, the function returns NULL. Result Type The result data type is VARBYTE(64000). Usage Notes Uncompressed character data in Teradata Database requires one byte per character when storing Latin character data. CAMSET_L takes Latin character input, compresses it into partial byte or one byte values, and returns the compressed result. CAMSET_L provides best results for short or medium Latin strings that: • contain mainly digits and English alphabet letters. • do not frequently switch between: • lowercase and uppercase letters. • digits and letters. For a detailed comparison between the Teradata-supplied compression functions and guidelines for choosing a compression function, see Database Administration. Although you can call the function directly, CAMSET_L is normally used with Algorithmic Compression (ALC) to compress table columns. If CAMSET_L is used with ALC, nulls are also compressed if those columns are nullable. For more information about ALC, see “COMPRESS and DECOMPRESS Phrases” in SQL Data Types and Literals. Decompressing Data Compressed with CAMSET_L To decompress Latin character data that was compressed using CAMSET_L, use the DECAMSET_L function. See “DECAMSET_L” on page 654. Example 1 In this example, the Latin values in the Description column are compressed using the CAMSET_L function with ALC. The DECAMSET_L function decompresses the previously compressed values. CREATE MULTISET TABLE Pendants (ItemNo INTEGER, Gem CHAR(10) UPPERCASE CHARACTER SET LATIN, Description VARCHAR(1000) CHARACTER SET LATIN COMPRESS USING TD_SYSFNLIB.CAMSET_L DECOMPRESS USING TD_SYSFNLIB.DECAMSET_L); Example 2 Given the following table definition: Chapter 16: Compression/Decompression Functions CAMSET_L SQL Functions, Operators, Expressions, and Predicates 651 CREATE TABLE Pendants (ItemNo INTEGER, Description VARCHAR(100) CHARACTER SET LATIN); The following query returns the compressed values of the Description column. SELECT TD_SYSFNLIB.CAMSET_L(Pendants.Description); Chapter 16: Compression/Decompression Functions DECAMSET 652 SQL Functions, Operators, Expressions, and Predicates DECAMSET Purpose Decompresses the Unicode data that was compressed using the CAMSET function. Syntax where: ANSI Compliance DECAMSET is a Teradata extension to the ANSI SQL:2008 standard. Invocation DECAMSET is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Argument Type and Rules Expressions passed to this function must have a data type of VARBYTE(n), where the maximum supported size (n) is 64000. The input to this function must be the output result of the CAMSET function. If you specify NULL as input, the function returns NULL. Result Type The result data type is VARCHAR(32000) CHARACTER SET UNICODE. Usage Notes DECAMSET takes Unicode data that was compressed using the CAMSET function, decompresses it, and returns an uncompressed Unicode character string as the result. Syntax element… Specifies… compressed_string Unicode character data that was compressed using the CAMSET function. 1101A784 TD_SYSFNLIB. DECAMSET (compressed_string) Chapter 16: Compression/Decompression Functions DECAMSET SQL Functions, Operators, Expressions, and Predicates 653 Although you can call the function directly, DECAMSET is normally used with Algorithmic Compression (ALC) to decompress table columns previously compressed with CAMSET. For more information about ALC, see “COMPRESS and DECOMPRESS Phrases” in SQL Data Types and Literals. Example In this example, the Unicode values in the Description column are compressed using the CAMSET function with ALC. The DECAMSET function decompresses the previously compressed values. CREATE MULTISET TABLE Pendants (ItemNo INTEGER, Gem CHAR(10) UPPERCASE CHARACTER SET UNICODE, Description VARCHAR(1000) CHARACTER SET UNICODE COMPRESS USING TD_SYSFNLIB.CAMSET DECOMPRESS USING TD_SYSFNLIB.DECAMSET); Chapter 16: Compression/Decompression Functions DECAMSET_L 654 SQL Functions, Operators, Expressions, and Predicates DECAMSET_L Purpose Decompresses the Latin data that was compressed using the CAMSET_L function. Syntax where: ANSI Compliance DECAMSET_L is a Teradata extension to the ANSI SQL:2008 standard. Invocation DECAMSET_L is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Argument Type and Rules Expressions passed to this function must have a data type of VARBYTE(n), where the maximum supported size (n) is 64000. The input to this function must be the output result of the CAMSET_L function. If you specify NULL as input, the function returns NULL. Result Type The result data type is VARCHAR(64000) CHARACTER SET LATIN. Usage Notes DECAMSET_L takes Latin data that was compressed using the CAMSET_L function, decompresses it, and returns an uncompressed Latin character string as the result. Syntax element… Specifies… compressed_string Latin character data that was compressed using the CAMSET_L function. 1101A783 TD_SYSFNLIB. DECAMSET_L (compressed_string) Chapter 16: Compression/Decompression Functions DECAMSET_L SQL Functions, Operators, Expressions, and Predicates 655 Although you can call the function directly, DECAMSET_L is normally used with Algorithmic Compression (ALC) to decompress table columns previously compressed with CAMSET_L. For more information about ALC, see “COMPRESS and DECOMPRESS Phrases” in SQL Data Types and Literals. Example In this example, the Latin values in the Description column are compressed using the CAMSET_L function with ALC. The DECAMSET_L function decompresses the previously compressed values. CREATE MULTISET TABLE Pendants (ItemNo INTEGER, Gem CHAR(10) UPPERCASE CHARACTER SET LATIN, Description VARCHAR(1000) CHARACTER SET LATIN COMPRESS USING TD_SYSFNLIB.CAMSET_L DECOMPRESS USING TD_SYSFNLIB.DECAMSET_L); Chapter 16: Compression/Decompression Functions LZCOMP 656 SQL Functions, Operators, Expressions, and Predicates LZCOMP Purpose Compresses the specified Unicode character data using the Lempel-Ziv algorithm. Syntax where: ANSI Compliance LZCOMP is a Teradata extension to the ANSI SQL:2008 standard. Invocation LZCOMP is a domain-specific function. For information on activating and invoking domainspecific functions, see “Domain-specific Functions” on page 20. Argument Type and Rules Expressions passed to this function must have a data type of VARCHAR(n) CHARACTER SET UNICODE, where the maximum supported size (n) is 32000. You can also pass arguments with data types that can be converted to VARCHAR(32000) CHARACTER SET UNICODE using the implicit data type conversion rules that apply to UDFs. For example, LZCOMP(CHAR) is allowed because it can be implicitly converted to LZCOMP(VARCHAR). Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If an argument cannot be converted to VARCHAR following the UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” in SQL External Routine Programming. The input to this function must be Unicode character data. If you specify NULL as input, the function returns NULL. Syntax element… Specifies… Unicode_string a Unicode character string or string expression. 1101A766 TD_SYSFNLIB. LZCOMP (Unicode_string) Chapter 16: Compression/Decompression Functions LZCOMP SQL Functions, Operators, Expressions, and Predicates 657 Result Type The result data type is VARBYTE(64000). Usage Notes Uncompressed character data in Teradata Database requires two bytes per character when storing Unicode data. LZCOMP takes Unicode character input, compresses it using the Lempel-Ziv algorithm, and returns the compressed result. See http://zlib.net for information about the compression algorithm used by LZCOMP. LZCOMP provides good compression results for long Unicode strings, but might not be as effective for short strings. It can also provide good results for medium strings that have many repeating characters. For a detailed comparison between the Teradata-supplied compression functions and guidelines for choosing a compression function, see Database Administration. Although you can call the function directly, LZCOMP is normally used with Algorithmic Compression (ALC) to compress table columns. If LZCOMP is used with ALC, nulls are also compressed if those columns are nullable. For more information about ALC, see “COMPRESS and DECOMPRESS Phrases” in SQL Data Types and Literals. Decompressing Data Compressed with LZCOMP To decompress Unicode data that was compressed using LZCOMP, use the LZDECOMP function. See “LZDECOMP” on page 660. Example 1 In this example, the Unicode values in the Description column are compressed using the LZCOMP function with ALC. The LZDECOMP function decompresses the previously compressed values. CREATE MULTISET TABLE Pendants (ItemNo INTEGER, Gem CHAR(10) UPPERCASE CHARACTER SET UNICODE, Description VARCHAR(1000) CHARACTER SET UNICODE COMPRESS USING TD_SYSFNLIB.LZCOMP DECOMPRESS USING TD_SYSFNLIB.LZDECOMP); Example 2 Given the following table definition: CREATE TABLE Pendants (ItemNo INTEGER, Description VARCHAR(100) CHARACTER SET UNICODE); The following query returns the compressed values of the Description column. SELECT TD_SYSFNLIB.LZCOMP(Pendants.Description); Chapter 16: Compression/Decompression Functions LZCOMP_L 658 SQL Functions, Operators, Expressions, and Predicates LZCOMP_L Purpose Compresses the specified Latin character data using the Lempel-Ziv algorithm. Syntax where: ANSI Compliance LZCOMP_L is a Teradata extension to the ANSI SQL:2008 standard. Invocation LZCOMP_L is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Argument Type and Rules Expressions passed to this function must have a data type of VARCHAR(n) CHARACTER SET LATIN, where the maximum supported size (n) is 64000. You can also pass arguments with data types that can be converted to VARCHAR(64000) CHARACTER SET LATIN using the implicit data type conversion rules that apply to UDFs. For example, LZCOMP_L(CHAR) is allowed because it can be implicitly converted to LZCOMP_L(VARCHAR). Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If an argument cannot be converted to VARCHAR following the UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” in SQL External Routine Programming. The input to this function must be Latin character data. If you specify NULL as input, the function returns NULL. Syntax element… Specifies… Latin_string a Latin character string or string expression. 1101A765 TD_SYSFNLIB. LZCOMP_L (Latin_string) Chapter 16: Compression/Decompression Functions LZCOMP_L SQL Functions, Operators, Expressions, and Predicates 659 Result Type The result data type is VARBYTE(64000). Usage Notes Uncompressed character data in Teradata Database requires one byte per character when storing Latin character data. LZCOMP_L takes Latin character input, compresses it using the Lempel-Ziv algorithm, and returns the compressed result. See http://zlib.net for information about the compression algorithm used by LZCOMP_L. LZCOMP_L provides good compression results for long Latin character strings, but might not be as effective for short strings. It can also provide good results for medium strings that have many repeating characters. For a detailed comparison between the Teradata-supplied compression functions and guidelines for choosing a compression function, see Database Administration. Although you can call the function directly, LZCOMP_L is normally used with Algorithmic Compression (ALC) to compress table columns. If LZCOMP_L is used with ALC, nulls are also compressed if those columns are nullable. For more information about ALC, see “COMPRESS and DECOMPRESS Phrases” in SQL Data Types and Literals. Decompressing Data Compressed with LZCOMP_L To decompress Latin data that was compressed using LZCOMP_L, use the LZDECOMP_L function. See “LZDECOMP_L” on page 662. Example 1 In this example, the Latin values in the Description column are compressed using the LZCOMP_L function with ALC. The LZDECOMP_L function decompresses the previously compressed values. CREATE MULTISET TABLE Pendants (ItemNo INTEGER, Gem CHAR(10) UPPERCASE CHARACTER SET LATIN, Description VARCHAR(1000) CHARACTER SET LATIN COMPRESS USING TD_SYSFNLIB.LZCOMP_L DECOMPRESS USING TD_SYSFNLIB.LZDECOMP_L); Example 2 Given the following table definition: CREATE TABLE Pendants (ItemNo INTEGER, Description VARCHAR(100) CHARACTER SET LATIN); The following query returns the compressed values of the Description column. SELECT TD_SYSFNLIB.LZCOMP_L(Pendants.Description); Chapter 16: Compression/Decompression Functions LZDECOMP 660 SQL Functions, Operators, Expressions, and Predicates LZDECOMP Purpose Decompresses the Unicode data that was compressed using the LZCOMP function. Syntax where: ANSI Compliance LZDECOMP is a Teradata extension to the ANSI SQL:2008 standard. Invocation LZDECOMP is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Argument Type and Rules Expressions passed to this function must have a data type of VARBYTE(n), where the maximum supported size (n) is 64000. The input to this function must be the output result of the LZCOMP function. If you specify NULL as input, the function returns NULL. Result Type The result data type is VARCHAR(32000) CHARACTER SET UNICODE. Usage Notes LZDECOMP takes Unicode data that was compressed using the LZCOMP function, decompresses it, and returns an uncompressed Unicode character string as the result. See http://zlib.net for information about the decompression algorithm used by LZDECOMP. Syntax element… Specifies… compressed_string Unicode character data that was compressed using the LZCOMP function. 1101A763 TD_SYSFNLIB. LZDECOMP (compressed_string) Chapter 16: Compression/Decompression Functions LZDECOMP SQL Functions, Operators, Expressions, and Predicates 661 Although you can call the function directly, LZDECOMP is normally used with Algorithmic Compression (ALC) to decompress table columns previously compressed with LZCOMP. For more information about ALC, see “COMPRESS and DECOMPRESS Phrases” in SQL Data Types and Literals. Example In this example, the Unicode values in the Description column are compressed using the LZCOMP function with ALC. The LZDECOMP function decompresses the previously compressed values. CREATE MULTISET TABLE Pendants (ItemNo INTEGER, Gem CHAR(10) UPPERCASE CHARACTER SET UNICODE, Description VARCHAR(1000) CHARACTER SET UNICODE COMPRESS USING TD_SYSFNLIB.LZCOMP DECOMPRESS USING TD_SYSFNLIB.LZDECOMP); Chapter 16: Compression/Decompression Functions LZDECOMP_L 662 SQL Functions, Operators, Expressions, and Predicates LZDECOMP_L Purpose Decompresses the Latin data that was compressed using the LZCOMP_L function. Syntax where: ANSI Compliance LZDECOMP_L is a Teradata extension to the ANSI SQL:2008 standard. Invocation LZDECOMP_L is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Argument Type and Rules Expressions passed to this function must have a data type of VARBYTE(n), where the maximum supported size (n) is 64000. The input to this function must be the output result of the LZCOMP_L function. If you specify NULL as input, the function returns NULL. Result Type The result data type is VARCHAR(64000) CHARACTER SET LATIN. Usage Notes LZDECOMP_L takes Latin data that was compressed using the LZCOMP_L function, decompresses it, and returns an uncompressed Latin character string as the result. Syntax element… Specifies… compressed_string Latin character data that was compressed using the LZCOMP_L function. 1101A764 TD_SYSFNLIB. LZDECOMP_L (compressed_string) Chapter 16: Compression/Decompression Functions LZDECOMP_L SQL Functions, Operators, Expressions, and Predicates 663 See http://zlib.net for information about the decompression algorithm used by LZDECOMP_L. Although you can call the function directly, LZDECOMP_L is normally used with Algorithmic Compression (ALC) to decompress table columns previously compressed with LZCOMP_L. For more information about ALC, see “COMPRESS and DECOMPRESS Phrases” in SQL Data Types and Literals. Example In this example, the Latin values in the Description column are compressed using the LZCOMP_L function with ALC. The LZDECOMP_L function decompresses the previously compressed values. CREATE MULTISET TABLE Pendants (ItemNo INTEGER, Gem CHAR(10) UPPERCASE CHARACTER SET LATIN, Description VARCHAR(1000) CHARACTER SET LATIN COMPRESS USING TD_SYSFNLIB.LZCOMP_L DECOMPRESS USING TD_SYSFNLIB.LZDECOMP_L); Chapter 16: Compression/Decompression Functions TransUnicodeToUTF8 664 SQL Functions, Operators, Expressions, and Predicates TransUnicodeToUTF8 Purpose Compresses the specified Unicode character data into UTF8 format. Syntax where: ANSI Compliance TransUnicodeToUTF8 is a Teradata extension to the ANSI SQL:2008 standard. Invocation TransUnicodeToUTF8 is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Argument Type and Rules Expressions passed to this function must have a data type of VARCHAR(n) CHARACTER SET UNICODE, where the maximum supported size (n) is 32000. You can also pass arguments with data types that can be converted to VARCHAR(32000) CHARACTER SET UNICODE using the implicit data type conversion rules that apply to UDFs. For example, TransUnicodeToUTF8(CHAR) is allowed because it can be implicitly converted to TransUnicodeToUTF8(VARCHAR). Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If an argument cannot be converted to VARCHAR following the UDF implicit conversion rules, it must be explicitly cast. For details, see “Compatible Types” in SQL External Routine Programming. The input to this function must be Unicode character data. If you specify NULL as input, the function returns NULL. Syntax element… Specifies… Unicode_string a Unicode character string or string expression. 1101A771 TD_SYSFNLIB. TransUnicodeToUTF8 (Unicode_string) Chapter 16: Compression/Decompression Functions TransUnicodeToUTF8 SQL Functions, Operators, Expressions, and Predicates 665 Result Type The result data type is VARBYTE(64000). Usage Notes TransUnicodeToUTF8 compresses the specified Unicode character data into UTF8 format, and returns the compressed result. This is useful when the input data is predominantly Latin characters because UTF8 uses one byte to represent Latin characters and Unicode uses two bytes. TransUnicodeToUTF8 provides good compression for Unicode strings of any length and is best used: • On a Unicode column that contains mostly US-ASCII characters • When the data frequently switches between: • Uppercase and lowercase letters • Digits and letters • Latin and non-Latin characters • When the data is very dynamic (under frequent update) For a detailed comparison between the Teradata-supplied compression functions and guidelines for choosing a compression function, see Database Administration. Although you can call the function directly, TransUnicodeToUTF8 is normally used with Algorithmic Compression (ALC) to compress table columns. If TransUnicodeToUTF8 is used with ALC, nulls are also compressed if those columns are nullable. For more information about ALC, see “COMPRESS and DECOMPRESS Phrases” in SQL Data Types and Literals. Restrictions TransUnicodeToUTF8 can only compress character values in the 7-bit ASCII character range, from U+0000 to U+007F, also known as US-ASCII. Decompressing Data Compressed with TransUnicodeToUTF8 To decompress Unicode data that was compressed using TransUnicodeToUTF8, use the TransUTF8ToUnicode function. See “TransUTF8ToUnicode” on page 667. Example In this example, assume that the default server character set is UNICODE. The values of the Description column are compressed using the TransUnicodeToUTF8 function with ALC, which stores the Unicode input in UTF8 format. The TransUTF8ToUnicode function decompresses the previously compressed values. CREATE TABLE Pendants (ItemNo INTEGER, Gem CHAR(10) UPPERCASE, Chapter 16: Compression/Decompression Functions TransUnicodeToUTF8 666 SQL Functions, Operators, Expressions, and Predicates Description VARCHAR(1000) COMPRESS USING TD_SYSFNLIB.TransUnicodeToUTF8 DECOMPRESS USING TD_SYSFNLIB.TransUTF8ToUnicode); Chapter 16: Compression/Decompression Functions TransUTF8ToUnicode SQL Functions, Operators, Expressions, and Predicates 667 TransUTF8ToUnicode Purpose Decompresses the Unicode data that was compressed using the TransUnicodeToUTF8 function. Syntax where: ANSI Compliance TransUTF8ToUnicode is a Teradata extension to the ANSI SQL:2008 standard. Invocation TransUTF8ToUnicode is a domain-specific function. For information on activating and invoking domain-specific functions, see “Domain-specific Functions” on page 20. Argument Type and Rules Expressions passed to this function must have a data type of VARBYTE(n), where the maximum supported size (n) is 64000. The input to this function must be the output result of the TransUnicodeToUTF8 function. If you specify NULL as input, the function returns NULL. Result Type The result data type is VARCHAR(32000) CHARACTER SET UNICODE Usage Notes TransUTF8ToUnicode takes Unicode data that was compressed using the TransUnicodeToUTF8 function, decompresses it, and returns an uncompressed Unicode character string as the result. Syntax element… Specifies… compressed_string Unicode character data that was compressed using the TransUnicodeToUTF8 function. 1101A770 TD_SYSFNLIB. TransUTF8ToUnicode (compressed_string) Chapter 16: Compression/Decompression Functions TransUTF8ToUnicode 668 SQL Functions, Operators, Expressions, and Predicates Although you can call the function directly, TransUTF8ToUnicode is normally used with Algorithmic Compression (ALC) to decompress table columns previously compressed with TransUnicodeToUTF8. For more information about ALC, see “COMPRESS and DECOMPRESS Phrases” in SQL Data Types and Literals. Example In this example, assume that the default server character set is UNICODE. The values of the Description column are compressed using the TransUnicodeToUTF8 function with ALC, which stores the Unicode input in UTF8 format. The TransUTF8ToUnicode function decompresses the previously compressed values. CREATE TABLE Pendants (ItemNo INTEGER, Gem CHAR(10) UPPERCASE, Description VARCHAR(1000) COMPRESS USING TD_SYSFNLIB.TransUnicodeToUTF8 DECOMPRESS USING TD_SYSFNLIB.TransUTF8ToUnicode); SQL Functions, Operators, Expressions, and Predicates 669 CHAPTER 17 Built-In Functions Built-in functions, which are niladic (have no arguments), return various information about the system. Built-in functions are sometimes referred to as special registers. The built-in functions can be used anywhere that a constant can appear. If a SELECT statement that contains a built-in function references a table name, then the result of the query contains one row for every row of the table that satisfies the search condition. Chapter 17: Built-In Functions ACCOUNT 670 SQL Functions, Operators, Expressions, and Predicates ACCOUNT Purpose Returns the account string for the current user. Syntax ANSI Compliance ACCOUNT is a Teradata extension to the ANSI SQL:2008 standard. Result Type and Attributes The data type and format for ACCOUNT are as follows: Usage Notes If a SET SESSION ACCOUNT statement has changed the current account string, then the ACCOUNT function returns the new account string based on the request level: whether for an entire session or for an individual request. Example The following statement requests the account string for the current user: SELECT ACCOUNT; The system responds with something like the following: Account ------------------------------ $M_D2102 FF07R001 ACCOUNT Data Type Format VARCHAR(30) CHARACTER SET UNICODE X(30) Chapter 17: Built-In Functions CURRENT_DATE SQL Functions, Operators, Expressions, and Predicates 671 CURRENT_DATE Purpose Returns the current date. Syntax where: ANSI Compliance CURRENT_DATE and the AT clause are ANSI SQL:2008 compliant. As an extension to ANSI, you can specify an AT clause after the CURRENT_DATE function, and you can specify the time zone displacement using additional expressions besides an INTERVAL expression. Syntax element … Specifies … AT LOCAL that the value returned is constructed from the session time and session time zone if the DBS Control flag TimeDateWZControl is enabled. If TimeDateWZControl is disabled, the value returned is constructed from the time value local to the Teradata Database server and the session time zone. AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. 1101A682 CURRENT_DATE expression time_zone_string AT LOCAL TIME ZONE Chapter 17: Built-In Functions CURRENT_DATE 672 SQL Functions, Operators, Expressions, and Predicates Usage Notes CURRENT_DATE returns the current date at the time when the request started. If CURRENT_DATE is invoked more than once during the request, the same date is returned. The date returned does not change during the duration of the request. If you specify CURRENT_DATE without the AT clause or CURRENT_DATE AT LOCAL, then the value returned depends on the setting of the DBS Control flag TimeDateWZControl as follows: • If the TimeDateWZControl flag is enabled, CURRENT_DATE returns a date constructed from the session time and session time zone. • If the TimeDateWZControl flag is disabled, CURRENT_DATE returns a date constructed from the time value local to the Teradata Database server and the session time zone. For more information, see “DBS Control (dbscontrol)” in Utilities. CURRENT_DATE returns a value that is adjusted to account for the start and end of daylight saving time (DST) only in the following cases: • CURRENT_DATE is specified with AT [TIME ZONE] time_zone_string, where time_zone_string follows different DST and standard time zone displacements. • CURRENT_DATE is specified with AT LOCAL or without an AT clause and the session time zone was defined with a time zone string that follows different DST and standard time zone displacements. For more information about time zone strings, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. Result Type and Attributes The result data type and format for CURRENT_DATE are: To convert CURRENT_DATE, use Teradata explicit conversion syntax or ANSI CAST syntax. For an example that uses Teradata explicit conversion syntax to change the default output format, see “Example 3: Changing the Default Output Format” on page 679. CURRENT_DATE versus DATE CURRENT_DATE provides similar functionality to the Teradata function DATE using ANSIcompliant syntax. For information on the Teradata DATE function, see “DATE” on page 687. Data Type Format DATE Default format for the DATE data type when the Dateform mode is set to IntegerDate. For more information on the default formats, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Chapter 17: Built-In Functions CURRENT_DATE SQL Functions, Operators, Expressions, and Predicates 673 Example 1 This example assumes that the default format for DATE values is 'yy/mm/dd'. Consider the following statements: SET TIME ZONE INTERVAL '01:00' HOUR TO MINUTE; SELECT CURRENT_DATE AT TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE; SELECT CURRENT_DATE AT INTERVAL -'08:00' HOUR TO MINUTE; SELECT CURRENT_DATE AT TIME ZONE INTERVAL -'08' HOUR; SELECT CURRENT_DATE AT INTERVAL -'08' HOUR; SELECT CURRENT_DATE AT TIME ZONE '-08:00'; SELECT CURRENT_DATE AT '-08:00'; SELECT CURRENT_DATE AT TIME ZONE '-8'; SELECT CURRENT_DATE AT '-8'; SELECT CURRENT_DATE AT TIME ZONE -8; SELECT CURRENT_DATE AT -8; SELECT CURRENT_DATE AT -8.0; The above SELECT statements return the current date based on the time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE. If the current timestamp at UTC is TIMESTAMP '2008-06-01 06:30:00.000000+00:00', these SELECT statements would return '08/05/31' as the date. If the SELECT statement was specified without an AT clause or with an AT LOCAL clause, and the DBS Control flag TimeDateWZControl is enabled, the statement would return '08/06/01' as the current date based on the current session time and time zone displacement, INTERVAL '01:00' HOUR TO MINUTE. For example: SELECT CURRENT_DATE; SELECT CURRENT_DATE AT LOCAL; The date returned is not adjusted to account for the start or end of daylight saving time. Example 2 This example assumes that the default format for DATE values is 'yy/mm/dd'. Consider the following statements: SET TIME ZONE INTERVAL '01:00' HOUR TO MINUTE; SELECT CURRENT_DATE AT INTERVAL '09:00' HOUR TO MINUTE; The above SELECT statement returns the current date based on the time zone displacement, INTERVAL '09:00' HOUR TO MINUTE. If the current timestamp at UTC is TIMESTAMP '2008-06-01 19:30:00.000000+00:00', the SELECT statement would return '08/06/02' as the date. If the SELECT statement was specified without an AT clause or with an AT LOCAL clause, and the DBS Control flag TimeDateWZControl is enabled, the statement would return '08/06/01' as the current date based on the current session time and time zone displacement, INTERVAL '01:00' HOUR TO MINUTE. The date returned is not adjusted to account for the start or end of daylight saving time. Chapter 17: Built-In Functions CURRENT_DATE 674 SQL Functions, Operators, Expressions, and Predicates Example 3 This example assumes that the default format for DATE values is 'yy/mm/dd'. Consider the following statements: SET TIME ZONE INTERVAL '10:00' HOUR TO MINUTE; SELECT CURRENT_DATE AT '05:45'; SELECT CURRENT_DATE AT 5.75; The above SELECT statements return the current date based on the time zone displacement, INTERVAL '05:45' HOUR TO MINUTE. If the current timestamp at UTC is TIMESTAMP '2008-06-01 17:30:00.000000+00:00', the SELECT statements would return '08/06/01' as the date. If the SELECT statement was specified without an AT clause or with an AT LOCAL clause, and the DBS Control flag TimeDateWZControl is enabled, the statement would return '08/06/02' as the current date based on the current session time and time zone displacement, INTERVAL '10:00' HOUR TO MINUTE. The date returned is not adjusted to account for the start or end of daylight saving time. Example 4 The following queries return the current date at the time zone displacement based on the time zone string, 'America Pacific'. Teradata Database determines the time zone displacement based on the time zone string and the CURRENT_TIMESTAMP AT '00:00' (that is, at UTC). The date returned is automatically adjusted to account for the start and end of daylight saving time. SELECT CURRENT_DATE AT TIME ZONE 'America Pacific'; SELECT CURRENT_DATE AT 'America Pacific'; Example 5: Changing the Default Output Format To change the default output format of the CURRENT_DATE result, use Teradata explicit conversion syntax and specify the FORMAT phrase. For example, the following statement requests the current date and specifies a format that is different from the default: SELECT CURRENT_DATE (FORMAT 'MMMBDD,BYYYY'); The result is similar to: Date ------------ May 31, 2007 For more information on Teradata explicit conversion syntax, see “Teradata Conversion Syntax in Explicit Data Type Conversions” on page 755. For more information on default data type formats and the FORMAT phrase, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Chapter 17: Built-In Functions CURRENT_ROLE SQL Functions, Operators, Expressions, and Predicates 675 CURRENT_ROLE Purpose Returns the current role of the current authorized user. Syntax ANSI Compliance CURRENT_ROLE is consistent with ANSI SQL:2008 usage. Result Type and Attributes The data type and format for CURRENT_ROLE are as follows: Result Value If you are not accessing the Teradata Database through a proxy connection, CURRENT_ROLE functions exactly like the ROLE built-in function and returns the session current role, which is the current role of the session user. For details, see “ROLE” on page 692. If you are accessing the Teradata Database through a proxy connection, then CURRENT_ROLE returns the current role of the proxy user as shown in the following table. 1101A565 CURRENT_ROLE Data Type Format VARCHAR(30) CHARACTER SET UNICODE X(30) IF the current role for the session is … THEN the result value is … a role set by PROXYROLE the name of the role. the default If there is one proxy role in the CONNECT THROUGH privilege of the proxy user, the result value is the name of the role. If there are multiple proxy roles in the CONNECT THROUGH privilege of the proxy user, the result value is ALL. PROXYROLE=ALL ALL PROXYROLE=NONE or NULL NULL Chapter 17: Built-In Functions CURRENT_ROLE 676 SQL Functions, Operators, Expressions, and Predicates Usage Notes CURRENT_ROLE is not supported in the FastLoad and MultiLoad utilities. Example You can identify the current role for the current authorized user with the following statement: SELECT CURRENT_ROLE; The system responds with something like the following: Current_Role ------------------------------ Buyers_role Chapter 17: Built-In Functions CURRENT_TIME SQL Functions, Operators, Expressions, and Predicates 677 CURRENT_TIME Purpose Returns the current time. Syntax where: ANSI Compliance CURRENT_TIME and the AT clause are ANSI SQL:2008 compliant. As an extension to ANSI, you can specify the time zone displacement using additional expressions besides an INTERVAL expression. 1101A714 CURRENT_TIME (fractional_precision ) expression time_zone_string AT LOCAL TIME ZONE Syntax element … Specifies … fractional_precision an optional precision range for the returned time value. The valid range is 0 through 6, inclusive. The default is 0. AT LOCAL that the value returned is constructed from the session time and session time zone if the DBS Control flag TimeDateWZControl is enabled. If TimeDateWZControl is disabled, the value returned is constructed from the time value local to the Teradata Database server and the session time zone. AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. Chapter 17: Built-In Functions CURRENT_TIME 678 SQL Functions, Operators, Expressions, and Predicates Usage Notes CURRENT_TIME returns the current time when the request started. If CURRENT_TIME is invoked more than once during the request, the same time is returned. The time returned does not change during the duration of the request. If you specify CURRENT_TIME without the AT clause or CURRENT_TIME AT LOCAL, then the value returned depends on the setting of the DBS Control flag TimeDateWZControl as follows: • If the TimeDateWZControl flag is enabled, CURRENT_TIME returns a time constructed from the session time and session time zone. • If the TimeDateWZControl flag is disabled, CURRENT_TIME returns a time constructed from the time value local to the Teradata Database server and the session time zone. For more information, see “DBS Control (dbscontrol)” in Utilities. CURRENT_TIME returns a value that is adjusted to account for the start and end of daylight saving time (DST) only in the following cases: • CURRENT_TIME is specified with AT [TIME ZONE] time_zone_string, where time_zone_string follows different DST and standard time zone displacements. • CURRENT_TIME is specified with AT LOCAL or without an AT clause and the session time zone was defined with a time zone string that follows different DST and standard time zone displacements. For more information about time zone strings, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. Result Type and Attributes The result data type and format for CURRENT_TIME are: To convert CURRENT_TIME, use Teradata explicit conversion syntax or ANSI CAST syntax. For an example that uses Teradata explicit conversion syntax to change the default output format, see “Example 3: Changing the Default Output Format” on page 679. Precision The seconds precision of the result of CURRENT_TIME is limited to hundredths of a second. CURRENT_TIME returns zeros for any digits to the right of the two most significant digits in the fractional portion of seconds. Data Type Format TIME WITH TIME ZONE Default format for the TIME WITH TIME ZONE data type. For more information on the default formats, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Chapter 17: Built-In Functions CURRENT_TIME SQL Functions, Operators, Expressions, and Predicates 679 CURRENT_TIME Fields The fields in CURRENT_TIME are: • HOUR • MINUTE • SECOND • TIMEZONE_HOUR • TIMEZONE_MINUTE CURRENT_TIME versus TIME CURRENT_TIME provides similar functionality to the Teradata function TIME using ANSIcompliant syntax. For information on the Teradata TIME function, see “TIME” on page 699. Example 1: Requesting the Current Time If the DBS Control flag TimeDateWZControl is enabled, the following statements request the current time based on the current session time and time zone. SELECT CURRENT_TIME; SELECT CURRENT_TIME AT LOCAL; The result is similar to: Current Time(0) --------------- 15:53:34+00:00 If the session time zone was defined with a time zone string that follows different DST and standard time zone displacements, then the time returned is automatically adjusted to account for the start and end of daylight saving time. Otherwise, no adjustment for daylight saving time is done. Example 2: Requesting the Current Time with a Time Zone String The following queries return the current time at the time zone displacement based on the time zone string, 'America Pacific'. The time returned is automatically adjusted to account for the start and end of daylight saving time. SELECT CURRENT_TIME AT TIME ZONE 'America Pacific'; SELECT CURRENT_TIME AT 'America Pacific'; Example 3: Changing the Default Output Format To change the default output format of the CURRENT_TIME result, use Teradata explicit conversion syntax and specify the FORMAT phrase. For example, the following statement requests the current time and specifies a format that is different from the default: SELECT CURRENT_TIME (FORMAT 'HH:MIBT'); The result looks like this: Current Time(0) --------------- Chapter 17: Built-In Functions CURRENT_TIME 680 SQL Functions, Operators, Expressions, and Predicates 02:29 PM For more information on Teradata explicit conversion syntax, see “Teradata Conversion Syntax in Explicit Data Type Conversions” on page 755. For more information on default data type formats and the FORMAT phrase, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Chapter 17: Built-In Functions CURRENT_TIMESTAMP SQL Functions, Operators, Expressions, and Predicates 681 CURRENT_TIMESTAMP Purpose Returns the current timestamp. Syntax where: ANSI Compliance CURRENT_TIMESTAMP and the AT clause are ANSI SQL:2008 compliant. As an extension to ANSI, you can specify the time zone displacement using additional expressions besides an INTERVAL expression. 1101A715 CURRENT_TIMESTAMP (fractional_precision ) expression time_zone_string AT LOCAL TIME ZONE Syntax element … Specifies … fractional_precision an optional precision range for the returned timestamp value. The valid range is 0 through 6, inclusive. The default is 6. AT LOCAL that the value returned is constructed from the session time and session time zone if the DBS Control flag TimeDateWZControl is enabled. If TimeDateWZControl is disabled, the value returned is constructed from the time value local to the Teradata Database server and the session time zone. AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. Chapter 17: Built-In Functions CURRENT_TIMESTAMP 682 SQL Functions, Operators, Expressions, and Predicates Usage Notes CURRENT_TIMESTAMP returns the current timestamp when the request started. If CURRENT_TIMESTAMP is invoked more than once during the request, the same timestamp is returned. The timestamp returned does not change during the duration of the request. If you specify CURRENT_TIMESTAMP without the AT clause or CURRENT_TIMESTAMP AT LOCAL, then the value returned depends on the setting of the DBS Control flag TimeDateWZControl as follows: • If the TimeDateWZControl flag is enabled, CURRENT_TIMESTAMP returns a timestamp constructed from the session time and session time zone. • If the TimeDateWZControl flag is disabled, CURRENT_TIMESTAMP returns a timestamp constructed from the time value local to the Teradata Database server and the session time zone. For more information, see “DBS Control (dbscontrol)” in Utilities. CURRENT_TIMESTAMP returns a value that is adjusted to account for the start and end of daylight saving time (DST) only in the following cases: • CURRENT_TIMESTAMP is specified with AT [TIME ZONE] time_zone_string, where time_zone_string follows different DST and standard time zone displacements. • CURRENT_TIMESTAMP is specified with AT LOCAL or without an AT clause and the session time zone was defined with a time zone string that follows different DST and standard time zone displacements. For more information about time zone strings, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. Result Type and Attributes The result data type and format for CURRENT_TIMESTAMP are: To convert CURRENT_TIMESTAMP, use Teradata explicit conversion syntax or ANSI CAST syntax. For an example that uses Teradata explicit conversion syntax to change the default output format, see “Example 4: Changing the Default Output Format” on page 684. Precision The seconds precision of the result of CURRENT_TIMESTAMP is limited to hundredths of a second. CURRENT_TIMESTAMP returns zeros for any digits to the right of the two most significant digits in the fractional portion of seconds. Data Type Format TIMESTAMP WITH TIME ZONE Default format for the TIMESTAMP WITH TIME ZONE data type. For more information on the default formats, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Chapter 17: Built-In Functions CURRENT_TIMESTAMP SQL Functions, Operators, Expressions, and Predicates 683 CURRENT_TIMESTAMP Fields The fields in CURRENT_TIMESTAMP are: • YEAR • MONTH • DAY • HOUR • MINUTE • SECOND • TIMEZONE_HOUR • TIMEZONE_MINUTE Example 1: Requesting the Current Timestamp If the DBS Control flag TimeDateWZControl is enabled, the following statements request the current timestamp based on the current session time and time zone. SELECT CURRENT_TIMESTAMP; SELECT CURRENT_TIMESTAMP AT LOCAL; The result is similar to: Current TimeStamp(6) -------------------------------- 2001-11-27 15:53:34.910000+00:00 If the session time zone was defined with a time zone string that follows different DST and standard time zone displacements, then the timestamp returned is automatically adjusted to account for the start and end of daylight saving time. Otherwise, no adjustment for daylight saving time is done. Example 2: CURRENT_TIMESTAMP and the TimeDateWZControl Flag This example shows the effect of the DBS Control flag TimeDateWZControl on the results returned by CURRENT_TIMESTAMP when the function is specified without an AT clause or with an AT LOCAL clause. Assume the following: • The time local to the Teradata Database server is 11:59:00 Coordinated Universal Time (UTC), January 31, 2010. • User TK lives in Tokyo, and has a time zone defined as +9 hours offset from UTC. • User LA lives in Los Angeles, and has a time zone defined as -8 hours offset from UTC. • User TK and User LA run the CURRENT_TIMESTAMP function at exactly the same time. If the TimeDateWZControl flag is enabled: For User TK, the CURRENT_TIMESTAMP function returns: 2010-02-01 10:59:00.000000+09:00 For User LA, the CURRENT_TIMESTAMP function returns: Chapter 17: Built-In Functions CURRENT_TIMESTAMP 684 SQL Functions, Operators, Expressions, and Predicates 2010-01-31 16:59:00.000000-08:00 If the TimeDateWZControl flag is disabled: For User TK, the CURRENT_TIMESTAMP function returns: 2010-01-31 11:59:00.000000+09:00 For User LA, the CURRENT_TIMESTAMP function returns: 2010-01-31 11:59:00.000000-08:00 Example 3: Requesting the Current Timestamp with a Time Zone String The following queries return the current timestamp at the time zone displacement based on the time zone string, 'America Pacific'. The timestamp returned is automatically adjusted to account for the start and end of daylight saving time. SELECT CURRENT_TIMESTAMP AT TIME ZONE 'America Pacific'; SELECT CURRENT_TIMESTAMP AT 'America Pacific'; Example 4: Changing the Default Output Format To change the default output format of the CURRENT_TIMESTAMP result, use Teradata explicit conversion syntax and specify the FORMAT phrase. For example, the following statement requests the current timestamp and specifies a format that is different from the default: SELECT CURRENT_TIMESTAMP (FORMAT 'MMMBDD,BYYYYBHH:MIBT'); The result looks like this: Current TimeStamp(6) --------------------- Feb 19, 2002 07:45 am For more information on Teradata explicit conversion syntax, see “Teradata Conversion Syntax in Explicit Data Type Conversions” on page 755. For more information on default data type formats and the FORMAT phrase, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Chapter 17: Built-In Functions CURRENT_USER SQL Functions, Operators, Expressions, and Predicates 685 CURRENT_USER Purpose Provides the user name of the current authorized user. Syntax ANSI Compliance CURRENT_USER is consistent with ANSI SQL:2008 usage. Result Type and Attributes The data type and format for CURRENT_USER are as follows: Result Value If you are accessing the Teradata Database through a proxy connection, CURRENT_USER returns the proxy user name. Otherwise, it functions exactly like the USER built-in function and returns the session user name. For details, see “USER” on page 702. Example 1 You can identify the current authorized user with the following statement: SELECT CURRENT_USER; The system responds with something like the following: Current_User ------------------------------ BO-JSMITH Example 2 The following example selects the job title for the current authorized user: SELECT JobTitle FROM Employee WHERE Name = CURRENT_USER; 1101A564 CURRENT_USER Data Type Format VARCHAR(30) CHARACTER SET UNICODE X(30) Chapter 17: Built-In Functions DATABASE 686 SQL Functions, Operators, Expressions, and Predicates DATABASE Purpose Returns the name of the default database for the current user. Syntax ANSI Compliance DATABASE is a Teradata extension to the ANSI SQL:2008 standard. Result Type and Attributes The data type and format for DATABASE are as follows: Usage Notes If a DATABASE request has changed the current default database, then the DATABASE function returns the new name of the default. Example The following statement requests the name of the default database: SELECT DATABASE; The system responds with something like the following: Database ------------------------------ Customer_Service FF07R002 DATABASE Data Type Format VARCHAR(30) CHARACTER SET UNICODE X(30) Chapter 17: Built-In Functions DATE SQL Functions, Operators, Expressions, and Predicates 687 DATE Purpose Returns the current date. Syntax where: ANSI Compliance DATE is a Teradata extension to the ANSI SQL:2008 standard. For the ANSI-compliant syntax and behavior for the equivalent function, see “CURRENT_DATE” on page 671. The AT clause is ANSI SQL:2008 compliant. As an extension to ANSI, you can specify an AT clause after the DATE function, and you can specify the time zone displacement using additional expressions besides an INTERVAL expression. Syntax element … Specifies … AT LOCAL that the value returned is constructed from the session time and session time zone if the DBS Control flag TimeDateWZControl is enabled. If TimeDateWZControl is disabled, the value returned is constructed from the time value local to the Teradata Database server and the session time zone. AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. 1101A683 DATE expression time_zone_string AT LOCAL TIME ZONE Chapter 17: Built-In Functions DATE 688 SQL Functions, Operators, Expressions, and Predicates Usage Notes DATE returns the current date at the time when the request started. If DATE is invoked more than once during the request, the same date is returned. The date returned does not change during the duration of the request. If you specify DATE without the AT clause or DATE AT LOCAL, then the value returned depends on the setting of the DBS Control flag TimeDateWZControl as follows: • If the TimeDateWZControl flag is enabled, DATE returns a date constructed from the session time and session time zone. • If the TimeDateWZControl flag is disabled, DATE returns a date constructed from the time value local to the Teradata Database server and the session time zone. For more information, see “DBS Control (dbscontrol)” in Utilities. DATE returns a value that is adjusted to account for the start and end of daylight saving time (DST) only in the following cases: • DATE is specified with AT [TIME ZONE] time_zone_string, where time_zone_string follows different DST and standard time zone displacements. • DATE is specified with AT LOCAL or without an AT clause and the session time zone was defined with a time zone string that follows different DST and standard time zone displacements. For more information about time zone strings, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. DATE cannot appear as the first argument in a user-defined method invocation. Result Type and Attributes DATE versus CURRENT_DATE DATE is deprecated. Use the ANSI SQL:2008 compliant CURRENT_DATE function instead. See “CURRENT_DATE” on page 671. Data Type FORMAT DATE The default format of DATE depends on the value of the Dateform mode. IF the value of the Dateform mode is … THEN the format of the DATE function is … INTEGERDATE the default format for DATE data types as specified in the SDF. ANSIDATE 'YYYY-MM-DD' For more information on default data type formats, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Chapter 17: Built-In Functions DATE SQL Functions, Operators, Expressions, and Predicates 689 Example 1 This example assumes that the default format for DATE values is 'yy/mm/dd'. Consider the following statements: SET TIME ZONE INTERVAL '01:00' HOUR TO MINUTE; SELECT DATE AT TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE; SELECT DATE AT INTERVAL -'08:00' HOUR TO MINUTE; SELECT DATE AT TIME ZONE INTERVAL -'08' HOUR; SELECT DATE AT INTERVAL -'08' HOUR; SELECT DATE AT TIME ZONE '-08:00'; SELECT DATE AT '-08:00'; SELECT DATE AT TIME ZONE '-8'; SELECT DATE AT '-8'; SELECT DATE AT TIME ZONE -8; SELECT DATE AT -8; SELECT DATE AT -8.0; The above SELECT statements return the current date based on the time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE. If the current timestamp at UTC is TIMESTAMP '2008-06-01 06:30:00.000000+00:00', these SELECT statements would return '08/05/31' as the date. If the SELECT statement was specified without an AT clause or with an AT LOCAL clause, and the DBS Control flag TimeDateWZControl is enabled, the statement would return '08/06/01' as the current date based on the current session time and time zone displacement, INTERVAL '01:00' HOUR TO MINUTE. For example: SELECT DATE; SELECT DATE AT LOCAL; The date returned is not adjusted to account for the start or end of daylight saving time. Example 2 The following queries return the current date at the time zone displacement based on the time zone string, 'America Pacific'. Teradata Database determines the time zone displacement based on the time zone string and the CURRENT_TIMESTAMP AT '00:00' (that is, at UTC). The date returned is automatically adjusted to account for the start and end of daylight saving time. SELECT DATE AT TIME ZONE 'America Pacific'; SELECT DATE AT 'America Pacific'; Example 3 Use the FORMAT phrase to change the presentation: SELECT DATE (FORMAT 'mm-dd-yy'); Date -------- 03-30-96 Chapter 17: Built-In Functions DATE 690 SQL Functions, Operators, Expressions, and Predicates Example 4 Another form gives: SELECT DATE (FORMAT 'mmmbdd,byyyy'); Date ------------ Mar 30, 1996 Chapter 17: Built-In Functions PROFILE SQL Functions, Operators, Expressions, and Predicates 691 PROFILE Purpose Returns the current profile for the session or NULL if none. Syntax ANSI Compliance PROFILE is a Teradata extension to the ANSI SQL:2008 standard. Result Type and Attributes The data type and format for PROFILE are as follows: Example You can identify the current profile for the session with the following statement: SELECT PROFILE ; PROFILE KZ01A006 Data Type Format VARCHAR(30) CHARACTER SET UNICODE X(30) Chapter 17: Built-In Functions ROLE 692 SQL Functions, Operators, Expressions, and Predicates ROLE Purpose Returns the session current role. Syntax ANSI Compliance ROLE is a Teradata extension to the ANSI SQL:2008 standard. Result Type and Attributes The data type and format for ROLE are as follows: Result Value ROLE KZ01A007 Data Type Format VARCHAR(30) CHARACTER SET UNICODE X(30) IF the session logon is … THEN … not directorybased IF the current role for the session is … THEN the result value is … an existing role the name of the role. ALL 'ALL'. NONE or NULL NULL. Chapter 17: Built-In Functions ROLE SQL Functions, Operators, Expressions, and Predicates 693 If you are accessing the Teradata Database through a proxy connection, and you want to get the current role of the proxy user, use the CURRENT_ROLE built-in function. For details, see “CURRENT_ROLE” on page 675. Usage Notes ROLE is not supported in the FastLoad and MultiLoad utilities. Example You can identify the session current role with the following statement: SELECT ROLE; The system responds with something like the following: Role directorybased IF the session … THEN the result value is … is assigned a set of directory-managed roles and does not change the current role 'EXTERNAL'. uses a SET ROLE EXTERNAL statement • does not have an assigned set of directory-managed roles, • maps to a permanent user that has a default databasemanaged role, and • does not change the current role the name of the default role of the permanent user. uses a SET ROLE role_name statement, where role_name is either a directory-managed or databasemanaged role the name of the specified role. uses a SET ROLE ALL statement 'ALL'. • is not assigned a set of directory-managed roles, • does not change the current role, and • one of the following conditions is true: • the directory-based logon does not map to a permanent user • the permanent user that the directory-based logon maps to does not have a default database-managed role NULL. uses a SET ROLE NONE statement uses a SET ROLE NULL statement IF the session logon is … THEN … Chapter 17: Built-In Functions ROLE 694 SQL Functions, Operators, Expressions, and Predicates ------------------------------ EXTERNAL Chapter 17: Built-In Functions SESSION SQL Functions, Operators, Expressions, and Predicates 695 SESSION Purpose Returns the number of the session for the current user. Syntax ANSI Compliance SESSION is a Teradata extension to the ANSI SQL:2008 standard. Result Type and Attributes The data type and format for SESSION are as follows: Example The following statement identifies the number of the session for the current user: SELECT SESSION; The system responds with something like the following: Session ----------- 1048 FF07R003 SESSION Data Type Format INTEGER Default format for the INTEGER data type. For more information on the default formats, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Chapter 17: Built-In Functions TEMPORAL_DATE 696 SQL Functions, Operators, Expressions, and Predicates TEMPORAL_DATE Purpose Returns the current transaction date where the evaluation is based on the session time zone. Syntax Result Type and Attributes The result data type and format for TEMPORAL_DATE are as follows: Usage Notes The value of TEMPORAL_DATE is the same for all requests submitted in a single transaction. The system uses the session time zone to evaluate TEMPORAL_DATE. When TEMPORAL_DATE appears in a CHECK constraint or DEFAULT clause, the result value is evaluated when the request applies the CHECK constraint (during an insert or update) or when the request uses the DEFAULT value for a given column. For information on using TEMPORAL_DATE with temporal tables, see Temporal Table Support. Restrictions TEMPORAL_DATE is not supported in a partitioning expression for the PARTITION BY clause that defines a partitioned primary index. 1182A008 TEMPORAL_DATE Data Type Format DATE Default format for the DATE data type when the Dateform mode is set to IntegerDate. For details on default formats, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Chapter 17: Built-In Functions TEMPORAL_TIMESTAMP SQL Functions, Operators, Expressions, and Predicates 697 TEMPORAL_TIMESTAMP Purpose Returns the current transaction timestamp where the evaluation is based on the session time zone. Syntax where: Result Type and Attributes The result data type and format for TEMPORAL_TIMESTAMP are as follows: Usage Notes The value of TEMPORAL_TIMESTAMP is the same for all requests submitted in a single transaction. The system uses the session time zone to evaluate TEMPORAL_TIMESTAMP. When TEMPORAL_TIMESTAMP appears in a CHECK constraint or DEFAULT clause, the result value is evaluated when the request applies the CHECK constraint (during an insert or update) or when the request uses the DEFAULT value for a given column. Syntax element … Specifies … precision an optional precision range for the returned timestamp value. The valid range is 0 through 6, inclusive. The default is 6. 1182A009 TEMPORAL_TIMESTAMP ( precision ) Data Type Format TIMESTAMP(n) WITH TIME ZONE, where n is the same as the precision argument or 6 if omitted Default format for the TIMESTAMP WITH TIME ZONE type. For details on default formats, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Chapter 17: Built-In Functions TEMPORAL_TIMESTAMP 698 SQL Functions, Operators, Expressions, and Predicates For information on using TEMPORAL_TIMESTAMP with temporal tables, see Temporal Table Support. Precision The seconds precision of the result of TEMPORAL_TIMESTAMP is limited to hundredths of a second. TEMPORAL_TIMESTAMP returns zeros for any digits to the right of the two most significant digits in the fractional portion of seconds. Chapter 17: Built-In Functions TIME SQL Functions, Operators, Expressions, and Predicates 699 TIME Purpose Returns the current time. Syntax where: ANSI Compliance TIME is a Teradata extension to the ANSI SQL:2008 standard. For the ANSI-compliant syntax and behavior for the equivalent function, see “CURRENT_TIME” on page 677. The AT clause is ANSI SQL:2008 compliant. As an extension to ANSI, you can specify the time zone displacement using additional expressions besides an INTERVAL expression. Syntax element … Specifies … AT LOCAL that the value returned is constructed from the session time and session time zone if the DBS Control flag TimeDateWZControl is enabled. If TimeDateWZControl is disabled, the value returned is constructed from the time value local to the Teradata Database server and the session time zone. AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. 1101A716 TIME expression time_zone_string AT LOCAL TIME ZONE Chapter 17: Built-In Functions TIME 700 SQL Functions, Operators, Expressions, and Predicates Usage Notes TIME returns the current time when the request started. If TIME is invoked more than once during the request, the same time is returned. The time returned does not change during the duration of the request. If you specify TIME without the AT clause or TIME AT LOCAL, then the value returned depends on the setting of the DBS Control flag TimeDateWZControl as follows: • If the TimeDateWZControl flag is enabled, TIME returns a time constructed from the session time and session time zone. • If the TimeDateWZControl flag is disabled, TIME returns a time constructed from the time value local to the Teradata Database server and the session time zone. For more information, see “DBS Control (dbscontrol)” in Utilities. TIME returns a value that is adjusted to account for the start and end of daylight saving time (DST) only in the following cases: • TIME is specified with AT [TIME ZONE] time_zone_string, where time_zone_string follows different DST and standard time zone displacements. • TIME is specified with AT LOCAL or without an AT clause and the session time zone was defined with a time zone string that follows different DST and standard time zone displacements. For more information about time zone strings, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. TIME cannot appear as the first argument in a user-defined method invocation. Result Type and Attributes The data type and format for TIME are as follows: TIME versus CURRENT_TIME TIME is deprecated. Use the ANSI SQL:2008 compliant CURRENT_TIME function instead. See “CURRENT_TIME” on page 677. Example 1 If the DBS Control flag TimeDateWZControl is enabled, the following statements request the current time based on the current session time and time zone. SELECT TIME; SELECT TIME AT LOCAL; The result is similar to: Data Type Format FLOAT HHMMSS.CC (hours, minutes, seconds, hundredths of a second) Chapter 17: Built-In Functions TIME SQL Functions, Operators, Expressions, and Predicates 701 Time -------- 16:20:20 If the session time zone was defined with a time zone string that follows different DST and standard time zone displacements, then the time returned is automatically adjusted to account for the start and end of daylight saving time. Otherwise, no adjustment for daylight saving time is done. Example 2 The following queries return the current time at the time zone displacement based on the time zone string, 'America Pacific'. The time returned is automatically adjusted to account for the start and end of daylight saving time. SELECT TIME AT TIME ZONE 'America Pacific'; SELECT TIME AT 'America Pacific'; Example 3 The hundredths of a second are not displayed by the default format, but you can use the FORMAT phrase to display it: SELECT TIME (FORMAT '99:99:99.99'); The system responds with something like the following: Time ----------- 16:26:30.19 Example 4 The following example inserts a row in a hypothetical table in which the column InsertTime has data type FLOAT and records the time that the row was inserted: INSERT INTO HypoTable (ColumnA, ColumnB, InsertTime) VALUES ('Abcde', 12345, TIME); Chapter 17: Built-In Functions USER 702 SQL Functions, Operators, Expressions, and Predicates USER Purpose Provides the session user name. Syntax ANSI Compliance USER is ANSI SQL:2008 compliant. Result Type and Attributes The data type and format for USER are as follows: Result Value If you are accessing the Teradata Database through a proxy connection, and you want to get the name of the proxy user, use the CURRENT_USER built-in function. For details, see “CURRENT_USER” on page 685. FF07D272 USER Data Type Format VARCHAR(30) CHARACTER SET UNICODE X(30) IF the session logon is … THEN … not directory-based the result value is the session user name. directory-based IF the session … THEN the result value … maps to a permanent user is the name of the permanent user. does not map to a permanent user is the authcid of the external user. Chapter 17: Built-In Functions USER SQL Functions, Operators, Expressions, and Predicates 703 Example 1 You can identify the session user name with the following statement: SELECT USER; The system responds with something like the following. User ------------------------------ JJ43901 Example 2 The following example selects the job title for the session user. SELECT JobTitle FROM Employee WHERE Name = USER; Chapter 17: Built-In Functions USER 704 SQL Functions, Operators, Expressions, and Predicates SQL Functions, Operators, Expressions, and Predicates 705 CHAPTER 18 User-Defined Functions SQL provides a set of useful functions, but they might not satisfy all of the particular requirements you have to process your data. Teradata Database supports two types of user-defined functions (UDFs) that allow you to extend SQL by writing your own functions: • SQL UDFs • External UDFs SQL UDFs allow you to encapsulate regular SQL expressions in functions and then use them like standard SQL functions. External UDFs allow you to write your own functions in the C, C++, or Java programming language, install them on the database, and then use them like standard SQL functions. For details on external UDFs, see SQL External Routine Programming. UDFs can be of the following types: • Scalar • Aggregate or Window Aggregate • Table A scalar UDF can appear almost anywhere a standard SQL scalar function can appear, and an aggregate UDF can appear almost anywhere a standard SQL aggregate function can appear. A table UDF can only appear in the FROM clause of an SQL SELECT statement. A window aggregate UDF is an aggregate UDF with a window specification applied to it. Chapter 18: User-Defined Functions SQL UDF 706 SQL Functions, Operators, Expressions, and Predicates SQL UDF Purpose A user-defined function written using regular SQL expressions and used like a standard SQL function. Syntax where: ANSI Compliance SQL UDFs are partially ANSI SQL:2008 compliant. The requirement that parentheses appear when the argument list is empty is a Teradata extension to preserve compatibility with existing applications. Restrictions Self-referencing, forward-referencing, and circular-referencing SQL UDFs are not allowed. Authorization You must have EXECUTE FUNCTION privilege on the function or on the database containing the function. You can specify an SQL SECURITY clause with the DEFINER option in the CREATE/ REPLACE FUNCTION statement. This option is the default for an SQL UDF. SQL SECURITY DEFINER means that when an SQL UDF is invoked, Teradata Database verifies that the immediate owner and the creator of the UDF have the appropriate privileges on the underlying database objects referenced in the UDF. If the creator does not exist when the privileges are checked, an error is returned. Syntax element … Specifies … udf_name the name of the SQL UDF. argument a valid SQL expression. See Usage Notes for rules that apply to SQL UDF arguments. 1101A640 udf_name , ( argument ( Chapter 18: User-Defined Functions SQL UDF SQL Functions, Operators, Expressions, and Predicates 707 To invoke a UDF that takes a UDT argument or returns a UDT, you must have the UDTUSAGE privilege on the SYSUDTLIB database or on the specified UDT. Usage Notes An SQL UDF is a function that is defined by a user and is written using SQL expressions. When Teradata Database evaluates an SQL UDF expression, it invokes the function with the arguments passed to it. The following rules apply to the arguments in the function call: • The arguments must be comma-separated expressions in the same order as the parameters declared in the function. • The number of arguments passed to the SQL UDF must be the same as the number of parameters declared in the function. • The data types of the arguments must be compatible with the corresponding parameter declarations in the function and follow the precedence rules that apply to compatible types. For details, see SQL Data Definition Language. To pass an argument that is not compatible with the corresponding parameter type, use CAST to explicitly convert the argument to the proper type. For information, see “CAST in Explicit Data Type Conversions” on page 752. • A NULL argument is compatible with a parameter of any data type. You can pass a NULL argument explicitly or by omitting the argument. • Any form of SQL expression can be used as an argument with three important rules: • The SQL expression must not be a Boolean value expression (that is, a conditional expression). • If the expression is a nondeterministic SQL expression (expressions involving random functions and/or nondeterministic UDFs), it must not correspond to a parameter that is used more than once in the RETURN statement. • The SQL expression must not be a scalar subquery. When an SQL UDF is invoked, Teradata Database searches for the UDF in the following locations: • In the database specified if the function call is qualified by a database name. • In the current database. • In the SYSLIB database. For details regarding UDF search resolution, see SQL Data Definition Language. The result type of an SQL UDF is based on the return type of the SQL UDF, which is specified in the RETURNS clause of the CREATE FUNCTION statement. The default title of an SQL UDF appears as: UDF_name(argument_list) Example 1 Consider the following function definition and query: CREATE FUNCTION Test.MyUDF (a INT, b INT, c INT) Chapter 18: User-Defined Functions SQL UDF 708 SQL Functions, Operators, Expressions, and Predicates RETURNS INT LANGUAGE SQL CONTAINS SQL DETERMINISTIC SQL SECURITY DEFINER COLLATION INVOKER INLINE TYPE 1 RETURN a + b - c; SELECT Test.MyUDF(t1.a1, t2.a2, t3.a3) FROM t1, t2, t3; The user executing the SELECT statement must have the following privileges: • SELECT privilege on tables t1, t2, and t3, their containing databases, or on the columns t1.a1, t2.a2, and t3.a3. • EXECUTE FUNCTION privilege on MyUDF or on the database named Test. The privileges of the creator or owner are not checked since the UDF does not reference any database objects in its definition. Example 2 In this example, the SQL UDF named MySQLUDF references an external UDF named MyExtUDF in the RETURN statement. Consider the following function definition and query: CREATE FUNCTION Test.MySQLUDF (a INT, b INT, c INT) RETURNS INT LANGUAGE SQL CONTAINS SQL DETERMINISTIC SQL SECURITY DEFINER COLLATION INVOKER INLINE TYPE 1 RETURN a + b * MyExtUDF(a, b) - c; SELECT Test.MySQLUDF(t1.a1, t2.a2, t3.a3) FROM t1, t2, t3; The user executing the SELECT statement must have the following privileges: • SELECT privilege on tables t1, t2, and t3, their containing databases, or on the columns t1.a1, t2.a2, and t3.a3. • EXECUTE FUNCTION privilege on MySQLUDF or on the database named Test. Because the SQL UDF references MyExtUDF, the following privileges are also checked: • The creator of MySQLUDF must exist and have the EXECUTE FUNCTION privilege on MyExtUDF or its containing database. • The database named Test (the immediate owner of MySQLUDF) must have the EXECUTE FUNCTION privilege on MyExtUDF or its containing database. Chapter 18: User-Defined Functions SQL UDF SQL Functions, Operators, Expressions, and Predicates 709 Example 3 In this example, invocations of the SQL UDF named MyUDF2 are passed as arguments to the SQL UDF named MyUDF1. CREATE FUNCTION test.MyUDF1 (a INT, b INT, c INT) RETURNS INT LANGUAGE SQL CONTAINS SQL DETERMINISTIC COLLATION INVOKER INLINE TYPE 1 RETURN a * b * c; CREATE FUNCTION test.MyUDF2 (d INT, e INT, f INT) RETURNS INT LANGUAGE SQL CONTAINS SQL DETERMINISTIC COLLATION INVOKER INLINE TYPE 1 RETURN d + e + f; SELECT test.MyUDF1(test.MyUDF2(t1.a1, 1, 2), test.MyUDF2(t1.b1, 2, 3), 5) FROM t1; Example 4 In this example, the UDF invocation argument data type (BYTEINT) is not the same as that of the corresponding UDF parameter data type (INTEGER) since the size of the argument data type is less than the UDF parameter data type. However, because the two data types are compatible and a BYTEINT argument can fit inside an INTEGER parameter, this is allowed. CREATE FUNCTION test.MyUDF (a INT, b INT, c INT) RETURNS INT LANGUAGE SQL CONTAINS SQL DETERMINISTIC COLLATION INVOKER INLINE TYPE 1 RETURN a * b * c; CREATE TABLE t1 (a1 BYTEINT, b1 INT); SELECT test.MyUDF(t1.a1, t1.b1, 2) FROM t1; Example 5 In this example, the UDF invocation argument data type (INTEGER) is not the same as that of the corresponding UDF parameter data type (BYTEINT) since the size of the argument data type is greater than the UDF parameter data type. Although the two data types are compatible, an INTEGER argument cannot fit inside a BYTEINT parameter, so an error is returned. Chapter 18: User-Defined Functions SQL UDF 710 SQL Functions, Operators, Expressions, and Predicates CREATE FUNCTION test.MyUDF (a BYTEINT, b INT, c INT) RETURNS INT LANGUAGE SQL CONTAINS SQL DETERMINISTIC COLLATION INVOKER INLINE TYPE 1 RETURN a * b * c; CREATE TABLE t1 (a1 INT, b1 INT); SELECT test.MyUDF(t1.a1, t1.b1, 2) FROM t1; The following error is returned: Failure 5589: Function "test.MyUDF" does not exist. To avoid the error, the caller must explicitly cast the value of t1.a1 to BYTEINT as follows: SELECT test.MyUDF(CAST(t1.a1 AS BYTEINT), t1.b1, 2) FROM t1; Related Topics FOR more information on … SEE … • CREATE FUNCTION • REPLACE FUNCTION • SQL Data Definition Language. • Database Administration. EXECUTE FUNCTION and UDTUSAGE privileges SQL Data Control Language. Chapter 18: User-Defined Functions Scalar UDF SQL Functions, Operators, Expressions, and Predicates 711 Scalar UDF Purpose A user-defined function that takes input arguments and returns a single value result. Syntax where: ANSI Compliance Scalar UDFs are partially ANSI SQL:2008 compliant. The requirement that parentheses appear when the argument list is empty is a Teradata extension to preserve compatibility with existing applications. Restrictions • Any restrictions that apply to standard SQL scalar functions also apply to scalar UDFs. • Scalar UDF expressions cannot be used in a partitioning expression of the CREATE TABLE statement. Authorization You must have EXECUTE FUNCTION privileges on the function or on the database containing the function. To invoke a scalar UDF that takes a UDT argument or returns a UDT, you must have the UDTUSAGE privilege on the SYSUDTLIB database or on the specified UDT. Syntax element … Specifies … udf_name the name of the scalar UDF. argument a valid SQL expression. See Usage Notes for rules that apply to scalar UDF arguments. 1101A640 udf_name , ( argument ( Chapter 18: User-Defined Functions Scalar UDF 712 SQL Functions, Operators, Expressions, and Predicates Usage Notes When Teradata Database evaluates a scalar UDF expression, it invokes the scalar function with the arguments passed to it. The following rules apply to the arguments in the function call: • The arguments must be comma-separated expressions in the same order as the parameters declared in the function. • The number of arguments passed to the scalar UDF must be the same as the number of parameters declared in the function. • The data types of the arguments must be compatible with the corresponding parameter declarations in the function and follow the precedence rules that apply to compatible types. For details, see SQL External Routine Programming. To pass an argument that is not compatible with the corresponding parameter type, use CAST to explicitly convert the argument to the proper type. For information, see “CAST in Explicit Data Type Conversions” on page 752. • A NULL argument is compatible with a parameter of any data type. You can pass a NULL argument explicitly or by omitting the argument. The result type of a scalar UDF is based on the return type of the scalar UDF, which is specified in the RETURNS clause of the CREATE FUNCTION statement. The default title of a scalar UDF appears as: UDF_name(argument_list) Example Consider the following table definition and data: CREATE TABLE pRecords (pname CHAR(30), pkey INTEGER); SELECT * FROM pRecords; The output from the SELECT statement is: pname pkey ------------------------------ ----------- Tom 6 Bob 5 Jane 4 The following is the SQL definition of a scalar UDF that calculates the factorial of an integer argument: CREATE FUNCTION factorial (i INTEGER) RETURNS INTEGER SPECIFIC factorial LANGUAGE C NO SQL PARAMETER STYLE TD_GENERAL NOT DETERMINISTIC RETURNS NULL ON NULL INPUT EXTERNAL NAME 'ss!factorial!factorial.c!F!fact' Chapter 18: User-Defined Functions Scalar UDF SQL Functions, Operators, Expressions, and Predicates 713 The following query uses the scalar UDF expression to calculate the factorial of the pkey column + 1. SELECT pname, factorial(pkey)+1 FROM pRecords; The output from the SELECT statement is: pname (factorial(pkey)+1) ------------------------------ ------------------- Tom 721 Bob 121 Jane 25 Related Topics FOR more information on … SEE … Implementing external UDFs SQL External Routine Programming. • CREATE FUNCTION • REPLACE FUNCTION • SQL Data Definition Language. • Database Administration. EXECUTE FUNCTION and UDTUSAGE privileges SQL Data Control Language. Chapter 18: User-Defined Functions Aggregate UDF 714 SQL Functions, Operators, Expressions, and Predicates Aggregate UDF Purpose A user-defined function that takes grouped sets of relational data, makes a pass over each group, and returns one result for the group. Syntax where: ANSI Compliance Aggregate UDFs are partially ANSI SQL:2008 compliant. The requirement that parentheses appear when the argument list is empty is a Teradata extension to preserve compatibility with existing applications. Restrictions • Any restrictions that apply to standard SQL aggregate functions also apply to aggregate UDFs. • Aggregate UDF expressions cannot appear in a recursive statement of a recursive query. However, a non-recursive seed statement in a recursive query can specify an aggregate UDF. Authorization You must have EXECUTE FUNCTION privileges on the function or on the database containing the function. To invoke an aggregate UDF that takes a UDT argument or returns a UDT, you must have the UDTUSAGE privilege on the SYSUDTLIB database or on the specified UDT. Syntax element … Specifies … udf_name the name of the aggregate UDF. argument a valid SQL expression. See Usage Notes for rules that apply to aggregate UDF arguments. 1101A640 udf_name , ( argument ( Chapter 18: User-Defined Functions Aggregate UDF SQL Functions, Operators, Expressions, and Predicates 715 Usage Notes When Teradata Database evaluates an aggregate UDF expression, it invokes the aggregate function once for each item in an aggregation group, passing the detail values of a group through the input arguments. To accumulate summary information, the context is retained each time the aggregate function is called. The following rules apply to the arguments in the function call: • The arguments must be comma-separated expressions in the same order as the parameters declared in the function. • The number of arguments passed to the aggregate UDF must be the same as the number of parameters declared in the function. • The data types of the arguments must be compatible with the corresponding parameter declarations in the function and follow the precedence rules that apply to compatible types. For details, see SQL External Routine Programming. To pass an argument that is not compatible with the corresponding parameter type, use CAST to explicitly convert the argument to the proper type. For information, see “CAST in Explicit Data Type Conversions” on page 752. • A NULL argument is compatible with a parameter of any data type. You can pass a NULL argument explicitly or by omitting the argument. The result type of an aggregate UDF is based on the return type of the aggregate UDF, which is specified in the RETURNS clause of the CREATE FUNCTION statement. The default title of an aggregate UDF appears as: UDF_name(argument_list) Example Consider the following table definition and data: CREATE TABLE Product_Life (Product_ID INTEGER, Product_class VARCHAR(30), Hours INTEGER); SELECT * FROM Product_Life; The output from the SELECT statement is: Product_ID Product_class Hours ----------- ------------------------------ ----------- 100 Bulbs 100 100 Bulbs 200 100 Bulbs 300 The following is the SQL definition of an aggregate UDF that calculates the standard deviation of the input arguments: CREATE FUNCTION STD_DEV (i INTEGER) RETURNS FLOAT CLASS AGGREGATE (64) SPECIFIC std_dev Chapter 18: User-Defined Functions Aggregate UDF 716 SQL Functions, Operators, Expressions, and Predicates LANGUAGE C NO SQL PARAMETER STYLE SQL NOT DETERMINISTIC CALLED ON NULL INPUT EXTERNAL NAME 'ss!stddev!stddev.c!f!STD_DEV' The following query uses the aggregate UDF expression to calculate the standard deviation for the life of a light bulb. SELECT Product_ID, SUM(Hours), STD_DEV(Hours) FROM Product_Life WHERE Product_class = 'Bulbs' GROUP BY Product_ID; The output from the SELECT statement is: Product_ID Sum(hours) std_dev(hours) ----------- ----------- ---------------------- 100 600 8.16496580927726E 001 Related Topics FOR more information on … SEE … SQL aggregate functions “Chapter 10 Aggregate Functions” on page 345. window aggregate UDFs “Window Aggregate UDF” on page 717. implementing aggregate UDFs SQL External Routine Programming. • CREATE FUNCTION • REPLACE FUNCTION • SQL Data Definition Language. • Database Administration. EXECUTE FUNCTION and UDTUSAGE privileges SQL Data Control Language. Chapter 18: User-Defined Functions Window Aggregate UDF SQL Functions, Operators, Expressions, and Predicates 717 Window Aggregate UDF Purpose An aggregate UDF with a window specification applied to it, which allows the function to operate on a specified window of rows. Syntax udf_name , ( argument ( 1101A786 A window A Chapter 18: User-Defined Functions Window Aggregate UDF 718 SQL Functions, Operators, Expressions, and Predicates where: Syntax element … Specifies … udf_name the name of the aggregate UDF on which the window specification is applied. argument a valid SQL expression. For rules that apply to aggregate UDF arguments, see “Aggregate UDF” on page 714. window OVER ( ROWS UNBOUNDED PRECEDING CURRENT ROW ROWS BETWEEN UNBOUNDED FOLLOWING CURRENT ROW B A PARTITION BY column_reference , value PRECEDING UNBOUNDED PRECEDING AND value PRECEDING value FOLLOWING UNBOUNDED FOLLOWING CURRENT ROW value PRECEDING value FOLLOWING value FOLLOWING value PRECEDING AND value FOLLOWING AND CURRENT ROW AND UNBOUNDED FOLLOWING CURRENT ROW value FOLLOWING UNBOUNDED FOLLOWING ORDER BY value_expression , ASC DESC A B 1101B464 RESET WHEN condition ) Chapter 18: User-Defined Functions Window Aggregate UDF SQL Functions, Operators, Expressions, and Predicates 719 OVER how values are grouped, ordered, and considered when computing the cumulative, group, or moving function. Values are grouped according to the PARTITION BY and RESET WHEN clauses, sorted according to the ORDER BY clause, and considered according to the aggregation group within the partition. PARTITION BY in its column_reference, or comma-separated list of column references, the group, or groups, over which the function operates. PARTITION BY is optional. If there is no PARTITION BY or RESET WHEN clauses, then the entire result set, delivered by the FROM clause, constitutes a single group, or partition. PARTITION BY clause is also called the window partition clause. ORDER BY in its value_expression the order in which the values in a group, or partition, are sorted. ASC ascending sort order. The default is ASC. DESC descending sort order. RESET WHEN the group or partition, over which the function operates, depending on the evaluation of the specified condition. If the condition evaluates to TRUE, a new dynamic partition is created inside the specified window partition. RESET WHEN is optional. If there is no RESET WHEN or PARTITION BY clauses, then the entire result set, delivered by the FROM clause, constitutes a single partition. If RESET WHEN is specified, then the ORDER BY clause must be specified also. condition a conditional expression used to determine conditional partitioning. The condition in the RESET WHEN clause is equivalent in scope to the condition in a QUALIFY clause with the additional constraint that nested ordered analytical functions cannot specify a RESET WHEN clause. In addition, you cannot specify SELECT as a nested subquery within the condition. The condition is applied to the rows in all designated window partitions to create sub-partitions within the particular window partitions. For more information, see “RESET WHEN Condition Rules” on page 433 and the “QUALIFY Clause” in SQL Data Manipulation Language. ROWS the starting point for the aggregation group within the partition. The aggregation group end is the current row. The aggregation group of a row R is a set of rows, defined relative to R in the ordering of the rows within the partition. If there is no ROWS or ROWS BETWEEN clause, the default aggregation group is ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING. Syntax element … Specifies … Chapter 18: User-Defined Functions Window Aggregate UDF 720 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance Window aggregate UDFs are partially ANSI SQL:2008 compliant. The requirement that parentheses appear when the argument list of an aggregate UDF is empty is a Teradata extension to preserve compatibility with existing applications. In the presence of an ORDER BY clause and the absence of a ROWS or ROWS BETWEEN clause, ANSI SQL:2008 window aggregate functions use ROWS UNBOUNDED PRECEDING as the default aggregation group, whereas Teradata SQL window aggregate functions use ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING. The RESET WHEN clause is a Teradata extension to the ANSI SQL standard. Authorization You must have EXECUTE FUNCTION privileges on the function or on the database containing the function. To invoke an aggregate UDF that takes a UDT argument or returns a UDT, you must have the UDTUSAGE privilege on the SYSUDTLIB database or on the specified UDT. ROWS BETWEEN the aggregation group start and end, which defines a set of rows relative to the current row in the ordering of the rows within the partition. The row specified by the group start must precede the row specified by the group end. If there is no ROWS or ROWS BETWEEN clause, the default aggregation group is ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING. UNBOUNDED PRECEDING the entire partition preceding the current row. UNBOUNDED FOLLOWING the entire partition following the current row. CURRENT ROW the start or end of the aggregation group as the current row. value PRECEDING the number of rows preceding the current row. The value for value is always a positive integer constant. The maximum number of rows in an aggregation group is 4096 when value PRECEDING appears as the group start or group end. value FOLLOWING the number of rows following the current row. The value for value is always a positive integer constant. The maximum number of rows in an aggregation group is 4096 when value FOLLOWING appears as the group start or group end. Syntax element … Specifies … Chapter 18: User-Defined Functions Window Aggregate UDF SQL Functions, Operators, Expressions, and Predicates 721 Arguments to Window Aggregate UDFs Window aggregate UDFs can take constants, constant expressions, column names (sales, for example), or column expressions (sales + profit) as arguments. Window aggregates can also take regular aggregates as input parameters to the PARTITION BY and ORDER BY clauses. The RESET WHEN clause can take an aggregate as part of the RESET WHEN condition clause. The rules that apply to the arguments of the window aggregate UDF are the same as those that apply to aggregate UDF arguments, see “Aggregate UDF” on page 714. Supported Window Types for Aggregate UDFs Consider the following table definition: CREATE TABLE t (id INTEGER, v INTEGER); The following query specifies a reporting window of rows which the window aggregate UDF MYSUM operates on: SELECT id, v, MYSUM(v) OVER (PARTITION BY id ORDER BY v) FROM t; The following query specifies a cumulative window of rows which the window aggregate UDF MYSUM operates on: SELECT id, v, MYSUM(v) OVER (PARTITION BY id ORDER BY v ROWS UNBOUNDED PRECEDING) FROM t; Window Type Aggregation Group Supported Partitioning Strategy Reporting window ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING Hash partitioning Cumulative window • ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW • ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING Hash partitioning Moving window • ROWS BETWEEN value PRECEDING AND CURRENT ROW • ROWS BETWEEN CURRENT ROW AND value FOLLOWING • ROWS BETWEEN value PRECEDING AND value FOLLOWING • ROWS BETWEEN value PRECEDING AND value PRECEDING • ROWS BETWEEN value FOLLOWING AND value FOLLOWING Hash partitioning and range partitioning Chapter 18: User-Defined Functions Window Aggregate UDF 722 SQL Functions, Operators, Expressions, and Predicates The following query specifies a moving window of rows which the window aggregate UDF MYSUM operates on: SELECT id, v, MYSUM(v) OVER (PARTITION BY id ORDER BY v ROWS BETWEEN 2 PRECEDING AND 3 FOLLOWING) FROM t; Unsupported Window Types for Aggregate UDFs Partitioning The range partitioning strategy helps to avoid hot AMP situations where the values of the columns of the PARTITION BY clause result in the distribution of too many rows to the same partition or AMP. Range and hash partitioning is supported for moving window types. Only hash partitioning is supported for the reporting and cumulative window types because of potential ambiguities that can occur when a user tries to reference previous values assuming a specific ordering within window types like reporting and cumulative, which are semantically not order dependant. You should use an appropriate set of column values for the PARTITION BY clause to avoid potential skew situations for the reporting or cumulative aggregate cases. For more information, see “Data in Partitioning Column of Window Specification and Resource Impact” on page 441. Result Type and Format The result data type of a window aggregate UDF is based on the return type of the aggregate UDF, which is specified in the RETURNS clause of the CREATE FUNCTION statement. The default format of a window aggregate UDF is the default format for the return type. For information on the default format of data types and an explanation of the formatting characters in the format, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Usage Notes You can apply a window specification to an aggregate UDF. The window feature provides a way to dynamically define a subset of data, or window, and allows the aggregate function to operate on that window of rows. Without a window specification, aggregate functions return one value for all qualified rows examined, but window aggregate functions return a new value for each of the qualifying rows participating in the query. Window Type Aggregation Group Moving window • ROWS BETWEEN UNBOUNDED PRECEDING AND value FOLLOWING • ROWS BETWEEN value PRECEDING AND UNBOUNDED FOLLOWING Chapter 18: User-Defined Functions Window Aggregate UDF SQL Functions, Operators, Expressions, and Predicates 723 Problems With Missing Data Ensure that data you analyze has no missing data points. Computing a moving function over data with missing points produces unexpected and incorrect results because the computation considers n physical rows of data rather than n logical data points. Restrictions • The window feature is supported only for aggregate UDFs written in C or C++. The window feature is not supported for aggregate UDFs written in Java. • Range partitioning for the reporting or cumulative window types is not supported. • Any restrictions that apply to aggregate UDFs also apply to window aggregate UDFs. • Any restrictions that apply to the window specification of a standard SQL aggregate function also apply to the window specification of an aggregate UDF. Example Consider the following table definition and inserted data: CREATE MULTISET TABLE t (id INTEGER, v INTEGER); INSERT INTO t VALUES (1,1); INSERT INTO t VALUES (1,2); INSERT INTO t VALUES (1,2); INSERT INTO t VALUES (1,4); INSERT INTO t VALUES (1,5); INSERT INTO t VALUES (1,5); INSERT INTO t VALUES (1,5); INSERT INTO t VALUES (1,8); INSERT INTO t VALUES (1,); The following is the SQL definition of a window aggregate UDF that performs the dense rank operation: REPLACE FUNCTION dense_rank (x INTEGER) RETURNS INTEGER CLASS AGGREGATE (1000) LANGUAGE C NO SQL PARAMETER STYLE SQL DETERMINISTIC CALLED ON NULL INPUT EXTERNAL; The dense_rank UDF evaluates dense rank over the set of values passed as arguments to the UDF. With dense ranking, items that compare equal receive the same ranking number, and the next item(s) receive the immediately following ranking number. In the following query and result, note the difference in the rank and dense rank value for v=4. The dense rank value is 4 whereas the rank of 4 is 5. SELECT v, dense_rank(v) OVER (PARTITION BY id ORDER BY v ROWS UNBOUNDED PRECEDING) as dr, rank() OVER (PARTITION BY id ORDER BY v) as r Chapter 18: User-Defined Functions Window Aggregate UDF 724 SQL Functions, Operators, Expressions, and Predicates FROM t ORDER BY dr; The output from the SELECT statement is: v dr r ----------- ----------- ----------- ? 1 1 1 2 2 2 3 3 2 3 3 4 4 5 5 5 6 5 5 6 5 5 6 8 6 9 For a C code example of the dense_rank UDF, see “C Window Aggregate Function” in SQL External Routine Programming. Related Topics FOR more information on … SEE … aggregate UDFs “Aggregate UDF” on page 714. ordered analytical functions and the window feature “Window Feature” on page 430. implementing window aggregate UDFs SQL External Routine Programming. • CREATE FUNCTION • REPLACE FUNCTION • SQL Data Definition Language. • Database Administration. EXECUTE FUNCTION and UDTUSAGE privileges SQL Data Control Language. Chapter 18: User-Defined Functions Table UDF SQL Functions, Operators, Expressions, and Predicates 725 Table UDF Purpose A user-defined function that is invoked in the FROM clause of a SELECT statement and returns a table to the statement. Syntax See the TABLE option of the FROM clause in SQL Data Manipulation Language. ANSI Compliance Table UDFs are partially ANSI SQL:2008 compliant. The requirement that parentheses appear when the argument list is empty is a Teradata extension to preserve compatibility with existing applications. Restrictions A table UDF can only appear in the FROM clause of an SQL SELECT statement. The SELECT statement containing the table function can appear as a subquery. Authorization You must have EXECUTE FUNCTION privileges on the function or on the database containing the function. To invoke a table UDF that takes a UDT argument or returns a UDT, you must have the UDTUSAGE privilege on the SYSUDTLIB database or on the specified UDT. Usage Notes When Teradata Database evaluates a table UDF expression, it invokes the table function which returns a table a row at a time in a loop to the SELECT statement. The function can produce the rows of a table from the input arguments passed to it or by reading an external file or message queue. A table function can have 128 input parameters. The following rules apply to the arguments in the function call: • The arguments must be comma-separated expressions in the same order as the parameters declared in the function. • The number of arguments passed to the table UDF must be the same as the number of parameters declared in the function. Chapter 18: User-Defined Functions Table UDF 726 SQL Functions, Operators, Expressions, and Predicates • The data types of the arguments must be compatible with the corresponding parameter declarations in the function and follow the precedence rules that apply to compatible types. For details, see SQL External Routine Programming. To pass an argument that is not compatible with the corresponding parameter type, use CAST to explicitly convert the argument to the proper type. For information, see “CAST in Explicit Data Type Conversions” on page 752. • A NULL argument is compatible with a parameter of any data type. You can pass a NULL argument explicitly or by omitting the argument. Table UDFs do not have return values. The columns in the result rows that they produce are returned as output parameters. The output parameters of a table function are defined by the RETURNS TABLE clause of the CREATE FUNCTION statement. The number of output parameters is limited by the maximum number of columns that can be defined for a regular table. The number and data types of the output parameters can be specified statically in the CREATE FUNCTION statement or dynamically at runtime in the SELECT statement that invokes the table function. Example In this example, the extract_field table UDF is used to extract the customer ID, store number, and item ID from the pending_data column of the raw_cust table. The raw_cust table is defined as: CREATE SET TABLE raw_cust ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL, CHECKSUM = DEFAULT ( region INTEGER, pending_data VARCHAR(32000) CHARACTER SET LATIN NOT CASESPECIFIC) PRIMARY INDEX (region); The pending_data text field is a string of numbers with the format: store number, entries:customer ID, item ID, ...; repeat; where: • store number is the store that sold these items to customers. • entries is the number of items that were sold. • customer ID, item ID represent the item each customer bought. customer ID, item ID is repeated entries times ending with a semi-colon ';'. • The above sequence can be repeated. The following shows sample data from the raw_cust table: region pending_data -------- --------------------------------------------------------- 2 7,2:879,3788,879,4500;08,2:500,9056,390,9004; Chapter 18: User-Defined Functions Table UDF SQL Functions, Operators, Expressions, and Predicates 727 1 25,3:9005,3789,9004,4907,398,9004;36,2:738,9387,738,9550; 1 25,2:9005,7896,9004,7839;36,1:737,9387; The following shows the SQL definition of the extract_field table UDF: CREATE FUNCTION extract_field (Text VARCHAR(32000), From_Store INTEGER) RETURNS TABLE (Customer_ID INTEGER, Store_ID INTEGER, Item_ID INTEGER) LANGUAGE C NO SQL PARAMETER STYLE SQL EXTERNAL NAME extract_field; The following query extracts and displays the customers and the items they bought from store 25 in region 1. SELECT DISTINCT cust.Customer_ID, cust.Item_ID FROM raw_cust, TABLE (extract_field(raw_cust.pending_data, 25)) AS cust WHERE raw_cust.region = 1; The output from the SELECT statement is similar to: Customer_ID Item_ID ------------ ----------- 9005 3789 9004 4907 398 9004 9005 7896 9004 7839 Related Topics FOR more information on … SEE … Implementing external UDFs SQL External Routine Programming. • CREATE FUNCTION • REPLACE FUNCTION • SQL Data Definition Language. • Database Administration. EXECUTE FUNCTION and UDTUSAGE privileges SQL Data Control Language. the TABLE option in the FROM clause of an SQL SELECT statement SQL Data Manipulation Language. Chapter 18: User-Defined Functions Table UDF 728 SQL Functions, Operators, Expressions, and Predicates SQL Functions, Operators, Expressions, and Predicates 729 CHAPTER 19 UDT Expressions and Methods This chapter describes expressions related to user-defined types (UDTs). Chapter 19: UDT Expressions and Methods UDT Expression 730 SQL Functions, Operators, Expressions, and Predicates UDT Expression Purpose Returns a distinct or structured UDT data type. Syntax where: Syntax element … Specifies … database_name an optional qualifier for the column_name. table_name an optional qualifier for the column_name. column_name the name of a distinct or structured UDT column. udf_name the name of a UDF that returns a distinct or structured UDT. argument an argument to the UDF. CAST a CAST expression that converts a source data type to a distinct or structured UDT. Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. CAST constructor_name udf_name NEW SYSUDTLIB. 1101B363 A table_name. expression AS udt_name column_name , ( database_name. argument A ( , ( argument ( ( ( method_name , ( argument ( . . Chapter 19: UDT Expressions and Methods UDT Expression SQL Functions, Operators, Expressions, and Predicates 731 ANSI Compliance UDT expressions are partially ANSI SQL:2008 compliant. The requirement that parentheses appear when the argument list is empty is a Teradata extension to preserve compatibility with existing applications. Authorization To use a UDT expression, you must have the UDTTYPE, UDTMETHOD, or UDTUSAGE on the SYSUDTLIB database or the UDTUSAGE privilege on all of the specified UDTs. Usage Notes You can use UDT expressions as input arguments to UDFs written in C or C++. You cannot use UDT expressions as input arguments to UDFs written in Java. You can also use UDT expressions as IN and INOUT parameters of stored procedures and external stored procedures written in C or C++. However, you cannot use UDT expressions as IN and INOUT parameters of external stored procedures written in Java. You can use UDT expressions with most SQL functions and operators, with the exception of ordered analytical functions, provided that a cast definition exists that casts the UDT to a expression an expression that results in a data type that is compatible as the source type of a cast definition for the target UDT. udt_name the name of a distinct or structured UDT data type. NEW an expression that constructs a new instance of a structured type and initializes it using the specified constructor method. For details on NEW, see “NEW” on page 734. SYSUDTLIB. the database in which the constructor exists. Teradata Database only searches the SYSUDTLIB database for UDT constructors, regardless of whether the database name appears in the expression. constructor_name the name of a constructor method associated with a UDT. Constructor methods have the same name as the UDT with which they are associated. argument an argument to pass to the constructor. Parentheses must appear even though the argument list may be empty. method_name the name of an instance method that returns a UDT. For details on method invocation, see “Method Invocation” on page 740. argument an argument to pass to the method. Parentheses must appear even though the argument list may be empty. Syntax element … Specifies … Chapter 19: UDT Expressions and Methods UDT Expression 732 SQL Functions, Operators, Expressions, and Predicates predefined type that is accepted by the function or operator. For details, see other chapters in this book. Examples Consider the following statements that create a distinct UDT named euro and a structured UDT named address: CREATE TYPE euro AS DECIMAL(8,2) FINAL; CREATE TYPE address AS (street VARCHAR(20) ,zip CHAR(5)) NOT FINAL; The following statement creates a table that defines an address column named location: CREATE TABLE european_sales (region INTEGER ,location address ,sales DECIMAL(8,2)); Example 1: Column Name The following statement creates a table that defines an address column named location: CREATE TABLE italian_sales (location address ,sales DECIMAL(8,2)); The location column reference in the following statement returns an address UDT expression. INSERT INTO italian_sales SELECT location, sales FROM european_sales WHERE region = 1151; Example 2: CAST The following statement creates a table that defines a euro column named sales: CREATE TABLE swiss_sales (location address ,sales euro); The following statement uses CAST to return a euro UDT expression. Using CAST requires a cast definition that converts the DECIMAL(8,2) predefined type to a euro type. INSERT INTO swiss_sales SELECT location, CAST (sales AS euro) FROM european_sales WHERE region = 1038; Chapter 19: UDT Expressions and Methods UDT Expression SQL Functions, Operators, Expressions, and Predicates 733 Example 3: NEW The following INSERT statement uses NEW to return an address UDT expression and insert it into the european_sales table. INSERT european_sales (1001, NEW address(), 0); Example 4: Methods and Functions The following statement uses the built-in constructor function and mutator methods to return a new instance of the address UDT and insert it into the european_sales table: INSERT INTO european_sales VALUES (101, address().street('210 Stanton').zip('76543'), 500); Teradata Database executes the UDT expression in the following order: The final result of the UDT expression is an instance of the address UDT with the street attribute set to '210 Stanton' and the zip attribute set to '76543'. Related Topics Step Invocation Result 1 address() constructor function Default UDT instance 2 mutator method for street UDT instance with street attribute set to '210 Stanton' 3 mutator method for zip UDT instance with zip attribute set to '76543' FOR more information on … SEE … creating a UDT CREATE TYPE in SQL Data Definition Language. creating cast definitions for a UDT CREATE CAST in SQL Data Definition Language. using UDT expressions in DML statements such as SELECT and INSERT CREATE TYPE in SQL Data Manipulation Language. Chapter 19: UDT Expressions and Methods NEW 734 SQL Functions, Operators, Expressions, and Predicates NEW Purpose Constructs a new instance of a structured type and initializes it using the specified constructor method or function. Syntax where ANSI Compliance NEW is partially ANSI SQL:2008 compliant. The requirement that parentheses appear when the argument list is empty is a Teradata extension to preserve compatibility with existing applications. Usage Notes You can also construct a new instance of a structured type by calling the constructor method or function. For an example, see “Example” on page 735. To construct a new instance of a dynamic UDT and define the run time composition of the UDT, you must use the NEW VARIANT_TYPE expression. For details, see “NEW VARIANT_TYPE” on page 737. NEW constructor_name SYSUDTLIB. 1101B364 , ( argument ( Syntax element … Specifies … SYSUDTLIB. the database in which the constructor exists. Teradata Database only searches the SYSUDTLIB database for UDT constructors, regardless of whether the database name appears in the NEW expression. constructor_name the name of the constructor, which is the same as the name of the structured type. argument an argument to pass to the constructor. Parentheses must appear even for constructors that take no arguments. Chapter 19: UDT Expressions and Methods NEW SQL Functions, Operators, Expressions, and Predicates 735 Default Constructor When a structured UDT is created, Teradata Database automatically generates a constructor function with an empty argument list that you can use to construct a new instance of the structured UDT and initialize the attributes to NULL. Determining Which Constructor is Invoked Teradata Database uses the rules in the following table to select a UDT constructor: Example Consider the following statement that creates a structured UDT named address: CREATE TYPE address AS (street VARCHAR(20) ,zip CHAR(5)) NOT FINAL; The following statement creates a table that defines an address column named location: CREATE TABLE european_sales (region INTEGER ,location address ,sales DECIMAL(8,2)); The following statement uses NEW to insert an address value into the european_sales table: INSERT european_sales (1001, NEW address(), 0); Teradata Database selects the default constructor function that was automatically generated for the address UDT because the argument list is empty and the address UDT was created with no constructor method. The default address constructor function initializes the street and zip attributes to NULL. IF the NEW expression specifies a constructor with an argument list that is … THEN … empty IF a constructor method that takes no parameters and has the same name as the UDT … THEN Teradata Database selects … exists in the SYSUDTLIB database that constructor method. does not exist in the SYSUDTLIB database the constructor function that is automatically generated when the structured UDT is created. not empty Teradata Database selects the constructor method in SYSUDTLIB with a parameter list that matches the arguments passed to the constructor in the NEW expression. Chapter 19: UDT Expressions and Methods NEW 736 SQL Functions, Operators, Expressions, and Predicates The following statement is equivalent to the preceding INSERT statement but calls the constructor function instead of using NEW: INSERT european_sales (1001, address(), 0); Related Topics FOR more information on … SEE … creating constructor methods CREATE METHOD in SQL Data Definition Language. the constructor function that Teradata Database automatically generates when the structured type is created CREATE TYPE (Structured Form) in SQL Data Definition Language. constructing a new instance of a dynamic UDT and defining the run time composition of the UDT “NEW VARIANT_TYPE” on page 737 Chapter 19: UDT Expressions and Methods NEW VARIANT_TYPE SQL Functions, Operators, Expressions, and Predicates 737 NEW VARIANT_TYPE Purpose Constructs a new instance of a dynamic or VARIANT_TYPE UDT and defines the run time composition of the UDT. Syntax where ANSI Compliance NEW VARIANT_TYPE is a Teradata extension to the ANSI SQL standard. Syntax element … Specifies … expression any valid SQL expression; however, the following restrictions apply: • expression cannot contain a dynamic UDT expression. Nesting of dynamic UDT expressions is not allowed. • the first expression (that is, the first attribute of the dynamic UDT) cannot be a LOB, UDT, or LOB-UDT expression. alias_name a name representing the expression or column reference which corresponds to an attribute of the dynamic UDT. When provided, alias_name is used as the name of the attribute. You must provide an alias name for any expression that is not a column reference. You cannot assign the same alias name to more than one attribute of the dynamic UDT. Also, you cannot specify an alias name that is the same as a column name if that column name is already used as an attribute name in the dynamic UDT. table_name the name of the table in which the column being referenced is stored. column_name the name of the column being referenced. If you do not provide an alias name, the column name is used as the name of the corresponding attribute in the dynamic UDT. The same column name cannot be used as an attribute name for more than one attribute of the dynamic UDT. If a column has the same name as an alias name, the column name cannot be used as an attribute name. NEW VARIANT_TYPE expression AS alias_name AS alias_name , table_name.column_name ( ) 1101A576 Chapter 19: UDT Expressions and Methods NEW VARIANT_TYPE 738 SQL Functions, Operators, Expressions, and Predicates Usage Notes You can use the NEW VARIANT_TYPE expression to define the run time composition or internal attributes of a dynamic UDT. Each expression you pass into the NEW VARIANT_TYPE constructor corresponds to one attribute of the dynamic UDT. You can assign an alias name to represent each NEW VARIANT_TYPE expression parameter. The name of the attribute will be the alias name provided or the column name associated with the column reference if no alias is provided. This is summarized in the following table: Note that you must provide an alias name for all expressions that are not column references. In addition, the attribute names must be unique. Therefore, you must provide unique alias names and/or column references. The data type of the attribute will be the result data type of the expression. The resultant value of the expression will become the value of the corresponding attribute. Restrictions • You can use the NEW VARIANT_TYPE expression only to construct dynamic UDTs for use as input parameters to UDFs. To construct a new instance of other structured UDTs, use the NEW expression. For details, see “NEW” on page 734. • UDFs support a maximum of 128 parameters. Therefore, you cannot use NEW VARIANT_TYPE to construct a dynamic UDT with more than 128 attributes. • The sum of the maximum sizes for all the attributes of the dynamic UDT must not exceed the maximum permissible column size as configured for the Teradata Database. Exceeding the maximum column size results in the following SQL error: “ERR_TEQRWOVRFLW _T("Row size or Sort Key size overflow.")”. Example 1 The following NEW VARIANT_TYPE expression creates a dynamic UDT with a single attribute named weight: NEW VARIANT_TYPE (Table1.a AS weight) In the next example, the NEW VARIANT_TYPE expression creates a dynamic UDT with a single attribute named height. In this example, no alias name is specified; therefore, the column name is used as the attribute name. NEW VARIANT_TYPE (Table1.height) IF... THEN the attribute name is... alias_name is provided alias_name table_name.column_name is provided, but alias_name is not provided column_name an expression is provided that is not a column reference and alias_name is not provided an error is returned. Chapter 19: UDT Expressions and Methods NEW VARIANT_TYPE SQL Functions, Operators, Expressions, and Predicates 739 In the next example, the first attribute is named height based on the column name. However, the second attribute is also named height based on the specified alias name. This is not allowed since attribute names must be unique; therefore, the Teradata Database returns the error, “ERRTEQDUPLATTRNAME - "Duplicate attribute names in the attribute list. %VSTR", being returned to the user.” NEW VARIANT_TYPE (Table1.height, Table1.a AS height) Example 2 This example shows a user-defined aggregate function with an input parameter named parameter_1 declared as VARIANT_TYPE data type. The SELECT statement calls the new function using the NEW VARIANT_TYPE expression to create a dynamic UDT with two attributes named a and b. CREATE TYPE INTEGERUDT AS INTEGER FINAL; CREATE FUNCTION udf_agch002002dynudt (parameter_1 VARIANT_TYPE) RETURNS INTEGERUDT CLASS AGGREGATE (4) LANGUAGE C NO SQL EXTERNAL NAME 'CS!udf_agch002002dynudt!udf_agch002002dynudt.c' PARAMETER STYLE SQL; SELECT udf_agch002002dynudt(NEW VARIANT_TYPE (Tbl1.a AS a, (Tbl1.b + Tbl1.c) AS b)) FROM Tbl1; Related Topics FOR more information on … SEE … dynamic UDTs “VARIANT_TYPE UDT” in SQL Data Types and Literals. constructing a new instance of a structured UDT that is not a dynamic UDT “NEW” on page 734. writing UDFs which use input parameters of VARIANT_TYPE data type SQL External Routine Programming Chapter 19: UDT Expressions and Methods Method Invocation 740 SQL Functions, Operators, Expressions, and Predicates Method Invocation Purpose Invokes a method associated with a UDT. Syntax where: 1101B365 A A method_name , ( argument ( CAST constructor_name udf_name NEW . . SYSUDTLIB. table_name. expression AS udt_name column_name , ( database_name. argument ( , ( argument ( ( ( Syntax element … Specifies … database_name an optional qualifier for the column_name. table_name an optional qualifier for the column_name. column_name the name of a distinct or structured UDT column. udf_name the name of a UDF that returns a distinct or structured UDT. argument an argument to the UDF. CAST a CAST expression that converts a source data type to a distinct or structured UDT. Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. Chapter 19: UDT Expressions and Methods Method Invocation SQL Functions, Operators, Expressions, and Predicates 741 ANSI Compliance Invocation of UDT methods is partially ANSI SQL:2008 compliant. The requirement that parentheses appear when the argument list is empty is a Teradata extension to preserve compatibility with existing applications. Additionally, when a statement specifies an ambiguous expression that can be interpreted as a UDF invocation or a method invocation, Teradata Database gives UDF invocation higher precedence over method invocation. ANSI SQL:2008 gives method invocation higher precedence over UDF invocation. Observer and Mutator Methods Teradata Database automatically generates observer and mutator methods for each attribute of a structured UDT. Observer and mutator methods have the same name as the attribute for which they are generated. expression an expression that results in a data type that is compatible as the source type of a cast definition for the target UDT. udt_name the name of a distinct or structured UDT. NEW an expression that constructs a new instance of a structured type and initializes it using the specified constructor method. For details on NEW, see “NEW” on page 734. SYSUDTLIB. the database in which the constructor exists. Teradata Database only searches the SYSUDTLIB database for UDT constructors, regardless of whether the database name appears in the expression. constructor_name the name of a constructor method associated with a UDT. Constructor methods have the same name as the UDT with which they are associated. argument an argument to pass to the constructor. Parentheses must appear even though the argument list may be empty. method_name the name of an observer, mutator, or user-defined method (UDM). You must precede each method name with a period. argument an argument to pass to the method. Parentheses must appear even though the argument list may be empty. Syntax element … Specifies … Method Description Invocation Example Observer Takes no arguments and returns the current value of the attribute. “Example” on page 742 Chapter 19: UDT Expressions and Methods Method Invocation 742 SQL Functions, Operators, Expressions, and Predicates Usage Notes When you invoke a UDM on a UDT, Teradata Database searches the SYSUDTLIB database for a UDM that has the UDT as its first parameter followed by the same number of parameters as the method invocation. If several UDMs have the same name, Teradata Database must determine which UDM to invoke. For details on the steps that Teradata Database uses, see SQL External Routine Programming. Restrictions To use any of the following functions as the first argument of a method invocation, you must enclose the function in parentheses: • DATE • TIME • VARGRAPHIC For example, consider a structured UDT called datetime_record that has a DATE type attribute called start_date. The following statement invokes the start_date mutator method, passing in the result of the DATE function: SELECT datetime_record_column.start_date((DATE)) FROM table1; Example Consider the following statement that creates a structured UDT named address: CREATE TYPE address AS (street VARCHAR(20) ,zip CHAR(5)) NOT FINAL; The following statement creates a table that defines an address column named location: CREATE TABLE european_sales (region INTEGER ,location address ,sales DECIMAL(8,2)); The following statement invokes the zip observer method to retrieve the value of each zip attribute in the location column: SELECT location.zip() FROM european_sales; Mutator Takes one argument and returns a new UDT instance with the specified attribute set to the value of the argument. “Example 4: Methods and Functions” on page 733 Method Description Invocation Example Chapter 19: UDT Expressions and Methods Method Invocation SQL Functions, Operators, Expressions, and Predicates 743 Related Topics FOR more information on … SEE … creating methods CREATE METHOD in SQL Data Definition Language. creating UDTs CREATE TYPE in SQL Data Definition Language. UDM programming SQL External Routine Programming. Chapter 19: UDT Expressions and Methods Method Invocation 744 SQL Functions, Operators, Expressions, and Predicates SQL Functions, Operators, Expressions, and Predicates 745 CHAPTER 20 Data Type Conversions This chapter describes the SQL CAST function and the rules for converting data from one type to another, both explicitly and implicitly. A data type conversion modifies the data definition (data type, data attributes, or both) of an expression and can be either implicit or explicit. Explicit conversions can be made using the CAST function or Teradata conversion syntax. For details on data types and data attributes, see SQL Data Types and Literals. Forms of Data Type Conversions Teradata Database supports the following forms of data conversion: • Implicit See “Implicit Type Conversions” on page 745. • Explicit using the CAST function See “CAST in Explicit Data Type Conversions” on page 752. • Explicit using Teradata conversion syntax See “Teradata Conversion Syntax in Explicit Data Type Conversions” on page 755. Implicit Type Conversions Teradata Database permits the assignment and comparison of some types without requiring the types to be explicitly converted. Teradata Database also performs implicit type conversions in the following cases: • On some argument types passed to macros, stored procedures, and SQL functions such as SQRT. • On the expression that defines a time zone displacement in an AT clause. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. ANSI Compliance Implicit conversions are Teradata extensions to the ANSI standard. Example 1: Implicit Type Conversion During Assignment Consider the following tables: Chapter 20: Data Type Conversions Implicit Type Conversions 746 SQL Functions, Operators, Expressions, and Predicates CREATE TABLE T1 (Fname VARCHAR(25) ,Fid INTEGER ,Yrs CHARACTER(2)); CREATE TABLE T2 (Wname VARCHAR(25) ,Wid INTEGER ,Age SMALLINT); In the following statement, Teradata Database implicitly converts the character string in T1.Yrs to a numeric value: UPDATE T2 SET Age = T1.Yrs + 5; This is not evident in the syntax of the source statement, but becomes evident when the dictionary information for tables T1 and T2 is accessed. Example 2: Implicit Type Conversion During Comparison Consider the table T1 in “Example 1: Implicit Type Conversion During Assignment.” In the following statement, Teradata Database implicitly converts both operands of the comparison operation to FLOAT values before performing the comparison: SELECT Fname, Fid FROM T1 WHERE T1.Yrs < 55; For details on implicit type conversion of operands for comparison operations, see “Implicit Type Conversion of Comparison Operands” on page 168. Example 3: Implicit Type Conversion in Parameter Passing Operations Consider the SQRT system function that computes the square root of an argument. In the following statement, Teradata Database implicitly converts the character argument to a FLOAT type: SELECT SQRT('13147688'); Supported Data Types Teradata Database performs implicit conversion on the following types: FROM … TO … For further details, see … Byte Byte Byte types include BYTE, VARBYTE, and BLOB. “Byte Conversion” on page 758. UDTa Chapter 20: Data Type Conversions Implicit Type Conversions SQL Functions, Operators, Expressions, and Predicates 747 Numeric Numeric “Numeric-to-Numeric Conversion” on page 837. DATE “Numeric-to-DATE Conversion” on page 832. Character “Numeric-to-Character Conversion” on page 827. UDTa “Numeric-to-UDT Conversion” on page 841. DATE Numeric “DATE-to-Numeric Conversion” on page 804. DATE “DATE-to-DATE Conversion” on page 802. Character “DATE-to-Character Conversion” on page 798. UDTa “DATE-to-UDT Conversion” on page 815. Character Numeric “Character-to-Numeric Conversion” on page 775. DATE “Character-to-DATE Conversion” on page 767. Character Character types include CHAR, VARCHAR, and CLOB. “Character-to-Character Conversion” on page 762. Period “Character-to-Period Conversion” on page 781. TIME “Character-to-TIME Conversion” on page 784. TIMESTAMP “Character-to-TIMESTAMP Conversion” on page 790. UDTa “Character-to-UDT Conversion” on page 795. TIME UDTa “TIME-to-UDT Conversion” on page 888. TIMESTAMP UDTa “TIMESTAMP-to-UDT Conversion” on page 923. Interval UDTa “INTERVAL-to-UDT Conversion” on page 825. UDT Predefined data types that are the target of implicit casts defined for the UDTb • “UDT-to-Character Conversion” on page 928. • “UDT-to-DATE Conversion” on page 932. • “UDT-to-INTERVAL Conversion” on page 935. • “UDT-to-Numeric Conversion” on page 938. • “UDT-to-TIME Conversion” on page 941. • “UDT-to-TIMESTAMP Conversion” on page 944. Other UDTs that are the target of implicit casts defined for the UDTb “UDT-to-UDT Conversion” on page 947. a. The UDT must have an implicit cast that casts the predefined type to a UDT. To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. FROM … TO … For further details, see … Chapter 20: Data Type Conversions Implicit Type Conversions 748 SQL Functions, Operators, Expressions, and Predicates For details on data types, see SQL Data Types and Literals. Implicit Conversion of DateTime types Teradata Database performs implicit conversion on DateTime data types in the following cases: • When passing data using dynamic parameter markers, or the question mark (?) placeholder. • With INSERT, INSERT...SELECT, and UPDATE statements. • With MERGE INTO statements. • When handling default values for the CREATE/ALTER TABLE statements. For details, see “DEFAULT Phrase” in SQL Data Types and Literals. • During stored procedure execution, including the execution of the following statements: DECLARE, SELECT...INTO, and SET. See SQL Stored Procedures and Embedded SQL. Implicit conversion is dependent on client-side support. For information about the client products which support implicit conversion of DateTime types, see the Teradata Tools and Utilities user documentation. The following conversions are supported: Teradata Database performs implicit conversion on DateTime data types during assignment in the following cases: b. To define an implicit cast for a UDT, use the CREATE CAST statement and specify the AS ASSIGNMENT clause. For more information on CREATE CAST, see SQL Data Definition Language. FROM... TO... For further details, see... DATE TIMESTAMP “Implicit DATE-to-TIMESTAMP Conversion” on page 812. TIME TIMESTAMP “Implicit TIME-to-TIMESTAMP Conversion” on page 880. TIMESTAMP DATE “Implicit TIMESTAMP-to-DATE Conversion” on page 897. TIMESTAMP TIME “Implicit TIMESTAMP-to-TIME Conversion” on page 911. INTERVAL INTERVAL “Implicit INTERVAL-to-INTERVAL Conversion” on page 821. FROM... TO... For further details, see... DATE TIMESTAMP “Implicit DATE-to-TIMESTAMP Conversion” on page 812. Chapter 20: Data Type Conversions Implicit Type Conversions SQL Functions, Operators, Expressions, and Predicates 749 Note: There is a general restriction that in Numeric-to-Interval conversions, the INTERVAL type must have only one DateTime field. However, this restriction is not an issue when implicitly converting the expression of an AT clause because the conversion is done with two CAST statements. See “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. For more information, see “ANSI DateTime and Interval Data Type Assignment Rules” on page 210. Teradata Database performs implicit conversion on DateTime data types in single table predicates and join predicates in the following cases: For more information, see “Implicit Type Conversion of Comparison Operands” on page 168. The following are not supported: • Implicit conversion from TIME to TIMESTAMP and from TIMESTAMP to TIME are not supported in comparisons. • Implicit conversion of DateTime types in set operations. For details on data types, see SQL Data Types and Literals. TIME TIMESTAMP “Implicit TIME-to-TIMESTAMP Conversion” on page 880. TIMESTAMP DATE “Implicit TIMESTAMP-to-DATE Conversion” on page 897. TIMESTAMP TIME “Implicit TIMESTAMP-to-TIME Conversion” on page 911. Intervala Exact Numeric “Implicit INTERVAL-to-Numeric Conversion” on page 824. Exact Numeric Intervala “Implicit Numeric-to-INTERVAL Conversion” on page 836. a. The INTERVAL type must have only one field, e.g. INTERVAL YEAR. FROM... TO... For further details, see... TIMESTAMP DATE “Implicit TIMESTAMP-to-DATE Conversion” on page 897. Intervala a. The INTERVAL type must have only one field, e.g. INTERVAL YEAR. Exact Numeric “Implicit INTERVAL-to-Numeric Conversion” on page 824. Exact Numeric Intervala “Implicit Numeric-to-INTERVAL Conversion” on page 836. FROM... TO... For further details, see... Chapter 20: Data Type Conversions Implicit Type Conversions 750 SQL Functions, Operators, Expressions, and Predicates Implicit Conversion Rules Teradata SQL performs implicit type conversions on expressions before any operation is performed. The implementation of implicit type conversion follows the same rules as the implementation of explicit type conversion using Teradata conversion syntax. For details, see “Teradata Conversion Syntax in Explicit Data Type Conversions” on page 755. For details on implicit type conversion of operands for comparison operations, see “Implicit Type Conversion of Comparison Operands” on page 168. Truncation During Conversion In some cases, implicit conversion can result in truncation of values without an error. Recommendation: As a best practice, use an explicit CAST instead of relying on implicit conversions when possible. Example 1 Consider the following table definition: CREATE TABLE Test1 (c1 INT, c2 VARCHAR(1)); The following two INSERT statements complete without any errors. INSERT INTO Test1 VALUES (1, '1'); INSERT INTO Test1 VALUES (2, 2); The following query returns two rows. SELECT * FROM Test1; c1 c2 ------------- 1 1 2 <<<< Note that the value inserted in c2 is a blank In the second INSERT statement, the number 2 was implicitly converted to CHAR using Teradata conversion syntax (that is, not using CAST). The process is as follows: 1 Convert the numeric value to a character string using the default or specified FORMAT for the numeric value. Leading and trailing pad characters are not trimmed. 2 Extend to the right with pad characters if required, or truncate from the right if required, to conform to the target length specification. If non-pad characters are truncated, no string truncation error is reported. The conversion right-justifies the number, but takes the first byte of the result which is a single blank character. For more information about numeric to character conversions, see “Numeric-to-Character Conversion” on page 827. Restrictions Teradata Database does not perform implicit conversion on input arguments to UDFs, UDMs, or external stored procedures (external routines). Arguments do not necessarily have Chapter 20: Data Type Conversions Implicit Type Conversions SQL Functions, Operators, Expressions, and Predicates 751 to be exact matches to the parameter types, but they must be compatible. For example, you can pass a SMALLINT argument to an external routine that expects an INTEGER argument because SMALLINT and INTEGER are compatible. To pass a DATE type argument to an external routine that expects an INTEGER argument, you must explicitly cast the DATE type to an INTEGER type. For details, see SQL External Routine Programming. Some SQL functions and operators require arguments that are exact matches to the parameter types. For details, refer to the documentation for the specific function or operator. Chapter 20: Data Type Conversions CAST in Explicit Data Type Conversions 752 SQL Functions, Operators, Expressions, and Predicates CAST in Explicit Data Type Conversions Purpose Converts an expression of a given data type to a different data type or the same data type with different attributes. Teradata SQL supports two different syntaxes for CAST functionality, only one of which is ANSI SQL:2008 compliant. Syntax where: ANSI Compliance The form of CAST syntax that specifies ansi_sql_data_type is ANSI SQL:2008 compliant. The form of CAST syntax that specifies data_definition_list is a Teradata extension to the ANSI SQL:2008 standard. Note that when data_definition_list consists solely of an ANSI data type declaration, then this form of the syntax is also ANSI-compliant. Usage Notes The ANSI SQL:2008 compliant form can be used to convert data types in either ANSI-compliant SQL statements or Teradata SQL statements. The Teradata extended syntax is more general. It allows a type declaration or data attributes or both. For more information on data types and attributes, see SQL Data Types and Literals. Avoid using the extended form of CAST for any application intended to be ANSI-compliant and portable. CAST functions identically in both ANSI and Teradata modes. Syntax element … Specifies … expression an expression with known data type to be cast as a different data type. ansi_sql_data_type the new data type for expression. data_definition_list the new data type or data attributes or both for expression. 1101A627 CAST AS ansi_sql_data_type data_definition_list ( expression ) Chapter 20: Data Type Conversions CAST in Explicit Data Type Conversions SQL Functions, Operators, Expressions, and Predicates 753 When converting DateTime data types, you can use the AT clause to specify the time zone used for the CAST. You can specify the source time zone, a specific time zone displacement, or the current session time zone. For more information, see the section on converting the specific data type, for example, TIMESTAMP-to-DATE Conversion. CAST does not convert the following data type pairs: • Numeric to character, if the server character set is GRAPHIC. • Character expressions having different server character sets. To make such a conversion, use the TRANSLATE function (see “TRANSLATE” on page 536). • Byte (BYTE, VARBYTE, and BLOB) to any data type other than UDT or byte, and data types other than byte or UDT to byte. • CLOB to any data type other than UDT or character, and data types other than character or UDT to CLOB. For information on casting to and from geospatial types, see SQL Geospatial Types. Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. Character Truncation Rules The following rules apply to character strings: Server Character Set Rules When data_definition_list specifies a data type of CHARACTER (CHAR) or CHARACTER VARYING (VARCHAR) and does not specify a CHARACTER SET clause to indicate which server character set to use, then the resulting server character set is as follows: IF the string is cast in this mode … THEN it is truncated of … ANSI trailing pad character spaces to achieve the desired length. Truncation of other characters, or part of a multibyte character, returns an error. Teradata trailing characters to achieve the desired length. Truncation on Kanji1 character data types containing multibyte characters might result in truncating one byte of the multibyte character. IF the data type of expression is … THEN the server character set of the resulting characters is … non-character the user default server character set. character the server character set of expression. Chapter 20: Data Type Conversions CAST in Explicit Data Type Conversions 754 SQL Functions, Operators, Expressions, and Predicates Numeric Overflow, Field Mode, and CAST Numeric overflows are handled differently depending on whether you are running ANSI or Teradata mode, and whether you are running in Field Mode or not. Field Mode is not ANSI SQL:2008 compatible. In Field Mode, conversion to a numeric or decimal data type that results in a numeric overflow is returned as asterisks (‘***’) rather than an error message. Record and Indicator Modes do not behave in this manner and return an error message. Related Topics For further rules that apply to the conversion between specific data types, for example, numeric-to numeric or character-to-numeric, see the appropriate succeeding topic in this chapter. Examples The following examples illustrate how to perform data type conversions using CAST. Example 1 Using ANSI CAST syntax: SELECT ID_Col, Name_Col FROM T1 WHERE Int_Col = CAST(SUBSTRING(Char_Col FROM 3 FOR 3) AS INTEGER); Example 2 Using ANSI CAST syntax: SELECT CAST(SUBSTRING(Char_Col FROM 1 FOR 2) AS INTEGER), CAST(SUBSTRING (Char_Col FROM 3 FOR 3) AS INTEGER) FROM T1; Example 3 Using Teradata extensions to the ANSI CAST syntax: CREATE TABLE t2 (f1 TIME(0) FORMAT 'HHhMIm'); INSERT t2 (CAST('15h33m' AS TIME(0) FORMAT 'HHhMIm')); SELECT f1 FROM t2; The result from the SELECT statement is: f1 ------ 15h33m Chapter 20: Data Type Conversions Teradata Conversion Syntax in Explicit Data Type Conversions SQL Functions, Operators, Expressions, and Predicates 755 Teradata Conversion Syntax in Explicit Data Type Conversions Teradata conversion syntax is defined as follows: Syntax where: ANSI Compliance This syntax is a Teradata extension to the ANSI SQL:2008 standard. Using CAST Instead of Teradata Conversion Syntax Using Teradata conversion syntax is strongly discouraged. It is an extension to the ANSI SQL:2008 standard and is retained only for backward compatibility with existing applications. Instead, use CAST to explicitly convert data types. Usage Notes When the conversion specifies data_type, then the data is converted at run time. At that time, a data conversion or range check error may occur. For any kind of data type conversion using Teradata conversion syntax, where the item that includes a data type declaration is an operand of a complex expression, you must either enclose the appropriate entities in parentheses or use the CAST syntax. Syntax element … Specifies … expression the data expression to be converted to the new definition specified by data_type and data_attributes. data_type a data type declaration such as INTEGER or DATE. data_attribute a data attribute such as FORMAT or TITLE. 1101A626 expression ( data_type , data_attribute , data_type ) , data_attribute , data_attribute Chapter 20: Data Type Conversions Teradata Conversion Syntax in Explicit Data Type Conversions 756 SQL Functions, Operators, Expressions, and Predicates You should always use the CAST function to perform conversions in new applications to ensure ANSI compatibility. Related Topics For further rules that apply to the conversion between specific data types, for example, numeric-to numeric or character-to-numeric, see the appropriate succeeding topic in this chapter. Example 1 To evaluate an expression of the following form correctly: column_name (INTEGER) + variable You could enter the expression as follows: (column_name (INTEGER)) + variable or, preferably, as: CAST (column_name AS INTEGER) + variable For more information on using CAST, see “CAST in Explicit Data Type Conversions” on page 752. Example 2 Here is an example that uses the Teradata conversion syntax, and specifies the FORMAT data attribute to convert the format of a DATE data type. CREATE TABLE date1 (d1 DATE FORMAT 'E4,BM4BDD,BY4'); CREATE TABLE char1 (c1 CHAR(10)); INSERT date1 ('Saturday, March 16, 2002'); INSERT INTO char1 (c1) SELECT ((d1 (FORMAT 'YYYY/MM/DD'))) FROM date1; SELECT * FROM char1; The result from the SELECT statement is: c1 ---------- 2002/03/16 If the second INSERT statement did not convert the DATE format to 'YYYY/MM/DD', the result from the SELECT statement is: c1 ---------- Saturday, Chapter 20: Data Type Conversions Data Conversions in Field Mode SQL Functions, Operators, Expressions, and Predicates 757 Data Conversions in Field Mode Field Mode: User Response Data In Field Mode, a report format used in BTEQ, all data is returned in character form. The alignment and spacing of columns is controlled by data formats and title information. Each row returned is essentially a character string ready for display. In Field Mode, it is unnecessary to explicitly convert numeric data to character format. Conversions to Numeric Types When in Field Mode, a numeric overflow returned for character to numeric data type conversion is not treated as an error. If the result exceeds the number of digits normally reserved for the numeric data type, the result appears as a set of asterisks in the report. For example, the character to SMALLINT conversion in the following statement results in numeric overflow because the number of digits normally reserved for a SMALLINT is five: SELECT '100000' (SMALLINT); The result is: '100000' -------- ****** Additionally, when in Field Mode, asterisks appear in the report for conversions to numeric types involving results that do not fit the specified output format. For example, the DATE to INTEGER conversion in the following statement results in a value that does not fit the format specified by the FORMAT phrase: SELECT CAST (CURRENT_DATE as integer format '9999'); The result is: Date ---- **** The same query executed in Record or Indicator Variable Mode reports an error. Chapter 20: Data Type Conversions Byte Conversion 758 SQL Functions, Operators, Expressions, and Predicates Byte Conversion Purpose Converts a byte expression to a different data definition. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant, provided the syntax does not specify data attributes. Teradata Conversion Syntax Syntax element … Specifies … byte_expression an expression in byte format to be cast to a different data definition. byte_data_type the new byte type to which byte_expression is to be converted. UDT_data_type a UDT that has a cast definition that casts the byte type to the UDT. To define a cast for a UDT, use the CREATE CAST statement. For details on CREATE CAST, see SQL Data Definition Language. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE CAST byte_expression AS byte_data_type data_attribute UDT_data_type ( ( 1101B335 data_attribute 1101A623 byte_expression ( byte_data_type , data_attribute , byte_data_type ) , data_attribute , data_attribute Chapter 20: Data Type Conversions Byte Conversion SQL Functions, Operators, Expressions, and Predicates 759 where: ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Conversions Where Source and Target Types Differ in Length If the length specified by byte_data_type is less than the length of byte_expression, bytes beyond the specified length are truncated. No error is reported. If byte_data_type is fixed-length and the length is greater than that of byte_expression, bytes of value binary zero are appended as required. Supported Source and Target Data Types Teradata Database supports byte data type conversions according to the following table. Syntax element … Specifies … byte_expression an expression in byte format to be cast to a different byte data definition. byte_data_type an optional byte type to which byte_expression is to be converted. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE Source Data Type Target Data Type Allowable Conversions BYTE • BYTE • VARBYTE • BLOB • Implicit • Explicit using CAST and Teradata conversion syntax VARBYTE BLOB BYTE UDTa a. Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. • Implicit • Explicit using CAST VARBYTE BLOB UDTa • BYTE • VARBYTE • BLOB • Implicit • Explicit using CAST and Teradata conversion syntax Chapter 20: Data Type Conversions Byte Conversion 760 SQL Functions, Operators, Expressions, and Predicates Rules for Implicit Byte-to-UDT Conversions Teradata Database performs implicit Byte-to-UDT conversions for the following operations: • UPDATE • INSERT • Passing arguments to stored procedures, external stored procedures, UDFs, and UDMs • Specific system operators and functions identified in other sections of this book, unless the DisableUDTImplCastForSysFuncOp field of the DBS Control Record is set to TRUE Performing an implicit Byte-to-UDT data type conversion requires a cast definition (see “Usage Notes”) that specifies the following: • the AS ASSIGNMENT clause • a BYTE, VARBYTE, or BLOB source data type The source data type of the cast definition does not have to be an exact match to the source of the implicit type conversion. If multiple implicit cast definitions exist for converting different byte types to the UDT, Teradata Database uses the implicit cast definition for the byte type with the highest precedence. The following list shows the precedence of byte types in order from lowest to highest precedence: • BYTE • VARBYTE • BLOB Using HASHBUCKET to Convert a BYTE Type to an INTEGER Type You can use the HASHBUCKET function to convert a BYTE(1) or BYTE(2) type to an INTEGER type. For details, see “Using HASHBUCKET to Convert a BYTE Type to an INTEGER Type” on page 641. Example 1: Explicit Conversion of BLOB to VARBYTE Consider the following table definition: CREATE TABLE large_images (id INTEGER ,image BLOB); The following statement casts the BLOB column to a VARBYTE type, and uses the result as an argument to the POSITION function: SELECT POSITION('FFF1'xb IN (CAST(image AS VARBYTE(64000)))) FROM large_images WHERE id = 5; Chapter 20: Data Type Conversions Byte Conversion SQL Functions, Operators, Expressions, and Predicates 761 Example 2: Implicit Conversion of VARBYTE to BLOB Consider the following table definitions: CREATE TABLE small_images (id INTEGER ,image1 VARBYTE(30000) ,image2 VARBYTE(30000)); CREATE TABLE large_images (id INTEGER ,image BLOB); Teradata Database performs a VARBYTE to BLOB implicit conversion for the following INSERT statement: INSERT large_images SELECT id, image1 || image2 FROM small_images; Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions Character-to-Character Conversion 762 SQL Functions, Operators, Expressions, and Predicates Character-to-Character Conversion Purpose Shortens or expands output character strings. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant, provided the syntax does not specify any data attributes. Teradata Conversion Syntax where: Syntax element … Specifies … character_expression a character expression to be cast to a different character data definition. character_data_type the new data type to which character_expression is to be converted. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE • CHARACTER SET CAST character_expression AS character_data_type data_attribute ( ( 1101A625 data_attribute 1101A624 character_expression ( character_data_type , data_attribute , character_data_type ) , data_attribute , data_attribute Chapter 20: Data Type Conversions Character-to-Character Conversion SQL Functions, Operators, Expressions, and Predicates 763 ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Implicit Character-to-Character Conversion CLOB types can only be converted to or from CHAR or VARCHAR types. For example, implicit conversion is performed on CLOB data that is inserted into a CHAR or VARCHAR column. Comparisons of strings (both fixed- and variable-length) require operands of equal length. The following table shows that the shorter string is converted by being padded on the right. where ? is a pad character. If a character is not in the repertoire of the target character set, an error is reported. For rules on the effect of server character sets on character conversion, see “Implicit Character-to-Character Translation” on page 765. CAST Syntax Usage Notes The server character set of character_expression must have the same server character set as the target data type. If CAST is used to convert data to a character string and non-pad characters would be truncated, an error is reported. Syntax element … Specifies … character_expression a character expression to be cast to a different character data definition. character_data_type an optional character type to which character_expression is to be converted. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE • CHARACTER SET If the syntax specifies character_data_type, CHARACTER SET can only appear after character_data_type. THIS expression … IS converted to … AND the result is … 'x'='x ' 'x?'='x ' TRUE 'x'='xx' 'x?'='xx' FALSE Chapter 20: Data Type Conversions Character-to-Character Conversion 764 SQL Functions, Operators, Expressions, and Predicates Teradata Conversion Syntax Usage Notes The server character set of character_expression can be changed to a different server character set specified as data_attribute, where data_attribute is the CHARACTER SET phrase. This is not the recommended way to perform this translation. Instead, use the TRANSLATE function. For information, see “TRANSLATE” on page 536. General Usage Notes If the source string (CHAR, VARCHAR, or CLOB) is longer than the target data type (CHAR, VARCHAR, or CLOB), excess characters are truncated. Pad characters are trimmed or appended, according to the following rules: Examples Following are examples of character to character conversions: IF the session doing an INSERT or UPDATE is in this mode … AND non-pad characters would be truncated to store character values in a table, THEN … ANSI an error is reported. Teradata no error is reported. IF the source string data type is … AND it is … AND the target data type is … THEN … CHAR longer than the target CLOB or VARCHAR any trailing pad characters are trimmed. CHAR, VARCHAR, or CLOB shorter than the target CHAR trailing pad characters are appended to the target. CHAR all pad characters CLOB or VARCHAR the field is truncated to zero length. Character String String Length Character Description Conversion Result Converted Length 'HELLO' 5 CHAR(3) 'HEL', if session is in Teradata mode 3 Error, if session is in ANSI mode 'HELLO' 5 CHAR(7) 'HELLO ' 7 'HELLO' 5 VARCHAR(7) 'HELLO' 5 'HELLO ' 7 VARCHAR(6) 'HELLO ' 6 Chapter 20: Data Type Conversions Implicit Character-to-Character Translation SQL Functions, Operators, Expressions, and Predicates 765 Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Implicit Character-to-Character Translation Implicit string translation occurs when two character strings are incompatible within a given operation. For example, SELECT * FROM string_table WHERE clatin < csjis; where clatin represents a character column defined as CHARACTER SET LATIN and csjis represents a character column defined as CHARACTER SET KANJISJIS. If an implicit translation of character string ‘string’ to a UNICODE character string is required, it is equivalent to executing the TRANSLATE(string USING source_repertoire_name_TO_Unicode) function, where source-repertoire-name is the server character set of string. More specifically, if as in the above example, string is of KANJISJIS type, then the translation is equivalent to executing the TRANSLATE(string USING KanjiSJIS_TO_Unicode) function. ANSI Compliance Implicit translations are Teradata extensions to the ANSI standard. Character Constants The following rules apply to implicit character-to-character translation involving character constants. 'HELLO ' 7 VARCHAR(3) 'HEL', if session is in Teradata mode 3 Error, if session is in ANSI mode Character String String Length Character Description Conversion Result Converted Length IF one operand is a … AND the other operand is a … THEN … constant constant both operands are translated to UNICODE. non-constant the constant is translated to the type of the nonconstant. If that fails, both are translated to UNICODE. constant expression the constant is translated to the type of the constant expression. If that fails, both are translated to UNICODE. Chapter 20: Data Type Conversions Implicit Character-to-Character Translation 766 SQL Functions, Operators, Expressions, and Predicates KANJISJIS Server Character Set Implicit character-to-character translation always converts a character string argument that has the KANJISJIS server character set to UNICODE. SQL Rules for Implicit Translation for Expression and Function Arguments The following are the rules for implicit translation between types of expressions and function arguments. For string functions that produce a character result, the results are summarized by this table. Note that the other string functions either do not involve conversion or the type of the result is based on the function and not the server character set of the argument. For example, in the following TRIM function, is first translated to Latin, and then the trim operation is performed. ... TRIM( FROM ) The result is Latin. constant expression constant expression both operands are translated to UNICODE. non-constant the constant expression is translated to the type of the non-constant. If that fails, both are translated to UNICODE. non-constant non-constant both operands are translated to UNICODE. IF one operand is a … AND the other operand is a … THEN … FOR this function … The result is … TRIM converted back to the type of the main string argument (last argument). || (concatenation) not translated and remains with the character data type of the arguments after any implicit translation. Chapter 20: Data Type Conversions Character-to-DATE Conversion SQL Functions, Operators, Expressions, and Predicates 767 Character-to-DATE Conversion Purpose Converts a character string to a date value. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attributes, such as the FORMAT phrase that enables alternative formatting for the date data. Teradata Conversion Syntax where: Syntax element … Specifies … character_expression a character expression to be cast to a DATE value. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE 1101B244 CAST ( character_expression AS DATE ) data_attribute Syntax element … Specifies … character_expression a character expression to be cast to a DATE value. 1101B255 character_expression data_attribute , ( DATE ) , data_attribute Chapter 20: Data Type Conversions Character-to-DATE Conversion 768 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Implicit Character-to-DATE Conversion If the string does not represent a valid date, an error is reported. In record or indicator mode, when the DateForm mode of the session is set to ANSIDate, the string must use the ANSI DATE format. Usage Notes The character expression is trimmed of leading and trailing pad characters and handled as if it was a string literal in the declaration of a DATE literal. Character-to-DATE conversion is supported for CHAR and VARCHAR types only. The source character type cannot be CLOB. If the string can be converted to a valid DATE, then it is. Otherwise, an error is returned. Character String Format If the dateform of the current session is INTEGERDATE, the date representation in the character string must match the DATE output format according to the rules in the following table: data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE Syntax element … Specifies … IF the statement … THEN … specifies a FORMAT phrase for the DATE the character string must match that DATE format. does not specify a FORMAT phrase IF the DATE column definition … THEN the character string must match … specifies a FORMAT phrase that DATE format. does not specify a FORMAT phrase ‘YY/MM/DD’, or the current setting of the default date format in the specification for data formatting (SDF) file Chapter 20: Data Type Conversions Character-to-DATE Conversion SQL Functions, Operators, Expressions, and Predicates 769 For an example, see “Example 1: IntegerDate Dateform Mode” on page 770. If the dateform of the current session is ANSIDATE, the date representation in the character string must match the DATE output format according to the rules in the following table: For an example, see “Example 2: ANSIDate Dateform Mode” on page 771. Forcing a FORMAT on CAST for Converting Character to DATE You can use a FORMAT phrase to convert a character string that does not match the format of the target DATE data type. A character string in a conversion that does not specify a FORMAT phrase uses the output format for the DATE data type. For example, suppose the session dateform is INTEGERDATE and the default DATE format of the system is set to 'yyyymmdd' through the tdlocaledef utility. The following statement fails, because the character string contains separators, which does not match the default DATE format: SELECT CAST ('2005-01-01' AS DATE); To override the default DATE format, and convert a character string that contains separators, specify a FORMAT phrase for the DATE target type: SELECT CAST ('2005-01-01' AS DATE FORMAT 'YYYY-MM-DD'); In character-to-DATE conversions, the FORMAT phrase must not consist solely of the following formatting characters: IF the statement … THEN … specifies a FORMAT phrase for the DATE the character string must match that DATE format. does not specify a FORMAT phrase IF in … THEN … field mode IF the DATE column definition … THEN the character string must match … specifies a FORMAT phrase that DATE format. does not specify a FORMAT phrase the ANSI format ('YYYY-MM-DD') record or indicator mode the character string must match the ANSI format ('YYYY-MM-DD') • EEEE • E4 • EEE • E3 Chapter 20: Data Type Conversions Character-to-DATE Conversion 770 SQL Functions, Operators, Expressions, and Predicates For more information on default formats and the FORMAT phrase, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Character Strings That Omit Day, Month, or Year If the character string and the format for a character-to-DATE conversion omits the day, month, or year, the system uses default values for the target DATE value. Consider the following table: CREATE TABLE date_log (id INTEGER ,start_date DATE ,end_date DATE ,log_date DATE); The following INSERT statement converts three character strings to DATE values. The first character string omits the day, the second character string omits the month, and the third character string omits the year. Assume the current year is 1992. INSERT date_log (1001 ,CAST ('January 1992' AS DATE FORMAT 'MMMMBYYYY') ,CAST ('1992-01' AS DATE FORMAT 'YYYY-DD') ,CAST ('01/01' AS DATE FORMAT 'MM/DD')); The result of the INSERT statement is as follows: SELECT * FROM date_log; id start_date end_date log_date ----------- ---------- -------- -------- 1001 92/01/01 92/01/01 92/01/01 Example 1: IntegerDate Dateform Mode For example, suppose the session dateform is INTEGERDATE, and the default DATE format of the system is set to 'yyyymmdd' through the tdlocaledef utility. Consider the following table, where the start_date column uses the default DATE format and the end_date column uses the format 'YYYY/MM/DD': CREATE TABLE date_log (id INTEGER ,start_date DATE ,end_date DATE FORMAT 'YYYY/MM/DD'); IF the character string omits the … THEN the system uses the … day value of 1 (the first day of the month). month value of 1 (the month of January). year current year (at the current session time zone). Chapter 20: Data Type Conversions Character-to-DATE Conversion SQL Functions, Operators, Expressions, and Predicates 771 The following INSERT statement works because the character strings match the formats of the corresponding DATE columns and Teradata Database can successfully perform implicit character-to-DATE conversion: INSERT INTO date_log (1099, '20030122', '2003/01/23'); To perform character-to-DATE conversion on character strings that do not match the formats of the corresponding DATE columns, you must use a FORMAT phrase: INSERT INTO date_log (1047 ,CAST ('Jan 12, 2003' AS DATE FORMAT 'MMMBDD,BYYYY') ,CAST ('Jan 13, 2003' AS DATE FORMAT 'MMMBDD,BYYYY')); Example 2: ANSIDate Dateform Mode Suppose the session dateform is ANSIDATE. The default DATE format of the system is 'YYYY-MM-DD'. Consider the following table, where the start_date column uses the default DATE format and the end_date column uses the format 'YYYY/MM/DD': CREATE TABLE date_log (id INTEGER ,start_date DATE ,end_date DATE FORMAT 'YYYY/MM/DD'); The following INSERT statement works because the character strings match the formats of the corresponding DATE columns and Teradata Database can successfully perform implicit character-to-DATE conversion: INSERT INTO date_log (1099, '2003-01-22', '2003/01/23'); To perform character-to-DATE conversion on character strings that do not match the formats of the corresponding DATE columns, you must use a FORMAT phrase: INSERT INTO date_log (1047 ,CAST ('Jan 12, 2003' AS DATE FORMAT 'MMMBDD,BYYYY') ,CAST ('Jan 13, 2003' AS DATE FORMAT 'MMMBDD,BYYYY')); Example 3: Implicit Character-to-DATE Conversion Assume that the DateForm mode of the session is set to ANSIDate. The following CREATE TABLE statement specifies a FORMAT phrase for the DATE data type column: CREATE SET TABLE datetab (f1 DATE FORMAT 'MMM-DD-YYYY'); In field mode, the following INSERT statement successfully performs the character to DATE implicit conversion because the format of the string conforms to the format of the DATE column in the datetab table: INSERT INTO datetab ('JAN-10-1999'); Chapter 20: Data Type Conversions Character-to-DATE Conversion 772 SQL Functions, Operators, Expressions, and Predicates In record or indicator mode, when the DateForm mode of the session is set to ANSIDate, the following INSERT statement successfully performs the character to DATE implicit conversion because the format of the string is in the ANSI DATE format: INSERT INTO datetab ('2002-05-10'); Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions Character-to-INTERVAL Conversion SQL Functions, Operators, Expressions, and Predicates 773 Character-to-INTERVAL Conversion Purpose Converts a character string to an interval value. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI SQL, Teradata supports the specification of data attributes. Teradata Conversion Syntax where: Syntax element … Specifies … character_expression a character expression to be cast to an INTERVAL value. interval_data_type an INTERVAL data type to which character_expression is to be converted. data_attribute one of the following optional data attributes: • NAMED • TITLE 1101B245 CAST ( character_expression AS interval_data_type ) data_attribute Syntax element … Specifies … character_expression a character expression to be cast to an INTERVAL value. data_attribute one of the following optional data attributes: • NAMED • TITLE 1101B256 character_expression ( interval_data_type ) data_attribute , , data_attribute Chapter 20: Data Type Conversions Character-to-INTERVAL Conversion 774 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Usage Notes The character value is trimmed of leading and trailing pad characters and handled as if it was a string literal in the declaration of an INTERVAL string literal. Character-to-INTERVAL conversion is supported for CHAR and VARCHAR types only. The source character type cannot be CLOB. If the contents of the character string can be converted to a valid INTERVAL, then they are; otherwise, an error is returned. You cannot convert a character data type of GRAPHIC to an INTERVAL string literal. Example 1 The following query returns ' -265-11'. SELECT CAST('-265-11' AS INTERVAL YEAR(4) TO MONTH); Example 2 If the source character string contains values not normalized in the INTERVAL form, but which nevertheless can be converted to a proper INTERVAL, the conversion is made. For example, the following query returns '-267-06' SELECT CAST('265-30' AS INTERVAL YEAR(4) TO MONTH); Related Topics For details on data types and data attributes, see SQL Data Types and Literals. interval_data_type an INTERVAL data type to which character_expression is to be converted. Syntax element … Specifies … Chapter 20: Data Type Conversions Character-to-Numeric Conversion SQL Functions, Operators, Expressions, and Predicates 775 Character-to-Numeric Conversion Purpose Converts a character data string to a numeric value. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attributes, such as the FORMAT phrase that enables alternative formatting for the numeric data. Teradata Conversion Syntax where: Syntax element … Specifies … character_expression a character expression to be cast to a numeric type. numeric_data_definition the numeric type to which character_expression is to be converted. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE 1101A628 (character_expression numeric_data_type data_attribute CAST AS ) Syntax element … Specifies … character_expression a character expression to be cast to a numeric type. 1101A629 data_attribute , , data_attribute character_expression ( numeric_data_type ) Chapter 20: Data Type Conversions Character-to-Numeric Conversion 776 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Implicit Character-to-Numeric Conversion Implicit character to numeric conversion produces a valid result only if the character string represents a numeric value. If a CHAR or VARCHAR character string is present in an expression that requires a numeric operand, it is read as a formatted numeric and is converted to a FLOAT value, using the default format for FLOAT. To override the implicit format, use a FORMAT phrase. Or, to change the default format for FLOAT, you can change the setting of the REAL element in the specification for data formatting (SDF) file. For information on default data type formats, the SDF file, and the FORMAT phrase, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. To use a CLOB type in an expression that requires a numeric operand, you must first explicitly convert the CLOB to CHAR or VARCHAR. An empty character string (zero length) or a character string consisting only of pad characters is interpreted as having a numeric value of zero. If the default format for FLOAT is -9.99E-99, then: If a column or parameter of numeric data type is specified with a string value, the string is again assumed to be a formatted numeric. For example, the following INSERT statement specifies the Salary as a numeric string: INSERT INTO Employee (EmpNo, Name, Salary) VALUES (10022, 'Clements D', '$38,000.00'); data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE numeric_data_type the numeric type to which character_expression is to be converted. Syntax element … Specifies … THIS expression … IS converted to … AND the result is … 1.1*’$20.00’ 1.10E 00*2.00E1 2.20E 01 ’2’+’2’ 2.00E 00+2.00E 00 4.00E 00 ’A’ + 2 ---------- error Chapter 20: Data Type Conversions Character-to-Numeric Conversion SQL Functions, Operators, Expressions, and Predicates 777 The conversion to numeric type removes editing symbols. When selected, the salary data contains only the special characters allowed by the FORMAT phrase for Salary in the CREATE TABLE statement. If the FORMAT phrase is ’G-(9)D9(2)’, then the output looks like this: Salary --------- 38,000.00 If the FORMAT phrase is ’G-L(9)D9(2)’, then the output looks like this: Salary ---------- $38,000.00 Supported Character Types The character expression to be converted must be CHAR or VARCHAR. CLOBs cannot be explicitly converted to numeric types. Usage Notes Before processing begins, the numeric description is scanned for a FORMAT phrase, which is used to determine the radix separator, group separator, currency sign or string, signzone (S), or implied decimal point (V) formatting. Conversion is performed positionally, character by character, from left to right, until the end of the number. Only all-numeric character strings can be converted from character to numeric formats. For example, you can convert the character strings ’US Dollars 123456’ or ‘123456’ to the integer value 123456, but you cannot convert the string ‘EX1AM2PL3E’ to a numeric value. The following list shows the steps for converting character type data to numeric. Note that you cannot convert a character_expression of GRAPHIC character type to numeric. Conversion is performed stage by stage, without returning to a previous stage; however, stages can be skipped. 1 Leading pad characters are ignored. Trailing pad characters are ignored, except for signed zoned decimal input. Embedded spaces are only allowed according to the following rules: • If the current SDF file defines the group separator as a space, then the character string can include spaces to separate groups of digits to the left of the radix separator, according to the grouping rule defined by GroupingRule or CurrencyGroupingRule. • If the current SDF file defines the radix separator as a space, then the character string can include one space as the radix character. • If the FORMAT phrase contains a currency formatting character, such as N, and the matching currency string in the SDF file, such as CurrencyName, contains a space, the character string can include spaces as part of that currency string. Chapter 20: Data Type Conversions Character-to-Numeric Conversion 778 SQL Functions, Operators, Expressions, and Predicates 2 The sign (+ or -) is saved as part of the number. A mantissa sign may appear before the first digit in the string, or after the last digit in the string. An exponent sign may appear with a preceding mantissa sign. 3 The currency sign is ignored if it matches the FORMAT. A currency string is ignored if it matches the FORMAT. Only one currency is allowed in the string. 4 Digits are saved as the integral, fractional, or exponent part of the number, depending on whether the radix or the letter E has been parsed. 5 Separators are ignored, unless they match the radix specified in the FORMAT. If a separator matches the radix specified in the FORMAT, the location is saved as the beginning of the fractional part of the number. V marks the fractional component for implied decimals. The allowance of currency and separators is a non-ANSI Teradata extension of character to numeric conversion. 6 Embedded dashes (between digits) are allowed, unless the number is signed or includes a radix, currency, or exponent. 7 The letter E is saved as the beginning of the exponent part of the number. One space is allowed following an E. 8 The exponent sign (+ or -) is saved. 9 The exponent digits are saved. A sign character cannot appear after any exponent digit. Numeric Overflow In Field Mode, numeric overflow in character to numeric conversion is not treated as an error. If the result exceeds the number of digits normally reserved for the data type, asterisks are displayed. In Record and Indicator Variable Modes, numeric overflow is reported as an error. This behavior applies to both the CAST and Teradata conversion syntax. FORMAT Phrase Controls Parsing of the Data A FORMAT phrase, by itself, cannot convert a character type value to a numeric type value. The phrase controls partially how the resultant value is parsed. Some examples of character to numeric conversion appear in the following table. For FORMAT phrases that contain G, D, C, and N formatting characters, assume that the related entries in the specification for data formatting file (SDF) are: RadixSeparator {"."} GroupSeparator {","} GroupingRule {"3"} Currency {"$"} ISOCurrency {"USD"} CurrencyName {"US Dollars"} Chapter 20: Data Type Conversions Character-to-Numeric Conversion SQL Functions, Operators, Expressions, and Predicates 779 A conversion that does not specify a FORMAT phrase uses the corresponding data type default format as defined in the SDF. For more information on default data type formats, the SDF file, and the meaning of formatting characters in a FORMAT phrase, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Character String Converted To Resultant Numeric Value Field Mode Display Result '$20,000.00' DECIMAL(10,2) 20000.00 20000.00 '$$$.50' DECIMAL(10,2) errora a. Only one currency is allowed in the character string. error '$.50' DECIMAL(8,2) .50 .50 '.345' DECIMAL(8,3) .345 .345 '-1.234E-02' FLOAT -.01234 -.01234 '-1E.-2' FLOAT errorb b. The radix must precede the exponent part of the number. error '00000000-.93' DECIMAL(12,4) errorc c. Embedded dashes cannot appear in a string containing a radix. error '- 55' INTEGER -55 -55 'E67' FLOAT 0.0 0.00000000000000E 000 '9876' DECIMAL(4,2) FORMAT '99V99' 98.76 9876 '-123' INTEGER -123 -123 '9876' DECIMAL(4,2) FORMAT '9(2)V9(2)' 98.76 9876 '1-2-3' INTEGER 123 123 '123-' INTEGER -123 -123 '123- ' INTEGER -123 -123 '-1.234E 02' FLOAT -123.4 -1.23400000000000E 002 '111,222,333' INTEGER FORMAT 'G9(I)' 111222333 0,111,222,333 '2.49US Dollars' DECIMAL(10,2) FORMAT 'GZ(I)D9(F)BN' 2.49 2.49 US Dollars '25000USD' INTEGER FORMAT '9(I)C' 25000 0000025000USD Chapter 20: Data Type Conversions Character-to-Numeric Conversion 780 SQL Functions, Operators, Expressions, and Predicates Example: Implicit Conversion of Character to Numeric The INSERT statement in the following example implicitly converts the character data type to the target numeric data type: CREATE TABLE t1 (f1 DECIMAL(10,2) FORMAT 'G-U(9)D9(2)'); INSERT t1 ('USD12,345,678.90'); If a column definition in a CREATE TABLE statement does not specify a FORMAT phrase for the data type, the column uses the corresponding data type default format as defined in the specification for data formatting (SDF) file. For more information on the default format of data types and the definition of formatting characters in a FORMAT phrase, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions Character-to-Period Conversion SQL Functions, Operators, Expressions, and Predicates 781 Character-to-Period Conversion Purpose Converts a character string to a Period value. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. Usage Notes A character value expression can be cast as PERIOD(DATE), PERIOD(TIME), or PERIOD(TIMESTAMP) using the CAST function or implicit casting. A character input value can also be implicitly cast as a Period type. After any leading and trailing pad characters in the source character value are trimmed, the resulting character string must conform to the format of the target type. Conversion of the beginning and ending portions of the character value expression to corresponding DateTime values follow the existing rules of CHARACTER/VARCHAR to DateTime data type conversions. The existing rules include conversion of the source value with a TIME or TIMESTAMP format to UTC based on the specified time zone in the source or, if not specified, the current session time zone. The exception to conversion to UTC for Period data types is when the ending Syntax element … Specifies … character_expression a character expression to be cast to a Period value. period_data_type Period data type to which character_expression is to be converted. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE CAST character_expression AS period_data_type data_attribute ( ) 1101A587 Chapter 20: Data Type Conversions Character-to-Period Conversion 782 SQL Functions, Operators, Expressions, and Predicates portion of the source character is a TIMESTAMP value without a time zone and the value is equal to the maximum value that is used to represent UNTIL_CHANGED; in this case, the value is not changed to UTC. If the target type has a TIME or TIMESTAMP element type and the beginning or ending bound portions of the character value expression contains leap seconds, the seconds portion gets adjusted to 59.999999 with the precision truncated to the target precision. If target type has a TIME or TIMESTAMP element type and the target precision is lower than either precision specified in the source character string, an error is reported. If the target precision is higher than a precision specified for a bound in the source character string, trailing zeros are added to the fractional seconds of the corresponding bound of the Period value resulting from the cast. The target elements are set to the corresponding resulting values. If the result beginning bound is not less than the result ending bound in their UTC forms, an error is reported. If an ANSI DateTime format is used to interpret the character data during conversion, then enclosing the beginning and ending values inside apostrophes is optional. For details, see “Character Strings that Use ANSI DateTime Format” on page 783. Implicit Character-to-Period Conversion A CHARACTER or VARCHAR value is implicitly cast as a Period data type for an assignment, update, insert, merge, or parameter passing operation when the target site has a Period data type and for a comparison operation if the other operand has a Period data type. If any other non-Period value is directly assigned to a Period target site, an error is reported. In the same manner, if any other non-Period value is directly compared to a Period value, an error is reported. Note: In some cases, a value may be explicitly cast as a Period data type in order to avoid this error. During implicit conversion from CHARACTER or VARCHAR to Period data type, the ANSI DateTime format string is used to interpret the beginning and ending element values in the character string, if the response mode is other than the Field mode or if the character string data is parameterized. If the response mode is Field mode and if the character string data is not parameterized, then the target period format is used to interpret the beginning and ending element values in the character string. The following table describes this in detail. Mode Parameterized Data Present Format for Implicit Cast Interpretation Field No Target format Field Yes ANSI format Non-field Yes ANSI format Non-field No ANSI format Chapter 20: Data Type Conversions Character-to-Period Conversion SQL Functions, Operators, Expressions, and Predicates 783 When the ANSI DateTime format string is used to interpret the beginning and ending element values in the character string, enclosing the beginning and ending values inside the apostrophes is optional. This relaxation applies even during an explicit cast. For details, see “Character Strings that Use ANSI DateTime Format” on page 783. Character Strings that Use ANSI DateTime Format Here is a list of valid character string representations when the implicit or explicit characterto- period conversion uses the ANSI DateTime format to interpret the beginning and ending bound elements. • '(''beginning_element_value'',?''ending_element_value'')' • '(beginning_element_value,?ending_element_value)' • '(''beginning_element_value'',?ending_element_value)' • '(beginning_element_value,?''ending_element_value'')' where formats of beginning_element_value and ending_element_value depend on the target data type. For the meanings of the format characters, see the description of the FORMAT phrase in SQL Data Types and Literals. Example In the following example, two concatenated character literals are cast as PERIOD(TIMESTAMP(2)). The output is adjusted according to the current session time zone during display. Assume the current session time zone displacement is INTERVAL -'08:00' HOUR TO MINUTE and the format derived from SDF is 'YYYY-MM-DDBHH:MI:SS.S(2)Z'. SELECT CAST('(''2005-02-02 12:12:12.34+08:00'', ' || '''2006-02-03 12:12:12.34+08:00'')' AS PERIOD(TIMESTAMP(2))); The following PERIOD(TIMESTAMP(2)) value is returned: ('2005-02-01 20:12:12.34', '2006-02-02 20:12:12.34') Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Target Data Type Format PERIOD(DATE) YYYY-MM-DD PERIOD(TIME[(n)]) HH:MI:SS.S(F) PERIOD(TIMESTAMP[(n)]) YYYY-MM-DDBHH:MI:SS.S(F) Chapter 20: Data Type Conversions Character-to-TIME Conversion 784 SQL Functions, Operators, Expressions, and Predicates Character-to-TIME Conversion Purpose Converts a character data string to a TIME or TIME WITH TIME ZONE value. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attributes, such as the FORMAT phrase that enables alternative output formatting for the time data. Note: TIME (without time zone) is not ANSI SQL:2008 compliant. Teradata Database internally converts a TIME value to UTC based on the current session time zone or on a specified time zone. Syntax element … Specifies … character_expression a character expression to be cast to a TIME type. fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. time_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE 1101A246 CAST character_expression AS ) ( A WITH TIME ZONE time_data_attribute TIME (fractional_seconds_precision) A Chapter 20: Data Type Conversions Character-to-TIME Conversion SQL Functions, Operators, Expressions, and Predicates 785 Teradata Conversion Syntax where: ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Implicit Character-to-TIME Conversion In field mode, the string must conform to the format of the target TIME type. In record or indicator mode, the string must use the ANSI TIME format. Usage Notes The character value is trimmed of leading and trailing pad characters and handled as if it were a string literal in the declaration of a TIME string literal. If the contents of the string can be converted to a valid TIME, the conversion is made; otherwise, an error is returned to the application. Character-to-TIME conversion is supported for CHAR and VARCHAR types only. You cannot convert a character data type of CLOB or GRAPHIC to TIME. Syntax element … Specifies … character_expression a character expression to be cast to a TIME type. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. 1101B257 character_expression (fractional_seconds_precision) ( TIME ) A WITH TIME ZONE , data_attribute A data_attribute , Chapter 20: Data Type Conversions Character-to-TIME Conversion 786 SQL Functions, Operators, Expressions, and Predicates You can use a FORMAT phrase to specify an explicit format for the TIME target data type. A conversion that does not specify a FORMAT phrase uses the default format for the TIME data type. For more information on default formats and the FORMAT phrase, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Conversions That Include Time Zone The following rules apply to character-to-TIME conversions that include time zone information: • If the target data type does not specify a time zone, for example, TIME(0), the source character string may contain a time zone of the format +hh:mi or -hh:mi, but only if it appears immediately before or immediately after the time. For example, the following conversion is successful: SELECT CAST ( '-02:0011:23:44' AS TIME(0) ); The following conversion is not successful because of the blank separator character between the time zone and the time: SELECT CAST ( '+02:00 11:23:44.56' AS TIME(2) ); • If the source character string contains a time zone, and the target data type does not specify a time zone, for example, TIME(0), the conversion uses the time zone in the character string to convert the character string to Universal Coordinated Time (UTC). This is done regardless of whether the FORMAT phrase contains the time zone formatting character. SELECT CAST ('10:15:12+12:30' AS TIME(0)); • If the source character string does not contain a time zone, and the target data type specifies a time zone and a target FORMAT phrase that includes time zone formatting characters, the output includes the session time zone. SELECT CAST ('10:15:12' AS TIME(0) WITH TIME ZONE FORMAT 'HH:MI:SSBZ'); • If both the source character string and the target data type do not specify a time zone, the source character string is internally converted to UTC based on the current session time zone. IF the character string is converted to … THEN the default format … TIME does not use the time zone formatting character and does not display a time zone. TIME WITH TIME ZONE uses the time zone formatting character to display the time zone. Chapter 20: Data Type Conversions Character-to-TIME Conversion SQL Functions, Operators, Expressions, and Predicates 787 Conversions That Include Fractional Seconds The following rules apply to conversions that include fractional seconds: • The fractional seconds precision in the source character string must be less than or equal to the fractional seconds precision specified by the target type. SELECT CAST('12:30:25.44' AS TIME(3)); If no fractional seconds appear in the source character string, then the fractional seconds precision is always less than or equal to the target data type fractional seconds precision, because the valid range for the precision is zero to six, where the default is six. SELECT CAST('12:30:25' AS TIME(3)); • If the target data type is defined by a FORMAT phrase, the fractional seconds precision formatting characters must be greater than or equal to the precision specified by the data type. SELECT CAST('12h:15.12s:30m' AS TIME(4) FORMAT 'HHh:SSDS(4)s:MIm'); A FORMAT phrase must specify a fractional seconds precision of six if the target data type does not specify a fractional seconds precision, because the default precision is six. SELECT CAST ('12:30:25' AS TIME FORMAT 'HH:MI:SSDS(6)'); Character Strings That Omit Hour, Minute, or Second If the character string in a character-to-TIME conversion omits the hour, minute, or second, the system uses default values for the target TIME value. Consider the following table: CREATE TABLE time_log (id INTEGER ,start_time TIME ,end_time TIME ,log_time TIME); The following INSERT statement converts three character strings to TIME values. The first character string omits the hour, the second character string omits the minute, and the third character string omits the second. INSERT time_log (1001 ,CAST ('01:02.030405' AS TIME FORMAT 'MI:SS.S(6)') ,CAST ('01:02.030405' AS TIME FORMAT 'HH:SS.S(6)') ,CAST ('01:02' AS TIME FORMAT 'HH:MI')); IF the character string omits the … THEN the system uses the … hour value of 0. minute second Chapter 20: Data Type Conversions Character-to-TIME Conversion 788 SQL Functions, Operators, Expressions, and Predicates The result of the INSERT statement is as follows: SELECT * FROM time_log; id start_time end_time log_time ----------- --------------- --------------- --------------- 1001 00:01:02.030405 01:00:02.030405 01:02:00.000000 FORMAT Phrase Restrictions In character-to-TIME conversions, the FORMAT phrase must not consist solely of the following formatting characters: • Z • T Example 1: Fractional Seconds This query returns the value ‘12:23:39.999900’ (with the fractional seconds extended to 6 places as requested by CASTing to a TIME(6) type). SELECT CAST(' 12:23:39.9999 ' AS TIME(6)); Example 2: Truncation of Non-pad Character Data This query returns an error because the requested conversion requires truncation of non-pad character data. SELECT CAST(' 12:23:39.9999 ' AS TIME(3)); Example 3: Invalid MINUTE Value This query returns an error because the MINUTE value of 63 is not valid. SELECT CAST(' 12:63:39.9999 ' AS TIME(6)); Example 4: FORMAT Phrase This query returns the value '15h33m'. SELECT CAST('15h33m' AS TIME(0) FORMAT 'HHhMIm'); Example 5: Implicit Conversion of Character to TIME The following CREATE TABLE statement specifies a FORMAT phrase for the TIME data type column: CREATE SET TABLE timetab (f1 TIME(0) FORMAT 'TBHHhMImSSs'); In field mode, the following INSERT statement successfully performs the character to TIME implicit conversion because the format of the string conforms to the format of the TIME column in the timetab table: Chapter 20: Data Type Conversions Character-to-TIME Conversion SQL Functions, Operators, Expressions, and Predicates 789 INSERT INTO timetab ('AM 10h20m30s'); In record or indicator mode, the following INSERT statement successfully performs the character to TIME implicit conversion because the format of the string is in the ANSI TIME format: INSERT timetab ('11:23:34'); Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions Character-to-TIMESTAMP Conversion 790 SQL Functions, Operators, Expressions, and Predicates Character-to-TIMESTAMP Conversion Purpose Converts a character data string to a TIMESTAMP or TIMESTAMP WITH TIME ZONE value. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attributes, such as the FORMAT phrase that enables alternative formatting for the time data. Note: TIMESTAMP (without time zone) is not ANSI SQL:2008 compliant. Teradata Database internally converts a TIMESTAMP value to UTC based on the current session time zone or on a specified time zone. Syntax element … Specifies … character_expression a character expression to be cast to a TIMESTAMP type. fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. timestamp_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE 1101A247 CAST character_expression AS TIMESTAMP ) ( A WITH TIME ZONE timestamp_data_attribute (fractional_seconds_precision) A Chapter 20: Data Type Conversions Character-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 791 Teradata Conversion Syntax where: ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Implicit Character-to-TIMESTAMP Conversion In field mode, the string must conform to the format of the target TIMESTAMP type. In record or indicator mode, the string must use the ANSI TIMESTAMP format. Usage Notes The source expression is trimmed of leading and trailing pad characters and then handled as if it were a string literal in the declaration of a TIMESTAMP string literal. Character-to-TIMESTAMP conversion is supported for CHAR and VARCHAR types only. You cannot convert a character data type of CLOB or GRAPHIC to TIMESTAMP. If the contents of the string can be converted to a valid TIMESTAMP value, then the conversion is performed; otherwise an error is returned. Syntax element … Specifies … character_expression a character expression to be cast to a TIMESTAMP type. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. 1101B258 character_expression (fractional_seconds_precision) TIMESTAMP ) ( A A data_attribute , WITH TIME ZONE , data_attribute Chapter 20: Data Type Conversions Character-to-TIMESTAMP Conversion 792 SQL Functions, Operators, Expressions, and Predicates You can use a FORMAT phrase to specify an explicit format for the TIMESTAMP target data type. A conversion that does not specify a FORMAT phrase uses the default format for the TIMESTAMP data type. For more information on default formats and the FORMAT phrase, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Example The following query returns ‘2007-12-31 23:59:59.999999-08:00’. SELECT CAST('2007-12-31 23:59:59.999999' AS TIMESTAMP(6) WITH TIME ZONE); Notice that the source character string did not need to have explicit Time Zone fields for this conversion to work properly. Conversions That Include Time Zone The following rules apply to character-to-TIMESTAMP conversions that include time zone information: • If the target data type does not specify a time zone, for example, TIMESTAMP(0), the source character string may contain a time zone of the format +hh:mi or -hh:mi, but only if it appears immediately before or immediately after the time. For example, the following conversion is successful: SELECT CAST ( '2008-09-19 11:23:44-02:00' AS TIMESTAMP(0) FORMAT 'Y4-MM-DDBHH:MI:SSBZ' ); The following conversion is not successful because of the blank separator character between the time zone and the time: SELECT CAST ( '2008-01-19 +02:00 11:23:44' AS TIMESTAMP(0) FORMAT 'Y4-MM-DDBZBHH:MI:SS' ); • If the source character string contains a time zone, and the target data type does not specify a time zone, the conversion uses the time zone in the character string to convert the character string to Universal Coordinated Time (UTC). This is done whether or not the FORMAT phrase contains the time zone formatting character. SELECT CAST ('2002-02-20 10:15:12+12:30' AS TIMESTAMP(0)); IF the character string is converted to … THEN the default format … TIMESTAMP does not use the time zone formatting character and does not display a time zone. TIMESTAMP WITH TIME ZONE uses the time zone formatting character to display the time zone. Chapter 20: Data Type Conversions Character-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 793 • If the target FORMAT phrase includes time zone formatting characters, and the source character string does not contain a time zone, the output includes the session time zone. This is done whether or not the target data type specifies a time zone. SELECT CAST ('2002-02-20 10:15:12' AS TIMESTAMP(0) WITH TIME ZONE FORMAT 'Y4-MM-DDBHH:MI:SSBZ'); • If both the source character string and the target data type do not specify a time zone, the source character string is internally converted to UTC based on the current session time zone. Conversions That Include Fractional Seconds The following rules apply to conversions that include fractional seconds: • The fractional seconds precision in the source character string must be less than or equal to the fractional seconds precision specified by the target type. SELECT CAST('2002-01-01 12:30:25.44' AS TIMESTAMP(3)); If no fractional seconds appear in the source character string, then the fractional seconds precision is always less than or equal to the target data type fractional seconds precision, because the valid range for the precision is zero to six, where the default is six. SELECT CAST('2002-01-01 12:30:25' AS TIMESTAMP(3)); • If the target data type is defined by a FORMAT phrase, the fractional seconds precision formatting characters must be greater than or equal to the precision specified by the data type. SELECT CAST('12-02-07 12:30:25' AS TIMESTAMP(3) FORMAT 'DD-MM-YYBHH:MI:SSDS(3)'); A FORMAT phrase must specify a fractional seconds precision of six if the target data type does not specify a fractional seconds precision, because the default precision is six. SELECT CAST('12-02-07 12h:15.12s:30m' AS TIMESTAMP FORMAT 'DD-MM-YYBHHh:SSDS(6)s:MIm'); Character Strings That Omit Day, Month, Year, Hour, Minute, or Second If the character string in a character-to-TIMESTAMP conversion omits the day, month, year, hour, minute, or second, the system uses default values for the target TIMESTAMP value. IF the character string omits the … THEN the system uses the … day value of 1 (the first day of the month). month value of 1 (the month of January). year current year. hour value of 0. minute second Chapter 20: Data Type Conversions Character-to-TIMESTAMP Conversion 794 SQL Functions, Operators, Expressions, and Predicates Consider the following table: CREATE TABLE timestamp_log (id INTEGER, start_ts TIMESTAMP, end_ts TIMESTAMP); The following INSERT statement converts two character strings to TIMESTAMP values. Both strings omit the hour, minute, and second. Additionally, the first character string omits the day and the second character string omits the month. INSERT timestamp_log (1001 ,CAST ('January 2006' AS TIMESTAMP FORMAT 'MMMMBYYYY') ,CAST ('2006-01' AS TIMESTAMP FORMAT 'YYYY-DD')); The result of the INSERT statement is as follows: SELECT * FROM timestamp_log; id start_ts end_ts ----------- -------------------------- -------------------------- 1001 2006-01-01 00:00:00.000000 2006-01-01 00:00:00.000000 Here is an INSERT statement where both character strings omit the year. Additionally, the first character string omits the hour and the second character string omits the minute. Assume the current year is 2003. INSERT timestamp_log (1002 ,CAST ('January 23 04:05' AS TIMESTAMP FORMAT 'MMMMBDDBMI:SS') ,CAST ('01-23 04:05' AS TIMESTAMP FORMAT 'MM-DDBHH:SS')); The result of the INSERT statement is as follows: SELECT * FROM timestamp_log WHERE id = 1002; id start_ts end_ts ----------- -------------------------- -------------------------- 1001 2003-01-23 00:04:05.000000 2003-01-23 04:00:05.000000 Restrictions on FORMAT Phrase In character-to-TIMESTAMP conversions, the FORMAT phrase must not consist solely of the following formatting characters: • EEEE • E4 • EEE • E3 • T • Z Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions Character-to-UDT Conversion SQL Functions, Operators, Expressions, and Predicates 795 Character-to-UDT Conversion Purpose Converts a character data string to a UDT. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases such as FORMAT. Usage Notes Explicit character-to-UDT conversion using Teradata conversion syntax is not supported. Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. Implicit Character-to-UDT Conversion Teradata Database performs implicit Character-to-UDT conversions for the following operations: • UPDATE • INSERT • Passing arguments to stored procedures, external stored procedures, UDFs, and UDMs • Specific system operators and functions identified in other sections of this book, unless the DisableUDTImplCastForSysFuncOp field of the DBS Control Record is set to TRUE Performing an implicit data type conversion requires that an appropriate cast definition (see “Usage Notes”) exists that specifies the AS ASSIGNMENT clause. Syntax element … Specifies … character_expression a character expression to be cast to a UDT. UDT_data_definition the UDT type to which character_expression is to be converted. CAST AS character_expression UDT_data_definition ( ( 1101A336 Chapter 20: Data Type Conversions Character-to-UDT Conversion 796 SQL Functions, Operators, Expressions, and Predicates The source character type of the cast definition does not have to be an exact match to the source character type of the implicit conversion. Teradata Database can use an implicit cast definition that specifies a CHAR, VARCHAR, or CLOB source type. If multiple implicit cast definitions exist for converting different character types to the UDT, Teradata Database uses the implicit cast definition for the character type with the highest precedence. The following list shows the precedence of character types in order from lowest to highest precedence: • CHAR • VARCHAR • CLOB For non-CLOB character types, if no Character-to-UDT implicit cast definitions exist, Teradata Database looks for other cast definitions that can substitute: IF the following combination of implicit cast definitions exists … THEN Teradata Database … Numericto- UDT DATEto- UDT TIMEto- UDT TIMESTAMPto- UDT X uses the numeric-to-UDT implicit cast definition. If multiple numeric-to-UDT implicit cast definitions exist, then Teradata Database returns an SQL error. X uses the DATE-to-UDT implicit cast definition. X uses the TIME-to-UDT implicit cast definition. X uses the TIMESTAMP-to-UDT implicit cast definition. X X reports an error. X X X X X X X X X X X X X X X X X X X X X X X X X X Chapter 20: Data Type Conversions Character Data Type Assignment Rules SQL Functions, Operators, Expressions, and Predicates 797 Substitutions are valid because Teradata Database can implicitly cast the non-CLOB character type to the substitute data type, and then use the implicit cast definition to cast from the substitute data type to the UDT. Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Character Data Type Assignment Rules Server Character Sets LATIN, UNICODE, KANJISJIS, KANJI1, and GRAPHIC server character sets are generally mutually assignable. Consider an assignment of an expression to a character string column. The assignment may be the result of the SQL UPDATE or INSERT statement, or it may be the result of a Load utility assignment. The expression is converted to the server character set of the character column. Exceptions to GRAPHIC Data The following exceptions apply to GRAPHIC data: • When you import GRAPHIC data and assign it to a character column, that column must be defined as GRAPHIC. • When you import character data that is not GRAPHIC, you cannot assign it to a column defined as GRAPHIC. For more information, see the documentation on the USING row descriptor in SQL Data Manipulation Language. • You cannot assign non-GRAPHIC data to a GRAPHIC column from BTEQ or load utilities. For more information, see the documentation on the USING row descriptor in SQL Data Manipulation Language. • You cannot assign or export GRAPHIC data from a single byte character set like ASCII or EBCDIC. Chapter 20: Data Type Conversions DATE-to-Character Conversion 798 SQL Functions, Operators, Expressions, and Predicates DATE-to-Character Conversion Purpose Converts a DATE value to a character string. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of character data attribute phrases. Teradata Conversion Syntax Syntax element … Specifies … date_expression a date expression to be cast to a character string. character_data_type the character data type to which date_expression is to be converted. character_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE 1101A248 CAST date_expression AS character_data_type ) ( A CHARACTER SET server_character_set character_data_attribute A 1101B259 data_expression ( character_data_type ) A CHARACTER SET server_character_set , data_attribute A data_attribute , Chapter 20: Data Type Conversions DATE-to-Character Conversion SQL Functions, Operators, Expressions, and Predicates 799 where: ANSI Compliance This is a Teradata extension to the ANSI SQL:2008 standard. Usage Notes When converting DATE to CHAR(n) or VARCHAR(n), then n must be equal to or greater than the length of the DATE value as represented by a character string literal. Restrictions DATE types cannot be implicitly or explicitly converted to character types if the server character set is GRAPHIC. DATE to CLOB conversion is not supported. Forcing a FORMAT on CAST for Converting DATE to Character The default format for DATE to character conversion uses the format in effect for the DATE value. Syntax element … Specifies … date_expression a date expression to be cast to a character string. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE character_data_type the character data type to which date_expression is to be converted. server_character_set the server character set to use for the conversion. If the CHARACTER SET clause is omitted, the user default character set is used for the conversion. IF the target data type is … AND n is … THEN … CHAR(n) greater than the length of the DATE value as represented by a character string literal trailing pad characters are added to pad the representation. too small a string truncation error is returned. VARCHAR(n) greater than the length of the DATE value as represented by a character string literal no blank padding is added to the character representation. too small a string truncation error is returned. Chapter 20: Data Type Conversions DATE-to-Character Conversion 800 SQL Functions, Operators, Expressions, and Predicates To override the default format, you can convert a DATE value to a string using a FORMAT phrase. The resulting format, however, is the same as the DATE value. If you want a different format for the string value, you need to also use CAST as described here. You must use nested CAST operations in order to convert values from DATE to CHAR and force an explicit FORMAT on the result regardless of the format associated with the DATE value. This is because of the rules for matching FORMAT phrases to data types. Example 1 The dateform mode of the session is INTEGERDATE and column F1 in the table INTDAT is a DATE value with the explicit format 'YYYY,MMM,DD'. SELECT F1 FROM INTDAT ; The result (without a type change) is the following report: F1 ---------- 1900,Dec,31 Assume that you want to convert this to a value of CHAR(12), and an explicit output format of 'MMMBDD,BYYYY'. Use nested CAST phrases and a FORMAT to obtain the desired result: a report in character format. SELECT CAST( (CAST (F1 AS FORMAT 'MMMBDD,BYYYY')) AS CHAR(12)) FROM INTDAT; The result after the nested CASTs is the following report. F1 ------------ Dec 31, 1900 The inner CAST establishes the display format for the DATE value and the outer CAST indicates the data type of the desired result. Example 2 Suppose you need to create a script to convert date values to the ANSI DATE format, regardless of the source of the DATE value or the DATEFORM mode of the session. You can use nested CASTs and a FORMAT to do this as demonstrated by the example that follows. SELECT CAST( (CAST (F1 AS FORMAT 'YYYY-MM-DD')) AS CHAR(10)) FROM INTDAT; The result after the nested CASTs is the following report. F1 ---------- 1900-12-31 Chapter 20: Data Type Conversions DATE-to-Character Conversion SQL Functions, Operators, Expressions, and Predicates 801 Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions DATE-to-DATE Conversion 802 SQL Functions, Operators, Expressions, and Predicates DATE-to-DATE Conversion Use DATE-to-DATE conversion to convert the format or title of a DATE type. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. The following are Teradata extensions to CAST: • CAST permits the use of data attributes, such as the FORMAT phrase that enables alternative output formatting of date data. • A DATE-to-DATE conversion involving a DATE type with a dateform of INTEGERDATE is a Teradata extension to the ANSI SQL:2008 standard. Teradata Conversion Syntax Syntax element … Specifies … date_expression a date expression to be converted. date_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE 1101A249 date_expression date_data_attribute CAST ( AS DATE ) date_data_attribute 1101B260 date_expression data_attribute DATE , DATE ( ) , , data_attribute , data_attribute Chapter 20: Data Type Conversions DATE-to-DATE Conversion SQL Functions, Operators, Expressions, and Predicates 803 where: ANSI Compliance This is a Teradata extension to the ANSI SQL:2008 standard. Example Consider a table named employee that was created with a session dateform mode of INTEGERDATE where dob is a DATE column with a format of M3BDDBY4. To list employees who were born between January 30, 1938, and March 30, 1943, you could specify the date information as follows: SELECT name, dob FROM employee WHERE dob BETWEEN 'Jan 30 1938' AND 'Mar 30 1943' ORDER BY dob; The result returns the date of birth information as specified for the Employee table: Name DOB ---------- ----------- Inglis C Mar 07 1938 Peterson J Mar 27 1942 To change the date format to an alternate form, change the SELECT to: SELECT name, dob (FORMAT 'yy-mm-dd') FROM employee WHERE dob BETWEEN 'Jan 30 1938' AND 'Mar 30 1943' ORDER BY dob ; The format specification changes the display to the following: Name DOB ---------- -------- Inglis C 38-03-07 Peterson J 42-03-27 Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Syntax element … Specifies … date_expression a date expression to be converted. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE Chapter 20: Data Type Conversions DATE-to-Numeric Conversion 804 SQL Functions, Operators, Expressions, and Predicates DATE-to-Numeric Conversion Introduction DATE data may be converted to the following numeric types: • SMALLINT • BYTEINT • INTEGER • BIGINT • DECIMAL(n,m) • FLOAT CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of numeric data attribute phrases. Teradata Conversion Syntax where: Syntax element … Specifies … date_expression a date expression to be converted. numeric_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE 1101A250 CAST date_expression AS numeric_data_type ) numeric_data_attribute ( 1101B261 date_expression ( numeric_data_type ) data_attribute , , data_attribute Chapter 20: Data Type Conversions DATE-to-Numeric Conversion SQL Functions, Operators, Expressions, and Predicates 805 ANSI Compliance This is a Teradata extension to the ANSI SQL:2008 standard. Usage Notes When a date is converted to a numeric, the value returned is the integer value for the internal stored date, which is encoded using the following formula: (year - 1900) * 10000 + (month * 100) + day Allowable date values range from AD January 1, 0001 to AD December 31, 9999. For example, December 31, 1985 would be stored as the integer 851231; July 4, 1776 stored as -1239296; and March 30, 2041 stored as 1410330. Conversion of DATE to DECIMAL(n,m) where the number of digits (n) is too small generates a numeric overflow error. Conversion of DATE to BYTEINT or SMALLINT generates a numeric overflow error if the value returned is outside the range of values that the data type can represent. No error is generated on conversion of DATE to INTEGER or FLOAT. FORMAT Phrase A FORMAT phrase in DATE to numeric conversion may only contain the 9 or Z formatting character. For example: SELECT CAST (DATE '2007-12-31' AS INTEGER FORMAT '9999999'); Implicit DATE-to-Numeric Conversion Teradata Database performs implicit DATE-to-numeric type conversion when you assign a DATE type to a numeric type, compare a DATE type and numeric type, or pass a DATE type to a system function that takes a numeric type. Syntax element … Specifies … date_expression a date expression to be converted. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE numeric_data_type the target numeric type to which the date expression is to be converted. Chapter 20: Data Type Conversions DATE-to-Numeric Conversion 806 SQL Functions, Operators, Expressions, and Predicates Example The following example converts DATE data in the dob column of the employee table to a numeric format. Note that the best practice is to define date data as a DATE type; do not define date data as a numeric type. To change the display from date format to integer format, change the statement to: SELECT name, dob (INTEGER) FROM employee WHERE dob BETWEEN 380307 AND 420825 ORDER BY dob ; or SELECT name, CAST (dob AS INTEGER) FROM employee WHERE dob BETWEEN 380307 AND 420825 ORDER BY dob ; and the display becomes: Name DOB ---------- ------ Inglis C 380307 Peterson J 420327 Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions DATE-to-Period Conversion SQL Functions, Operators, Expressions, and Predicates 807 DATE-to-Period Conversion Casts as PERIOD(DATE) or PERIOD(TIMESTAMP[(n)] [WITH TIME ZONE]). CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases. Usage Notes A DATE value can be cast as PERIOD(DATE) or PERIOD(TIMESTAMP[(n)] [WITH TIME ZONE]) using the CAST function. If an attempt is made to cast a DATE value as PERIOD(TIME[(n)] [WITH TIME ZONE]), an error is reported. If the target type is PERIOD(DATE), the result beginning element is set to the source value. The result ending element is set to the result beginning bound plus one granule of the target type (that is, INTERVAL '1' DAY). If the result ending bound exceeds the maximum DATE value (that is, the source value is equal to the maximum DATE value), or the result ending bound equal to maximum DATE value (that is, the resulting ending bound value equal to value of UNTIL_CHANGED) an error is reported. If the target type is PERIOD(TIMESTAMP[(n)]), the result beginning element is set to the UTC value obtained using the current session time zone and a timestamp value formed from Syntax element … Specifies … date_expression a date expression to be converted. period_data_type the target Period type to which the date expression is to be converted. period_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE CAST ( date_expression AS period_data_type ) 1101A589 period_data_attribute Chapter 20: Data Type Conversions DATE-to-Period Conversion 808 SQL Functions, Operators, Expressions, and Predicates the source DATE value and a time portion of zero. The result ending element is set to the result beginning bound plus one granule of the target type (note that this cannot cause an error). If the target type is PERIOD(TIMESTAMP[(n)] WITH TIME ZONE), the time portion of the result beginning element is set to the UTC value obtained using the current session time zone and a timestamp value formed from the source DATE value and a time portion of zero. The time zone of the result beginning element is set to the current session time zone displacement. The result ending element is set to the result beginning bound plus one granule of the target type (note that this cannot cause an error). Note: The result has the same value for the beginning bound and last value. Example 1 In the following example, a DATE literal is cast as PERIOD(DATE). The result beginning bound is obtained from the source. The result ending element is set to the result beginning bound plus INTERVAL '1' DAY. SELECT CAST(DATE '2005-02-03' AS PERIOD(DATE)); The following PERIOD(DATE) value is returned: ('2005-02-03', '2005-02-04') Example 2 In the following example, a DATE literal is cast as PERIOD(TIMESTAMP(4)). The result beginning bound is formed from the DATE literal and a time portion of zero. The result ending element is set to the result beginning bound plus INTERVAL '0.0001' SECOND. SELECT CAST(DATE '2005-02-03' AS PERIOD(TIMESTAMP(4))); The following PERIOD(TIMESTAMP(4)) value is returned: ('2005-02-03 00:00:00.0000', '2005-02-03 00:00:00.0001') Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions DATE-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 809 DATE-to-TIMESTAMP Conversion Purpose Converts a DATE value to a TIMESTAMP or TIMESTAMP WITH TIME ZONE value. CAST Syntax where: Syntax element … Specifies … date_expression a date expression to be converted. fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. AT LOCAL that the time zone displacement based on the current session time zone is used. This is the default. AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. date_expression expression time_zone_string (fractional_seconds_precision) CAST ( AS TIMESTAMP A WITH TIME ZONE AT LOCAL TIME ZONE A B 1101C251 ) timestamp_data_attribute B Chapter 20: Data Type Conversions DATE-to-TIMESTAMP Conversion 810 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of the FORMAT phrase to enable alternative output formatting of timestamp data. The AT clause is ANSI SQL:2008 compliant. As an extension to ANSI, the AT clause is supported when converting from DATE to TIMESTAMP using CAST. In addition, you can specify the time zone displacement using additional expressions besides an INTERVAL expression. Note: TIMESTAMP (without time zone) is not ANSI SQL:2008 compliant. Teradata Database internally converts a TIMESTAMP value to UTC based on the current session time zone or on a specified time zone. Teradata Conversion Syntax where: AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement used for the CAST. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. timestamp_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE Syntax element … Specifies … 1101D262 date_expression (fractional_seconds_precision) ( TIMESTAMP A data_attribute , expression time_zone_string , WITH TIME ZONE AT LOCAL TIME ZONE A B ) , data_attribute B Chapter 20: Data Type Conversions DATE-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 811 ANSI Compliance Teradata Conversion Syntax is a Teradata extension to the ANSI SQL:2008 standard. The AT clause is ANSI SQL:2008 compliant. As an extension to ANSI, the AT clause is supported when converting from DATE to TIMESTAMP using Teradata Conversion Syntax. In addition, you can specify the time zone displacement using additional expressions besides an INTERVAL expression. Note: TIMESTAMP (without time zone) is not ANSI SQL:2008 compliant. Teradata Database internally converts a TIMESTAMP value to UTC based on the current session time zone or on a specified time zone. Usage Notes The following table shows the result of the CAST function or Teradata conversion based on the various options specified. If the target precision is higher than zero, trailing zeros are added in the result to adjust the precision. Syntax element … Specifies … date_expression a date expression to be converted. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. AT LOCAL that the time zone displacement based on the current session time zone is used. This is the default. AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement used for the conversion. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. Chapter 20: Data Type Conversions DATE-to-TIMESTAMP Conversion 812 SQL Functions, Operators, Expressions, and Predicates Implicit DATE-to-TIMESTAMP Conversion Teradata Database performs implicit conversion from DATE to TIMESTAMP types in some cases. See “Implicit Conversion of DateTime types” on page 748. The following conversions are supported: The TIMESTAMP value is always converted to DATE in case of comparison. See “TIMESTAMP-to-DATE Conversion” on page 894. Example 1 In this example, the result of the CAST is the timestamp formed from the source expression value '2008-05-14' and the default time '00:00:00' adjusted to UTC by the current session time zone displacement, INTERVAL '01:00' HOUR TO MINUTE. Thus, the value of the CAST is '2008-05-13 23:00:00' at UTC. The result value of the CAST at UTC is adjusted to the current session time zone displacement, INTERVAL '01:00' HOUR TO MINUTE, so the result of the SELECT statements is: TIMESTAMP '2008-05-14 00:00:00'. SET TIME ZONE INTERVAL '01:00' HOUR TO MINUTE; SELECT CAST(DATE '2008-05-14' AS TIMESTAMP(0)); SELECT CAST(DATE '2008-05-14' AS TIMESTAMP(0) AT LOCAL); IF you specify... THEN... AT LOCAL a local timestamp value is formed from the source date_expression with the time portion set to '00:00:00'. Then, the result is formed from this local timestamp value adjusted to UTC by subtracting the time zone displacement based on the current session time zone. This is the same as not specifying the AT clause. AT expression or AT TIME ZONE expression a local timestamp value is formed from the source date_expression with the time portion set to '00:00:00'. Then, the result is formed from this local timestamp value adjusted to UTC by subtracting the time zone displacement defined by expression. AT time_zone_string or AT TIME ZONE time_zone_string a local timestamp value is formed from the source date_expression with the time portion set to '00:00:00'. The time zone displacement is determined based on time_zone_string and the local timestamp value. Then, the result is formed from the local timestamp value adjusted to UTC by subtracting the time zone displacement. From source type... To target type... DATEa a. ANSIDate dateform mode or IntegerDate dateform mode TIMESTAMP TIMESTAMP WITH TIME ZONE Chapter 20: Data Type Conversions DATE-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 813 Example 2 In this example, the result of the CAST is the timestamp formed from the source expression value '2008-05-14' and the default time '00:00:00' adjusted to UTC by the current session time zone displacement, INTERVAL '06:00' HOUR TO MINUTE. Thus, the value of the CAST is '2008-05-13 18:00:00' at UTC with the current session time zone displacement INTERVAL '06:00' HOUR TO MINUTE. The result value of the CAST at UTC is adjusted to its time zone displacement, INTERVAL '06:00' HOUR TO MINUTE, so the result of the SELECT statements is: TIMESTAMP '2008- 05-14 00:00:00+06:00'. SET TIME ZONE INTERVAL '06:00' HOUR TO MINUTE; SELECT CAST(DATE '2008-05-14' AS TIMESTAMP(0) WITH TIME ZONE); SELECT CAST(DATE '2008-05-14' AS TIMESTAMP(0) WITH TIME ZONE AT LOCAL); Example 3 In the following SELECT statement, the result of the CAST is the timestamp formed from the date '2008-05-14' and the default time '00:00:00' adjusted to UTC by the specified time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE. Thus, the value of the CAST is '2008- 05-14 08:00:00' at UTC. The result value of the CAST at UTC is adjusted to the current session time zone displacement, INTERVAL '05:00' HOUR TO MINUTE, so the result of the SELECT statement is: TIMESTAMP '2008-05-14 13:00:00'. SET TIME ZONE INTERVAL '05:00' HOUR TO MINUTE; SELECT CAST(DATE '2008-05-14' AS TIMESTAMP(0) AT -8); Consider the following SELECT statement: SELECT CAST(DATE '2008-05-14' AS TIMESTAMP(0) WITH TIME ZONE AT -8); In this case, the result of the CAST is the timestamp formed from the source expression value '2008-05-14' and the default time '00:00:00' adjusted to UTC by the specified time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE. Thus, the value of the CAST is '2008- 05-14 08:00:00' at UTC with the specified time zone displacement INTERVAL -'08:00' HOUR TO MINUTE. The result value of the CAST at UTC is adjusted to its time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, so the result of the SELECT statement is: TIMESTAMP '2008- 05-14 00:00:00-08:00'. The current session time zone has no effect. Example 4 In this example, the current timestamp is: Current TimeStamp(6) -------------------------------- 2010-03-09 19:23:27.620000+00:00 Chapter 20: Data Type Conversions DATE-to-TIMESTAMP Conversion 814 SQL Functions, Operators, Expressions, and Predicates The following statement converts the DATE value '2010-03-09' to a TIMESTAMP value, where the time zone displacement is based on the time zone string, 'America Pacific'. SELECT CAST(DATE '2010-03-09' AS TIMESTAMP(0) AT 'America Pacific'); The result of the query is: 2010-03-09 ------------------- 2010-03-09 08:00:00 Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions DATE-to-UDT Conversion SQL Functions, Operators, Expressions, and Predicates 815 DATE-to-UDT Conversion Purpose Converts DATE data to UDT data. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases such as FORMAT. Usage Notes Explicit DATE-to-UDT conversion using Teradata conversion syntax is not supported. Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. Implicit DATE-to-UDT Conversion Performing an implicit data type conversion requires that an appropriate cast definition (see “Usage Notes”) exists that specifies the AS ASSIGNMENT clause. Teradata Database performs implicit DATE-to-UDT conversions for the following operations: • UPDATE • INSERT • Passing arguments to stored procedures, external stored procedures, UDFs, and UDMs • Specific system operators and functions identified in other sections of this book, unless the DisableUDTImplCastForSysFuncOp field of the DBS Control Record is set to TRUE Syntax element … Specifies … date_expression a DATE expression to be cast to a UDT. UDT_data_definition the UDT type to which date_expression is to be converted. CAST AS date_expression UDT_data_definition ( ( 1101A337 Chapter 20: Data Type Conversions DATE-to-UDT Conversion 816 SQL Functions, Operators, Expressions, and Predicates If no DATE-to-UDT implicit cast definition exists, Teradata Database looks for other cast definitions that can substitute: Substitutions are valid because Teradata Database can implicitly cast a DATE type to the substitute data type, and then use the implicit cast definition to cast from the substitute data type to the UDT. Related Topics For details on data types and data attributes, see SQL Data Types and Literals. IF the following combination of implicit cast definitions exists … THEN Teradata Database … Numeric-to-UDT Charactera-to-UDT a. a non-CLOB character type X uses the Numeric-to-UDT implicit cast definition. If multiple Numeric-to-UDT implicit cast definitions exist, then Teradata Database returns an SQL error. X uses the Character-to-UDT implicit cast definition. If multiple Character-to-UDT implicit cast definitions exist, then Teradata Database returns an SQL error. X X reports an error. Chapter 20: Data Type Conversions INTERVAL-to-Character Conversion SQL Functions, Operators, Expressions, and Predicates 817 INTERVAL-to-Character Conversion Purpose Use CAST syntax or Teradata explicit conversion syntax to convert an INTERVAL type to its canonical character string representation. INTERVAL-to-Character conversion is supported for CHAR and VARCHAR types only. The target type cannot be CLOB. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of character data attribute phrases. Teradata Conversion Syntax Syntax element … Specifies … interval_expression an INTERVAL expression to be converted. character_data_type the target character type to which the interval expression is to be converted. character_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE 1101A252 CAST interval_expression AS character_data_type ) ( A CHARACTER SET server_character_set character_data_attribute A 1101B263 interval_expression ( character_data_type ) A CHARACTER SET server_character_set , data_attribute A data_attribute , Chapter 20: Data Type Conversions INTERVAL-to-Character Conversion 818 SQL Functions, Operators, Expressions, and Predicates where: ANSI Compliance This is a Teradata extension to the ANSI SQL:2008 standard. INTERVAL-to-Fixed CHARACTER Conversion When the target data type is CHAR(n), then n must be equal to or greater than the length of the canonical form of the value as represented by a character string literal. If n is greater than that length, trailing pad characters are added to pad the canonical representation. If n is too small, then a string truncation error is returned. INTERVAL-to-VARCHAR Conversion When the target data type is VARCHAR(n), then n must be equal to or greater than the length of the canonical form of the value as represented by a varying character string literal. If n is too small, then a string truncation error is returned. Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Syntax element … Specifies … interval_expression an INTERVAL expression to be converted. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE character_data_type the target character type to which the interval expression is to be converted. server_character_set which server character set to use for the conversion. If the CHARACTER SET clause is omitted, the user default character set is used to convert the INTERVAL expression. Chapter 20: Data Type Conversions INTERVAL-to-INTERVAL Conversion SQL Functions, Operators, Expressions, and Predicates 819 INTERVAL-to-INTERVAL Conversion CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases. Teradata Conversion Syntax where: Syntax element … Specifies … interval_expression an INTERVAL expression to be converted. interval_data_type the target INTERVAL type to which the interval expression is to be converted. interval_data_attribute one of the following optional data attributes: • NAMED • TITLE 1101A253 CAST interval_expression AS interval_data_type ) interval_data_attribute ( interval_data_attribute Syntax element … Specifies … interval_expression an INTERVAL expression to be converted. 1101B264 interval_expression , data_attribute , interval_data_type ( interval_data_type ) data_attribute , , data_attribute Chapter 20: Data Type Conversions INTERVAL-to-INTERVAL Conversion 820 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance This is a Teradata extension to the ANSI SQL:2008 standard. Compatible Types Both data types must be from the same INTERVAL family: either Year-Month or Day-Time. Types cannot be mixed. Conversion of INTERVAL types is performed only when the fields and precisions are different. Precision of Source and Target Types A conversion can result in an overflow error if the precision of the target data type is smaller than the corresponding precision for the source data type. If the least significant value of the source is lower than that of the target, then those source values having lower precision than the least significant field of the target are ignored. The result is truncation. Recovery from this action is installation-dependent. If the most significant field in the source value has higher significance than the most significant field in the target value, then the higher order fields of the source are converted into interval_data_type the optional target INTERVAL type to which the interval expression is to be converted. data_attribute one of the following optional data attributes: • NAMED • TITLE Syntax element … Specifies … This INTERVAL data type … Belongs to this INTERVAL family … • INTERVAL YEAR • INTERVAL YEAR TO MONTH • INTERVAL MONTH Year-Month • INTERVAL DAY • INTERVAL DAY TO HOUR • INTERVAL DAY TO MINUTE • INTERVAL DAY TO SECOND • INTERVAL HOUR • INTERVAL HOUR TO MINUTE • INTERVAL HOUR TO SECOND • INTERVAL MINUTE • INTERVAL MINUTE TO SECOND • INTERVAL SECOND Day-Time Chapter 20: Data Type Conversions INTERVAL-to-INTERVAL Conversion SQL Functions, Operators, Expressions, and Predicates 821 a scalar value of the precision of the most significant field in the target, using the factors of 12 months per year, 24 hours per day and so on. If the compared scalar value overflows the defined precision for the target field, an error is returned. Implicit INTERVAL-to-INTERVAL Conversion Teradata Database performs implicit conversion from INTERVAL to INTERVAL data types in some cases. See “Implicit Conversion of DateTime types” on page 748. Conversion of INTERVAL types is performed only when both data types are from the same INTERVAL family: either Year-Month or Day-Time. See “Compatible Types” on page 820. Example 1: Least Significant Field in Source Lower Than Target The following query converts ‘ 3-11’ to ‘ 3’. Source is INTERVAL YEAR(2). The truncation completes the conversion. SELECT CAST(INTERVAL '3-11' YEAR TO MONTH AS INTERVAL YEAR(2)); Example 2: Least Significant Field in Source Lower Than Target The following query converts ‘ 135 12:37:25.26’ to ‘3252’. Source is DAY(3) TO SECOND(2) SELECT CAST(INTERVAL '135 12:37:25.26' DAY(3) TO SECOND(2) AS INTERVAL HOUR(4)); Example 3: Least Significant Field in Source Higher Than Target The following query converts ‘3’ to ‘3-00’. Source is INTERVAL YEAR. The insertion of zeros completes the conversion. SELECT CAST(INTERVAL '3' YEAR AS INTERVAL YEAR TO MONTH); Example 4: Least Significant Field in Source Higher Than Target The following query converts ‘ 135 00:00:00.0’ to ‘ 3240:00:00.00’ after you perform the additional conversion of multiplying 135 * 24 to obtain 3240, which is the final HOUR value. The source had a data type of DAY. SELECT CAST(INTERVAL ' 135 00:00:00.0' DAY AS INTERVAL HOUR TO SECOND); Example 5: Most Significant Field in Source Higher Than Target The following query first treats the source INTERVAL value as ‘135 12’ and then computes HOURS as (135*24)+12=3252. The result of the query is INTERVAL ‘3252’ HOUR unless the precision for the target value is less than 4, in which case an error is returned. The source had a data type of DAY TO SECOND. SELECT CAST(INTERVAL '135 12:37:25.26' DAY TO SECOND AS INTERVAL HOUR); Chapter 20: Data Type Conversions INTERVAL-to-INTERVAL Conversion 822 SQL Functions, Operators, Expressions, and Predicates Example 6: Implicit Type Conversion During Assignment Consider the following table which has an INTERVAL YEAR TO MONTH column: CREATE TABLE TimeInfo (YrToMon INTERVAL YEAR TO MONTH); If you insert data into the column using the following parameterized request, and you pass an INTERVAL YEAR or INTERVAL MONTH value to the request, Teradata Database implicitly converts the value to an INTERVAL YEAR TO MONTH value before inserting the value. INSERT INTO TimeInfo VALUES (?); Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions INTERVAL-to-Numeric Conversion SQL Functions, Operators, Expressions, and Predicates 823 INTERVAL-to-Numeric Conversion Purpose Convert an INTERVAL with only one field to an exact numeric data type. This numeric value is the value of the single numeric field in the INTERVAL record. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases. Teradata Conversion Syntax where: Syntax element … Specifies … interval_expression an INTERVAL expression to be converted. numeric_data_type the target numeric type to which the interval expression is to be converted. numeric_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE 1101A254 CAST interval_expression AS numeric_data_type ) numeric_data_attribute ( Syntax element … Specifies … interval_expression an INTERVAL expression to be converted. 1101B265 interval_expression data_attribute , ( numeric_data_type ) , data_attribute Chapter 20: Data Type Conversions INTERVAL-to-Numeric Conversion 824 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance This is a Teradata extension to the ANSI SQL:2008 standard. Implicit INTERVAL-to-Numeric Conversion Teradata Database performs implicit conversion of an Interval data type to an exact numeric data type in some cases. See“Implicit Conversion of DateTime types” on page 748. Example Consider the following table definition: CREATE TABLE sales_intervals ( sdate DATE , sinterval INTERVAL MONTH , stotals DECIMAL(5,0)); The following query uses CAST to convert INTERVAL MONTH values in the sinterval column to INTEGER. SELECT stotals, (EXTRACT (MONTH FROM sdate)) + (CAST(sinterval AS INTEGER)) FROM sales_intervals; Related Topics For details on data types and data attributes, see SQL Data Types and Literals. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE numeric_data_type the target numeric type to which the interval expression is to be converted. Syntax element … Specifies … Chapter 20: Data Type Conversions INTERVAL-to-UDT Conversion SQL Functions, Operators, Expressions, and Predicates 825 INTERVAL-to-UDT Conversion Purpose Converts interval data to UDT data. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases such as FORMAT. Usage Notes Explicit INTERVAL-to-UDT conversion using Teradata conversion syntax is not supported. Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. Implicit INTERVAL-to-UDT Conversion Performing an implicit data type conversion requires a cast definition (see “Usage Notes” on page 825) that specifies the following: • the AS ASSIGNMENT clause • a source data type that is in the same INTERVAL family as the source of the implicit cast: Syntax element … Specifies … interval_expression an interval expression to be cast to a UDT. UDT_data_definition the UDT type to which interval_expression is to be converted. CAST AS interval_expression UDT_data_definition ( ( 1101A338 Chapter 20: Data Type Conversions INTERVAL-to-UDT Conversion 826 SQL Functions, Operators, Expressions, and Predicates The source data type of the cast definition does not have to be an exact match to the source of the implicit type conversion. Teradata Database performs implicit INTERVAL-to-UDT conversions for the following operations: • UPDATE • INSERT • Passing arguments to stored procedures, external stored procedures, UDFs, and UDMs • Specific system operators and functions identified in other sections of this book, unless the DisableUDTImplCastForSysFuncOp field of the DBS Control Record is set to TRUE Related Topics For details on data types and data attributes, see SQL Data Types and Literals. This INTERVAL data type … Belongs to this INTERVAL family … • INTERVAL YEAR • INTERVAL YEAR TO MONTH • INTERVAL MONTH Year-Month • INTERVAL DAY • INTERVAL DAY TO HOUR • INTERVAL DAY TO MINUTE • INTERVAL DAY TO SECOND • INTERVAL HOUR • INTERVAL HOUR TO MINUTE • INTERVAL HOUR TO SECOND • INTERVAL MINUTE • INTERVAL MINUTE TO SECOND • INTERVAL SECOND Day-Time Chapter 20: Data Type Conversions Numeric-to-Character Conversion SQL Functions, Operators, Expressions, and Predicates 827 Numeric-to-Character Conversion Purpose Converts a numeric data type to a character data type. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases such as FORMAT. Teradata Conversion Syntax where: Syntax element … Specifies … numeric_expression the numeric data expression to be cast to a character type. character_data_type the character type to which the numeric data expression is to be converted. data_attribute one of the following optional data attributes: • CHARACTER SET • FORMAT • NAMED • TITLE If no CHARACTER SET clause is specified to indicate which server character set to use, the user default server character set is used. data_attribute 1101A630 CAST (numeric_expression AS character_data_type ) 1101A631 numeric_expression ( character_data_type ) A CHARACTER SET server_character_set , data_attribute A data_attribute , Chapter 20: Data Type Conversions Numeric-to-Character Conversion 828 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Implicit Numeric-to-Character Conversion How CAST Differs from Teradata Conversion Syntax The process for the CAST function is as follows: 1 Convert the numeric value to a character string using the default or specified format for the numeric value. 2 Trim leading and trailing pad characters. 3 Extend to the right as required by the target string length. Syntax element … Specifies … numeric_expression the numeric data expression to be cast to a character type. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE character_data_type the character type to which the numeric data expression is to be converted. If character_data_definition does not specify a CHARACTER SET clause to indicate which server character set to use, the user default server character set is used. server_character_set which server character set to use. If the CHARACTER SET clause is not specified, the user default server character set is used. If a numeric argument in an SQL string function is implicitly converted to a CHAR or VARCHAR character type, and the format of the numeric argument includes any of the following formatting characters, the server character set of the character type is UNICODE: • G • F • O • A • D • L • U • I • C • N For all other formats, the server character set is LATIN. Numeric items cannot be converted to CLOB types or GRAPHIC characters. For information on data type formats, formatting characters, and the FORMAT phrase, see “Data Type Formats and Format Phrases” in SQL Data Types and Literals. Chapter 20: Data Type Conversions Numeric-to-Character Conversion SQL Functions, Operators, Expressions, and Predicates 829 4 If truncation of non-pad characters is required to conform to the target string length, report string truncation error. The CAST operation differs from the Teradata SQL conversion as follows: • Results are left justified. Column displays are not aligned. • Truncation of significant data generates a string truncation error. Using Teradata conversion syntax (that is, not using CAST) for explicit conversion of numericto- character data requires caution. The process is as follows: 1 Convert the numeric value to a character string using the default or specified FORMAT for the numeric value. Leading and trailing pad characters are not trimmed. 2 Extend to the right with pad characters if required, or truncate from the right if required, to conform to the target length specification. If non-pad characters are truncated, no string truncation error is reported. For an example of numeric to character conversion that results in truncation of significant data, see “Example 1” on page 830. Supported Character Types Numeric to character conversion is supported for CHAR and VARCHAR types only. Numeric types cannot be converted to CLOB types. Usage Notes To convert a numeric type value to a character string, the character description must contain a data type declaration. A FORMAT phrase, by itself, cannot be used to convert a numeric type value to a character type value. The phrase only controls how to display the resultant value. If the character description does not include a FORMAT phrase, then the format of the original numeric value determines how to display the data. The Teradata conversion syntax form of numeric-to-character conversion uses explicit or default FORMATs to convert to a character representation. It then truncates or extends with pad characters, depending what length the character string dictates. This can lead to a loss of significance. Attempting to convert from a numeric type to a character type that uses a GRAPHIC server character set generates an error. As a general rule, you should store numbers as numeric data, not as character data. For example, a table is created with the following code: CREATE TABLE job AS (job_code CHAR(6) PRIMARY KEY ,description CHAR(70) ); Subsequently, the following query is made: Chapter 20: Data Type Conversions Numeric-to-Character Conversion 830 SQL Functions, Operators, Expressions, and Predicates SELECT job_code, description FROM job WHERE job_code = 1234; The problem here is that ‘1234’, ‘ 1234’, ‘01234’, ‘001234’, ‘+1234’, and so on, are all valid character representations of the numeric literal value, and the system cannot tell which value to use for hashing. Therefore, the system must do a full table scan to convert all job_code values to their numeric equivalents so that it can do the comparisons. Example 1 T1.Field1 has a numeric INTEGER data type with the default format ‘-(10)9’. The user has values such as 123456, with no values of over 999999. The values, defined as being in INTEGER format, are to be converted to CHAR(8). The following example illustrates the Teradata syntax for performing this numeric-tocharacter conversion. SELECT Field1(CHAR(8)) FROM T1; returns ‘ 123’ for the value 123456, where the result includes 5 leading pad characters and truncates significant digits. Example 2 Based on the following description of Salary, data is converted as illustrated in the following table (? = pad character): Salary (DECIMAL(8,2), FORMAT '$$$,$$9.99') The resultant character string is either extended with pad characters or truncated to conform to the given character description. Example 3 Suppose EmpNo was defined as SMALLINT with the default format of ‘9(6)’. Suppose a value in EmpNo is 12501. The statement: SELECT EmpNo(CHAR(5)) FROM Employee; returns the ‘1250’, with a leading pad character and the low order digit missing. The CAST function used for the same conversion, converts to the character representation of the numeric value, trims leading pad characters, and finally truncates or pads on the right. For example, the following SELECT statement returns ‘12501’. Data Conversion Result 20000.00 Salary (CHAR(10)) '$20,000.00' 9000.00 Salary (CHAR(10)) '?$9,000.00' 20000.00 Salary (FORMAT'9(5)') (CHAR (5)) '20000' 9000.00 CAST (Salary AS CHAR(10)) '$9,000.00?' Chapter 20: Data Type Conversions Numeric-to-Character Conversion SQL Functions, Operators, Expressions, and Predicates 831 SELECT CAST (EmpNo AS CHAR(5)) FROM Employee; Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions Numeric-to-DATE Conversion 832 SQL Functions, Operators, Expressions, and Predicates Numeric-to-DATE Conversion Purpose Converts a numeric expression to a DATE data type. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant; however, converting a numeric type to a date type is a Teradata extension to the ANSI SQL:2008 standard. Teradata Conversion Syntax where: Syntax element … Specifies … numeric_expression an expression or existing field having a numeric data type. data_attribute any of the following optional data attributes: • FORMAT • NAMED • TITLE A date_data_definition that specifies a FORMAT clause enables an alternative format. Specifying data attributes in CAST is a non-ANSI Teradata extension. 1101B077 CAST ( numeric_expression AS DATE ) data_attribute Syntax element … Specifies … numeric_expression an expression or existing field having a numeric data type. 1101B385 numeric_expression data_attribute , ( DATE ) , data_attribute Chapter 20: Data Type Conversions Numeric-to-DATE Conversion SQL Functions, Operators, Expressions, and Predicates 833 ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Translation of Numbers to Dates Although not recommended, you can explicitly convert numbers to dates. Teradata Database stores each DATE value as a four-byte integer using the following formula: (year - 1900) * 10000 + (month * 100) + day For example, December 31, 1985 would be stored as the integer 851231; July 4, 1776 stored as -1239296; and March 30, 2041 stored as 1410330. The following table demonstrates how numeric dates are interpreted when inserted into a column. Note the translation of the third date, which was probably intended to be 1990-12-01. Notice that this formula best fits two-digit dates in the 1900s. Because of the difficulty of using this format outside of the 1900s, dates are best specified as ANSI date literals instead. Range of Allowable Values Allowable date values range from AD January 1, 0001 (-18989899) to AD December 31, 9999 (80991231). If the numeric value does not represent a valid date, an error is reported. Numeric-to-DATE Implicit Type Conversion Although not recommended, you can specify a numeric type in the assignment of a DATE type. Teradata Database performs implicit numeric-to-DATE type conversion prior to the assignment. The value of the numeric type must represent a valid date. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE Specifying a FORMAT clause enables an alternative format. Syntax element … Specifies … This numeric value … Translates to this date value … 901201 1990-12-01 1001201 2000-12-01 19901201 3890-12-01 Chapter 20: Data Type Conversions Numeric-to-DATE Conversion 834 SQL Functions, Operators, Expressions, and Predicates However, for comparison operations involving a numeric type operand and a DATE type operand, Teradata Database converts the DATE type to a numeric type. If you compare a numeric type and a DATE type and expect the comparison to be between two DATE types, you must explicitly convert the numeric type to a DATE type. Example This example casts the numeric integer expression to a date format. SELECT CAST (1071201 AS DATE); The result looks like this when the DateForm mode of the session is set to ANSIDate: 1071201 ---------- 2007-12-01 Related Topics FOR information on … SEE … implicit type conversion of operands for comparison operations “Implicit Type Conversion of Comparison Operands” on page 168. data type compatibility rules for assignments involving DateTime types “ANSI DateTime and Interval Data Type Assignment Rules” on page 210. data type compatibility rules for arithmetic operations involving DateTime types “Arithmetic Operators” on page 229. data types and data attributes SQL Data Types and Literals. Chapter 20: Data Type Conversions Numeric-to-INTERVAL Conversion SQL Functions, Operators, Expressions, and Predicates 835 Numeric-to-INTERVAL Conversion Purpose Convert numeric data to an INTERVAL value with a single DateTime field. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of interval data attribute phrases. Teradata Conversion Syntax where: Syntax element … Specifies … numeric_expression an expression or existing field having a numeric data type. interval_data_type the target INTERVAL data type to which the numeric expression is being converted. interval_data_attribute one of the following optional data attributes: • NAMED • TITLE 1101A281 CAST numeric_expression AS interval_data_type ) interval_data_attribute ( Syntax element … Specifies … numeric_expression an expression or existing field having a numeric data type. 1101B273 numeric_expression data_attribute , ( interval_data_type ) , data_attribute Chapter 20: Data Type Conversions Numeric-to-INTERVAL Conversion 836 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Usage Notes Numeric data is converted to an INTERVAL value with a single DateTime field. If the numeric value is in the value range allowed for the INTERVAL, the value is used as the single field of the INTERVAL. Otherwise, an overflow error is returned. Implicit Numeric-to-INTERVAL Conversion Teradata Database performs implicit conversion of an exact numeric data type to an Interval data type in some cases. See “Implicit Conversion of DateTime types” on page 748. Example The following query returns ' -5' (with three leading pad characters). SELECT CAST(-5 AS INTERVAL YEAR(4)); Related Topics For details on data types and data attributes, see SQL Data Types and Literals. data_attribute one of the following optional data attributes: • NAMED • TITLE interval_data_type the target INTERVAL data type to which the numeric expression is being converted. Syntax element … Specifies … Chapter 20: Data Type Conversions Numeric-to-Numeric Conversion SQL Functions, Operators, Expressions, and Predicates 837 Numeric-to-Numeric Conversion Purpose Converts a numeric expression defined with one data type to a different numeric data type. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI SQL, CAST permits data attributes such as the FORMAT phrase that enables an alternative format for numeric_expression. Teradata Conversion Syntax where: Syntax element … Specifies … numeric_expression an expression or existing field having a numeric data type. numeric_data_type the optional numeric data type to which numeric_expression is to be converted. numeric_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE 1101A632 ( numeric_expression numeric_data_type numeric_data_attribute CAST AS ) numeric_data_attribute 1101A633 numeric_expression , data_attribute , numeric_data_type ( numeric_data_type ) data_attribute , , data_attribute Chapter 20: Data Type Conversions Numeric-to-Numeric Conversion 838 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Implicit Numeric-to-Numeric Conversion Numeric items are converted to the same numeric type before any arithmetic or comparison operation is performed. The result returned is of this same underlying type. For example, before an INTEGER value is added to a FLOAT value, the INTEGER value is converted to FLOAT, the data type of the result. For details on implicit type conversions for binary arithmetic expressions, see “Binary Arithmetic Result Data Types” on page 49. For details on implicit type conversions for comparison operations, see “Implicit Type Conversion of Comparison Operands” on page 168. Conversion to FLOAT/REAL/DOUBLE PRECISION Because floating point numbers are not exact values, conversion of DECIMAL and integer values to FLOAT values might result in a loss of precision or produce a number that cannot be represented exactly. For example, a value like 0.1, when cast to FLOAT, no longer exactly equals to 0.1. Truncation and Rounding During Conversion Conversion of DECIMAL/NUMERIC to BIGINT, INTEGER, BYTEINT, or SMALLINT truncates any decimal portion. Conversion to DECIMAL produces a rounded result. If a range violation occurs, the operation may fail. Conversion to FLOAT/REAL/DOUBLE PRECISION rounds to the nearest value available. Neither decimal fractions nor numbers greater than 9,007,199,254,740,992 can be guaranteed to be represented exactly, so the nearest representable value is chosen. If there are two representable values that qualify as the nearest value, then the representation with a '0' in the least significant bit is chosen. For example, 0.1, when stored in a FLOAT column, is rounded to a value slightly higher: 0.1000000000000000055511151231257827021181583404541015625. For details on rounding, see “Decimal/Numeric Data Types" in SQL Data Types and Literals. Syntax element … Specifies … numeric_expression an expression or existing field having a numeric data type. numeric_data_type the optional numeric data type to which numeric_expression is to be converted. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE Chapter 20: Data Type Conversions Numeric-to-Numeric Conversion SQL Functions, Operators, Expressions, and Predicates 839 Some examples of numeric conversions are: Using CAST in Applications With DECIMAL Type Size Restrictions Some applications require DECIMAL types to have 15 digits or less. Applications with this requirement may need to access DECIMAL columns that have more than 15 digits or use expressions that may produce DECIMAL results with more than 15 digits. To help with DECIMAL type size requirements, you can use CAST to convert DECIMAL types to a size of 15 or fewer digits. For example, consider the following expression where A, B, and C are columns defined as DECIMAL(8,2): SELECT (A*B)/C FROM table1; The resulting value may be less than 15 digits, but A*B could be up to 18. To ensure a result of less than 16 digits, use CAST: SELECT CAST ((A*B)/C AS DECIMAL(15,2)) FROM table1; Using CAST To Avoid Numeric Overflow Because of the way the Teradata SQL compiler works, it is essential that you CAST the arguments of your expressions whenever large values are expected. For example, suppose f1 is defined as DECIMAL(14,2) and you are going to multiply by an integer or get SUM(f1). In this case, the following operations: CAST(f1 AS DECIMAL(18,2))*100 or SUM(CAST(f1 AS DECIMAL(18,2))) are proper techniques for ensuring correct answer sets. On the other hand, if you were to cast the results of the expressions, such as the following: CAST(f1*100 AS DECIMAL(18,2)) or CAST(SUM(f1) AS DECIMAL(18,2) Value Converted To Result 20000.99 INTEGER 20000 20000.99 DECIMAL(6,1) 20001.0 20000.99 DECIMAL(4, 1) error 200000 SMALLINT error Chapter 20: Data Type Conversions Numeric-to-Numeric Conversion 840 SQL Functions, Operators, Expressions, and Predicates then you will likely experience overflow during the computations (and before the CAST is made)—not the desired result. Example 1 This example casts the numeric integer expression named IntegerField to DECIMAL(7,2). CAST (IntegerField AS DECIMAL (7,2)) Example 2 Although the FORMAT phrase cannot be used to change the underlying data type defined for a column, the phrase may be used to change the display for a numeric value. For example, if the field values for columns Wholesale and Retail, both defined as DECIMAL(7,2), are 12467.75 and 21500.50, respectively, the result of the expression: CAST (Wholesale - Retail AS FORMAT '-99999') is: -09033 A FORMAT phrase does not affect data that is returned to the client system in Record Mode (client system internal format). In the previous example, the value returned to the client system is still in packed decimal format (for example, -9032.75). The use of FORMAT in CAST is a Teradata extension to the ANSI standard. Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions Numeric-to-UDT Conversion SQL Functions, Operators, Expressions, and Predicates 841 Numeric-to-UDT Conversion Purpose Converts numeric data to UDT data. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases such as FORMAT. Usage Notes Explicit numeric-to-UDT conversion using Teradata conversion syntax is not supported. Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. Implicit Numeric-to-UDT Conversion Teradata Database performs implicit Numeric-to-UDT conversions for the following operations: • UPDATE • INSERT • Passing arguments to stored procedures, external stored procedures, UDFs, and UDMs • Specific system operators and functions identified in other sections of this book, unless the DisableUDTImplCastForSysFuncOp field of the DBS Control Record is set to TRUE Syntax element … Specifies … numeric_expression a numeric expression to be cast to a UDT. UDT_data_definition the UDT type, followed by any optional FORMAT, NAMED or TITLE data attribute phrases, to which numeric_expression is to be converted. CAST AS numeric_expression UDT_data_definition ( ( 1101A334 Chapter 20: Data Type Conversions Numeric-to-UDT Conversion 842 SQL Functions, Operators, Expressions, and Predicates Performing an implicit data type conversion requires that an appropriate cast definition (see “Usage Notes”) exists that specifies the AS ASSIGNMENT clause. The source numeric type of the cast definition does not have to be an exact match to the source numeric type of the implicit conversion. Teradata Database can use an implicit cast definition that specifies a BYTEINT, SMALLINT, INTEGER, BIGINT, DECIMAL/ NUMERIC, or REAL/FLOAT/DOUBLE target type. If multiple implicit cast definitions exist for converting different numeric types to the UDT, Teradata Database uses the implicit cast definition for the numeric type with the highest precedence. The following list shows the precedence of numeric types in order from lowest to highest precedence: • BYTEINT • SMALLINT • INTEGER • BIGINT • DECIMAL/NUMERIC • REAL/FLOAT/DOUBLE If no numeric-to-UDT implicit cast definitions exist, Teradata Database looks for other cast definitions that can substitute: Substitutions are valid because Teradata Database can implicitly cast a numeric type to the substitute data type, and then use the implicit cast definition to cast from the substitute data type to the UDT. Related Topics For details on data types and data attributes, see SQL Data Types and Literals. IF the following combination of implicit cast definitions exists … THEN Teradata Database … DATE-to- UDT Charactera-to- UDT a. a non-CLOB character type X uses the DATE-to-UDT implicit cast definition. X uses the character-to-UDT implicit cast definition. If multiple character-to-UDT implicit cast definitions exist, then Teradata Database returns an SQL error. X X reports an error. Chapter 20: Data Type Conversions Period-to-Character Conversion SQL Functions, Operators, Expressions, and Predicates 843 Period-to-Character Conversion Purpose Converts a Period data type to its canonical character string representation. Period-to-Character conversion is supported for CHAR and VARCHAR types only. The target type cannot be CLOB. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of character data attribute phrases. Syntax element … Specifies … period_expression the Period data expression to be cast to a character type. character_data_type the character type to which the Period data expression is to be converted. server_character_set the server character set to use for the conversion. If no CHARACTER SET clause is specified to indicate which server character set to use, the user default server character set is used. character_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE CAST ( period_expression AS character_data_type 1101A598 CHARACTER SET server_character_set ) A A character_data_attribute Chapter 20: Data Type Conversions Period-to-Character Conversion 844 SQL Functions, Operators, Expressions, and Predicates Teradata Conversion Syntax where: ANSI Compliance This is a Teradata extension to the ANSI SQL:2008 standard. Usage Notes A period value expression can be cast as a character string representation using the CAST function or the Teradata cast syntax, or when forming the output for field mode. Assume L is the maximum length of the formatted character string for the format associated with the period value expression being cast. The resulting character string contains two strings representing the beginning and ending bounds of the period value expression, each up to length L, and each enclosed in apostrophes (' '), separated by comma and a space ( , ), and then enclosed within a left parenthesis and a right parenthesis [( )]. Thus, the maximum length of the resulting character string is 2*L+8. Assume the actual length is K (which may be less than 2*L+8, for example, if the format includes the full names of months and the specific month for a bound is July) and the target type is CHARACTER(n) or VARCHAR(n): • If n is equal to K, the period is cast into the resulting character string of length K. • If n is greater than K and the target is VARCHAR(n), the period is cast into the resulting character string with length K. Syntax element … Specifies … period_expression the Period data expression to be cast to a character type. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE character_data_type the character type to which the Period data expression is to be converted. server_character_set the server character set to use for the conversion. If no CHARACTER SET clause is specified to indicate which server character set to use, the user default server character set is used. 1101A599 period_expression ( character_data_type ) A CHARACTER SET server_character_set , data_attribute A data_attribute , Chapter 20: Data Type Conversions Period-to-Character Conversion SQL Functions, Operators, Expressions, and Predicates 845 • If n is greater than K and the target is CHARACTER(n), the period is cast into the resulting character string and trailing pad characters are added to extend to length n. • If n less than K and the session is in ANSI mode, a truncation error is reported. • If n less than K and the session is in Teradata mode, a truncated string of length n is returned. For data of Period data types with TIME and TIMESTAMP element types, the UTC value of the Period value expression is adjusted to the time zone of the value or the current session time zone if the value does not have a time zone. The exception to conversion from UTC is for an ending bound of a PERIOD(TIMESTAMP(n)) value equal to the maximum value that is used to represent UNTIL_CHANGED; in this case, the value is not changed. Due to such adjustments, the ending bound may appear less than the beginning bound in the result, although in UTC the ending bound is greater than the beginning bound. This happens since the hour value for the TIME data type wraps over every 24 hours (that is, the hour value is obtained using 'module 24'). Example Assume pts is a PERIOD(TIMESTAMP(2)) column in table t with a value of PERIOD '(2005-02-02 12:12:12.34, 2006-02-03 12:12:12.34)'. In the following example, a PERIOD(TIMESTAMP(2)) column is cast as CHARACTER(52) using the CAST function. SELECT CAST(pts AS CHARACTER(52)) FROM t; The following is returned: ('2005-02-02 12:12:12.34', '2006-02-03 12:12:12.34') Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions Period-to-DATE Conversion 846 SQL Functions, Operators, Expressions, and Predicates Period-to-DATE Conversion Purpose Converts Period data to a DATE value. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of DATE data attribute phrases. Usage Notes A PERIOD(DATE) or PERIOD(TIMESTAMP(n) [WITH TIME ZONE]) value can be cast as DATE using the CAST function. The source last value must be equal to the source beginning bound; otherwise, an error is reported. If the source type is PERIOD(DATE), the result is the source beginning bound. If the source type is PERIOD(TIMESTAMP(n) [WITH TIME ZONE]), the result is the date portion of the source beginning bound after adjusting to the current session time zone. If the source type is PERIOD(TIME(n) [WITH TIME ZONE]), an error is reported. Example Assume pd is a PERIOD(DATE) column in table t with a value of PERIOD '(2005-02-02, 2005-02-03)'. Syntax element … Specifies … period_expression the Period data expression to be cast to a DATE type. date_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE CAST ( period_expression AS DATE ) 1101A600 date_data_attribute Chapter 20: Data Type Conversions Period-to-DATE Conversion SQL Functions, Operators, Expressions, and Predicates 847 In the following example, a PERIOD(DATE) column is cast as DATE. The result is the beginning bound of the column. SELECT CAST(pd AS DATE) FROM t; The following is returned: 2005-02-02 Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions Period-to-Period Conversion 848 SQL Functions, Operators, Expressions, and Predicates Period-to-Period Conversion CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases. Compatible Types The following table describes the allowed combinations of source and target types when both the source and the target types are Period data types. CAST period_expression period_data_type period_data_attribute ( AS ) 1101A568 period_data_attribute Syntax element … Specifies … period_expression the Period data expression to be converted. period_data_type the optional Period type to which period_expression is to be converted. period_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE Source Type Target Type PERIOD(DATE) PERIOD(DATE) PERIOD(TIMESTAMP[(m)] [WITH TIME ZONE]) Chapter 20: Data Type Conversions Period-to-Period Conversion SQL Functions, Operators, Expressions, and Predicates 849 PERIOD(DATE) to PERIOD(TIMESTAMP) A PERIOD(DATE) value can be cast as PERIOD(TIMESTAMP[(n)] [WITH TIME ZONE]) using the CAST function. The UTC value of the result elements are obtained after adjustment with respect to the current session time zone from the timestamps created by setting the date portion to the corresponding source elements and the time portions to 0. If the target type is PERIOD(TIMESTAMP[(n)] WITH TIME ZONE), both result time zone fields are set to the current session time zone displacement. An exception to this is if the source ending bound is the maximum DATE value; in that case, the result ending bound is set to the maximum TIMESTAMP value. PERIOD(TIME) to PERIOD(TIME) A PERIOD(TIME(n) [WITH TIME ZONE]) value can be cast as PERIOD(TIME[(n)] [WITH TIME ZONE]) using the CAST function. The UTC value of the source is copied to the UTC value in the result. If the target type specifies WITH TIME ZONE and the source contains time zones, the time zone displacements from the source are copied to the corresponding result elements. If the source does not contain time zones, the current session time zone displacement is copied to both result elements. For example, assume the current session time zone displacement is INTERVAL - "08:00" HOUR TO MINUTE and the source PERIOD(TIME(0) WITH TIME ZONE) has the value PERIOD '(12:12:12+08:00, 12:12:13+08:00)'. The UTC value of this source is ('04:12:12', '04:12:13'). The UTC value of the result is set to this value. On output of this result, the UTC value is adjusted to the current session time zone and the result is ('20:12:12', '20:12:13'). PERIOD(TIME[(n)] [WITH TIME ZONE]) PERIOD(TIME[(m)] [WITH TIME ZONE]) where m is the target precision, m must be greater than or equal to the source precision n. The default for m is 6. PERIOD(TIMESTAMP[(m)] [WITH TIME ZONE]) where m is the target precision, m must be greater than or equal to the source precision n. The default for m is 6. PERIOD(TIMESTAMP[(n)] WITH TIME ZONE) PERIOD(DATE) PERIOD(TIME[(m)] [WITH TIME ZONE]) where m is the target precision, m must be greater than or equal to the source precision n. The default for m is 6. PERIOD(TIMESTAMP[(m)] [WITH TIME ZONE]) where m is the target precision, m must be greater than or equal to the source precision n. The default for m is 6. Source Type Target Type Chapter 20: Data Type Conversions Period-to-Period Conversion 850 SQL Functions, Operators, Expressions, and Predicates Note: This value is actually for a previous day and, assuming that the CURRENT_DATE at UTC is DATE '2006-07-28', the output beginning bound would be '2006-07-27 20:12:12' if it was a timestamp element. If the target precision is higher than the source precision, trailing zeros are appended to the fractional seconds. If the target precision is lower than the source precision, an error is reported. PERIOD(TIME) to PERIOD(TIMESTAMP) A PERIOD(TIME(n) [WITH TIME ZONE]) value can be cast as PERIOD(TIMESTAMP[(n)] [WITH TIME ZONE]) using the CAST function. The source time values get adjusted with respect to the session time zone displacement from the corresponding UTC value. The date portion of each result element is set to CURRENT_DATE. The hour, minute, and, second are copied from the source after the above adjustment and the timestamp value is converted to corresponding UTC value. If the target type specifies WITH TIME ZONE and the source contains time zones, the time zone displacements from the source are copied to the corresponding result elements. If the source does not contain time zones, the current session time zone displacement is copied to both result elements. If the target precision is higher than the source precision, trailing zeros are appended to the fractional seconds. If the target precision is lower than the source precision, an error is reported. PERIOD(TIMESTAMP) to PERIOD(DATE) A PERIOD(TIMESTAMP(n) [WITH TIME ZONE]) value can be cast as PERIOD(DATE) using the CAST function. The result elements are each set to the date portion of the corresponding source bound after the source bound is adjusted according to the current session time zone (the adjustment is not done for the source ending bound if it is the maximum value). If the adjustment for time zone changes the date, the changed value is used. If the result date portions are the same, an error is reported. PERIOD(TIMESTAMP) to PERIOD(TIME) A PERIOD(TIMESTAMP(n) [WITH TIME ZONE]) value can be cast as PERIOD(TIME[(n)] [WITH TIME ZONE]) using the CAST function. The date portion in the beginning and ending UTC values of the source must have the same DATE value. Otherwise, an error is reported. The time portions of the result elements are copied from the corresponding source time portions. If the target type specifies WITH TIME ZONE and the source also contains time zones, the source time zone displacements are copied to the corresponding result elements. If the source does not contain time zones, the current session time zone displacement is copied to both result elements. Chapter 20: Data Type Conversions Period-to-Period Conversion SQL Functions, Operators, Expressions, and Predicates 851 If the target precision is higher than the source precision, trailing zeros are added to the fractional seconds. If the target precision is lower than the source precision, an error is reported. PERIOD(TIMESTAMP) to PERIOD(TIMESTAMP) A PERIOD(TIMESTAMP(n) [WITH TIME ZONE]) value can be cast as PERIOD(TIMESTAMP[(n)] [WITH TIME ZONE]) using the CAST function. The result date and time portions are set to the corresponding source date and time portions. If the target type specifies WITH TIME ZONE and the source also contains time zones, the time zone displacements in the source are copied to the corresponding result elements. If the source does not contain time zones, the current session time zone displacement is copied to both result elements except if the source ending bound is the maximum value, the time zone for the result ending bound is +00:00. If the target precision is higher than the source precision, trailing zeros are added in the fractional seconds. If the target precision is lower than the source precision, an error is reported. Example 1: PERIOD(DATE) to PERIOD(TIMESTAMP) Assume p is a PERIOD(DATE) column in table t1 with a value of PERIOD '(2005-02-02, 2006-02-03)' and the current session time zone displacement is INTERVAL -'08:00' HOUR TO MINUTE. In the following example, a PERIOD(DATE) column is cast as PERIOD(TIMESTAMP(6)). The date portion is obtained from the source for the corresponding result element and the time portions are set to zero. SELECT CAST(p AS PERIOD(TIMESTAMP(6))) FROM t1; The following is returned: ('2005-02-02 00:00:00.000000', '2006-02-03 00:00:00.000000') Example 2: Least Significant Field in Source Lower Than Target Assume p is a PERIOD(TIME(2)) column in table t with a value of PERIOD '(12:12:12.45, 13:12:12.67)' and the current session time zone displacement is INTERVAL -'08:00' HOUR TO MINUTE. In the following example, a PERIOD(TIME(2)) column is cast as PERIOD(TIME(6) WITH TIME ZONE). The time portion is obtained from the source with trailing zeros added to the fractional seconds to make the precision 6 for the corresponding result element and both result time zone fields are set to the current session time zone displacement. SELECT CAST(p AS PERIOD(TIME(6)WITH TIME ZONE)) FROM t; The following is returned: ('12:12:12.450000-08:00', '13:12:12.670000-08:00') Chapter 20: Data Type Conversions Period-to-Period Conversion 852 SQL Functions, Operators, Expressions, and Predicates Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions Period-to-TIME Conversion SQL Functions, Operators, Expressions, and Predicates 853 Period-to-TIME Conversion Purpose Converts Period data to a TIME value. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of TIME data attribute phrases. Usage Notes A PERIOD(TIME(n) [WITH TIME ZONE]) or PERIOD(TIMESTAMP(n) [WITH TIME ZONE]) value can be cast as TIME[(n)] [WITH TIME ZONE] using the CAST function. The source last value must be equal to the source beginning bound; otherwise, an error is reported. CAST period_expression AS (fractional_seconds_precision) WITH TIME ZONE time_data_attribute ( ) 1101A604 TIME A A Syntax element … Specifies … period_expression the Period data expression to be converted. fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. time_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE Chapter 20: Data Type Conversions Period-to-TIME Conversion 854 SQL Functions, Operators, Expressions, and Predicates If the target precision is higher than the source precision, trailing zeros are added in the result to adjust the precision. If the target precision is lower than the source precision, an error is reported. If the source type is PERIOD(TIME(n) [WITH TIME ZONE]) or PERIOD(TIMESTAMP(n) [WITH TIME ZONE]), the result time portion is obtained from time portion of the source beginning bound. If both the source and target type are WITH TIME ZONE, the result time zone field is set to the time zone displacement of the source beginning bound. If only the target type is WITH TIME ZONE, the result time zone field is set to the current session time zone displacement. If the source type is PERIOD(DATE), an error is reported. Example Assume pt is a PERIOD(TIME(2)) column in table t with a value of PERIOD '(12:12:12.34, 12:12:12.35)'. In the following example, a PERIOD(TIME(2)) column is cast as TIME(6). The TIME(6) result is obtained from the source beginning element with trailing zeros added to the fractional seconds to make the precision 6. SELECT CAST(pt AS TIME(6)) FROM t; The following is returned: 12:12:12.340000 Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions Period-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 855 Period-to-TIMESTAMP Conversion Purpose Converts Period data to a TIMESTAMP value. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of the FORMAT phrase to enable alternative output formatting of DateTime data. Usage Notes A PERIOD(DATE), PERIOD(TIME(n) [WITH TIME ZONE]), or PERIOD(TIMESTAMP(n) [WITH TIME ZONE]) value can be cast as TIMESTAMP[(n)] [WITH TIME ZONE] using Syntax element … Specifies … period_expression the Period data expression to be converted. fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. timestamp_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE CAST period_expression AS TIMESTAMP (fractional_seconds_precision) WITH TIME ZONE timestamp_data_attribute ( ) 1101A605 A A Chapter 20: Data Type Conversions Period-to-TIMESTAMP Conversion 856 SQL Functions, Operators, Expressions, and Predicates the CAST function. The source last value must be equal to the source beginning bound; otherwise, an error is reported. If the source type is PERIOD(TIME(n) [WITH TIME ZONE]) or PERIOD(TIMESTAMP(n) [WITH TIME ZONE]): • If the target precision is higher than the source precision, trailing zeros are added in the result to adjust the precision. • If the target precision is lower than the source precision, an error is reported. If the source type is PERIOD(DATE), the result is formed from the source beginning bound and a time portion of 0 adjusted with respect to the current session time zone, and, if the target type is WITH TIME ZONE, the current session time zone displacement. If the source type is PERIOD(TIME(n) [WITH TIME ZONE]), the source beginning bound (in UTC) is adjusted with respect to the current session time zone displacement. The timestamp portion of the result is formed from CURRENT_DATE and the time portion of the source beginning bound obtained after the above adjustment. The resulting timestamp value is converted to UTC. If both the source and target type are WITH TIME ZONE, the result time zone field is set to the time zone displacement of the source beginning bound. If only the target type is WITH TIME ZONE, the result time zone field is set to the current session time zone displacement. If the source type is PERIOD(TIMESTAMP(n) [WITH TIME ZONE]), the result timestamp portion is the timestamp portion of the source beginning bound. If both the source and target type are WITH TIME ZONE, the result time zone field is set to the time zone displacement of the source beginning bound. If only the target type is WITH TIME ZONE, the result time zone field is set to the current session time zone displacement. Example Assume pts is a PERIOD(TIMESTAMP(2)) column in table t with a value of PERIOD '(2005-02-03 12:12:12.34, 2005-02-03 12:12:12.35)'. In the following example, column pts is cast as TIMESTAMP(6). The result is the source beginning bound with trailing zeros added to the fractional seconds to make the precision 6. SELECT CAST(pts AS TIMESTAMP(6)) FROM t; The following is returned: 2005-02-03 12:12:12.340000 Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions Signed Zone DECIMAL Conversion SQL Functions, Operators, Expressions, and Predicates 857 Signed Zone DECIMAL Conversion Introduction Teradata SQL can convert input data that is in signed zone (external) DECIMAL format to a NUMERIC data type, thus allowing numeric operations to be performed on row values. The column in which the signed zone decimal data is to be stored may be any numeric data type. A FORMAT phrase incorporating the S sign character filters the data as it passes in and out of Teradata Database. The rightmost character of the input data string is assumed to contain the zone (overpunch) bit. The following table shows the characters representing zone-numeric combinations. The sign FORMAT phrase can be included in a CREATE TABLE or ALTER TABLE statement when the column is defined, or in the INSERT statement when the data is loaded. The chosen method depends on how the stored value is to be used. When a sign FORMAT phrase is specified at column creation time, it is considered attached to the column because it translates data at the column level; that is, both when the data is loaded and when it is retrieved. Using FORMAT in CREATE TABLE When the FORMAT phrase is used in the CREATE TABLE statement, as follows: CREATE TABLE Test1 (Col1 DECIMAL(4) FORMAT '9999S'); Last Character (Input String) Numeric Conversion Last Character (Input String) Numeric Conversion Last Character (Input String) Numeric Conversion { A B C D E F G H I n … 0 n … 1 n … 2 n … 3 n … 4 n … 5 n … 6 n … 7 n … 8 n … 9 } J K L M N O P Q R -n … 0 -n … 1 -n … 2 -n … 3 -n … 4 -n … 5 -n … 6 -n … 7 -n … 8 -n … 9 0 1 2 3 4 5 6 7 8 9 n … 0 n … 1 n … 2 n … 3 n … 4 n … 5 n … 6 n … 7 n … 8 n … 9 Chapter 20: Data Type Conversions Signed Zone DECIMAL Conversion 858 SQL Functions, Operators, Expressions, and Predicates then zoned input character strings can be loaded with standard INSERT statements, whether the data is defined: INSERT INTO Test1 (Col1) VALUES ('123J'); or read from a client system data record via the USING modifier: USING Ext1 (CHAR(4)) INSERT INTO Test1 (Col1) VALUES (:Ext1); The data record contains the string ’123J’. Subsequently, a simple select, such as: SELECT Col1 FROM Test1; returns: Col1 ---- 123J Using Another FORMAT in the SELECT Statement To override an attached format, another FORMAT phrase is needed in the retrieval statement. Using the preceding table, one of the two following statements must be used to retrieve the numeric value: SELECT Col1 (FORMAT '+9999') FROM Test1; or SELECT CAST (Col1 AS INTEGER) FROM Test1; The result is as follows. Col1 ----- -1231 If FORMAT is Not Attached to the Column If the format is not attached to the column, the sign FORMAT phrase must be used each time signed zoned decimal data is loaded and each time the row value is to be retrieved in signed zoned decimal format. For example, if a table is defined using a CREATE TABLE statement like this: CREATE TABLE Test2 (Col2 DECIMAL(5)); then the sign FORMAT phrase must be included whenever signed zoned decimal strings are inserted. This is true whether the definition is explicitly defined, as it is in Examples 1 and 2, or defined implicitly by being read from a client system data record as it is in Examples 3 and 4. Chapter 20: Data Type Conversions Signed Zone DECIMAL Conversion SQL Functions, Operators, Expressions, and Predicates 859 Example 1 INSERT INTO Test2 (Col2) VALUES ('5678B' (DECIMAL(5), FORMAT '99999S')); Example 2 INSERT INTO Test2 (Col2) VALUES ('9012L' (DECIMAL(5), FORMAT '99999S')); Example 3 USING Ext2 (CHAR(5)) INSERT INTO Test2 (Col2) VALUES (:Ext2 (DECIMAL(5), FORMAT '99999S')); Example 4 USING Ext2 (CHAR(5)) INSERT INTO Test2 (Col2) VALUES (:Ext2 (DECIMAL(5), FORMAT '99999S')); where Ext2 contains the strings ’5678B’ and ’9012L’. Because Col2 does not have an attached FORMAT phrase, a simple SELECT, such as the following example, returns the results as seen immediately following. SELECT Col2 FROM Test2; Col2 ------- 56782. -90123. A sign FORMAT phrase must be included in the SELECT statement in order to retrieve the values ’5678B’ and ’9012L’. It is important to remember this rule when manipulating signed zoned decimal values, especially when using sophisticated facilities like subqueries. Example 5 This example is based on the data from Example 4. Consider a column created with a CHARACTER data type. CREATE TABLE Test3 (Col3 CHAR(5)); The column is loaded by selecting, without a sign FORMAT phrase, values from an “unattached” column, as follows. INSERT INTO Test3 (Col3) SELECT Col2 FROM Test2 ; Chapter 20: Data Type Conversions Signed Zone DECIMAL Conversion 860 SQL Functions, Operators, Expressions, and Predicates The values that are inserted are the following: Col3 ----- 5678 -9012 The sign FORMAT phrase must be included in the query specification in order to insert the values ’5678B’ and ’9012L’. Related Topics For information on data types, data type formats, formatting characters, and the FORMAT phrase, see SQL Data Types and Literals. Chapter 20: Data Type Conversions TIME-to-Character Conversion SQL Functions, Operators, Expressions, and Predicates 861 TIME-to-Character Conversion Purpose Convert TIME data to a character string. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of the FORMAT phrase to enable alternative output formatting for the character representations of DateTime data. Syntax element … Specifies … time_expression the TIME expression to be cast to a character type. character_data_type the character type to which the TIME expression is to be converted. server_character_set the server character set to use for the conversion. If no CHARACTER SET clause is specified to indicate which server character set to use, the user default server character set is used. character_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE 1101A266 CAST time_expression AS character_data_type ) ( A CHARACTER SET server_character_set character_data_attribute A Chapter 20: Data Type Conversions TIME-to-Character Conversion 862 SQL Functions, Operators, Expressions, and Predicates Teradata Conversion Syntax where: ANSI Compliance This is a Teradata extension to the ANSI SQL:2008 standard. Usage Notes When converting TIME to CHAR(n) or VARCHAR(n), then n must be equal to or greater than the length of the TIME value as represented by a character string literal. Syntax element … Specifies … time_expression the TIME expression to be cast to a character type. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE character_data_type the character type to which the TIME expression is to be converted. server_character_set the server character set to use for the conversion. If no CHARACTER SET clause is specified to indicate which server character set to use, the user default server character set is used. 1101B274 time_expression ( character_data_type ) A CHARACTER SET server_character_set , data_attribute A data_attribute , IF the target data type is … AND n is … THEN … CHAR(n) greater than the length of the TIME value as represented by a character string literal trailing pad characters are added to pad the representation too small a string truncation error is returned VARCHAR(n) greater than the length of the TIME value as represented by a character string literal no blank padding is added to the character representation too small a string truncation error is returned Chapter 20: Data Type Conversions TIME-to-Character Conversion SQL Functions, Operators, Expressions, and Predicates 863 TIME to CLOB conversion is not supported. You cannot convert a TIME value to a character string when the server character set is GRAPHIC. Forcing a FORMAT on CAST for Converting TIME to Character The default format for TIME to character conversion is the format in effect for the TIME value. You can convert a TIME value to a character string using a FORMAT phrase. The resulting format, however, is the same as the TIME value. If you want a different format for the string value, you need to also use CAST as described here. You must use nested CAST operations in order to convert values from TIME to CHAR and force an explicit FORMAT on the result regardless of the format associated with the TIME value. This is because of the rules for matching FORMAT phrases to data types. Example Field T1 in the table INTTIME is a TIME(6) value with the explicit format 'HH:MI:SSDS(6)'. Assume that you want to convert this to a value of CHAR(6), and an explicit output format of 'HHhMIm'. SELECT T1 FROM INTTIME ; The result (without a type change) is the following report: T1 --------------- 05:57:11.362271 Now use nested CAST phrases and a FORMAT to obtain the desired result: a report in character format. SELECT CAST( (CAST (T1 AS FORMAT 'HHhMim')) AS CHAR(6)) FROM INTTIME; The result after the nested CASTs is the following report. T1 ------ 05h57m The inner CAST establishes the display format for the TIME value and the outer CAST indicates the data type of the desired result. Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions TIME-to-Period Conversion 864 SQL Functions, Operators, Expressions, and Predicates TIME-to-Period Conversion Purpose Converts TIME data as PERIOD(TIME[(n)] [WITH TIME ZONE]) or PERIOD(TIMESTAMP[(n)][WITH TIME ZONE]). CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases. Usage Notes A TIME(n) [WITH TIME ZONE] value can be cast as PERIOD(TIME[(n)] [WITH TIME ZONE]) or PERIOD(TIMESTAMP[(n)] [WITH TIME ZONE]) using the CAST function. If the target precision is higher than the source precision, trailing zeros are added in the result bounds to adjust the precision. If the target precision is lower than the source precision, an error is reported. Syntax element … Specifies … time_expression the TIME data expression to be converted. period_data_type the target Period type to which time_expression is to be converted. period_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE CAST time_expression AS period_data_type period_data_attribute ( ) 1101A610 Chapter 20: Data Type Conversions TIME-to-Period Conversion SQL Functions, Operators, Expressions, and Predicates 865 If the TIME source value contains leap seconds, the seconds portion gets adjusted to 59.999999 with the precision truncated to the target precision. If the target type is PERIOD(TIME[(n)] [WITH TIME ZONE]), the result beginning element is set to the source value (in UTC). If the target type is PERIOD(TIMESTAMP[(n)] [WITH TIME ZONE]), the source time value get adjusted with respect to the current session time zone displacement from the corresponding UTC value; the date portion in the result beginning element is set to CURRENT_DATE, the time portion is set to the source value obtained after the above adjustment, and the resulting timestamp value is converted to UTC. If both the source and target are WITH TIME ZONE, the time zone field of the result beginning element is set to the source time zone field. If only the target has WITH TIME ZONE, the time zone field of the result beginning element is set to the current session time zone displacement. The result ending element is set to the result beginning bound plus one granule of the target type. If the result ending bound has a lower value than the result beginning bound for a target type of PERIOD(TIME[(n)] [WITH TIME ZONE) or the result ending element value exceeds the maximum corresponding TIMESTAMP value for a target type of PERIOD(TIMESTAMP[(n)] [WITH TIME ZONE), an error is reported. Note: If the target type is WITH TIME ZONE, the result beginning and ending bounds have the same time zones. Also, note that the result has the same value for the beginning bound and last value. Example Assume pt is a TIME(0) column in table t with a value of TIME '12:12:12' and the current session time zone displacement is INTERVAL -'08:00' HOUR TO MINUTE. In the following example, a TIME(0) column is cast as PERIOD(TIME(4) WITH TIME ZONE). The result beginning bound is formed form the source (in UTC) with trailing zeros added to make the precision 4 and the current session time zone displacement. The result ending element is set to the result beginning bound plus INTERVAL '0.0001' SECOND. Note: The time zones of the result beginning and ending elements are the same. SELECT CAST(pt AS PERIOD(TIME(4) WITH TIME ZONE)) FROM t; Returns a PERIOD(TIME(4) WITH TIME ZONE) value as follows: ('12:12:12.0000-08:00', '12:12:12.0001-08:00') Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions TIME-to-TIME Conversion 866 SQL Functions, Operators, Expressions, and Predicates TIME-to-TIME Conversion Purpose Converts TIME or TIME WITH TIME ZONE to TIME or TIME WITH TIME ZONE using optional data attributes. CAST Syntax where: Syntax element … Specifies … time_expression the TIME expression to be converted. fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. AT LOCAL that the time zone displacement based on the current session time zone is used. 1101B267 time_expression expression time_zone_string (fractional_seconds_precision) CAST ( AS TIME A WITH TIME ZONE AT LOCAL SOURCE TIME ZONE TIME ZONE A B ) time_data_attribute B Chapter 20: Data Type Conversions TIME-to-TIME Conversion SQL Functions, Operators, Expressions, and Predicates 867 ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of the FORMAT phrase to enable alternative output formatting for DateTime data. The AT clause is ANSI SQL:2008 compliant. As an extension to ANSI, the AT clause is supported when using CAST to convert from TIME (with or without time zone) to TIME WITH TIME ZONE. In addition, you can specify the time zone displacement using additional expressions besides an INTERVAL expression. Note: TIME (without time zone) is not ANSI SQL:2008 compliant. Teradata Database internally converts a TIME value to UTC based on the current session time zone or on a specified time zone. AT SOURCE [TIME ZONE] that the time zone associated with time_expression is used in the following cases: • AT SOURCE TIME ZONE is specified. • AT SOURCE is specified without TIME ZONE and there is no column named source in the scope. Otherwise, if AT SOURCE is specified without TIME ZONE and a column named source exists, then SOURCE references this column, and the value of the column is used as the time zone displacement for the CAST. If needed, the column value is implicitly converted to type INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. If there are multiple columns named source in the scope, an error is returned. AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement used for the CAST. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. time_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE Syntax element … Specifies … Chapter 20: Data Type Conversions TIME-to-TIME Conversion 868 SQL Functions, Operators, Expressions, and Predicates Teradata Conversion Syntax where: Syntax element … Specifies … time_expression the TIME expression to be converted. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. AT LOCAL that the time zone displacement based on the current session time zone is used. 1101C275 time_expression (fractional_seconds_precision) ( TIME A data_attribute , expression time_zone_string , WITH TIME ZONE AT LOCAL SOURCE TIME ZONE TIME ZONE A B ) , data_attribute B Chapter 20: Data Type Conversions TIME-to-TIME Conversion SQL Functions, Operators, Expressions, and Predicates 869 ANSI Compliance Teradata Conversion Syntax is a Teradata extension to the ANSI SQL:2008 standard. The AT clause is ANSI SQL:2008 compliant. As an extension to ANSI, the AT clause is supported when using Teradata Conversion Syntax to convert from TIME (with or without time zone) to TIME WITH TIME ZONE. In addition, you can specify the time zone displacement using additional expressions besides an INTERVAL expression. Note: TIME (without time zone) is not ANSI SQL:2008 compliant. Teradata Database internally converts a TIME value to UTC based on the current session time zone or on a specified time zone. Usage Notes If you specify an AT clause for a TIME[(n)] without time zone target data type, an error is returned. If you specify an AT clause for a TIME[(n)] WITH TIME ZONE target data type, the following table shows the result of the CAST function or Teradata conversion based on the various options specified. If the target precision is higher than the source precision, trailing zeros are added in the result to adjust the precision. If the target precision is lower than the source precision, an error is returned. AT SOURCE [TIME ZONE] that the time zone associated with time_expression is used in the following cases: • AT SOURCE TIME ZONE is specified. • AT SOURCE is specified without TIME ZONE and there is no column named source in the scope. Otherwise, if AT SOURCE is specified without TIME ZONE and a column named source exists, then SOURCE references this column, and the value of the column is used as the time zone displacement in the conversion. If needed, the column value is implicitly converted to type INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. If there are multiple columns named source in the scope, an error is returned. AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement used in the conversion. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. Syntax element … Specifies … Chapter 20: Data Type Conversions TIME-to-TIME Conversion 870 SQL Functions, Operators, Expressions, and Predicates Example 1 In this example, the current session time zone displacement, INTERVAL '01:00' HOUR TO MINUTE, is used to determine the UTC value, '07:30:00' of the TIME literal. The result of the CAST is the time formed from the time portion of the source expression value '07:30:00' at UTC and the current time zone displacement, INTERVAL '01:00' HOUR TO MINUTE. The result value of the CAST '07:30:00' at UTC is adjusted to its time zone displacement, INTERVAL '01:00' HOUR TO MINUTE, and the result of the SELECT statements is: TIME '08:30:00+01:00'. The result of the SELECT statements is equal to TIME '07:30:00+00:00' since values are compared based on their UTC values. IF you specify... AND the data type of time_expression is... THEN... AT LOCAL with or without TIME ZONE the result is formed from the source time_expression (in UTC) and the time zone displacement based on the current session time zone. If the data type of time_expression is without time zone, this is the same as not specifying the AT clause. AT SOURCE (where SOURCE is a keyword and not a column reference) WITH TIME ZONE the result is formed from the time portion of the source time_expression (in UTC) and the time zone displacement associated with time_expression. Note that this is the same as not specifying the AT clause. AT SOURCE (where SOURCE is a keyword and not a column reference) without TIME ZONE an error is returned. AT SOURCE TIME ZONE WITH TIME ZONE the result is formed from the time portion of the source time_expression (in UTC) and the time zone displacement associated with time_expression. Note that this is the same as not specifying the AT clause. AT SOURCE TIME ZONE without TIME ZONE an error is returned. AT expression or AT TIME ZONE expression with or without TIME ZONE the result is formed from the time portion of the source time_expression (in UTC) and the time zone displacement defined by expression. AT time_zone_string or AT TIME ZONE time_zone_string with or without TIME ZONE the result is formed from the time portion of the source time_expression (in UTC) and the time zone displacement based on time_zone_string. The time zone displacement is determined based on time_zone_string, CURRENT_TIMESTAMP AT '00:00', and the TIME value of time_expression at UTC. Chapter 20: Data Type Conversions TIME-to-TIME Conversion SQL Functions, Operators, Expressions, and Predicates 871 SET TIME ZONE INTERVAL '01:00' HOUR TO MINUTE; SELECT CAST(TIME '08:30:00' AS TIME(0) WITH TIME ZONE); SELECT CAST(TIME '08:30:00' AS TIME(0) WITH TIME ZONE AT LOCAL); Example 2 In this example, the time zone displacement specified in the literal, INTERVAL '04:00' HOUR TO MINUTE, is used to determine the UTC value '04:30:00' for the TIME literal. The result of the CAST is the time formed from the time portion of the source expression value '04:30:00' at UTC and the current session time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE. The result value of the CAST '04:30:00' at UTC is adjusted to its time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, and the result of the SELECT statement is: TIME '20:30:00-08:00'. The result of the SELECT statement is equal to TIME '04:30:00+00:00'. SET TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE; SELECT CAST(TIME '08:30:00+04:00' AS TIME(0) WITH TIME ZONE AT LOCAL); Example 3 The following SELECT statement returns an error because the source expression does not have a time zone displacement. SELECT CAST(TIME '08:30:00' AS TIME(0) WITH TIME ZONE AT SOURCE TIME ZONE); Example 4 In this example, the time zone displacement specified in the literal, INTERVAL '04:00' HOUR TO MINUTE, is used to determine the UTC value '04:30:00' for the TIME literal. The result of the CAST is the time formed from the time portion of the source expression value '04:30:00' at UTC, and the time zone displacement of the source expression, INTERVAL '04:00' HOUR TO MINUTE. The result value of the CAST '04:30:00' at UTC is adjusted to its time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, and the result of the SELECT statements is: TIME '08:30:00+04:00'. The result of the SELECT statements is equal to TIME '04:30:00+00:00'. The current session time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, has no effect. SET TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE; SELECT CAST(TIME '08:30:00+04:00' AS TIME(0) WITH TIME ZONE); SELECT CAST(TIME '08:30:00+04:00' AS TIME(0) WITH TIME ZONE AT SOURCE); Chapter 20: Data Type Conversions TIME-to-TIME Conversion 872 SQL Functions, Operators, Expressions, and Predicates Example 5 In this example, the current session time zone displacement, INTERVAL -'04:00' HOUR TO MINUTE, is used to determine the UTC value '12:30:00' for the TIME literal. The result of the CAST is the time formed from the time portion of the source expression value '12:30:00' at UTC, and the specified time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE. The result value of the CAST '12:30:00' at UTC is adjusted to its time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, and the result of the SELECT statement is: TIME '04:30:00-08:00'. The result of the SELECT statement is equal to TIME '12:30:00+00:00'. SET TIME ZONE INTERVAL -'04:00' HOUR TO MINUTE; SELECT CAST(TIME '08:30:00' AS TIME(0) WITH TIME ZONE AT -8); Example 6 In this example, the time zone displacement specified in the literal, INTERVAL '04:00' HOUR TO MINUTE, is used to determine the UTC value '04:30:00' for the TIME literal. The result of the CAST is the time formed from the time portion of the source expression value '04:30:00' at UTC, and the specified time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE. The result value of the CAST '04:30:00' at UTC is adjusted to its time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, and the result of the SELECT statement is: TIME '20:30:00-08:00'. This result of the SELECT statement is equal to TIME '04:30:00+00:00'. The current session time zone displacement, INTERVAL '08:00' HOUR TO MINUTE, has no effect. SET TIME ZONE INTERVAL '08:00' HOUR TO MINUTE; SELECT CAST(TIME '08:30:00+04:00' AS TIME(0) WITH TIME ZONE AT -8); Example 7 In this example, the current timestamp is: Current TimeStamp(6) -------------------------------- 2010-03-09 19:23:27.620000+00:00 The following statement converts the TIME value '08:30:00' to a TIME WITH TIME ZONE value, where the time zone displacement is based on the time zone string, 'America Pacific'. SELECT CAST(TIME '08:30:00' AS TIME(0) WITH TIME ZONE AT 'America Pacific'); The result of the query is: 08:30:00 -------------- Chapter 20: Data Type Conversions TIME-to-TIME Conversion SQL Functions, Operators, Expressions, and Predicates 873 00:30:00-08:00 Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions TIME-to-TIMESTAMP Conversion 874 SQL Functions, Operators, Expressions, and Predicates TIME-to-TIMESTAMP Conversion Purpose Converts TIME or TIME WITH TIME ZONE to TIMESTAMP or TIMESTAMP WITH TIME ZONE using optional data attributes. CAST Syntax where: Syntax element … Specifies … time_expression the TIME expression to be converted. fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. AT LOCAL that the time zone displacement based on the current session time zone is used. 1101B268 time_expression expression time_zone_string (fractional_seconds_precision) CAST ( AS TIMESTAMP A WITH TIME ZONE AT LOCAL SOURCE TIME ZONE TIME ZONE A B ) timestamp_data_attribute B Chapter 20: Data Type Conversions TIME-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 875 ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of the FORMAT phrase to enable alternative output formatting of DateTime data. The AT clause is ANSI SQL:2008 compliant. As an extension to ANSI, the AT clause is supported when using CAST to convert from TIME to TIMESTAMP. In addition, you can specify the time zone displacement using additional expressions besides an INTERVAL expression. Note: TIME (without time zone) and TIMESTAMP (without time zone) are not ANSI SQL:2008 compliant. Teradata Database internally converts a TIME or TIMESTAMP value to UTC based on the current session time zone or on a specified time zone. AT SOURCE [TIME ZONE] that the time zone associated with time_expression is used in the following cases: • AT SOURCE TIME ZONE is specified. • AT SOURCE is specified without TIME ZONE and there is no column named source in the scope. Otherwise, if AT SOURCE is specified without TIME ZONE and a column named source exists, then SOURCE references this column, and the value of the column is used as the time zone displacement for the CAST. If needed, the column value is implicitly converted to type INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. If there are multiple columns named source in the scope, an error is returned. AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement used for the CAST. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE Syntax element … Specifies … Chapter 20: Data Type Conversions TIME-to-TIMESTAMP Conversion 876 SQL Functions, Operators, Expressions, and Predicates Teradata Conversion Syntax where: Syntax element … Specifies … time_expression the TIME expression to be converted. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. AT LOCAL that the time zone displacement based on the current session time zone is used. 1101C276 time_expression (fractional_seconds_precision) ( TIMESTAMP A data_attribute , expression time_zone_string , WITH TIME ZONE AT LOCAL SOURCE TIME ZONE TIME ZONE A B ) , data_attribute B Chapter 20: Data Type Conversions TIME-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 877 ANSI Compliance Teradata Conversion Syntax is a Teradata extension to the ANSI SQL:2008 standard. The AT clause is ANSI SQL:2008 compliant. As an extension to ANSI, the AT clause is supported when using Teradata Conversion Syntax to convert from TIME to TIMESTAMP. In addition, you can specify the time zone displacement using additional expressions besides an INTERVAL expression. Note: TIME (without time zone) and TIMESTAMP (without time zone) are not ANSI SQL:2008 compliant. Teradata Database internally converts a TIME or TIMESTAMP value to UTC based on the current session time zone or on a specified time zone. Usage Notes If you specify the AT clause for a TIMESTAMP[(n)] without time zone target data type, the following table shows the result of the CAST function or Teradata conversion based on the various options specified. If the target precision is higher than the source precision, trailing zeros are added in the result to adjust the precision. If the target precision is lower than the source precision, an error is returned. AT SOURCE [TIME ZONE] that the time zone associated with time_expression is used in the following cases: • AT SOURCE TIME ZONE is specified. • AT SOURCE is specified without TIME ZONE and there is no column named source in the scope. Otherwise, if AT SOURCE is specified without TIME ZONE and a column named source exists, then SOURCE references this column, and the value of the column is used as the time zone displacement in the conversion. If needed, the column value is implicitly converted to type INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. If there are multiple columns named source in the scope, an error is returned. AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement used in the conversion. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. Syntax element … Specifies … Chapter 20: Data Type Conversions TIME-to-TIMESTAMP Conversion 878 SQL Functions, Operators, Expressions, and Predicates IF you specify... AND the data type of time_expression is... THEN... AT LOCAL with or without TIME ZONE the source time_expression (in UTC) is adjusted by adding the time zone displacement based on the current session time zone. A local timestamp value is formed from CURRENT_DATE (at the above time zone displacement) and the time portion of time_expression obtained after the previous adjustment. The result is this local timestamp value adjusted to UTC by subtracting the above time zone displacement. This is the same as not specifying the AT clause. AT SOURCE (where SOURCE is a keyword and not a column reference) WITH TIME ZONE the source time_expression (in UTC) is adjusted by adding the time zone displacement of time_expression. A local timestamp value is formed from CURRENT_DATE (based on the time zone displacement of time_expression) and the time portion of time_expression obtained after the previous adjustment. The result is this local timestamp value adjusted to UTC by subtracting the time zone displacement of time_expression. AT SOURCE (where SOURCE is a keyword and not a column reference) without TIME ZONE an error is returned. AT SOURCE TIME ZONE WITH TIME ZONE the source time_expression (in UTC) is adjusted by adding the time zone displacement of time_expression. A local timestamp value is formed from CURRENT_DATE (based on the time zone displacement of time_expression) and the time portion of time_expression obtained after the previous adjustment. The result is this local timestamp value adjusted to UTC by subtracting the time zone displacement of time_expression. AT SOURCE TIME ZONE without TIME ZONE an error is returned. AT expression or AT TIME ZONE expression with or without TIME ZONE the source time_expression (in UTC) is adjusted by adding the time zone displacement defined by expression. A local timestamp value is formed from CURRENT_DATE at the above time zone displacement and the time portion of time_expression obtained after the above adjustment. The result is this local timestamp value adjusted to UTC by subtracting the above time zone displacement. Chapter 20: Data Type Conversions TIME-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 879 If you specify the AT clause for a TIMESTAMP[(n)] WITH TIME ZONE target data type, the following table shows the result of the CAST function or Teradata conversion based on the various options specified. If the target precision is higher than the source precision, trailing zeros are added in the result to adjust the precision. If the target precision is lower than the source precision, an error is returned. AT time_zone_string or AT TIME ZONE time_zone_string with or without TIME ZONE the source time_expression (in UTC) is adjusted by adding the time zone displacement based on time_zone_string. The time zone displacement is determined based on time_zone_string, CURRENT_TIMESTAMP AT '00:00', and the TIME value of time_expression at UTC. A local timestamp value is formed from CURRENT_DATE at the above time zone displacement and the time portion of time_expression obtained after the above adjustment. The result is this local timestamp value adjusted to UTC by subtracting the above time zone displacement. IF you specify... AND the data type of time_expression is... THEN... IF you specify... AND the data type of time_expression is... THEN... AT LOCAL with or without TIME ZONE the source time_expression (in UTC) is adjusted by adding the time zone displacement based on the current session time zone. A local timestamp value is formed from CURRENT_DATE (at the above time zone displacement) and the time portion of time_expression obtained after the above adjustment. This resulting timestamp is adjusted to UTC, and the result value of the CAST at UTC is adjusted to the above time zone displacement. If the data type of time_expression is without time zone, this is the same as not specifying the AT clause. AT SOURCE (where SOURCE is a keyword and not a column reference) WITH TIME ZONE the source time_expression (in UTC) is adjusted by adding the time zone displacement of time_expression. A local timestamp value is formed from CURRENT_DATE (based on the time zone displacement of time_expression) and the time portion of time_expression obtained after the previous adjustment. This resulting timestamp is adjusted to UTC, and the result value of the CAST at UTC is adjusted to the time zone displacement of time_expression. AT SOURCE (where SOURCE is a keyword and not a column reference) without TIME ZONE an error is returned. Chapter 20: Data Type Conversions TIME-to-TIMESTAMP Conversion 880 SQL Functions, Operators, Expressions, and Predicates Implicit TIME-to-TIMESTAMP Conversion Teradata Database performs implicit conversion from TIME to TIMESTAMP data types in some cases. However, implicit conversion from TIME to TIMESTAMP is not supported for comparisons. See “Implicit Conversion of DateTime types” on page 748. The following conversions are supported: AT SOURCE TIME ZONE WITH TIME ZONE the source time_expression (in UTC) is adjusted by adding the time zone displacement of time_expression. A local timestamp value is formed from CURRENT_DATE (based on the time zone displacement of time_expression) and the time portion of time_expression obtained after the previous adjustment. This resulting timestamp is adjusted to UTC, and the result value of the CAST at UTC is adjusted to the time zone displacement of time_expression. AT SOURCE TIME ZONE without TIME ZONE an error is returned. AT expression or AT TIME ZONE expression with or without TIME ZONE the source time_expression (in UTC) is adjusted by adding the time zone displacement defined by expression. A local timestamp value is formed from CURRENT_DATE (at the above time zone displacement) and the time portion of time_expression obtained after the above adjustment. This resulting timestamp is adjusted to UTC, and the result value of the CAST at UTC is adjusted to the above time zone displacement. AT time_zone_string or AT TIME ZONE time_zone_string with or without TIME ZONE the source time_expression (in UTC) is adjusted by adding the time zone displacement based on time_zone_string. The time zone displacement is determined based on time_zone_string, CURRENT_TIMESTAMP AT '00:00', and the TIME value of time_expression at UTC. A local timestamp value is formed from CURRENT_DATE (at the above time zone displacement) and the time portion of time_expression obtained after the above adjustment. This resulting timestamp is adjusted to UTC, and the result value of the CAST at UTC is adjusted to the above time zone displacement. IF you specify... AND the data type of time_expression is... THEN... Chapter 20: Data Type Conversions TIME-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 881 Example 1 Assuming the current date is DATE '2008-05-14' at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, the following SELECT statements return the result: TIMESTAMP '2008-05-14 08:30:00'. SET TIME ZONE INTERVAL '09:00' HOUR TO MINUTE; SELECT CAST(TIME '08:30:00' AS TIMESTAMP(0)); SELECT CAST(TIME '08:30:00' AS TIMESTAMP(0) AT LOCAL); The current session time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, is used to determine the UTC value '23:30:00' of the literal. For the CAST, the source expression value '23:30:00' at UTC is adjusted to the current session time zone displacement, INTERVAL '09:00' TO MINUTE, to yield '08:30:00'. A timestamp is formed from the current date '2008-05-14' at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, and the time portion of the source expression value '08:30:00'. Then, this timestamp, '2008-05-14 08:30:00', at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, is adjusted to UTC so that the CAST result is '2008-05-13 23:30:00' at UTC. The result value of the CAST at UTC is adjusted to the current session time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, so the result of the SELECT statements is: TIMESTAMP '2008-05-14 08:30:00'. Example 2 Assuming the current date is DATE '2008-05-14' at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, the following SELECT statements return the result: TIMESTAMP '2008-05-14 13:30:00'. SET TIME ZONE INTERVAL '09:00' HOUR TO MINUTE; SELECT CAST(TIME '08:30:00+04:00' AS TIMESTAMP(0)); SELECT CAST(TIME '08:30:00+04:00' AS TIMESTAMP(0) AT LOCAL); The time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, in the literal is used to determine the UTC value '04:30:00' and time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, of the literal. For the CAST, the source expression value '04:30:00' at UTC is adjusted to the current session time zone displacement, INTERVAL '09:00' HOUR TO MINUTE to yield '13:30:00'. From source type... To target type... TIME TIMESTAMP TIMESTAMP WITH TIME ZONE TIME WITH TIME ZONE TIMESTAMP TIMESTAMP WITH TIME ZONE Chapter 20: Data Type Conversions TIME-to-TIMESTAMP Conversion 882 SQL Functions, Operators, Expressions, and Predicates A timestamp is formed from the current date '2008-05-14' at time zone displacement, INTERVAL HOUR '09:00' TO MINUTE, and the time portion of the source expression value '13:30:00'. Then this timestamp, '2008-05-14 13:30:00', at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, is adjusted to UTC so that the CAST result is '2008-05-14 04:30:00' at UTC. The result value of the CAST at UTC is adjusted to the current session time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, so the result of the SELECT statements is: TIMESTAMP '2008-05-14 13:30:00'. Example 3 An error is returned for the following SELECT statements because the source expression does not have a time zone. SELECT CAST(TIME '08:30:00' AS TIMESTAMP(0) AT SOURCE TIME ZONE); SELECT CAST(TIME '08:30:00' AS TIMESTAMP(0) AT SOURCE); SELECT CAST(TIME '08:30:00' AS TIMESTAMP(0) WITH TIME ZONE AT SOURCE TIME ZONE); SELECT CAST(TIME '08:30:00' AS TIMESTAMP(0) WITH TIME ZONE AT SOURCE); Example 4 Assume that the current date is DATE '2008-05-14' at time zone displacement, INTERVAL '9:00' HOUR TO MINUTE, but the current date is DATE '2008-05-13' at time zone displacement, INTERVAL '04:00' HOUR TO MINUTE. The following SELECT statement returns the result: TIMESTAMP '2008-05-13 13:30:00'. SET TIME ZONE INTERVAL '09:00' HOUR TO MINUTE; SELECT CAST(TIME '08:30:00+04:00' AS TIMESTAMP(0) AT SOURCE TIME ZONE); The time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, in the literal is used to determine the UTC value '04:30:00' and time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, of the literal. For the CAST, the source expression value '04:30:00' at UTC is adjusted to the time zone displacement of the source, INTERVAL '04:00' HOUR TO MINUTE, to yield '08:30:00'. A timestamp is formed from the current date '2008-05-13' at time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, and the time portion of the source expression value '08:30:00' obtained after the above adjustment. Then this timestamp '2008-05-13 08:30:00' at time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, is adjusted to UTC so that the CAST result is '2008-05-13 04:30:00' at UTC. The result value of the CAST at UTC is adjusted to the current session time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, so the result of the SELECT statement is: TIMESTAMP '2008-05-13 13:30:00'. Example 5 Assume that the current date is DATE '2008-05-14' at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, but the current date is DATE '2008-05-13' at time zone, Chapter 20: Data Type Conversions TIME-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 883 INTERVAL -'08:00' HOUR TO MINUTE. The following SELECT statement returns the result: TIMESTAMP '2008-05-14 08:30:00'. SET TIME ZONE INTERVAL '09:00' HOUR TO MINUTE; SELECT CAST(TIME '08:30:00' AS TIMESTAMP(0) AT -8); The current session time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, is used to determine the UTC value '23:30:00' of the literal. For the CAST, the source expression value '23:30:00' at UTC is adjusted to the target time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, to yield '15:30:00'. A timestamp is formed from the current date '2008-05-13' at time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, and the time portion of the source expression value '15:30:00' obtained after the above adjustment. Then this resulting timestamp '2008-05-13 15:30:00' at time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, is adjusted to UTC so that the CAST result is '2008-05-13 23:30:00' at UTC. The result value of the CAST at UTC is adjusted to the current session time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, so the result of the SELECT statement is: TIMESTAMP '2008-05-14 08:30:00'. Example 6 Assume that the current date is DATE '2008-05-14' at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, but the current date is DATE '2008-05-13' at time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE. The following SELECT statement returns the result: TIMESTAMP '2008-05-14 13:30:00'. SET TIME ZONE INTERVAL '09:00' HOUR TO MINUTE; SELECT CAST(TIME '08:30:00+04:00' AS TIMESTAMP(0) AT -8); The time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, in the literal is used to determine the UTC value '04:30:00' and time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, of the literal. For the CAST, the source expression value '04:30:00' at UTC is adjusted to the target time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, to yield '20:30:00'. A timestamp is formed from the current date '2008-05-13' at time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, and the time portion of the source expression value '20:30:00' obtained after the above adjustment. Then this timestamp '2008-05-13 20:30:00' at time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, is adjusted to UTC so that the CAST result is '2008-05-14 04:30:00' at UTC. The result value of the CAST at UTC is adjusted to the current session time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, so the result of the SELECT statement is: TIMESTAMP '2008-05-14 13:30:00'. Example 7 Assuming the current date is DATE '2008-05-14' at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, the following SELECT statements return the result: TIMESTAMP '2008-05-14 08:30:00+09:00'. Chapter 20: Data Type Conversions TIME-to-TIMESTAMP Conversion 884 SQL Functions, Operators, Expressions, and Predicates SET TIME ZONE INTERVAL '09:00' HOUR TO MINUTE; SELECT CAST(TIME '08:30:00' AS TIMESTAMP(0) WITH TIME ZONE); SELECT CAST(TIME '08:30:00' AS TIMESTAMP(0) WITH TIME ZONE AT LOCAL); The current session time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, is used to determine the UTC value '23:30:00' of the literal. For the CAST, the source expression value '23:30:00' at UTC is adjusted to the current session time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, to yield '08:30:00'. A timestamp is formed from the current date '2008-05-14' at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, and the time portion of the source expression value '08:30:00' obtained after the above adjustment. Then this timestamp '2008-05-14 08:30:00' at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, is adjusted to UTC so that the CAST result is '2008-05-13 23:30:00' at UTC with time zone displacement, INTERVAL '09:00' HOUR TO MINUTE. The result value of the CAST at UTC is adjusted to time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, so the result of the SELECT statements is: TIMESTAMP '2008-05-14 08:30:00+09:00'. Example 8 Assuming the current date is DATE '2008-05-14' at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, the following SELECT statement returns the result: TIMESTAMP '2008-05-14 13:30:00+09:00'. SET TIME ZONE INTERVAL '09:00' HOUR TO MINUTE; SELECT CAST(TIME '08:30:00+04:00' AS TIMESTAMP(0) WITH TIME ZONE AT LOCAL); The time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, in the literal is used to determine the UTC value '04:30:00' and time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, of the literal. For the CAST, the source expression value '04:30:00 at UTC is adjusted to the current session time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, to yield '13:30:00'. A timestamp is formed from the current date '2008-05-14' at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, and the time portion of the source expression value '13:30:00' obtained after the above adjustment. Then this timestamp '2008-05-14 13:30:00' at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, is adjusted to UTC so that the CAST result is '2008-05-14 04:30:00' at UTC with time zone displacement, INTERVAL '09:00' HOUR TO MINUTE. The result value of the CAST at UTC is adjusted to time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, so the result of the SELECT statement is: TIMESTAMP '2008-05-14 13:30:00+09:00'. Example 9 Assume that the current date is DATE '2008-05-14' at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, but the current date is DATE '2008-05-13' at time zone Chapter 20: Data Type Conversions TIME-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 885 displacement, INTERVAL '04:00' HOUR TO MINUTE. The following SELECT statement returns the result: TIMESTAMP '2008-05-14 08:30:00+04:00'. SET TIME ZONE INTERVAL '09:00' HOUR TO MINUTE; SELECT CAST(TIME '08:30:00+04:00' AS TIMESTAMP(0) WITH TIME ZONE); The time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, in the literal is used to determine the UTC value '04:30:00' and time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, of the literal. For the CAST, the source expression value '04:30:00' at UTC is adjusted to the current session time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, to yield '13:30:00'. A timestamp is formed from the current date '2008-05-14' at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, and the time portion of the source expression value '13:30:00' obtained after the above adjustment. Then this timestamp '2008-05-14 13:30:00' at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, is adjusted to UTC so that the CAST result is '2008-05-14 04:30:00' at UTC with time zone displacement, INTERVAL '04:00' HOUR TO MINUTE. The result value of the CAST at UTC is adjusted to time zone displacement, INTERVAL '04:00' INTERVAL TO MINUTE, so the result of the SELECT statement is: TIMESTAMP '2008-05-14 08:30:00+04:00'. Example 10 Assume that the current date is DATE '2008-05-14' at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, but the current date is DATE '2008-05-13' at time zone displacement, INTERVAL '04:00' HOUR TO MINUTE. The following SELECT statement returns the result: TIMESTAMP '2008-05-13 08:30:00+04:00'. SET TIME ZONE INTERVAL '09:00' HOUR TO MINUTE; SELECT CAST(TIME '08:30:00+04:00' AS TIMESTAMP(0) WITH TIME ZONE AT SOURCE); The time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, in the literal is used to determine the UTC value '04:30:00' and time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, of the literal. For the CAST, the source expression value '04:30:00' at UTC is adjusted to the time zone displacement of the source expression, INTERVAL '04:00' HOUR TO MINUTE, to yield '08:30:00'. A timestamp is formed from the current date '2008-05-13' at time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, and the time portion of the source expression value '08:30:00' obtained after the above adjustment. Then this timestamp '2008-05-13 08:30:00' at time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, is adjusted to UTC so that the CAST result is '2008-05-13 04:30:00' at UTC with time zone displacement, INTERVAL '04:00' HOUR TO MINUTE. The result value of the CAST at UTC is adjusted to time zone, INTERVAL '04:00' HOUR TO MINUTE, so the result of the SELECT statement is: TIMESTAMP '2008-05-13 08:30:00+04:00'. The current session time zone has no effect. Chapter 20: Data Type Conversions TIME-to-TIMESTAMP Conversion 886 SQL Functions, Operators, Expressions, and Predicates Example 11 Assume that the current date is DATE '2008-05-14' at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, but the current date is DATE '2008-05-13' at time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE. The following SELECT statement returns the result: TIMESTAMP '2008-05-13 15:30:00-08:00'. SET TIME ZONE INTERVAL '09:00' HOUR TO MINUTE; SELECT CAST(TIME '08:30:00' AS TIMESTAMP(0) WITH TIME ZONE AT -8); The current session time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, is used to determine the UTC value '23:30:00' of the literal. For the CAST, the source expression value '23:30:00' at UTC is adjusted to the target time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, to yield '15:30:00'. A timestamp is formed from the current date '2008-05-13' at time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, and the time portion of the source expression value '15:30:00' obtained after the above adjustment. Then this timestamp '2008-05-13 15:30:00' at time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, is adjusted to UTC so that the CAST result is '2008-05-13 23:30:00' at UTC with time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE. The result value of the CAST at UTC is adjusted to time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, so the result of the SELECT statement is: TIMESTAMP '2008- 05-13 15:30:00-08:00'. Example 12 Assume that the current date is DATE '2008-05-14' at time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, but the current date is DATE '2008-05-13' at time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE. The following SELECT statement returns the result: TIMESTAMP '2008-05-13 20:30:00-08:00'. SET TIME ZONE INTERVAL '09:00' HOUR TO MINUTE; SELECT CAST(TIME '08:30:00+04:00' AS TIMESTAMP(0) WITH TIME ZONE AT -8); The time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, in the literal is used to determine the UTC value '04:30:00' and time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, of the literal. For the CAST, the source expression value '04:30:00' at UTC is adjusted to the target time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, to yield '20:30:00'. A timestamp is formed from the current date '2008-05-13' at time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, and the time portion of the source expression value '20:30:00' obtained after the above adjustment. Then this timestamp '2008-05-13 20:30:00' at time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, is adjusted to UTC so that the CAST result is '2008-05-14 04:30:00' at UTC with time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE. The result value of the CAST at UTC is adjusted to time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, so the result of the SELECT statement is: TIMESTAMP '2008- 05-13 20:30:00-08:00'. The current session time zone has no effect. Chapter 20: Data Type Conversions TIME-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 887 Example 13 In this example, the current timestamp is: Current TimeStamp(6) -------------------------------- 2010-03-09 19:23:27.620000+00:00 The following statement converts the TIME value '08:30:00' to a TIMESTAMP value, where the time zone displacement is based on the time zone string, 'America Pacific'. SELECT CAST(TIME '08:30:00' AS TIMESTAMP(0) AT 'America Pacific'); The result of the query is: 08:30:00 ------------------- 2010-03-09 08:30:00 Example 14 In this example, the current timestamp is: Current TimeStamp(6) -------------------------------- 2010-03-09 19:23:27.620000+00:00 The following statement converts the TIME value '08:30:00+04:00' to a TIMESTAMP value, where the time zone displacement is based on the time zone string, 'America Pacific'. SELECT CAST(TIME '08:30:00+04:00' AS TIMESTAMP(0) AT 'America Pacific'); The result of the query is: 08:30:00+04:00 ------------------- 2010-03-10 04:30:00 Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions TIME-to-UDT Conversion 888 SQL Functions, Operators, Expressions, and Predicates TIME-to-UDT Conversion Purpose Converts TIME data to UDT data. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases such as FORMAT. Usage Notes Explicit TIME-to-UDT conversion using Teradata conversion syntax is not supported. Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. Implicit TIME-to-UDT Conversion Teradata Database performs implicit TIME-to-UDT conversions for the following operations: • UPDATE • INSERT • Passing arguments to stored procedures, external stored procedures, UDFs, and UDMs • Specific system operators and functions identified in other sections of this book, unless the DisableUDTImplCastForSysFuncOp field of the DBS Control Record is set to TRUE Performing an implicit data type conversion requires that an appropriate cast definition (see “Usage Notes”) exists that specifies the AS ASSIGNMENT clause. Syntax element … Specifies … time_expression a TIME expression to be cast to a UDT. UDT_data_definition the UDT type, followed by any optional FORMAT, NAMED, or TITLE data attribute phrases, to which time_expression is to be converted. CAST AS time_expression UDT_data_definition ( ( 1101A340 Chapter 20: Data Type Conversions TIME-to-UDT Conversion SQL Functions, Operators, Expressions, and Predicates 889 If no TIME-to-UDT implicit cast definition exists, Teradata Database looks for a CHAR-to- UDT or VARCHAR-to-UDT implicit cast definition that can substitute for the TIME-to-UDT implicit cast definition. Substitutions are valid because Teradata Database can implicitly cast a TIME type to the character data type, and then use the implicit cast definition to cast from the character data type to the UDT. If multiple character-to-UDT implicit cast definitions exist, then Teradata Database returns an SQL error. Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions TIMESTAMP-to-Character Conversion 890 SQL Functions, Operators, Expressions, and Predicates TIMESTAMP-to-Character Conversion Purpose Convert TIMESTAMP data to a character string. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of character data attribute phrases. Syntax element … Specifies … timestamp_expression the TIMESTAMP expression to be cast to a character type. character_data_type the character type to which the TIMESTAMP expression is to be converted. server_character_set the server character set to use for the conversion. If no CHARACTER SET clause is specified to indicate which server character set to use, the user default server character set is used. character_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE 1101A269 CAST timestamp_expression AS character_data_type ) ( A CHARACTER SET server_character_set character_data_attribute A Chapter 20: Data Type Conversions TIMESTAMP-to-Character Conversion SQL Functions, Operators, Expressions, and Predicates 891 Teradata Conversion Syntax where: ANSI Compliance This is a Teradata extension to the ANSI SQL:2008 standard. Usage Notes When converting TIMESTAMP to CHAR(n) or VARCHAR(n), then n must be equal to or greater than the length of the TIMESTAMP value as represented by a character string literal. Syntax element … Specifies … timestamp_expression the TIMESTAMP expression to be cast to a character type. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE character_data_type the character type to which the TIMESTAMP expression is to be converted. server_character_set the server character set to use for the conversion. If no CHARACTER SET clause is specified to indicate which server character set to use, the user default server character set is used. 1101B277 timestamp_expression ( character_data_type ) A CHARACTER SET server_character_set , data_attribute A data_attribute , IF the target data type is … AND n is … THEN … CHAR(n) greater than the length of the TIMESTAMP value as represented by a character string literal trailing pad characters are added to pad the representation. too small a string truncation error is returned. Chapter 20: Data Type Conversions TIMESTAMP-to-Character Conversion 892 SQL Functions, Operators, Expressions, and Predicates TIMESTAMP to CLOB conversion is not supported. You cannot convert a TIME value to a character string if the server character set is GRAPHIC. Forcing a FORMAT on CAST for Converting TIMESTAMP to Character The default format for TIMESTAMP to character conversion is the format in effect for the TIMESTAMP value. To override the format, you can convert a TIMESTAMP value to a string using a FORMAT phrase. The resulting format, however, is the same as the TIMESTAMP value. If you want a different format for the string value, you need to also use CAST as described here. You must use nested CAST operations in order to convert values from TIMESTAMP to CHAR and force an explicit FORMAT on the result regardless of the format associated with the TIMESTAMP value. This is because of the rules for matching FORMAT phrases to data types. Example Field TS1 in the table INTTIMESTAMP is a TIMESTAMP value with the explicit format 'Y4- MM-DDBHH:MI:SSDS(6)'. Assume that you want to convert this to a value of CHAR(19), and an explicit output format of 'M3BDD,BY4BHHhMIm'. SELECT TS1 FROM INTTIMESTAMP; The result (without a type change) is the following report: TS1 -------------------------- 1900-12-31 08:25:37.899231 Now use nested CAST phrases and a FORMAT to obtain the desired result: a report in character format. SELECT CAST( (CAST (TS1 AS FORMAT 'M3BDD,BY4BHHhMIm')) AS CHAR(19)) FROM INTTIMESTAMP; The result after the nested CASTs is the following report. TS1 ------------------- Dec 31, 1900 08h25m The inner CAST establishes the display format for the TIMESTAMP value and the outer CAST indicates the data type of the desired result. VARCHAR(n) greater than the length of the TIMESTAMP value as represented by a character string literal no blank padding is added to the character representation. too small a string truncation error is returned. IF the target data type is … AND n is … THEN … Chapter 20: Data Type Conversions TIMESTAMP-to-Character Conversion SQL Functions, Operators, Expressions, and Predicates 893 Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions TIMESTAMP-to-DATE Conversion 894 SQL Functions, Operators, Expressions, and Predicates TIMESTAMP-to-DATE Conversion Purpose Convert TIMESTAMP data to a DATE value. CAST Syntax where: Syntax element … Specifies … timestamp_expression the TIMESTAMP expression to be converted. timestamp_expression may include an AT clause. AT LOCAL that the time zone displacement based on the current session time zone is used. This is the default. AT SOURCE [TIME ZONE] that the time zone associated with timestamp_expression is used in the following cases: • AT SOURCE TIME ZONE is specified. • AT SOURCE is specified without TIME ZONE and there is no column named source in the scope. Otherwise, if AT SOURCE is specified without TIME ZONE and a column named source exists, then SOURCE references this column, and the value of the column is used as the time zone displacement for the CAST. If needed, the column value is implicitly converted to type INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. If there are multiple columns named source in the scope, an error is returned. 1101B270 CAST ( timestamp_expression AS DATE expression time_zone_string AT LOCAL SOURCE TIME ZONE TIME ZONE B B ) date_data_attribute Chapter 20: Data Type Conversions TIMESTAMP-to-DATE Conversion SQL Functions, Operators, Expressions, and Predicates 895 ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of DATE data attribute phrases, such as FORMAT that enables an alternative format. The AT clause is ANSI SQL:2008 compliant. As an extension to ANSI, the AT clause is supported when using CAST to convert from TIMESTAMP to DATE. In addition, you can specify the time zone displacement using additional expressions besides an INTERVAL expression. Note: TIMESTAMP (without time zone) is not ANSI SQL:2008 compliant. Teradata Database internally converts a TIMESTAMP value to UTC based on the current session time zone or on a specified time zone. Teradata Conversion Syntax where: AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement used for the CAST. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. date_data_attribute any of the following optional data attributes: • FORMAT • NAMED • TITLE Syntax element … Specifies … expression time_zone_string AT LOCAL SOURCE TIME ZONE TIME ZONE B B ) , data_attribute 1101C278 timestamp_expression data_attribute , ( DATE Chapter 20: Data Type Conversions TIMESTAMP-to-DATE Conversion 896 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance Teradata Conversion Syntax is a Teradata extension to the ANSI SQL:2008 standard. The AT clause is ANSI SQL:2008 compliant. As an extension to ANSI, the AT clause is supported when using Teradata Conversion Syntax to convert from TIMESTAMP to DATE. In addition, you can specify the time zone displacement using additional expressions besides an INTERVAL expression. Note: TIMESTAMP (without time zone) is not ANSI SQL:2008 compliant. Teradata Database internally converts a TIMESTAMP value to UTC based on the current session time zone or on a specified time zone. Syntax element … Specifies … timestamp_expression the TIMESTAMP expression to be converted. timestamp_expression may include an AT clause. data_attribute any of the following optional data attributes: • FORMAT • NAMED • TITLE AT LOCAL that the time zone displacement based on the current session time zone is used. This is the default. AT SOURCE [TIME ZONE] that the time zone associated with timestamp_expression is used in the following cases: • AT SOURCE TIME ZONE is specified. • AT SOURCE is specified without TIME ZONE and there is no column named source in the scope. Otherwise, if AT SOURCE is specified without TIME ZONE and a column named source exists, then SOURCE references this column, and the value of the column is used as the time zone displacement in the conversion. If needed, the column value is implicitly converted to type INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. If there are multiple columns named source in the scope, an error is returned. AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement used in the conversion. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. Chapter 20: Data Type Conversions TIMESTAMP-to-DATE Conversion SQL Functions, Operators, Expressions, and Predicates 897 Usage Notes The following table shows the result of the CAST function or Teradata conversion based on the various options specified. Note that the time zone adjustment may change the YEAR, MONTH, and DAY fields of the DATE value. Implicit TIMESTAMP-to-DATE Conversion Teradata Database performs implicit conversion from TIMESTAMP types to DATE in some cases. See “Implicit Conversion of DateTime types” on page 748. The following conversions are supported: IF you specify... AND the data type of timestamp_expression is... THEN... AT LOCAL with or without TIME ZONE the result is the date portion of the source timestamp_expression after adjusting its UTC value by adding the time zone displacement based on the current session time zone. This is the same as not specifying the AT clause. AT SOURCE (where SOURCE is a keyword and not a column reference) WITH TIME ZONE the result is the date portion of the source timestamp_expression after adjusting its UTC value by adding the time zone displacement associated with timestamp_expression. AT SOURCE (where SOURCE is a keyword and not a column reference) without TIME ZONE an error is returned. AT SOURCE TIME ZONE WITH TIME ZONE the result is the date portion of the source timestamp_expression after adjusting its UTC value by adding the time zone displacement associated with timestamp_expression. AT SOURCE TIME ZONE without TIME ZONE an error is returned. AT expression or AT TIME ZONE expression with or without TIME ZONE the result is the date portion of the source timestamp_expression after adjusting its UTC value by adding the time zone displacement defined by expression. AT time_zone_string or AT TIME ZONE time_zone_string with or without TIME ZONE the result is the date portion of the source timestamp_expression after adjusting its UTC value by adding the time zone displacement based on time_zone_string. The time zone displacement is determined based on time_zone_string and the TIMESTAMP value of timestamp_expression at UTC. Chapter 20: Data Type Conversions TIMESTAMP-to-DATE Conversion 898 SQL Functions, Operators, Expressions, and Predicates The TIMESTAMP value is always converted to DATE in case of comparison. Example 1 A single column table has three rows of type TIMESTAMP(0) WITH TIME ZONE. A query that requests the field values and CASTs them as DATE is performed during a session that has its Local Time Zone defined as -’08:00’. The results table is as follows. TimeStampWithTimeZone CastAsDate ------------------------------------------------- 1997-10-07 15:43:00+08:00 1997-10-06 1997-10-07 15:47:52-08:00 1997-10-07 1997-10-07 15:43:00-00:00 1997-10-07 Notice that the difference between the stored Time Zone and the Local Time Zone is 16 hours in the first row, but at the same time the TimeStamp value is 15:43, which is less than 16. This puzzling result can be clarified using a similar query that casts TIMESTAMP(0) WITH TIME ZONE as TIMESTAMP(0), omitting the Time Zone information. The results table for this query is as follows. TimeStampWithTimeZone CastAsTimeStamp ------------------------------------------------- 1997-10-07 15:43:00+08:00 1997-10-06 23:43:00 1997-10-07 15:47:52-08:00 1997-10-07 15:47:52 1997-10-07 15:43:00-00:00 1997-10-07 07:43:00 After the CAST, the values are all displayed at Local Time Zone, and the value in the first row indicates that the 16 hour adjustment rolled the date back 1, to a time near the end of that date. Example 2 Consider the following statements: SET TIME ZONE INTERVAL '01:00' HOUR TO MINUTE; SELECT CAST(TIMESTAMP '2008-05-31 22:30:00-08:00' AS DATE AT SOURCE TIME ZONE); SELECT TIMESTAMP '2008-06-01 06:30:00+00:00' AT '-08:00' (DATE, AT SOURCE); SELECT TIMESTAMP '2008-06-01 06:30:00+00:00' (DATE, AT -8); From source type... To target type... TIMESTAMP DATEa a. ANSIDate dateform mode or IntegerDate dateform mode TIMESTAMP WITH TIME ZONE Chapter 20: Data Type Conversions TIMESTAMP-to-DATE Conversion SQL Functions, Operators, Expressions, and Predicates 899 SELECT TIMESTAMP '2008-06-01 07:30:00' (DATE, AT -8); These SELECT statements return the date for time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE; that is, the statements return '08/05/31'. If the SELECT statements were specified without an AT clause or with an AT LOCAL clause, these statements would return '08/06/01' for the current session time zone displacement, INTERVAL '01:00' HOUR TO MINUTE. The following shows the results of the SELECT statements if the AT clause was not specified: SET TIME ZONE INTERVAL '01:00' HOUR TO MINUTE; SELECT CAST(TIMESTAMP '2008-05-31 22:30:00-08:00' AS DATE); 2008-05-31 22:30:00-08:00 ------------------------- 08/06/01 SELECT TIMESTAMP '2008-06-01 06:30:00+00:00' AT TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE; 2008-06-01 06:30:00+00:00 AT TIME ZONE INTERVAL -8:00 HOUR TO MINUTE -------------------------------------------------------------------- 2008-05-31 22:30:00-08:00 SELECT TIMESTAMP '2008-06-01 06:30:00+00:00' AT TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE (DATE); 2008-06-01 06:30:00+00:00 AT TIME ZONE INTERVAL -8:00 HOUR TO MINUTE -------------------------------------------------------------------- 08/06/01 SELECT TIMESTAMP '2008-06-01 06:30:00+00:00' (DATE); 2008-06-01 06:30:00+00:00 ------------------------- 08/06/01 SELECT TIMESTAMP '2008-06-01 07:30:00' (DATE); 2008-06-01 07:30:00 ------------------- 08/06/01 The following shows the results of the SELECT statements if the AT clause was not specified, and the current session time zone displacement is INTERVAL -'08:00' HOUR TO MINUTE. SET TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE; SELECT CAST(TIMESTAMP '2008-05-31 22:30:00-08:00' AS DATE); 2008-05-31 22:30:00-08:00 ------------------------- 08/05/31 SELECT TIMESTAMP '2008-06-01 06:30:00+00:00' AT TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE (DATE); 2008-06-01 06:30:00+00:00 AT TIME ZONE INTERVAL -8:00 HOUR TO MINUTE Chapter 20: Data Type Conversions TIMESTAMP-to-DATE Conversion 900 SQL Functions, Operators, Expressions, and Predicates -------------------------------------------------------------------- 08/05/31 SELECT TIMESTAMP '2008-06-01 06:30:00+00:00' (DATE); 2008-06-01 06:30:00+00:00 ------------------------- 08/05/31 SELECT CAST(TIMESTAMP '2008-06-01 07:30:00+01:00' AS TIMESTAMP(0)) (DATE); 2008-06-01 07:30:00+01:00 ------------------------- 08/05/31 Example 3 Consider the following statements: SET TIME ZONE INTERVAL '01:00' HOUR TO MINUTE; SELECT CAST(TIMESTAMP '2008-06-02 04:30:00+09:00' AS DATE AT SOURCE TIME ZONE); SELECT TIMESTAMP '2008-06-01 20:30:00+01:00' AT TIME ZONE INTERVAL '09' HOUR (DATE, AT SOURCE); SELECT TIMESTAMP '2008-06-01 20:30:00' (DATE, AT +9); These SELECT statements return the date for time zone displacement, INTERVAL '09:00' HOUR TO MINUTE; that is, the statements return '08/06/02'. If the SELECT statements were specified without an AT clause or with an AT LOCAL clause, these statements would return '08/06/01' for the current session time zone displacement, INTERVAL '01:00' HOUR TO MINUTE. The following shows the results of the SELECT statements if the AT clause was not specified: SET TIME ZONE INTERVAL '01:00' HOUR TO MINUTE; SELECT CAST(TIMESTAMP '2008-06-02 04:30:00+09:00' AS DATE); 2008-06-02 04:30:00+09:00 ------------------------- 08/06/01 SELECT TIMESTAMP '2008-06-01 20:30:00+01:00' AT TIME ZONE INTERVAL '09:00' HOUR TO MINUTE; 2008-06-01 20:30:00+01:00 AT TIME ZONE INTERVAL 9:00 HOUR TO MINUTE -------------------------------------------------------------------- 2008-06-02 04:30:00+09:00 SELECT TIMESTAMP '2008-06-01 20:30:00+01:00' AT TIME ZONE INTERVAL '09:00' HOUR TO MINUTE (DATE); 2008-06-01 20:30:00+01:00 AT TIME ZONE INTERVAL 9:00 HOUR TO MINUTE -------------------------------------------------------------------- Chapter 20: Data Type Conversions TIMESTAMP-to-DATE Conversion SQL Functions, Operators, Expressions, and Predicates 901 08/06/01 SELECT TIMESTAMP '2008-06-01 20:30:00' (DATE); 2008-06-01 20:30:00 ------------------- 08/06/01 The following shows the results of the SELECT statements if the AT clause was not specified, and the current session time zone displacement is INTERVAL '09:00' TO MINUTE. SET TIME ZONE INTERVAL '09:00' HOUR TO MINUTE; SELECT CAST(TIMESTAMP '2008-06-02 04:30:00+09:00' AS DATE); 2008-06-02 04:30:00+09:00 ------------------------- 08/06/02 SELECT TIMESTAMP '2008-06-01 20:30:00+01:00' AT TIME ZONE INTERVAL '09:00' HOUR TO MINUTE (DATE); 2008-06-01 20:30:00+01:00 AT TIME ZONE INTERVAL 9:00 HOUR TO MINUTE -------------------------------------------------------------------- 08/06/02 SELECT CAST(TIMESTAMP '2008-06-01 20:30:00+01:00' AS TIMESTAMP(0)) (DATE); 2008-06-01 20:30:00+01:00 ------------------------- 08/06/02 Example 4 Consider the following statements: SET TIME ZONE INTERVAL '10:00' HOUR TO MINUTE; SELECT CAST((TIMESTAMP '2008-06-01 18:30:00+01:00' AT '05:45') AS DATE AT SOURCE); SELECT CAST((TIMESTAMP '2008-06-01 18:30:00+01:00' AT 5.75) AS DATE AT SOURCE); SELECT TIMESTAMP '2008-06-01 23:15:00+05:45' (DATE, AT SOURCE TIME ZONE); SELECT TIMESTAMP '2008-06-02 03:30:00' (DATE, AT '05:45'); SELECT TIMESTAMP '2008-06-02 03:30:00' (DATE, AT 5.75); These SELECT statements return the date for time zone displacement, INTERVAL '05:45' HOUR TO MINUTE; that is, the statements return '08/06/01'. If the SELECT statements were specified without an AT clause or with an AT LOCAL clause, these statements would return '08/06/02' for the current session time zone displacement, INTERVAL '10:00' HOUR TO MINUTE. The following shows the results of the SELECT statements if the AT clause was not specified: Chapter 20: Data Type Conversions TIMESTAMP-to-DATE Conversion 902 SQL Functions, Operators, Expressions, and Predicates SET TIME ZONE INTERVAL '10:00' HOUR TO MINUTE; SELECT TIMESTAMP '2008-06-01 18:30:00+01:00' AT TIME ZONE INTERVAL '05:45' HOUR TO MINUTE; 2008-06-01 18:30:00+01:00 AT TIME ZONE INTERVAL 5:45 HOUR TO MINUTE -------------------------------------------------------------------- 2008-06-01 23:15:00+05:45 SELECT CAST((TIMESTAMP '2008-06-01 18:30:00+01:00' AT TIME ZONE INTERVAL '05:45' HOUR TO MINUTE) AS DATE); 2008-06-01 18:30:00+01:00 AT TIME ZONE INTERVAL 5:45 HOUR TO MINUTE -------------------------------------------------------------------- 08/06/02 SELECT TIMESTAMP '2008-06-01 23:15:00+05:45' (DATE); 2008-06-01 23:15:00+05:45 ------------------------- 08/06/02 SELECT TIMESTAMP '2008-06-02 03:30:00' (DATE); 2008-06-02 03:30:00 ------------------- 08/06/02 The following shows the results of the SELECT statements if the AT clause was not specified, and the current session time zone displacement is INTERVAL '05:45' HOUR TO MINUTE. SET TIME ZONE INTERVAL '05:45' HOUR TO MINUTE; SELECT CAST((TIMESTAMP '2008-06-01 18:30:00+01:00' AT TIME ZONE INTERVAL'05:45' HOUR TO MINUTE) AS DATE); 2008-06-01 18:30:00+01:00 AT TIME ZONE INTERVAL 5:45 HOUR TO MINUTE -------------------------------------------------------------------- 08/06/01 SELECT TIMESTAMP '2008-06-01 23:15:00+05:45' (DATE); 2008-06-01 23:15:00+05:45 ------------------------- 08/06/01 SELECT CAST(TIMESTAMP '2008-06-02 03:30:00+10:00' AS TIMESTAMP(0)) (DATE); 2008-06-02 03:30:00+10:00 ------------------------- 08/06/01 Example 5 Consider the following statements: SET TIME ZONE +1; SELECT CAST((TIMESTAMP '2008-06-01 08:30:00' AT TIME ZONE -8) Chapter 20: Data Type Conversions TIMESTAMP-to-DATE Conversion SQL Functions, Operators, Expressions, and Predicates 903 AS DATE AT SOURCE TIME ZONE); This SELECT statement returns the date for time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE; that is, the statement returns '08/05/31'. If the SELECT statement was specified without an AT clause or with an AT LOCAL clause, the statement would return '08/ 06/01' for the current session time zone displacement, INTERVAL HOUR '01:00' MINUTE. The following shows the result of the SELECT statement if the AT clause was not specified: SET TIME ZONE INTERVAL '01:00' HOUR TO MINUTE; SELECT TIMESTAMP '2008-06-01 08:30:00' AT TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE; 2008-06-01 08:30:00 AT TIME ZONE INTERVAL -8:00 HOUR TO MINUTE -------------------------------------------------------------- 2008-05-31 23:30:00-08:00 SELECT CAST((TIMESTAMP '2008-06-01 08:30:00' AT TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE) AS DATE); 2008-06-01 08:30:00 AT TIME ZONE INTERVAL -8:00 HOUR TO MINUTE -------------------------------------------------------------- 08/06/01 The following shows the result of the SELECT statement if the AT clause was not specified, and the current session time zone displacement is INTERVAL -'08:00' HOUR TO MINUTE. SET TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE; SELECT CAST((CAST(TIMESTAMP '2008-06-01 08:30:00+01:00' AS TIMESTAMP(0)) AT TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE) AS DATE); 2008-06-01 08:30:00+01:00 AT TIME ZONE INTERVAL -8:00 HOUR TO MINUTE -------------------------------------------------------------------- 08/05/31 Example 6 In this example, the current timestamp is: Current TimeStamp(6) -------------------------------- 2010-03-09 19:23:27.620000+00:00 The following statement converts the TIMESTAMP value '2010-03-09 22:30:00-08:00' to a DATE value, where the time zone displacement is based on the time zone string, 'America Pacific'. SELECT CAST(TIMESTAMP '2010-03-09 22:30:00-08:00' AS DATE AT 'America Pacific'); The result of the query is: 2010-03-09 22:30:00-08:00 ------------------------- 10/03/09 Chapter 20: Data Type Conversions TIMESTAMP-to-DATE Conversion 904 SQL Functions, Operators, Expressions, and Predicates Example 7 The following SELECT statements return an error because the source expression does not have a TIMESTAMP WITH TIME ZONE data type. SELECT CAST(TIMESTAMP '2008-06-01 08:30:00' AS DATE AT SOURCE); SELECT CAST(TIME '08:30:00+03:00' AS DATE AT SOURCE TIME ZONE); SELECT CAST(TIME '08:30:00' AS DATE AT SOURCE); SELECT CAST(DATE '2008-06-01' AS DATE AT SOURCE TIME ZONE); Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions TIMESTAMP-to-Period Conversion SQL Functions, Operators, Expressions, and Predicates 905 TIMESTAMP-to-Period Conversion Purpose Converts a TIMESTAMP value as PERIOD(DATE), PERIOD(TIME[(n)][WITH TIME ZONE]), or PERIOD(TIMESTAMP[(n)][WITH TIME ZONE]). CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases. Usage Notes A TIMESTAMP(n) [WITH TIME ZONE] value can be cast as PERIOD(DATE), PERIOD(TIME[(n)] [WITH TIME ZONE]), or PERIOD(TIMESTAMP[(n)] [WITH TIME ZONE]) using the CAST function. If the target type is PERIOD(TIME[(n)] [WITH TIME ZONE]) or PERIOD(TIMESTAMP[(n)] [WITH TIME ZONE]): • If the target precision is higher than the source precision, trailing zeros are added in the result bounds to adjust the precision. • If the target precision is lower than the source precision, an error is reported. CAST timestamp_expression AS period_data_type period_data_attribute ( ) 1101A608 Syntax element … Specifies … timestamp_expression the TIMESTAMP data expression to be converted. period_data_type the target Period type to which timestamp_expression is to be converted. period_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE Chapter 20: Data Type Conversions TIMESTAMP-to-Period Conversion 906 SQL Functions, Operators, Expressions, and Predicates If the target type is PERIOD(DATE), the result beginning bound is the date portion of the source beginning bound adjusted to the current session time zone. If the target type is PERIOD(TIME[(n)]), the result beginning bound is the time portion of the source value (in UTC). If the target type is PERIOD(TIME[(n)] WITH TIME ZONE), the result beginning bound is formed from the time portion of the source value (in UTC) and, if the source type is WITH TIME ZONE, the source time zone displacement and, if not, the current session time zone displacement. If the target type is PERIOD(TIMESTAMP[(n)]), the result beginning bound is the timestamp portion of the source value (in UTC). If the target type is PERIOD(TIMESTAMP[(n)] WITH TIME ZONE), the result beginning bound is formed from the timestamp portion of the source value (in UTC) and, if the source type is WITH TIME ZONE, the source time zone displacement and, if not, the current session time zone displacement. If the TIMESTAMP source value contains leap seconds, the seconds portion gets adjusted to 59.999999 with the precision truncated to the target precision. The result ending element is set to the result beginning bound plus one granule of the target type. If the result ending bound exceeds the maximum allowed DATE or TIMESTAMP value for a target type of PERIOD(DATE) or PERIOD(TIMESTAMP[(n)]), respectively, or the ending bound has a lower value than the result beginning bound in their UTC forms for a target type of PERIOD(TIME[(n)]), an error is reported. Note: If the target type is WITH TIME ZONE, the result beginning and ending bounds have the same time zones. Also, note that the result has the same value for the beginning bound and last value. Example In the following example, a TIMESTAMP(6) literal is cast as PERIOD(DATE). The result beginning element is set to the date portion of the source value. The result ending element is set to result beginning bound plus INTERVAL '1' DAY. SELECT CAST(TIMESTAMP '2005-02-03 12:12:12.340000' AS PERIOD(DATE)); The following is returned: ('2005-02-03', '2005-02-04') Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions TIMESTAMP-to-TIME Conversion SQL Functions, Operators, Expressions, and Predicates 907 TIMESTAMP-to-TIME Conversion Purpose Convert TIMESTAMP data to a TIME value. CAST Syntax where: Syntax element … Specifies … timestamp_expression the TIMESTAMP expression to be converted. fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. AT LOCAL that the time zone displacement based on the current session time zone is used. 1101B271 timestamp_expression (fractional_seconds_precision) CAST ( AS TIME A expression time_zone_string WITH TIME ZONE AT LOCAL SOURCE TIME ZONE TIME ZONE A B ) time_data_attribute B Chapter 20: Data Type Conversions TIMESTAMP-to-TIME Conversion 908 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of TIME data attribute phrases. The AT clause is ANSI SQL:2008 compliant. As an extension to ANSI, the AT clause is supported when using CAST to convert from TIMESTAMP to TIME. In addition, you can specify the time zone displacement using additional expressions besides an INTERVAL expression. Note: TIME (without time zone) and TIMESTAMP (without time zone) are not ANSI SQL:2008 compliant. Teradata Database internally converts a TIME or TIMESTAMP value to UTC based on the current session time zone or on a specified time zone. AT SOURCE [TIME ZONE] that the time zone associated with timestamp_expression is used in the following cases: • AT SOURCE TIME ZONE is specified. • AT SOURCE is specified without TIME ZONE and there is no column named source in the scope. Otherwise, if AT SOURCE is specified without TIME ZONE and a column named source exists, then SOURCE references this column, and the value of the column is used as the time zone displacement for the CAST. If needed, the column value is implicitly converted to type INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. If there are multiple columns named source in the scope, an error is returned. AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement used for the CAST. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. time_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE Syntax element … Specifies … Chapter 20: Data Type Conversions TIMESTAMP-to-TIME Conversion SQL Functions, Operators, Expressions, and Predicates 909 Teradata Conversion Syntax where: Syntax element … Specifies … timestamp_expression the TIMESTAMP expression to be converted. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. AT LOCAL that the time zone displacement based on the current session time zone is used. 1101C279 timestamp_expression (fractional_seconds_precision) ( TIME A data_attribute , expression time_zone_string , WITH TIME ZONE AT LOCAL SOURCE TIME ZONE TIME ZONE A B ) , data_attribute B Chapter 20: Data Type Conversions TIMESTAMP-to-TIME Conversion 910 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance Teradata Conversion Syntax is a Teradata extension to the ANSI SQL:2008 standard. The AT clause is ANSI SQL:2008 compliant. As an extension to ANSI, the AT clause is supported when using Teradata Conversion Syntax to convert from TIMESTAMP to TIME. In addition, you can specify the time zone displacement using additional expressions besides an INTERVAL expression. Note: TIME (without time zone) and TIMESTAMP (without time zone) are not ANSI SQL:2008 compliant. Teradata Database internally converts a TIME or TIMESTAMP value to UTC based on the current session time zone or on a specified time zone. Usage Notes If you specify an AT clause for a TIME[(n)] without time zone target data type, an error is returned. If you specify an AT clause for a TIME[(n)] WITH TIME ZONE target data type, the following table shows the result of the CAST function or Teradata conversion based on the various options specified. If the target precision is higher than the source precision, trailing zeros are added in the result to adjust the precision. If the target precision is lower than the source precision, an error is returned. AT SOURCE [TIME ZONE] that the time zone associated with timestamp_expression is used in the following cases: • AT SOURCE TIME ZONE is specified. • AT SOURCE is specified without TIME ZONE and there is no column named source in the scope. Otherwise, if AT SOURCE is specified without TIME ZONE and a column named source exists, then SOURCE references this column, and the value of the column is used as the time zone displacement in the conversion. If needed, the column value is implicitly converted to type INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. If there are multiple columns named source in the scope, an error is returned. AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement used in the conversion. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. Syntax element … Specifies … Chapter 20: Data Type Conversions TIMESTAMP-to-TIME Conversion SQL Functions, Operators, Expressions, and Predicates 911 Implicit TIMESTAMP-to-TIME Conversion Teradata Database performs implicit conversion from TIMESTAMP to TIME data types in some cases. However, implicit conversion from TIMESTAMP to TIME is not supported for comparisons. See “Implicit Conversion of DateTime types” on page 748. The following conversions are supported: IF you specify... AND the data type of timestamp_expression is... THEN... AT LOCAL with or without TIME ZONE the result is formed from the source timestamp_expression (in UTC) and the time zone displacement based on the current session time zone. If the data type of timestamp_expression is without time zone, this is the same as not specifying the AT clause. AT SOURCE (where SOURCE is a keyword and not a column reference) WITH TIME ZONE the result is formed from the time portion of the source timestamp_expression (in UTC) and the time zone displacement associated with timestamp_expression. Note that this is the same as not specifying the AT clause. AT SOURCE (where SOURCE is a keyword and not a column reference) without TIME ZONE an error is returned. AT SOURCE TIME ZONE WITH TIME ZONE the result is formed from the time portion of the source timestamp_expression (in UTC) and the time zone displacement associated with timestamp_expression. Note that this is the same as not specifying the AT clause. AT SOURCE TIME ZONE without TIME ZONE an error is returned. AT expression or AT TIME ZONE expression with or without TIME ZONE the result is formed from the time portion of the source timestamp_expression (in UTC) and the time zone displacement defined by expression. AT time_zone_string or AT TIME ZONE time_zone_string with or without TIME ZONE the result is formed from the time portion of the source timestamp_expression (in UTC) and the time zone displacement based on time_zone_string. The time zone displacement is determined based on time_zone_string and the TIMESTAMP value of timestamp_expression at UTC. From source type... To target type... TIMESTAMP TIME TIME WITH TIME ZONE Chapter 20: Data Type Conversions TIMESTAMP-to-TIME Conversion 912 SQL Functions, Operators, Expressions, and Predicates Example 1 In this example, the current session time zone displacement, INTERVAL '01:00' HOUR TO MINUTE, is used to determine the UTC value, '2008-06-01 07:30:00', of the TIMESTAMP literal. The result of the CAST is the time formed from the time portion of the source expression value '2008-06-01 07:30:00' at UTC and the current time zone displacement, INTERVAL '01:00' HOUR TO MINUTE. The result value of the CAST '07:30:00' at UTC is adjusted to its time zone displacement, INTERVAL '01:00' HOUR TO MINUTE, and the result of the SELECT statements is: TIME '08:30:00+01:00'. The result of the SELECT statements is equal to TIME '07:30:00+00:00' since values are compared based on their UTC values. SET TIME ZONE INTERVAL '01:00' HOUR TO MINUTE; SELECT CAST(TIMESTAMP '2008-06-01 08:30:00' AS TIME(0) WITH TIME ZONE); SELECT CAST(TIMESTAMP '2008-06-01 08:30:00' AS TIME(0) WITH TIME ZONE AT LOCAL); Example 2 In this example, the time zone displacement specified in the literal, INTERVAL '04:00' HOUR TO MINUTE, is used to determine the UTC value '2008-06-01 04:30:00' for the TIMESTAMP literal. The result of the CAST is the time formed from the time portion of the source expression value '2008-06-01 04:30:00' at UTC and the current session time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE. The result value of the CAST '04:30:00' at UTC is adjusted to its time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, and the result of the SELECT statement is: TIME '20:30:00-08:00'. The result of the SELECT statement is equal to TIME '04:30:00+00:00'. SET TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE; SELECT CAST(TIMESTAMP '2008-06-01 08:30:00+04:00' AS TIME(0) WITH TIME ZONE AT LOCAL); TIMESTAMP WITH TIME ZONE TIME TIME WITH TIME ZONE From source type... To target type... Chapter 20: Data Type Conversions TIMESTAMP-to-TIME Conversion SQL Functions, Operators, Expressions, and Predicates 913 Example 3 The following SELECT statement return an error because the source expression does not have a time zone displacement. SELECT CAST(TIMESTAMP '2008-06-01 08:30:00' AS TIME(0) WITH TIME ZONE AT SOURCE); Example 4 In this example, the time zone displacement specified in the literal, INTERVAL '04:00' HOUR TO MINUTE, is used to determine the UTC value '2008-06-01 04:30:00' for the TIMESTAMP literal. The result of the CAST is the time formed from the time portion of the source expression value '2008-06-01 04:30:00' at UTC, and the time zone displacement of the source expression, INTERVAL '04:00' HOUR TO MINUTE. The result value of the CAST '04:30:00' at UTC is adjusted to its time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, and the result of the SELECT statements is: TIME '08:30:00+04:00'. The result of the SELECT statements is equal to TIME '04:30:00+00:00'. The current session time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, has no effect. SET TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE; SELECT CAST(TIMESTAMP '2008-06-01 08:30:00+04:00' AS TIME(0) WITH TIME ZONE); SELECT CAST(TIMESTAMP '2008-06-01 08:30:00+04:00' AS TIME(0) WITH TIME ZONE AT SOURCE TIME ZONE); Example 5 In this example, the current session time zone displacement, INTERVAL -'04:00' HOUR TO MINUTE, is used to determine the UTC value '2008-06-01 12:30:00' for the TIMESTAMP literal. The result of the CAST is the time formed from the time portion of the source expression value '2008-06-01 12:30:00' at UTC, and the specified time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE. The result value of the CAST '12:30:00' at UTC is adjusted to its time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, and the result of the SELECT statement is: TIME '04:30:00-08:00'. The result of the SELECT statement is equal to TIME '12:30:00+00:00'. SET TIME ZONE INTERVAL -'04:00' HOUR TO MINUTE; SELECT CAST(TIMESTAMP '2008-06-01 08:30:00' AS TIME(0) WITH TIME ZONE AT -8); Chapter 20: Data Type Conversions TIMESTAMP-to-TIME Conversion 914 SQL Functions, Operators, Expressions, and Predicates Example 6 In this example, the time zone displacement specified in the literal, INTERVAL '04:00' HOUR TO MINUTE, is used to determine the UTC value '2008-06-01 04:30:00' for the TIMESTAMP literal. The result of the CAST is the time formed from the time portion of the source expression value '2008-06-01 04:30:00' at UTC, and the specified time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE. The result value of the CAST '04:30:00' at UTC is adjusted to its time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, and the result of the SELECT statement is: TIME '20:30:00-08:00'. This result of the SELECT statement is equal to TIME '04:30:00+00:00'. The current session time zone displacement, INTERVAL '08:00' HOUR TO MINUTE, has no effect. SET TIME ZONE INTERVAL '08:00' HOUR TO MINUTE; SELECT CAST(TIMESTAMP '2008-06-01 08:30:00+04:00' AS TIME(0) WITH TIME ZONE AT -8); Example 7 In this example, the current timestamp is: Current TimeStamp(6) -------------------------------- 2010-03-09 19:23:27.620000+00:00 The following statement converts the TIMESTAMP value '2010-03-09 08:30:00' to a TIME WITH TIME ZONE value, where the time zone displacement is based on the time zone string, 'America Pacific'. SELECT CAST(TIMESTAMP '2010-03-09 08:30:00' AS TIME(0) WITH TIME ZONE AT 'America Pacific'); The result of the query is: 2010-03-09 08:30:00 ------------------- 00:30:00-08:00 Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions TIMESTAMP-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 915 TIMESTAMP-to-TIMESTAMP Conversion Purpose Convert TIMESTAMP data to a TIMESTAMP value with different precision information or WITH TIME ZONE definition. CAST Syntax where: Syntax element … Specifies … timestamp_expression the TIMESTAMP expression to be converted. fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. AT LOCAL that the time zone displacement based on the current session time zone is used. 1101B272 timestamp_expression (fractional_seconds_precision) TIMESTAMP CAST ( AS A WITH TIME ZONE A expression time_zone_string AT LOCAL SOURCE TIME ZONE TIME ZONE B B ) data_attribute Chapter 20: Data Type Conversions TIMESTAMP-to-TIMESTAMP Conversion 916 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of the FORMAT phrase to enable alternative output formatting for the character representations of DateTime and Interval data. The AT clause is ANSI SQL:2008 compliant. As an extension to ANSI, the AT clause is supported when using CAST to convert from TIMESTAMP to TIMESTAMP. In addition, you can specify the time zone displacement using additional expressions besides an INTERVAL expression. Note: TIMESTAMP (without time zone) is not ANSI SQL:2008 compliant. Teradata Database internally converts a TIMESTAMP value to UTC based on the current session time zone or on a specified time zone. AT SOURCE [TIME ZONE] that the time zone associated with timestamp_expression is used in the following cases: • AT SOURCE TIME ZONE is specified. • AT SOURCE is specified without TIME ZONE and there is no column named source in the scope. Otherwise, if AT SOURCE is specified without TIME ZONE and a column named source exists, then SOURCE references this column, and the value of the column is used as the time zone displacement for the CAST. If needed, the column value is implicitly converted to type INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. If there are multiple columns named source in the scope, an error is returned. AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement used for the CAST. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE Syntax element … Specifies … Chapter 20: Data Type Conversions TIMESTAMP-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 917 Teradata Conversion Syntax where: Syntax element … Specifies … timestamp_expression the TIMESTAMP expression to be converted. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. AT LOCAL that the time zone displacement based on the current session time zone is used. 1101C280 timestamp_expression (fractional_seconds_precision) ( TIMESTAMP A data_attribute , expression time_zone_string , WITH TIME ZONE AT LOCAL SOURCE TIME ZONE TIME ZONE A B ) , data_attribute B Chapter 20: Data Type Conversions TIMESTAMP-to-TIMESTAMP Conversion 918 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance Teradata Conversion Syntax is a Teradata extension to the ANSI SQL:2008 standard. The AT clause is ANSI SQL:2008 compliant. As an extension to ANSI, the AT clause is supported when using Teradata Conversion Syntax to convert from TIMESTAMP to TIMESTAMP. In addition, you can specify the time zone displacement using additional expressions besides an INTERVAL expression. Note: TIMESTAMP (without time zone) is not ANSI SQL:2008 compliant. Teradata Database internally converts a TIMESTAMP value to UTC based on the current session time zone or on a specified time zone. Usage Notes If you specify an AT clause for a TIMESTAMP[(n)] without time zone target data type, an error is returned. If you specify an AT clause for a TIMESTAMP[(n)] WITH TIME ZONE target data type, the following table shows the result of the CAST function or Teradata conversion based on the various options specified. If the target precision is higher than the source precision, trailing zeros are added in the result to adjust the precision. If the target precision is lower than the source precision, an error is returned. AT SOURCE [TIME ZONE] that the time zone associated with timestamp_expression is used in the following cases: • AT SOURCE TIME ZONE is specified. • AT SOURCE is specified without TIME ZONE and there is no column named source in the scope. Otherwise, if AT SOURCE is specified without TIME ZONE and a column named source exists, then SOURCE references this column, and the value of the column is used as the time zone displacement in the conversion. If needed, the column value is implicitly converted to type INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. If there are multiple columns named source in the scope, an error is returned. AT [TIME ZONE] expression that the time zone displacement defined by expression is used. The data type of expression should be INTERVAL HOUR(2) TO MINUTE or it must be a data type that can be implicitly converted to INTERVAL HOUR(2) TO MINUTE. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. AT [TIME ZONE] time_zone_string that time_zone_string is used to determine the time zone displacement used in the conversion. For details, see “AT LOCAL and AT TIME ZONE Time Zone Specifiers” on page 215. Syntax element … Specifies … Chapter 20: Data Type Conversions TIMESTAMP-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 919 Example 1 The following SELECT statements return an error because the target data type does not have a TIMESTAMP WITH TIME ZONE data type. SELECT CAST(TIMESTAMP '2008-06-01 08:30:00' AS TIMESTAMP(0) AT LOCAL); SELECT CAST(TIMESTAMP '2008-06-01 08:30:00+01:00' AS TIMESTAMP(0) AT LOCAL); SELECT CAST(TIMESTAMP '2008-06-01 08:30:00' AS TIMESTAMP(0) AT SOURCE TIME ZONE); SELECT CAST(TIMESTAMP '2008-06-01 08:30:00+01:00' AS TIMESTAMP(0) IF you specify... AND the data type of timestamp_expression is... THEN... AT LOCAL with or without TIME ZONE the result is formed from the timestamp portion of the source timestamp_expression (in UTC) with the result time zone displacement based on the current session time zone. If the source data type is without time zone, this is the same as not specifying the AT clause. AT SOURCE (where SOURCE is a keyword and not a column reference) WITH TIME ZONE the result is formed from the timestamp portion of the source timestamp_expression (in UTC) and the time zone displacement associated with timestamp_expression. Note that this is the same as not specifying the AT clause. AT SOURCE (where SOURCE is a keyword and not a column reference) without TIME ZONE an error is returned. AT SOURCE TIME ZONE WITH TIME ZONE the result is formed from the timestamp portion of the source timestamp_expression (in UTC) and the time zone displacement associated with timestamp_expression. Note that this is the same as not specifying the AT clause. AT SOURCE TIME ZONE without TIME ZONE an error is returned. AT expression or AT TIME ZONE expression with or without TIME ZONE the result is formed from the timestamp portion of the source timestamp_expression (in UTC) and the time zone displacement defined by expression. AT time_zone_string or AT TIME ZONE time_zone_string with or without TIME ZONE the result is formed from the timestamp portion of the source timestamp_expression (in UTC) and the time zone displacement based on time_zone_string. The time zone displacement is determined based on time_zone_string and the TIMESTAMP value of timestamp_expression at UTC. Chapter 20: Data Type Conversions TIMESTAMP-to-TIMESTAMP Conversion 920 SQL Functions, Operators, Expressions, and Predicates AT SOURCE); SELECT CAST(TIMESTAMP '2008-06-01 08:30:00' AS TIMESTAMP(0) AT +3); SELECT CAST(TIMESTAMP '2008-06-01 08:30:00+01:00' AS TIMESTAMP(0) AT -6); Example 2 In this example, the time zone displacement specified in the literal, INTERVAL '04:00' HOUR TO MINUTE, is used to determine the UTC value '2008-06-01 04:30:00' and time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, of the literal. The CAST result is the source expression value '2008-06-01 04:30:00' at UTC with the current session time zone displacement, INTERVAL '09:00' HOUR TO MINUTE. The result value of the CAST at UTC is adjusted to time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, and the result of the SELECT statement is: TIMESTAMP '2008-06-01 13:30:00+09:00'. SET TIME ZONE INTERVAL '09:00' HOUR TO MINUTE; SELECT CAST(TIMESTAMP '2008-06-01 08:30:00+04:00' AS TIMESTAMP(0) WITH TIME ZONE AT LOCAL); Example 3 The following SELECT statements return an error because the source expression does not have a time zone displacement. SELECT CAST(TIMESTAMP '2008-06-01 08:30:00' AS TIMESTAMP(0) WITH TIME ZONE AT SOURCE TIME ZONE); SELECT CAST(TIMESTAMP '2008-06-01 08:30:00' AS TIMESTAMP(0) WITH TIME ZONE AT SOURCE); Example 4 In this example, the time zone displacement specified in the literal, INTERVAL '04:00' HOUR TO MINUTE, is used to determine the UTC value '2008-06-01 04:30:00' and time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, of the literal. The CAST result is source expression value '2008-06-01 04:30:00' at UTC with its time zone displacement, INTERVAL '04:00' HOUR TO MINUTE. The result value of the CAST at UTC is adjusted to time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, and the result of the SELECT is: TIMESTAMP '2008-06-01 08:30:00+04:00'. The current session time zone has no effect. SET TIME ZONE INTERVAL '09:00' HOUR TO MINUTE; SELECT CAST(TIMESTAMP '2008-06-01 08:30:00+04:00' AS TIMESTAMP(0) WITH TIME ZONE); SELECT CAST(TIMESTAMP '2008-06-01 08:30:00+04:00' AS TIMESTAMP(0) WITH TIME ZONE AT SOURCE TIME ZONE); Chapter 20: Data Type Conversions TIMESTAMP-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 921 Example 5 In this example, the current session time zone displacement, INTERVAL '09:00' HOUR TO MINUTE, is used to determine the UTC value '2008-05-31 23:30:00' of the literal. The CAST result is the source expression value '2008-05-31 23:30:00' at UTC with the target time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE. The result value of the CAST at UTC is adjusted to time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, and the result of the SELECT statement is: TIMESTAMP '2008- 05-31 15:30:00-08:00'. SET TIME ZONE INTERVAL '09:00' HOUR TO MINUTE; SELECT CAST(TIMESTAMP '2008-06-01 08:30:00' AS TIMESTAMP(0) WITH TIME ZONE AT -8); Example 6 In this example, the time zone displacement specified in the literal, INTERVAL '04:00' HOUR TO MINUTE, is used to determine the UTC value '2008-06-01 04:30:00' and time zone displacement, INTERVAL '04:00' HOUR TO MINUTE, of the literal. The CAST result is the source expression value '2008-06-01 04:30:00' at UTC with the target time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE. The result value of the CAST at UTC is adjusted to time zone displacement, INTERVAL -'08:00' HOUR TO MINUTE, and the result of the SELECT statement is: TIMESTAMP '2008- 05-31 20:30:00-08:00'. The current session time zone has no effect. SET TIME ZONE INTERVAL '09:00' HOUR TO MINUTE; SELECT CAST(TIMESTAMP '2008-06-01 08:30:00+04:00' AS TIMESTAMP(0) WITH TIME ZONE AT -8); Example 7 In this example, the current timestamp is: Current TimeStamp(6) -------------------------------- 2010-03-09 19:23:27.620000+00:00 The following statement converts the TIMESTAMP value '2010-03-09 08:30:00' to a TIMESTAMP WITH TIME ZONE value, where the time zone displacement is based on the time zone string, 'America Pacific'. SELECT CAST(TIMESTAMP '2010-03-09 08:30:00' AS TIMESTAMP(0) WITH TIME ZONE AT 'America Pacific'); The result of the query is: 2010-03-09 08:30:00 ------------------------- 2010-03-09 00:30:00-08:00 Chapter 20: Data Type Conversions TIMESTAMP-to-TIMESTAMP Conversion 922 SQL Functions, Operators, Expressions, and Predicates Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions TIMESTAMP-to-UDT Conversion SQL Functions, Operators, Expressions, and Predicates 923 TIMESTAMP-to-UDT Conversion Purpose Converts TIMESTAMP data to UDT data. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases such as FORMAT. Usage Notes Explicit TIMESTAMP-to-UDT conversion using Teradata conversion syntax is not supported. Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. Implicit TIMESTAMP-to-UDT Conversion Teradata Database performs implicit TIMESTAMP-to-UDT conversions for the following operations: • UPDATE • INSERT • Passing arguments to stored procedures, external stored procedures, UDFs, and UDMs • Specific system operators and functions identified in other sections of this book, unless the DisableUDTImplCastForSysFuncOp field of the DBS Control Record is set to TRUE Syntax element … Specifies … timestamp_expression a TIMESTAMP expression to be cast to a UDT. UDT_data_definition the UDT type, followed by any optional FORMAT, NAMED, or TITLE data attribute phrases, to which timestamp_expression is to be converted. CAST AS timestamp_expression UDT_data_definition ( ( 1101A341 Chapter 20: Data Type Conversions TIMESTAMP-to-UDT Conversion 924 SQL Functions, Operators, Expressions, and Predicates Performing an implicit data type conversion requires that an appropriate cast definition (see “Usage Notes”) exists that specifies the AS ASSIGNMENT clause. If no TIMESTAMP-to-UDT implicit cast definition exists, Teradata Database looks for a CHAR-to-UDT or VARCHAR-to-UDT implicit cast definition that can substitute. Substitutions are valid because Teradata Database can implicitly cast a TIMESTAMP type to the character data type, and then use the implicit cast definition to cast from the character data type to the UDT. If multiple character-to-UDT implicit cast definitions exist, then Teradata Database returns an SQL error. Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions UDT-to-Byte Conversion SQL Functions, Operators, Expressions, and Predicates 925 UDT-to-Byte Conversion Purpose Converts a UDT expression to a byte data type. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases such as FORMAT. Teradata Conversion Syntax where: Syntax element … Specifies … UDT_expression an expression that results in a UDT data type. For details on expressions that can result in UDT data types, see “SQL UDF” on page 706. byte_data_definition the BLOB, BYTE or VARBYTE byte type followed by optional FORMAT, NAMED, or TITLE attribute phrases to which UDT_expression is to be converted. 1101A344 CAST ( UDT_expression AS byte_data_definition ) Syntax element … Specifies … UDT_expression an expression that results in a UDT data type. For details on expressions that can result in UDT data types, see “SQL UDF” on page 706. 1101B345 UDT_expression data_attribute , ( byte_data_type ) , data_attribute Chapter 20: Data Type Conversions UDT-to-Byte Conversion 926 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Usage Notes Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. Implicit Type Conversion Teradata Database performs implicit UDT-to-byte conversions for the following operations: • UPDATE • INSERT • Passing arguments to stored procedures, external stored procedures, UDFs, and UDMs • Specific system operators and functions identified in other sections of this book, unless the DisableUDTImplCastForSysFuncOp field of the DBS Control Record is set to TRUE Performing an implicit UDT-to-byte data type conversion requires a cast definition (see “Usage Notes”) that specifies the following: • the AS ASSIGNMENT clause • a BYTE, VARBYTE, or BLOB target data type The target data type of the cast definition does not have to be an exact match to the target of the implicit type conversion. If multiple implicit cast definitions exist for converting the UDT to different byte types, Teradata Database uses the implicit cast definition for the byte type with the highest precedence. The following list shows the precedence of byte types in order from lowest to highest precedence: • BYTE • VARBYTE • BLOB data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE byte_data_type the BLOB, BYTE or VARBYTE byte type to which UDT_expression is to be converted. Syntax element … Specifies … Chapter 20: Data Type Conversions UDT-to-Byte Conversion SQL Functions, Operators, Expressions, and Predicates 927 Example Consider the following table definition, where image is a UDT: CREATE TABLE history (id INTEGER ,information image ); Assuming an appropriate cast definition exists for the image UDT, the following statement converts the values in the information column to BYTE: SELECT CAST (information AS BYTE(20)) FROM history WHERE id = 100121; Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions UDT-to-Character Conversion 928 SQL Functions, Operators, Expressions, and Predicates UDT-to-Character Conversion Purpose Converts a UDT expression to a character data type. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases such as FORMAT. Teradata Conversion Syntax where: Syntax element … Specifies … UDT_expression an expression that results in a UDT data type. For details on expressions that can result in UDT data types, see “SQL UDF” on page 706. character_data_definition the target character type, for example CHAR or VARCHAR, followed by optional FORMAT, NAMED, or TITLE attribute phrases. 1101A346 CAST ( UDT_expression AS character_data_definition ) Syntax element … Specifies … UDT_expression an expression that results in a UDT data type. For details on expressions that can result in UDT data types, see “SQL UDF” on page 706. 1101B347 UDT_expression data_attribute , ( character_data_type ) , data_attribute Chapter 20: Data Type Conversions UDT-to-Character Conversion SQL Functions, Operators, Expressions, and Predicates 929 ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Usage Notes Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. Implicit Type Conversion Teradata Database performs implicit UDT-to-character conversions for the following operations: • UPDATE • INSERT • Passing arguments to stored procedures, external stored procedures, UDFs, and UDMs • Specific system operators and functions identified in other sections of this book, unless the DisableUDTImplCastForSysFuncOp field of the DBS Control Record is set to TRUE Performing an implicit data type conversion requires that an appropriate cast definition (see “Usage Notes”) exists that specifies the AS ASSIGNMENT clause. The target character type of the cast definition does not have to be an exact match to the target character type of the implicit conversion. Teradata Database can use an implicit cast definition that specifies a CHAR, VARCHAR, or CLOB target type. If multiple implicit cast definitions exist for converting the UDT to different character types, Teradata Database uses the implicit cast definition for the character type with the highest precedence. The following list shows the precedence of character types in order from lowest to highest precedence: • CHAR • VARCHAR • CLOB If no UDT-to-character implicit cast definitions exist, Teradata Database looks for other cast definitions that can substitute for the UDT-to-character implicit cast definition: data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE character_data_type the target character type, for example CHAR or VARCHAR. Syntax element … Specifies … Chapter 20: Data Type Conversions UDT-to-Character Conversion 930 SQL Functions, Operators, Expressions, and Predicates Substitutions are valid because Teradata Database can use the implicit cast definition to cast the UDT to the substitute data type, and then implicitly cast the substitute data type to a character type. Example Consider the following table definition, where euro is a UDT: CREATE TABLE euro_sales_table (quarter INTEGER ,region VARCHAR(20) ,sales euro ); Assuming an appropriate cast definition exists for the euro UDT, the following statement converts the values in the sales column to CHAR(10): IF the following combination of implicit cast definitions exists … THEN Teradata Database … UDT-tonumeric UDT-to- DATE UDT-to- TIME UDT-to- TIMESTAMP X uses the UDT-to-numeric implicit cast definition. If multiple UDT-to-numeric implicit cast definitions exist, then Teradata Database returns an SQL error. X uses the UDT-to-DATE implicit cast definition. X uses the UDT-to-TIME implicit cast definition. X uses the UDT-to-TIMESTAMP implicit cast definition. X X reports an error. X X X X X X X X X X X X X X X X X X X X X X X X X X Chapter 20: Data Type Conversions UDT-to-Character Conversion SQL Functions, Operators, Expressions, and Predicates 931 SELECT region, CAST (sales AS CHAR(10)) FROM euro_sales_table WHERE quarter = 1; Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions UDT-to-DATE Conversion 932 SQL Functions, Operators, Expressions, and Predicates UDT-to-DATE Conversion Purpose Converts a UDT expression to a DATE data type. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases such as FORMAT. Teradata Conversion Syntax where: Syntax element … Specifies … UDT_expression an expression that results in a UDT data type. For details on expressions that can result in UDT data types, see “SQL UDF” on page 706. date_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE 1101B348 CAST ( UDT_expression AS DATE ) date_data_attribute Syntax element … Specifies … UDT_expression an expression that results in a UDT data type. For details on expressions that can result in UDT data types, see “SQL UDF” on page 706. 1101B349 UDT_expression data_attribute , ( DATE ) , data_attribute Chapter 20: Data Type Conversions UDT-to-DATE Conversion SQL Functions, Operators, Expressions, and Predicates 933 ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Usage Notes Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. Implicit Type Conversion Performing an implicit data type conversion requires that an appropriate cast definition (see “Usage Notes”) exists that specifies the AS ASSIGNMENT clause. Teradata Database performs implicit UDT-to-DATE conversions for the following operations: • UPDATE • INSERT • Passing arguments to stored procedures, external stored procedures, UDFs, and UDMs • Specific system operators and functions identified in other sections of this book, unless the DisableUDTImplCastForSysFuncOp field of the DBS Control Record is set to TRUE If no UDT-to-DATE implicit cast definition exists, Teradata Database looks for other cast definitions that can substitute for the UDT-to-DATE implicit cast definition: data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE Syntax element … Specifies … IF the following combination of implicit cast definitions exists … THEN Teradata Database … UDT-to-Numeric UDT-to-Character (non-CLOB) X uses the UDT-to-numeric implicit cast definition. If multiple UDT-to-numeric implicit cast definitions exist, then Teradata Database returns an SQL error. X uses the UDT-to-character implicit cast definition. If multiple UDT-to-character implicit cast definitions exist, then Teradata Database returns an SQL error. X X reports an error. Chapter 20: Data Type Conversions UDT-to-DATE Conversion 934 SQL Functions, Operators, Expressions, and Predicates Substitutions are valid because Teradata Database can use the implicit cast definition to cast the UDT to the substitute data type, and then implicitly cast the substitute data type to a DATE type. Example Consider the following table definition, where datetime_record is a UDT: CREATE TABLE support (id INTEGER ,information datetime_record ); Assuming an appropriate cast definition exists for the datetime_record UDT, the following statement converts the values in the information column to DATE: SELECT id, CAST (information AS DATE) FROM support; Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions UDT-to-INTERVAL Conversion SQL Functions, Operators, Expressions, and Predicates 935 UDT-to-INTERVAL Conversion Purpose Converts a UDT expression to an INTERVAL data type. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases such as FORMAT. Teradata Conversion Syntax where: Syntax element … Specifies … UDT_expression an expression that results in a UDT data type. For details on expressions that can result in UDT data types, see “SQL UDF” on page 706. interval_data_definition the target predefined interval type followed by optional NAMED or TITLE attribute phrases. 1101A350 CAST ( UDT_expression AS interval_data_definition ) Syntax element … Specifies … UDT_expression an expression that results in a UDT data type. For details on expressions that can result in UDT data types, see “SQL UDF” on page 706. 1101B351 UDT_expression data_attribute , ( interval_data_type ) , data_attribute Chapter 20: Data Type Conversions UDT-to-INTERVAL Conversion 936 SQL Functions, Operators, Expressions, and Predicates ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Usage Notes Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. Implicit Type Conversion Performing an implicit data type conversion requires a cast definition (see “Usage Notes”) that specifies the following: • the AS ASSIGNMENT clause • a target data type that is in the same INTERVAL family as the target of the implicit cast: The target data type of the cast definition does not have to be an exact match to the target of the implicit type conversion. data_attribute one of the following optional data attributes: • NAMED • TITLE interval_data_type the target predefined interval type to which UDT_expression is to be converted. Syntax element … Specifies … This INTERVAL data type … Belongs to this INTERVAL family … • INTERVAL YEAR • INTERVAL YEAR TO MONTH • INTERVAL MONTH Year-Month • INTERVAL DAY • INTERVAL DAY TO HOUR • INTERVAL DAY TO MINUTE • INTERVAL DAY TO SECOND • INTERVAL HOUR • INTERVAL HOUR TO MINUTE • INTERVAL HOUR TO SECOND • INTERVAL MINUTE • INTERVAL MINUTE TO SECOND • INTERVAL SECOND Day-Time Chapter 20: Data Type Conversions UDT-to-INTERVAL Conversion SQL Functions, Operators, Expressions, and Predicates 937 Teradata Database performs implicit UDT-to-INTERVAL conversions for the following operations: • UPDATE • INSERT • Passing arguments to stored procedures, external stored procedures, UDFs, and UDMs • Specific system operators and functions identified in other sections of this book, unless the DisableUDTImplCastForSysFuncOp field of the DBS Control Record is set to TRUE Example Consider the following table definition, where datetime_record is a UDT: CREATE TABLE support (id INTEGER ,information datetime_record ); Assuming an appropriate cast definition exists for the datetime_record UDT, the following statement converts the values in the information column to INTERVAL MONTH: SELECT id, CAST (information AS INTERVAL MONTH) FROM support; Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions UDT-to-Numeric Conversion 938 SQL Functions, Operators, Expressions, and Predicates UDT-to-Numeric Conversion Purpose Converts a UDT expression to a numeric data type. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases such as FORMAT. Teradata Conversion Syntax where: Syntax element … Specifies … UDT_expression an expression that results in a UDT data type. For details on expressions that can result in UDT data types, see “SQL UDF” on page 706. numeric_data_definition the target predefined numeric type followed by any optional FORMAT, NAMED, or TITLE attribute phrases. 1101A352 CAST ( UDT_expression AS numeric_data_definition ) Syntax element … Specifies … UDT_expression an expression that results in a UDT data type. For details on expressions that can result in UDT data types, see “SQL UDF” on page 706. 1101B353 UDT_expression data_attribute , ( numeric_data_type ) , data_attribute Chapter 20: Data Type Conversions UDT-to-Numeric Conversion SQL Functions, Operators, Expressions, and Predicates 939 ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Usage Notes Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. Implicit Type Conversion Teradata Database performs implicit UDT-to-numeric conversions for the following operations: • UPDATE • INSERT • Passing arguments to stored procedures, external stored procedures, UDFs, and UDMs • Specific system operators and functions identified in other sections of this book, unless the DisableUDTImplCastForSysFuncOp field of the DBS Control Record is set to TRUE Performing an implicit data type conversion requires that an appropriate cast definition (see “Usage Notes” on page 929) exists that specifies the AS ASSIGNMENT clause. The target numeric type of the cast definition does not have to be an exact match to the target numeric type of the implicit conversion. Teradata Database can use an implicit cast definition that specifies a BYTEINT, SMALLINT, INTEGER, BIGINT, DECIMAL/NUMERIC, or REAL/ FLOAT/DOUBLE target type. If multiple implicit cast definitions exist for converting the UDT to different numeric types, Teradata Database uses the implicit cast definition for the numeric type with the highest precedence. The following list shows the precedence of numeric types in order from lowest to highest precedence: • BYTEINT • SMALLINT • INTEGER • BIGINT data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE numeric_data_type a predefined numeric type to which UDT_expression is to be converted. Syntax element … Specifies … Chapter 20: Data Type Conversions UDT-to-Numeric Conversion 940 SQL Functions, Operators, Expressions, and Predicates • DECIMAL/NUMERIC • REAL/FLOAT/DOUBLE If no UDT-to-numeric implicit cast definitions exist, Teradata Database looks for other cast definitions that can substitute for the UDT-to-character implicit cast definition: Substitutions are valid because Teradata Database can use the implicit cast definition to cast the UDT to the substitute data type, and then implicitly cast the substitute data type to a numeric type. Example Consider the following table definition, where euro is a UDT: CREATE TABLE euro_sales_table (quarter INTEGER ,region VARCHAR(20) ,sales euro ); Assuming an appropriate cast definition exists for the euro UDT, the following statement converts the values in the sales column to DECIMAL(10,2): SELECT SUM (CAST (sales AS DECIMAL(10,2))) FROM euro_sales_table; Related Topics For details on data types and data attributes, see SQL Data Types and Literals. IF the following combination of implicit cast definitions exists … THEN Teradata Database … UDT-to- DATE UDT-to- Charactera a. a non-CLOB character type X uses the UDT-to-DATE implicit cast definition. X uses the UDT-to-character implicit cast definition. If multiple UDT-to-character implicit cast definitions exist, then Teradata Database returns an SQL error. X X reports an error. Chapter 20: Data Type Conversions UDT-to-TIME Conversion SQL Functions, Operators, Expressions, and Predicates 941 UDT-to-TIME Conversion Purpose Converts a UDT expression to a TIME data type. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases such as FORMAT. Syntax element … Specifies … UDT_expression an expression that results in a UDT data type. For details on expressions that can result in UDT data types, see “SQL UDF” on page 706. fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. time_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE 1101A354 CAST AS TIME (fractional_seconds_precision) ( UDT_expression ) A WITH TIME ZONE time_data_attribute A Chapter 20: Data Type Conversions UDT-to-TIME Conversion 942 SQL Functions, Operators, Expressions, and Predicates Teradata Conversion Syntax where: ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Usage Notes Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. Implicit Type Conversion Teradata Database performs implicit UDT-to-TIME conversions for the following operations: • UPDATE • INSERT • Passing arguments to stored procedures, external stored procedures, UDFs, and UDMs • Specific system operators and functions identified in other sections of this book, unless the DisableUDTImplCastForSysFuncOp field of the DBS Control Record is set to TRUE Syntax element … Specifies … UDT_expression an expression that results in a UDT data type. For details on expressions that can result in UDT data types, see “SQL UDF” on page 706. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. 1101B342 UDT_expression (fractional_seconds_precision) TIME ) ( A A data_attribute , WITH TIME ZONE , data_attribute Chapter 20: Data Type Conversions UDT-to-TIME Conversion SQL Functions, Operators, Expressions, and Predicates 943 Performing an implicit data type conversion requires that an appropriate cast definition (see “Usage Notes”) exists that specifies the AS ASSIGNMENT clause. If no UDT-to-TIME implicit cast definition exists, Teradata Database looks for a UDT-to- CHAR or UDT-to-VARCHAR cast definition that can substitute for the UDT-to-TIME implicit cast definition. Substitutions are valid because Teradata Database can use the implicit cast definition to cast the UDT to a character data type, and then implicitly cast the character data type to a DATE type. If multiple UDT-to-character implicit cast definitions exist, then Teradata Database returns an SQL error. Example Consider the following table definition, where datetime_record is a UDT: CREATE TABLE support (id INTEGER ,information datetime_record ); Assuming an appropriate cast definition exists for the datetime_record UDT, the following statement converts the values in the information column to TIME WITH TIME ZONE: SELECT id, CAST (information AS TIME WITH TIME ZONE) FROM support; Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions UDT-to-TIMESTAMP Conversion 944 SQL Functions, Operators, Expressions, and Predicates UDT-to-TIMESTAMP Conversion Purpose Converts a UDT expression to a TIMESTAMP data type. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases such as FORMAT. Syntax element … Specifies … UDT_expression an expression that results in a UDT data type. For details on expressions that can result in UDT data types, see “SQL UDF” on page 706. fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. timestamp_data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE 1101A355 CAST AS TIMESTAMP (fractional_seconds_precision) ( UDT_expression ) A WITH TIME ZONE timestamp_data_attribute A Chapter 20: Data Type Conversions UDT-to-TIMESTAMP Conversion SQL Functions, Operators, Expressions, and Predicates 945 Teradata Conversion Syntax where: ANSI Compliance Teradata conversion syntax is a Teradata extension to the ANSI SQL:2008 standard. Usage Notes Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. Implicit Type Conversion Teradata Database performs implicit UDT-to-TIMESTAMP conversions for the following operations: • UPDATE • INSERT • Passing arguments to stored procedures, external stored procedures, UDFs, and UDMs Syntax element … Specifies … UDT_expression an expression that results in a UDT data type. For details on expressions that can result in UDT data types, see “SQL UDF” on page 706. data_attribute one of the following optional data attributes: • FORMAT • NAMED • TITLE fractional_seconds_precision a single digit representing the number of significant digits in the fractional portion of the SECOND field. Values for fractional_seconds_precision range from 0 through 6 inclusive. The default precision is 6. 1101B343 UDT_expression (fractional_seconds_precision) TIMESTAMP ) ( A A data_attribute , WITH TIME ZONE , data_attribute Chapter 20: Data Type Conversions UDT-to-TIMESTAMP Conversion 946 SQL Functions, Operators, Expressions, and Predicates • Specific system operators and functions identified in other sections of this book, unless the DisableUDTImplCastForSysFuncOp field of the DBS Control Record is set to TRUE Performing an implicit data type conversion requires that an appropriate cast definition (see “Usage Notes”) exists that specifies the AS ASSIGNMENT clause. If no UDT-to-TIMESTAMP implicit cast definition exists, Teradata Database looks for a UDT-to-CHAR or UDT-to-VARCHAR cast definition that can substitute for the UDT-to- TIMESTAMP implicit cast definition. Substitutions are valid because Teradata Database can use the implicit cast definition to cast the UDT to a character data type, and then implicitly cast the character data type to a TIMESTAMP type. If multiple UDT-to-character implicit cast definitions exist, then Teradata Database returns an SQL error. Example Consider the following table definition, where datetime_record is a UDT: CREATE TABLE support (id INTEGER ,information datetime_record ); Assuming an appropriate cast definition exists for the datetime_record UDT, the following statement converts the values in the information column to TIMESTAMP: SELECT id, CAST (information AS TIMESTAMP) FROM support; Related Topics For details on data types and data attributes, see SQL Data Types and Literals. Chapter 20: Data Type Conversions UDT-to-UDT Conversion SQL Functions, Operators, Expressions, and Predicates 947 UDT-to-UDT Conversion Purpose Converts a UDT expression to a different UDT type. CAST Syntax where: ANSI Compliance CAST is ANSI SQL:2008 compliant. As an extension to ANSI, CAST permits the use of data attribute phrases such as FORMAT. Usage Notes Explicit UDT-to-UDT conversion using Teradata conversion syntax is not supported. Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language. Implicit Type Conversion Teradata Database performs implicit UDT-to-UDT casts for the following operations: • UPDATE • INSERT Syntax element … Specifies … UDT_expression an expression that results in a UDT data type. For details on expressions that can result in UDT data types, see “SQL UDF” on page 706. UDT_data_definition a UDT type to which UDT_expression is to be converted, followed by any of the following optional attribute phrases: • FORMAT • NAMED • TITLE 1101A356 CAST ( UDT_expression AS UDT_data_definition ) Chapter 20: Data Type Conversions UDT-to-UDT Conversion 948 SQL Functions, Operators, Expressions, and Predicates • Passing arguments to stored procedures, external stored procedures, UDFs, and UDMs • Specific system operators and functions identified in other sections of this book, unless the DisableUDTImplCastForSysFuncOp field of the DBS Control Record is set to TRUE An implicit data type conversion involving a UDT can only be performed if the cast definition specifies the AS ASSIGNMENT clause. For more information, see “CREATE CAST” in SQL Data Definition Language. Example Consider the following table definitions, where euro and us_dollar are UDTs: CREATE TABLE euro_sales_table (euro_quarter INTEGER ,euro_region VARCHAR(20) ,euro_sales euro ); CREATE TABLE us_sales_table (us_quarter INTEGER ,us_region VARCHAR(20) ,us_sales us_dollar ); Assuming an appropriate cast definition exists for converting the euro UDT to a us_dollar UDT, the following statement performs a us_dollar UDT to euro UDT conversion: INSERT INTO euro_sales_table SELECT us_quarter, us_region, CAST (us_sales AS euro) FROM us_sales_table; Related Topics For details on data types and data attributes, see SQL Data Types and Literals. SQL Functions, Operators, Expressions, and Predicates 949 APPENDIX A Notation Conventions This appendix describes the notation conventions used in this book. This book uses three conventions to describe the SQL syntax and code: Symbols from the predicate calculus are also used occasionally to describe logical operations. For details, see “Predicate Calculus Notation Used In This Book” on page 956. Syntax Diagram Conventions Notation Conventions Convention Description Syntax diagrams Describes SQL syntax form, including options. For details, see “Syntax Diagram Conventions” on page 949. Square braces in the text Represent options. The indicated parentheses are required when you specify options. For example: • DECIMAL [(n[,m])] means the decimal data type can be defined optionally: • without specifying the precision value n or scale value m • specifying precision (n) only • specifying both values (n,m) You cannot specify scale without first defining precision. • CHARACTER [(n)] means that use of (n) is optional. The values for n and m are integers in all cases. Japanese character code shorthand notation Represent unprintable Japanese characters. For details, see “Character Shorthand Notation Used In This Book” on page 954. Item Definition / Comments Letter An uppercase or lowercase alphabetic character ranging from A through Z. Number A digit ranging from 0 through 9. Do not use commas when typing a number with more than 3 digits. Appendix A: Notation Conventions Syntax Diagram Conventions 950 SQL Functions, Operators, Expressions, and Predicates Paths The main path along the syntax diagram begins at the left with a keyword, and proceeds, left to right, to the vertical bar, which marks the end of the diagram. Paths that do not have an arrow or a vertical bar only show portions of the syntax. The only part of a path that reads from right to left is a loop. Continuation Links Paths that are too long for one line use continuation links. Continuation links are circled letters indicating the beginning and end of a link: When you see a circled letter in a syntax diagram, go to the corresponding circled letter and continue reading. Word Keywords and variables. • UPPERCASE LETTERS represent a keyword. Syntax diagrams show all keywords in uppercase, unless operating system restrictions require them to be in lowercase. • lowercase letters represent a keyword that you must type in lowercase, such as a Linux command. • lowercase italic letters represent a variable such as a column or table name. Substitute the variable with a proper value. • lowercase bold letters represent an excerpt from the diagram. The excerpt is defined immediately following the diagram that contains it. • UNDERLINED LETTERS represent the default value. This applies to both uppercase and lowercase words. Spaces Use one space between items such as keywords or variables. Punctuation Type all punctuation exactly as it appears in the diagram. Item Definition / Comments FE0CA002 A A Appendix A: Notation Conventions Syntax Diagram Conventions SQL Functions, Operators, Expressions, and Predicates 951 Required Entries Required entries appear on the main path: If you can choose from more than one entry, the choices appear vertically, in a stack. The first entry appears on the main path: Optional Entries You may choose to include or disregard optional entries. Optional entries appear below the main path: If you can optionally choose from more than one entry, all the choices appear below the main path: Some commands and statements treat one of the optional choices as a default value. This value is UNDERLINED. It is presumed to be selected if you type the command or statement without specifying one of the options. Strings String literals appear in apostrophes: FE0CA003 SHOW FE0CA005 SHOW VERSIONS CONTROLS FE0CA004 SHOW CONTROLS JC01A010 SHARE READ ACCESS JC01A004 'msgtext ' Appendix A: Notation Conventions Syntax Diagram Conventions 952 SQL Functions, Operators, Expressions, and Predicates Abbreviations If a keyword or a reserved word has a valid abbreviation, the unabbreviated form always appears on the main path. The shortest valid abbreviation appears beneath. In the above syntax, the following formats are valid: • SHOW CONTROLS • SHOW CONTROL Loops A loop is an entry or a group of entries that you can repeat one or more times. Syntax diagrams show loops as a return path above the main path, over the item or items that you can repeat: Read loops from right to left. The following conventions apply to loops: FE0CA042 SHOW CONTROL CONTROLS IF... THEN... there is a maximum number of entries allowed the number appears in a circle on the return path. In the example, you may type cname a maximum of 4 times. there is a minimum number of entries required the number appears in a square on the return path. In the example, you must type at least three groups of column names. a separator character is required between entries the character appears on the return path. If the diagram does not show a separator character, use one blank space. In the example, the separator character is a comma. JC01B012 ( , 4 cname ) , 3 Appendix A: Notation Conventions Syntax Diagram Conventions SQL Functions, Operators, Expressions, and Predicates 953 Excerpts Sometimes a piece of a syntax phrase is too large to fit into the diagram. Such a phrase is indicated by a break in the path, marked by (|) terminators on each side of the break. The name for the excerpted piece appears between the terminators in boldface type. The boldface excerpt name and the excerpted phrase appears immediately after the main diagram. The excerpted phrase starts and ends with a plain horizontal line: Multiple Legitimate Phrases In a syntax diagram, it is possible for any number of phrases to be legitimate: In this example, any of the following phrases are legitimate: • dbname • DATABASE dbname • tname a delimiter character is required around entries the beginning and end characters appear outside the return path. Generally, a space is not needed between delimiter characters and entries. In the example, the delimiter characters are the left and right parentheses. IF... THEN... LOCKING excerpt where_cond A cname excerpt JC01A014 A HAVING con , col_pos , JC01A016 DATABASE dbname TABLE tname VIEW vname Appendix A: Notation Conventions Character Shorthand Notation Used In This Book 954 SQL Functions, Operators, Expressions, and Predicates • TABLE tname • vname • VIEW vname Sample Syntax Diagram Diagram Identifier The alphanumeric string that appears in the lower right corner of every diagram is an internal identifier used to catalog the diagram. The text never refers to this string. Character Shorthand Notation Used In This Book Introduction This book uses the Unicode naming convention for characters. For example, the lowercase character ‘a’ is more formally specified as either LATIN SMALL LETTER A or U+0041. The U+xxxx notation refers to a particular code point in the Unicode standard, where xxxx stands for the hexadecimal representation of the 16-bit value defined in the standard. JC01A018 CREATE VIEW viewname AS cname A C CV , LOCKING LOCK A ACCESS DATABASE dbname TABLE tname VIEW vname FOR IN B SHARE READ WRITE EXCLUSIVE EXCL MODE B SEL FROM C .aname expr , tname , qual_cond qual_cond WHERE cond cname , col_pos , GROUP BY HAVING cond ; Appendix A: Notation Conventions Character Shorthand Notation Used In This Book SQL Functions, Operators, Expressions, and Predicates 955 In parts of the book, it is convenient to use a symbol to represent a special character, or a particular class of characters. This is particularly true in discussion of the following Japanese character encodings. • KanjiEBCDIC • KanjiEUC • KanjiShift-JIS These encodings are further defined in International Character Set Support. Character Symbols The symbols, along with character sets with which they are used, are defined in the following table. Symbol Encoding Meaning a–z A–Z 0–9 Any Any single byte Latin letter or digit. a–z A–Z 0–9 Unicode compatibility zone Any fullwidth Latin letter or digit. < KanjiEBCDIC Shift Out [SO] (0x0E). Indicates transition from single to multibyte character in KanjiEBCDIC. > KanjiEBCDIC Shift In [SI] (0x0F). Indicates transition from multibyte to single byte KanjiEBCDIC. T Any Any multibyte character. The encoding depends on the current character set. For KanjiEUC, code set 3 characters are sometimes preceded by “ss3”. I Any Any single byte Hankaku Katakana character. In KanjiEUC, it must be preceded by “ss2”, forming an individual multibyte character. ? Any Represents the graphic pad character. ? Any Represents a single or multibyte pad character, depending on context. ss2 KanjiEUC Represents the EUC code set 2 introducer (0x8E). ss3 KanjiEUC Represents the EUC code set 3 introducer (0x8F). Appendix A: Notation Conventions Predicate Calculus Notation Used In This Book 956 SQL Functions, Operators, Expressions, and Predicates For example, string “TEST”, where each letter is intended to be a fullwidth character, is written as TEST. Occasionally, when encoding is important, hexadecimal representation is used. For example, the following mixed single byte/multibyte character data in KanjiEBCDIC character set LMNQRS is represented as: D3 D4 D5 0E 42E3 42C5 42E2 42E3 0F D8 D9 E2 Pad Characters The following table lists the pad characters for the various server character sets. Predicate Calculus Notation Used In This Book Relational databases are based on the theory of relations as developed in set theory. Predicate calculus is often the most unambiguous way to express certain relational concepts. Occasionally this book uses the following predicate calculus notation to explain concepts. Server Character Set Pad Character Name Pad Character Value LATIN SPACE 0x20 UNICODE SPACE U+0020 GRAPHIC IDEOGRAPHIC SPACE U+3000 KANJISJIS ASCII SPACE 0x20 KANJI1 ASCII SPACE 0x20 This symbol … Represents this phrase … iff If and only if ? For all ? There exists SQL Functions, Operators, Expressions, and Predicates 957 Glossary AMP Access Module Process ANSI American National Standards Institute BLOB Binary Large Object BTEQ Basic Teradata Query BYNET Banyan Network CJK Chinese, Japanese, and Korean CLIv2 Call Level Interface Version 2 CLOB Character Large Object CPPI Character Partitioned Primary Index. A partitioned primary index where the partitioning expression involves comparison of CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data types. cs0, cs1, cs2, cs3 Four code sets (codeset 0, 1, 2, and 3) used in EUC encoding. distinct type A UDT that is based on a single predefined data type E2I External-to-Internal EUC Extended UNIX Code FK Foreign Key HI Hash Index I2E Internal-to-External JI Join Index JIS Japanese Industrial Standards LOB Large Object LT/ST Large Table/Small Table (join) NPPI Nonpartitioned Primary Index NUPI Nonunique Primary Index NUSI Nonunique Secondary Index OLAP OnLine Analytical Processing OLTP OnLine Transaction Processing Glossary 958 SQL Functions, Operators, Expressions, and Predicates PDE Parallel Database Extensions PE Parsing Engine vproc PI Primary Index PK Primary Key PPI Partitioned Primary Index predefined type Teradata Database system type such as INTEGER and VARCHAR RDBMS Relational Database Management System SDF Specification for Data Formatting structured type A UDT that is a collection of one or more fields called attributes, each of which is defined as a predefined data type or other UDT (which allows nesting) UCS Universal Coded Character Set, specified by International Standard ISO/IEC 10646 UDF User-Defined Function UDM User-Defined Method UDT User-Defined Type UDT expression An expression that returns a distinct or structured UDT data type UJI Unique Join Index. A noncompressed, single-table join index where the definition includes a unique primary index (UPI), the ROWID keyword in the select list of the SELECT clause, and a WHERE clause that covers a query on the base table (the WHERE clause qualifies a superset of the row set qualified by the WHERE clause of a query on the base table). UPI Unique Primary Index USI Unique Secondary Index vproc Virtual Process SQL Functions, Operators, Expressions, and Predicates 959 Index Symbols ||, concatenation operator 502 A ABS function 56 ACCOUNT function 670 ACOS inverse trigonometric function 110 ACOSH hyperbolic function 116 ADD_MONTHS function 236 Addition operator 48 Aggregate functions AVG 350 constant expressions and 346 CORR 353 COUNT 356 COVAR_POP 361 COVAR_SAMP 364 date and 346 DateTime types and 231 DISTINCT option and 349 floating point data and 348 GROUP BY clause and 346 GROUPING 367 HAVING clause and 349 interval types and 231 KURTOSIS 370 LOB data types and 348 MAX 372 MIN 375 nesting 347 nulls and 347 Period data types and 348 recursive queries and 349 REGR_AVGX 378 REGR_AVGY 381 REGR_COUNT 384 REGR_INTERCEPT 388 REGR_R2 392 REGR_SLOPE 396 REGR_SXX 400 REGR_SXY 403 REGR_SYY 406 select list containing 345 SKEW 409 STDDEV_POP 412 STDDEV_SAMP 415 SUM 418 VAR_POP 421 VAR_SAMP 424 when expression evaluates to zero 347 WHERE clause and 349 Aggregate UDF 714 ALL predicate quantifier 573 AMP, identify with HASHAMP 634 AND logical operator 608 truth table 610 ANY predicate quantifier 573 Arithmetic functions ABS 56 CEILING 68 DEGREES 113 EXP 71 FLOOR 73 LN 76 LOG 78 RADIANS 113 RANDOM 83 SQRT 101 WIDTH_BUCKET 103 ZEROIFNULL 107 Arithmetic operators 287 - 48 * 48 ** 48 + 48 / 48 addition operator 48 division operator 48 exponentiate 48 LOB data types and 48 MOD operator 48 multiplication 48 Period data types and 48 subtraction operator 48 unary minus operator 48 unary plus operator 48 ASIN inverse trigonometric function 110 ASINH hyperbolic function 116 ATAN inverse trigonometric function 110 ATAN2 inverse trigonometric function 110 ATANH hyperbolic function 116 Attribute functions 613 BYTES 614 Index 960 SQL Functions, Operators, Expressions, and Predicates CHARACTER_LENGTH 616 CHARACTERS 619 DEFAULT 621 FORMAT 625 MCHARACTERS 613, 616 OCTET_LENGTH 626 TITLE 629 TYPE 630 AVERAGE aggregate function. See AVG aggregate function AVG aggregate function DateTime types and 231 described 350 Interval types and 231 AVG window function 449 B BEGIN function 291 BETWEEN predicate 578 BITAND function 125 BITNOT function 128 BITOR function 130 BITXOR function 133 Blank, as used in strings 597 BLOB data types aggregate functions and 348 arithmetic operators and 48 comparison operators and 161 predicates and 570 Bound functions BEGIN function 291 End function 295, 302 Built-in functions 669 ACCOUNT 670 CURRENT_DATE 671 CURRENT_TIME 677 CURRENT_TIMESTAMP 681 CURRENT_USER 685 DATABASE function 686 DATE function 687 PROFILE 691 ROLE 675, 692 SESSION 695 TIME 699 USER 702 Byte conversion 758 HASHBUCKET function and 641 Byte/bit manipulation functions BITAND 125 BITNOT 128 BITOR 130 BITXOR 133 COUNTSET 136 GETBIT 138 ROTATELEFT 140 ROTATERIGHT 143 SETBIT 146 SHIFTLEFT 149 SHIFTRIGHT 152 SUBBITSTR 155 TO_BYTE 158 BYTES function 614 C Calendar functions day_of_calendar 260 day_of_month 256 day_of_week 254 day_of_year 258 month_of_calendar 274 month_of_quarter 270 month_of_year 272 quarter_of_calendar 278 quarter_of_year 276 week_of_calendar 268 week_of_month 264 week_of_year 266 weekday_of_month 262 year_of_calendar 280 CALENDAR system view cumulative sum 468 moving difference 474 CAMSET function 646 CAMSET_L function 649 CASE expression and nulls 42 CASE operation COALESCE expression 42 data type of, rules governing 34 defined 25 NULLIF expression 44 searched 29 valued 26 Case sensitivity in comparisons 173 CASE_N function 58 CAST DECIMAL(18) with a DECIMAL(15) default 839 CAST function 752, 755 ANSI DateTime conversion and 823 DECIMAL data type conversions and 839 CEILING function 68 CHAR function. See CHARACTERS function. CHAR2HEXINT function 508 Character assignability rules for 797 conversion to formatted DATE conversion 769 translation 765 translation (internal to external) 500 Index SQL Functions, Operators, Expressions, and Predicates 961 Character string functions. See String functions CHARACTER_LENGTH function 616 CHARACTERS function 619 ANSI equivalent 616 CHARS function. See CHARACTERS function CLOB data types aggregate functions and 348 arithmetic operators and 48 comparison operators and 161 predicates and 570 COALESCE expression 42 Comparison evaluations by data type 166 Comparison operators = 162 > 162 >= 162 GE 162 general rules 165 GT 162 Japanese character sets 175 LE 162 LOB data types and 161 LT 162 NE 162 Period data types 289 results 165 Comparison rules floating point data and 166 string 172 Compression functions CAMSET 646 CAMSET_L 649 LZCOMP 656 LZCOMP_L 658 TransUnicodeToUTF8 664 Concatenation operator 502 Conditional expressions 608 Constant expressions, aggregate functions and 346 CONTAINS predicate 293 Conversion byte 758 byte to INTEGER using HASHBUCKET 641 CAST function and 752 character to character 762 character to DATE 767 character to formatted date 769 character to INTERVAL 773 character to numeric 775 character to Period 781 character to TIME 784 character to TIME WITH TIME ZONE 784 character to TIME, implici 747 character to TIME, implicit 785, 791 character to TIMESTAMP 790 character to TIMESTAMP, implicit 747, 785, 791 character to UDT 795 data type 745 DATE to character 798 DATE to Period 807 DATE to TIMESTAMP 809 DATE to UDT 815 field mode 757 implicit 745 interval to character 817 INTERVAL to INTERVAL 819 interval to numeric 823 interval to UDT 825 numeric 837 numeric to character 827 numeric to INTERVAL 835 numeric to UDT 841 Period to character 843 Period to DATE 846 Period to Period 848 Period to TIME 853 Period to TIMESTAMP 855 rounding rules 838 signed zone decimal 857 string functions and 500 table showing supported types 746 Teradata DATE 802 Teradata syntax and 755 TIME to character 861 TIME to Period 864 TIME to TIME 866 TIME to TIMESTAMP 874 TIME to UDT 888 TIMESTAMP to character 890 TIMESTAMP to DATE 894 TIMESTAMP to Period 905 TIMESTAMP to TIMESTAMP 907, 915 TIMESTAMP to UDT 923 truncation rules 838 CORR aggregate function 353 CORR window function 449 COS trigonometric function 110 COSH hyperbolic function 116 COUNT aggregate function 356 COUNT function DateTime types and 231 Interval types and 232 COUNT window function 449 COUNTSET function 136 COVAR_POP aggregate function 361 COVAR_POP window function 449 COVAR_SAMP aggregate function 364 COVAR_SAMP window function 449 CSUM function 467 Index 962 SQL Functions, Operators, Expressions, and Predicates CUBE grouping set, GROUPING aggregate function and 367 Cumulative sum CALENDAR view 468 computing 467 CURRENT_DATE function 671 CURRENT_TIME function 677 CURRENT_TIMESTAMP function 681 CURRENT_USER function 685 D Data conversion rules explicit 755 implicit 745 rounding 838 truncation 838 Data definition 752 byte conversion 758 byte to INTEGER conversion, HASHBUCKET and 641 CAST, data type converion and 752 character to character conversion 762 character-to-DATE conversion 767 character-to-formatted DATE conversion 769 character-to-INTERVAL conversion 773 character-to-numeric conversion 775 character-to-Period 781 character-to-TIME conversion 784 character-to-TIMESTAMP conversion 790 character-to-UDT conversion 795 data type conversions 745 DATE conversion (Teradata) 802 DATE-to-character conversion 798 DATE-to-Period conversion 807 DATE-to-TIMESTAMP conversion 809 DATE-to-UDT conversion 815 Exact numeric-to-INTERVAL conversion 835 explicit type conversion rules 755 implicit type conversion rules 745 interval-to-character conversion 817 interval-to-exact numeric conversion 823 INTERVAL-to-INTERVAL conversion 819 interval-to-UDT conversion 825 numeric-to-character conversion 827 numeric-to-numeric conversion 837 numeric-to-UDT conversion 841 Period-to-character conversion 843 Period-to-DATE conversion 846 Period-to-Period conversion 848 Period-to-TIME conversion 853 Period-to-TIMESTAMP conversion 855 signed zone decimal conversion 857 TIMESTAMP-to-character conversion 890 TIMESTAMP-to-DATE conversion 894 TIMESTAMP-to-Period conversion 905 TIMESTAMP-to-TIMESTAMP conversion 907, 915 TIMESTAMP-to-UDT conversion 923 TIME-to-character conversion 861 TIME-to-Period conversion 864 TIME-to-TIME conversion 866 TIME-to-TIMESTAMP conversion 874 TIME-to-UDT conversion 888 Database, get default database 686 DATE as logical predicate 171 scalar operations on 234 Date get current date (Teradata) 687 get system date 671 Date expressions, Teradata 233 DATE to UDT conversion 815 Date, aggregate operations and 346 DateTime expressions 213 rules for, ANSI 219 DateTime functions, and scalar operations 232 DateTime scalar operations arithmetic 229 restrictions on 213 DateTime types aggregate functions and 231 assignment rules 210, 211 DATE-to-Period conversion 807 DATE-to-TIMESTAMP conversion 809 day_of_calendar function 260 day_of_month function 256 day_of_week function 254 day_of_year function 258 DECAMSET function 652 DECAMSET_L function 654 DECIMAL/NUMERIC types, arithmetic expression and rounding 53 Decompression functions DECAMSET 652 DECAMSET_L 654 LZDECOMP 660 LZDECOMP_L 662 TransUTF8ToUnicode 667 DEFAULT function 621 DEGREES function 113 DISTINCT, SELECT option 349 Division operator 48 E End function 295, 302 ESCAPE, with LIKE predicate 597, 602 Exact numeric-to-INTERVAL conversion 835 EXCEPT operator 198 EXISTS predicate 579 Index SQL Functions, Operators, Expressions, and Predicates 963 EXP function 71 Exponentiation operator 48 Expressions, defined 22 EXTRACT function 242 F Fallback AMP, identify with HASHBAKAMP 637 FALSE 609 Field mode, data type conversions and 757 FLOAT data types aggregate functions and 348 comparison operations and 166 FLOOR function 73 FORMAT phrase 625 Functions defined 19 TEMPORAL_DATE 696 TEMPORAL_TIMESTAMP 697 types of 19 G GETBIT function 138 GetTimeZoneDisplacement function 246 GROUP BY clause aggregate functions and 346 rules for aggregate functions and constant expressions 346 Group count, example 461 GROUPING aggregate function CUBE and 367 described 367 GROUPING SET and 367 ROLLUP and 367 H Hash index, ordered analytical functions and 440 HASHAMP function 634 HASHBAKAMP function 637 HASHBUCKET function 640 Hash-related functions 633 HASHAMP 634 HASHBAKAMP 637 HASHBUCKET 640 HASHROW 643 HASHROW function 643 Hyperbolic functions 116 ACOSH 116 ASINH 116 ATANH 116 COSH 116 SINH 116 TANH 116 I Implicit type conversion 745 byte-to-UDT 760 character-to-UDT 795 comparison operators and 168 DATE-to-UDT 815, 888, 923 interval-to-UDT 825 numeric-to-UDT 841 IN predicate 585 INDEX function 511 ANSI equivalent 498 INTERSECT operator 195 Interval conversion interval-to-character 817 interval-to-interval 819 interval-to-UDT 825 Interval expressions 222 rules for, ANSI 228 INTERVAL function 300 Interval scalar operations arithmetic 229 restrictions on 213 Interval types aggregate functions and 231 assignment rules 210, 211 Interval-to-character conversion 817 Interval-to-exact numeric conversion 823 INTERVAL-to-INTERVAL conversion 819 Interval-to-UDT conversion 825 Inverse trigonometric functions 110 ACOS 110 ASIN 110 ATAN 110 ATAN2 110 IS NOT NULL predicate 592 IS NOT UNTIL_CHANGED predicate 297 IS NOT UNTIL_CLOSED predicate 299 IS NULL predicate 592 IS UNTIL_CHANGED predicate 297 IS UNTIL_CLOSED predicate 299 J Japanese character code notation, how to read 954 Join index, ordered analytical functions and 440 K KURTOSIS aggregate function 370 L LDIFF operator 320 Least squares, computing 476 LIKE predicate 594 Index 964 SQL Functions, Operators, Expressions, and Predicates Linear regression, computing 476 LN function 76 LOG function 78 Logical expressions BETWEEN predicate 578 FALSE result 609 NOT BETWEEN predicate 578 TRUE result 609 UNKNOWN result 609 Logical operators AND 608 defined 608 NOT 608 OR 608 search conditions and 608 Logical predicate conditional expression as 569 DATE as 171 DEFAULT function and 177, 572 defined 569 LOB data types and 570 order of evaluation 609 primitives, tabular summary of 570 SQL use of 569 LOWER function 517 LZCOMP function 656 LZCOMP_L function 658 LZDECOMP function 660 LZDECOMP_L function 662 M MAVG function 470 MAX aggregate function DateTime types and 231 described 372 Interval types and 232 MAX window function 449 MAXIMUM aggregate function. See MAX aggregate function MCHARACTERS function 613, 616 ANSI equivalent 613 MDIFF function 473 MEETS predicate 304 MIN aggregate function DateTime types and 231 described 375 Interval types and 232 MIN window function 449 MINDEX function 498, 520 ANSI equivalent 498 MINIMUM aggregate function. See MIN aggregate function MINUS operator 198 MLINREG function 476 MOD operator 48 month_of_calendar function 274 month_of_quarter function 270 month_of_year function 272 Moving average, computing 470 Moving difference CALENDAR view 474 computing 473 Moving sum, computing 479 MSUBSTR function 498, 532 ANSI equivalent 498 MSUM function 479 Multiplication operator 48 Mutator methods 740 N Name, get user name 685, 702 NEW expression 734 NEXT function 306 Normalize functions TD_NORMALIZE_MEET 328 TD_NORMALIZE_OVERLAP 326 TD_NORMALIZE_OVERLAP_MEET 330 TD_SUM_NORMALIZE_MEET 334 TD_SUM_NORMALIZE_OVERLAP 332 TD_SUM_NORMALIZE_OVERLAP_MEET 336 NOT BETWEEN predicate 578 NOT EXISTS predicate 579 NOT IN predicate 585 NOT EXISTS predicate and 580 recursive queries and 590 NOT logical operator 608 NULLIF expression 44 NULLIFZERO function 80 Nulls aggregate operations and 347 CASE expression and 42 searching for/excluding 592 Numeric conversion numeric-to-character 827 numeric-to-date 833 numeric-to-UDT 841 Numeric-to-character conversion 827 Numeric-to-date conversion 833 Numeric-to-numeric conversion 837 Numeric-to-UDT conversion 841 O Observer methods 740 OCTET_LENGTH function 626 OLAP functions. See Ordered analytical functions. operators arithmetic operators 287 defined 21 Index SQL Functions, Operators, Expressions, and Predicates 965 LDIFF operator 320 P_INTERSECT operator 312 P_NORMALIZE operator 314 RDIFF operator 322 OR logical operator 608 truth table 610 ORDER BY clause ordered analytical functions and 432, 440 window specification and 440 Order of evaluation. See Logical predicate Ordered analytical functions 427 aggregates and 442 AVG window function 449 common characteristics of 439 CORR window function 449 COUNT window function 449 COVAR_POP window function 449 COVAR_SAMP window function 449 CSUM 467 derived tables and 442 description 428 examples 446 GROUP BY clause 443 hash indexes and 440 HAVING clause 442 join indexes and 440 MAVG 470 MAX window function 449 MDIFF 473 MIN window function 449 MLINREG 476 MSUM 479 ORDER BY clause 432, 440 PARTITION BY clause 431, 441 PERCENT_RANK window function 481 Period data types and 440 QUALIFY clause 439, 442 QUANTILE 485 RANK 488 RANK window function 491 recursive queries and 440 REGR_AVGX window function 449 REGR_AVGY window function 449 REGR_COUNT window function 449 REGR_INTERCEPT window function 449 REGR_R2 window function 449 REGR_SLOPE window function 449 REGR_SXX window function 449 REGR_SXY window function 449 REGR_SYY window function 449 result order 440 ROW_NUMBER window function 494 ROWS clause 436 STDDEV_POP window function 449 STDDEV_SAMP window function 449 SUM window function 449 syntax alternatives for 429 Teradata OLAP functions 430 Teradata queries, extending 428 Teradata Warehouse Miner and 428 UDF window function 449 VAR_POP window function 449 VAR_SAMP window function 449 views and 442 window 430 window functions 430 OVERLAPS predicate 308, 604 P P_INTERSECT operator 312 P_NORMALIZE operator 314 PARTITION BY clause affect on spool space 441 ordered analytical functions and 431, 441 Partitioned primary index. See PPI PERCENT_RANK window function, described 481 Period data types, logical predicates and 571 Period Value Constructor 284 Period-to-character conversion 843 Period-to-DATE conversion 846 Period-to-Period conversion 848 Period-to-TIME conversion 853 Period-to-TIMESTAMP conversion 855 POSITION function 498, 520 PPI defined 60, 91 maximum partitions when using CASE_N 61 maximum partitions when using RANGE_N 92 multilevel 60, 91 system-derived columns 61, 92 PPI functions CASE_N 58 RANGE_N 87 Precedence arithmetic expressions 53 logical operators 609 operator 53 set operators 182 PRECEDES predicate 316 Predicate quantifiers ALL 573 ANY 573 SOME 573 Predicates BETWEEN 578 CONTAINS 293 DEFAULT function and 177, 572 Index 966 SQL Functions, Operators, Expressions, and Predicates defined 23 EXISTS 579 IN 585 IS NOT NULL 592 IS NOT UNTIL_CHANGED 297 IS NOT UNTIL_CLOSED 299 IS NULL 592 IS UNTIL_CHANGED 297 IS UNTIL_CLOSED 299 LIKE 594 logical 569 MEETS 304 NOT BETWEEN 578 NOT EXISTS 579 NOT IN 585 OVERLAPS 308, 604 PRECEDES 316 quantifiers 573 SUCCEEDS 324 PRIOR function 318 PROFILE function 691 Profiles, getting the current profile 691 Proximity functions NEXT function 306 PRIOR function 318 Q QUALIFY clause, ordered analytical functions and 439 Quantifiers ALL 573 ANY 573 SOME 573 QUANTILE function, described 485 quarter_of_calendar function 278 quarter_of_year function 276 R RADIANS function 113 RANDOM function 83 valued CASE and 26 RANGE_N function 87 RANK function 488 RANK window function 491 RDIFF operator 322 REGR_AVGX aggregate function 378 REGR_AVGX window function 449 REGR_AVGY aggregate function 381 REGR_AVGY window function 449 REGR_COUNT aggregate function 384 REGR_COUNT window function 449 REGR_INTERCEPT aggregate function 388 REGR_INTERCEPT window function 449 REGR_R2 aggregate function 392 REGR_R2 window function 449 REGR_SLOPE aggregate function 396 REGR_SLOPE window function 449 REGR_SXX aggregate function 400 REGR_SXX window function 449 REGR_SXY aggregate function 403 REGR_SXY window function 449 REGR_SYY aggregate function 406 REGR_SYY window function 449 Remaining average 440 Remaining count 461 Remaining sum 466 ROLE function 675, 692 Roles, getting the current role 675, 692 ROLLUP grouping set, GROUPING aggregate function and 367 ROTATELEFT function 140 ROTATERIGHT function 143 Rounding arithmetic operators and DECIMAL/NUMERIC data 53 data type conversion rules 838 Row length errors, UNION operator 201 ROW_NUMBER window function, described 494 Rowhash, identify with HASHROW function 643 ROWNUM. See ROW_NUMBER window function. ROWNUMBER. See ROW_NUMBER window function. ROWS clause defined 436 ordered analytical functions and 436 S Scalar UDF 711 Scalar, converting scalar value expressions 752 SDF Currency 778 CurrencyName 778 data type default formats and 780 FORMAT phrase, relationship to 778 GroupingRule 778 GroupSeparator 778 RadixSeparator 778 Search conditions defined 608 logical operators and 608 Sequenced aggregation functions TD_SEQUENCED_AVG 340 TD_SEQUENCED_COUNT 342 TD_SEQUENCED_SUM 338 SESSION function 695 Session, get session number 695 Set operators ALL option 183 derived tables and 185 Index SQL Functions, Operators, Expressions, and Predicates 967 EXCEPT operator 198 INSERT...SELECT statements containing 188 INTERSECT operator 195 MINUS operator 198 overview 179 precedence 182 rules for 181 rules for connecting queries by 191 set result, attributes of 183 subqueries containing 186 UNION operator 200 view definitions containing 190 SETBIT function 146 SHIFTLEFT function 149 SHIFTRIGHT function 152 Signed zone decimal conversion 857 SIN trigonometric function 110 SINH hyperbolic function 116 SKEW aggregate function 409 SOME predicate quantifier 573 SOUNDEX function, described 523 Specification for data formatting, see SDF SQL expressions aggregate functions AVG 350 CORR 353 COUNT 356 COVAR_POP 361 COVAR_SAMP 364 DISTINCT option 349 GROUPING 367 HAVING clause and 349 KURTOSIS 370 MAX 372 MIN 375 REGR_AVGX 378 REGR_AVGY 381 REGR_COUNT 384 REGR_INTERCEPT 388 REGR_R2 392 REGR_SLOPE 396 REGR_SXX 400 REGR_SXY 403 REGR_SYY 406 SKEW 409 STDDEV_POP 412 STDDEV_SAMP 415 SUM 418 VAR_POP 421 VAR_SAMP 424 WHERE clause and 349 arithmetic functions ABS 56 CEILING 68 DEGREES 113 EXP 71 FLOOR 73 LN 76 LOG 78 NULLIFZERO 80 RADIANS 113 RANDOM 83 SQRT 101 ZEROIFNULL 107 arithmetic operators addition operator 48 division operator 48 exponentiation 48 MOD operator 48 multiplication operator 48 precedence 53 subtraction operator 48 unary minus operator 48 unary plus operator 48 CASE operation 25 COALESCE expression 42 NULLIF expression 44 searched CASE 29 valued CASE 26 comparison operators = 162 > 162 >= 162 EQ 162 GE 162 GT 162 Japanese character set comparison operators 175 LE 162 LT 162 NE 162 Period data type comparison operators 289 conditional expressions 608 hyperbolic functions 116 ACOSH 116 ASINH 116 ATANH 116 COSH 116 SINH 116 TANH 116 inverse trigonometric functions 110 ACOS 110 ASIN 110 ATAN 110 ATAN2 110 logical expressions BETWEEN 578 NOT BETWEEN 578 trigonometric functions 110 Index 968 SQL Functions, Operators, Expressions, and Predicates COS 110 SIN 110 TAN 110 SQL functions attribute functions 613 BYTES 614 CHARACTER_LENGTH 616 CHARACTERS 619 DEFAULT 621 FORMAT 625 MCHARACTERS 613, 616 OCTET_LENGTH 626 TITLE 629 TYPE 630 built-in functions ACCOUNT 670 CURRENT_DATE 671 CURRENT_TIME 677 CURRENT_TIMESTAMP 681 CURRENT_USER 685 DATABASE 686 DATE 687 PROFILE 691 ROLE 675, 692 SESSION 695 TIME 699 USER 702 byte strings BYTES 614 TRIM 549 byte/bit manipulation functions BITAND 125 BITNOT 128 BITOR 130 BITXOR 133 COUNTSET 136 GETBIT 138 ROTATELEFT 140 ROTATERIGHT 143 SETBIT 146 SHIFTLEFT 149 SHIFTRIGHT 152 SUBBITSTR 155 TO_BYTE 158 Calendar functions day_of_calendar 260 day_of_month 256 day_of_week 254 day_of_year 258 month_of_calendar 274 month_of_quarter 270 month_of_year 272 quarter_of_calendar 278 quarter_of_year 276 week_of_calendar 268 week_of_month 264 week_of_year 266 weekday_of_month 262 year_of_calendar 280 Compression functions CAMSET 646 CAMSET_L 649 LZCOMP 656 LZCOMP_L 658 TransUnicodeToUTF8 664 Decompression functions DECAMSET 652 DECAMSET_L 654 LZDECOMP 660 LZDECOMP_L 662 TransUTF8ToUnicode 667 hash-related functions 633 HASHAMP 634 HASHBAKAMP 637 HASHBUCKET 640 HASHROW 643 Ordered analytical functions AVG window function 449 CORR window function 449 COUNT window function 449 COVAR_POP window function 449 COVAR_SAMP window function 449 CSUM 467 MAVG 470 MAX window function 449 MDIFF 473 MIN window function 449 MLINREG 476 MSUM 479 PERCENT_RANK window function 481 QUANTILE 485 RANK 488 RANK window function 491 REGR_AVGX window function 449 REGR_AVGY window function 449 REGR_COUNT window function 449 REGR_INTERCEPT window function 449 REGR_R2 window function 449 REGR_SLOPE window function 449 REGR_SXX window function 449 REGR_SXY window function 449 REGR_SYY window function 449 ROW_NUMBER window function 494 STDDEV_POP window function 449 STDDEV_SAMP window function 449 SUM window function 449 UDF window function 449 VAR_POP window function 449 Index SQL Functions, Operators, Expressions, and Predicates 969 VAR_SAMP window function 449 partitioning functions CASE_N 58 RANGE_N 87 string functions 497 CHAR2HEXINT 508 concatenation operator 502 INDEX 511 LOWER 517 MINDEX 520 MSUBSTR 532 POSITION 520 SOUNDEX 523 STRING_CS 527 SUBSTR 530, 532 SUBSTRING 530 TRANSLATE 536 TRANSLATE_CHK 545 TRIM 549 TRIM and concatenation 551 UPPER 553 VARGRAPHIC 556 WIDTH_BUCKET function 103 SQL UDF 706 SQRT function 101 STDDEV_POP aggregate function 412 STDDEV_POP window function 449 STDDEV_SAMP aggregate function 415 STDDEV_SAMP window function 449 String functions CHAR2HEXINT 508 implicit character type conversion 500 INDEX 511 LOWER 517 MINDEX 498, 520 MSUBSTR 498, 532 POSITION 520 rules 500 server character sets and 500 SOUNDEX 523 STRING_CS 527 SUBSTR 530, 532 SUBSTRING 530 TRANSLATE 536 TRANSLATE_CHK 545 TRIM 549 UPPER 553 VARGRAPHIC 556 STRING_CS function 527 SUBBITSTR function 155 Subqueries, comparison operators and 164 SUBSTR function 530, 532 ANSI equivalent 498 SUBSTRING function 498, 530 Subtraction operator 48 SUCCEEDS predicate 324 SUM aggregate function 418 SUM function, Interval types and 232 SUM window function 449 Syntax, how to read 949 SYS_CALENDAR system database 468, 474 T Table UDF 725 TAN trigonometric function 110 TANH hyperbolic function 116 TD_NORMALIZE_MEET function 328 TD_NORMALIZE_OVERLAP function 326 TD_NORMALIZE_OVERLAP_MEET function 330 TD_SEQUENCED_AVG function 340 TD_SEQUENCED_COUNT function 342 TD_SEQUENCED_SUM function 338 TD_SUM_NORMALIZE_MEET function 334 TD_SUM_NORMALIZE_OVERLAP function 332 TD_SUM_NORMALIZE_OVERLAP_MEET function 336 TEMPORAL_DATE, reference 696 TEMPORAL_TIMESTAMP reference 697 Teradata conversion syntax 755 Teradata OLAP functions. See Ordered analytical functions Teradata Warehouse Miner 428 Time get current time (Teradata) 699 get system time 677 Time expressions, Teradata 233 TIME function 699 Time stamp, get system time stamp 681 Time zone comparisons 221 Time zone, get time zone displacement 677 TIME, conversion to character 861 TIMESTAMP arithmetic 228 conversion to character 890 TIMESTAMP-to-DATE conversion 894 TIMESTAMP-to-Period conversion 905 TIMESTAMP-to-TIMESTAMP conversion 907, 915 TIMESTAMP-to-UDT conversion 923 TIME-to-Period conversion 864 TIME-to-TIME conversion 866 TIME-to-TIMESTAMP conversion 874 TIME-to-UDT conversion 888 TITLE function 629 TO_BYTE function 158 TRANSLATE function 536 TRANSLATE_CHK function 545 Translation, character 765 TransUnicodeToUTF8 function 664 Index 970 SQL Functions, Operators, Expressions, and Predicates TransUTF8ToUnicode function 667 Trigonometric functions 110 COS 110 SIN 110 TAN 110 TRIM function 549 TRIM function, concatenation and 551 TRUE 609 Type conversion, implicit 745 TYPE function 630 U UDF window function 449 UDM invocation 740 UDT data types aggregate functions and 351 arithmetic operators and 49 CASE expression and 27, 30, 34 COALESCE expression and 43 comparison operators and 167 conversion 925, 928, 932, 935, 938, 941, 944, 947 hash-related functions and 635, 638 implicit type conversions and 747 logical predicates and 571 method invocation 740 mutator methods 740 NEW expression 730, 734 NULL value 347 NULLIF expression and 45 observer methods 740 ordered analytical functions and 440 set operators and 182 string functions and 503 UDT expression 730 UDT-to-byte type conversion 925 UDT-to-character type conversion 928 UDT-to-DATE type conversion 932 UDT-to-INTERVAL type conversion 935 UDT-to-numeric type conversion 938 UDT-to-TIME type conversion 941 UDT-to-TIMESTAMP type conversion 944 UDT-to-UDT type conversion 947 Unary minus operator 48 Unary plus operator 48 UNION operator 200 outer join and 204 reason for unexpected row length errors 201 Universal Coordinated Time, see UTC UNKNOWN 609 UNTIL_CLOSED value 284 UPPER function 553 USER function 702 User-defined function aggregate UDF 714 scalar UDF 711 SQL UDF 706 table UDF 725 window aggregate UDF 717 User-defined types. See UDT data types Username, get user name 685, 702 UTC time conversions and 786, 792 UTF16 client character set KANJI1 translation, internal to external 500 OCTET_LENGTH and 627 UTF8 client character set KANJI1 translation 500 OCTET_LENGTH and 627 V VAR_POP aggregate function 421 VAR_POP window function 449 VAR_SAMP aggregate functions 424 VAR_SAMP window function 449 VARGRAPHIC function 556 VARGRAPHIC function conversion tables 559 W week_of_calendar function 268 week_of_month function 264 week_of_year function 266 weekday_of_month function 262 WIDTH_BUCKET function 103 Wildcards, used with LIKE predicate 595 Window aggregate functions defined 438 difference between aggregate functions and 438 Window aggregate UDF 717 Window functions. See Ordered analytical functions Window, defined 430 Y year_of_calendar function 280 Z ZEROIFNULL function 107 Teradata Database SQL Reference Fundamentals Release V2R6.2 B035-1141-096A September 2006The product described in this book is a licensed product of Teradata, a division of NCR Corporation. NCR, Teradata and BYNET are registered trademarks of NCR Corporation. Adaptec and SCSISelect are registered trademarks of Adaptec, Inc. EMC, PowerPath, SRDF, and Symmetrix are registered trademarks of EMC Corporation. Engenio is a trademark of Engenio Information Technologies, Inc. Ethernet is a trademark of Xerox Corporation. GoldenGate is a trademark of GoldenGate Software, Inc. Hewlett-Packard and HP are registered trademarks of Hewlett-Packard Company. IBM, CICS, DB2, MVS, RACF, OS/390, Tivoli, and VM are registered trademarks of International Business Machines Corporation. Intel, Pentium, and XEON are registered trademarks of Intel Corporation. KBMS is a registered trademark of Trinzic Corporation. Linux is a registered trademark of Linus Torvalds. LSI, SYM, and SYMplicity are registered trademarks of LSI Logic Corporation. Active Directory, Microsoft, Windows, Windows Server, and Windows NT are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Novell is a registered trademark of Novell, Inc., in the United States and other countries. SUSE is a trademark of SUSE LINUX Products GmbH, a Novell business. QLogic and SANbox are registered trademarks of QLogic Corporation. SAS and SAS/C are registered trademark of SAS Institute Inc. Sun Microsystems, Sun Java, Solaris, SPARC, and Sun are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. or other countries. Unicode is a registered trademark of Unicode, Inc. UNIX is a registered trademark of The Open Group in the US and other countries. NetVault is a trademark and BakBone is a registered trademark of BakBone Software, Inc. NetBackup and VERITAS are trademarks of VERITAS Software Corporation. Other product and company names mentioned herein may be the trademarks of their respective owners. THE INFORMATION CONTAINED IN THIS DOCUMENT IS PROVIDED ON AN “AS-IS” BASIS, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NONINFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT APPLY TO YOU. IN NO EVENT WILL NCR CORPORATION (NCR) BE LIABLE FOR ANY INDIRECT, DIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS OR LOST SAVINGS, EVEN IF EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. The information contained in this document may contain references or cross references to features, functions, products, or services that are not announced or available in your country. Such references do not imply that NCR intends to announce such features, functions, products, or services in your country. Please consult your local NCR representative for those features, functions, products, or services available in your country. Information contained in this document may contain technical inaccuracies or typographical errors. Information may be changed or updated without notice. NCR may also make improvements or changes in the products or services described in this information at any time without notice. To maintain the quality of our products and services, we would like your comments on the accuracy, clarity, organization, and value of this document. Please e-mail: teradata-books@lists.ncr.com Any comments or materials (collectively referred to as “Feedback”) sent to NCR will be deemed non-confidential. NCR will have no obligation of any kind with respect to Feedback and will be free to use, reproduce, disclose, exhibit, display, transform, create derivative works of and distribute the Feedback and derivative works thereof without limitation on a royalty-free basis. Further, NCR will be free to use any ideas, concepts, know-how or techniques contained in such Feedback for any purpose whatsoever, including developing, manufacturing, or marketing products or services incorporating Feedback. Copyright © 2000 - 2006 by NCR Corporation. All Rights Reserved.SQL Reference: Fundamentals iii Preface Purpose SQL Reference: Fundamentals describes basic SQL data handling, SQL data definition, control, and manipulation, and the SQL lexicon. Use this book with the other books in the SQL Reference book set. Audience System administrators, database administrators, security administrators, application programmers, NCR field engineers, end users, and other technical personnel responsible for designing, maintaining, and using the Teradata Database will find this book useful. Experienced SQL users can also see simplified statement, data type, function, and expression descriptions in SQL/Data Dictionary Quick Reference. Supported Software Release This book supports Teradata ® Database V2R6.2. Prerequisites If you are not familiar with Teradata Database, you will find it useful to read Introduction to Teradata Warehouse before reading this book. You should be familiar with basic relational database management technology. This book is not an SQL primer.Preface Changes to This Book iv SQL Reference: Fundamentals Changes to This Book This book includes the following changes to support the current release. Date Description September 2006 • Added material to support BIGINT data type • Removed the restriction that the PARTITION BY option is not allowed in the CREATE JOIN INDEX statement for non-compressed join indexes • Removed the restriction that triggers cannot be defined on a table on which a join index is already defined • Updated the section on altering table structure and definition to indicate that ALTER TABLE can now be used to define, modify, or delete a COMPRESS attribute on an existing column • Updated Appendix E with new syntax for ALTER TABLE and CREATE TABLE • Moved the topics that identified valid and non-valid character ranges for KanjiEBCDIC, KanjiEUC, and KanjiShift-JIS object names from Chapter 2 to the International Character Set Support book May 2006 Removed RESTRICT from list of Teradata Database reserved words November 2005 • Added material to support new UDT and UDM feature • Added Appendix E, which details the differences in SQL between this release and previous releases • Removed the restriction that the PARTITION BY option is not allowed in the CREATE TABLE statement for global temporary tables and volatile tables November 2004 • Removed colons from stored procedure examples because colons are no longer required when local stored procedure variables or parameters are referenced in SQL statements • Added material to support new table function feature and new external stored procedure feature • Added overview of event processing using queue tables and the SELECT AND CONSUME statement • Removed the restriction that triggers cannot call stored procedures • Added material on new recursive query feature • Added material on new iterated requests feature • Added the restricted word list back into Appendix BPreface Additional Information SQL Reference: Fundamentals v Additional Information Additional information that supports this product and the Teradata Database is available at the following Web sites. Type of Information Description Source Overview of the release Information too late for the manuals The Release Definition provides the following information: • Overview of all the products in the release • Information received too late to be included in the manuals • Operating systems and Teradata Database versions that are certified to work with each product • Version numbers of each product and the documentation for each product • Information about available training and support center http://www.info.ncr.com/ Click General Search. In the Publication Product ID field, enter 1725 and click Search to bring up the following Release Definition: • Base System Release Definition B035-1725-096K Additional information related to this product Use the NCR Information Products Publishing Library site to view or download the most recent versions of all manuals. Specific manuals that supply related or additional information to this manual are listed. http://www.info.ncr.com/ Click General Search, and do one of the following: • In the Product Line field, select Software - Teradata Database for a list of all of the publications for this release, • In the Publication Product ID field, enter a book number. CD-ROM images This site contains a link to a downloadable CD-ROM image of all customer documentation for this release. Customers are authorized to create CDROMs for their use from this image. http://www.info.ncr.com/ Click General Search. In the Title or Keyword field, enter CD-ROM, and Click Search. Ordering information for manuals Use the NCR Information Products Publishing Library site to order printed versions of manuals. http://www.info.ncr.com/ Click How to Order under Print & CD Publications.Preface References to Microsoft Windows vi SQL Reference: Fundamentals References to Microsoft Windows This book refers to “Microsoft Windows.” For Teradata Database V2R6.2, such references mean Microsoft Windows Server 2003 32-bit and Microsoft Windows Server 2003 64-bit. General information about Teradata The Teradata home page provides links to numerous sources of information about Teradata. Links include: • Executive reports, case studies of customer experiences with Teradata, and thought leadership • Technical information, solutions, and expert advice • Press releases, mentions and media resources Teradata.com Type of Information Description SourceSQL Reference: Fundamentals vii Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii Audience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii Supported Software Release . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii Changes to This Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv Additional Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .v References to Microsoft Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vi Chapter 1: Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Databases and Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 Global Temporary Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 Volatile Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 Columns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Primary Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Secondary Indexes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Join Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Hash Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Referential Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 External Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 User-Defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55Table of Contents viii SQL Reference: Fundamentals Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57 User-Defined Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58 Chapter 2: Basic SQL Syntax and Lexicon . . . . . . . . . . . . . . . . . . . . . . . .63 Structure of an SQL Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63 SQL Lexicon Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65 Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67 Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67 Standard Form for Data in Teradata Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71 Unqualified Object Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73 Default Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75 Name Validation on Systems Enabled with Japanese Language Support . . . . . . . . . . . . . . . . .77 Object Name Translation and Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81 Object Name Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82 Finding the Internal Hexadecimal Representation for Object Names. . . . . . . . . . . . . . . . . . . .84 Specifying Names in a Logon String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87 NULL Keyword as a Literal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92 Delimiters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93 Separators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95 Terminators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96 Null Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98 Chapter 3: SQL Data Definition, Control, and Manipulation . .99 SQL Functional Families and Binding Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99 Embedded SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100 Data Definition Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101 Altering Table Structure and Definition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103 Dropping and Renaming Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .104 Data Control Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .105Table of Contents SQL Reference: Fundamentals ix Data Manipulation Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Recursive Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Query and Workload Analysis Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Help and Database Object Definition Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Chapter 4: SQL Data Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Invoking SQL Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Transaction Processing in ANSI Session Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Transaction Processing in Teradata Session Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Multistatement Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Iterated Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Dynamic and Static SQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Dynamic SQL in Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Using SELECT With Dynamic SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Event Processing Using Queue Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Manipulating Nulls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Session Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Session Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Return Codes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Statement Responses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Success Response. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Warning Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Error Response (ANSI Session Mode Only). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Failure Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Chapter 5: Query Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Query Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Table Access. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Full-Table Scans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Collecting Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164Table of Contents x SQL Reference: Fundamentals Appendix A: Notation Conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167 Syntax Diagram Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167 Character Shorthand Notation Used In This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .171 Predicate Calculus Notation Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172 Appendix B: Restricted Words for V2R6.2. . . . . . . . . . . . . . . . . . . . . . .173 Reserved Words and Keywords for V2R6.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173 Appendix C: Teradata Database Limits. . . . . . . . . . . . . . . . . . . . . . . . . . .203 System Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204 Database Limits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .206 Session Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211 Appendix D: ANSI SQL Compliance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .213 ANSI SQL Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .213 Terminology Differences Between ANSI SQL and Teradata . . . . . . . . . . . . . . . . . . . . . . . . . .216 SQL Flagger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217 Differences Between Teradata and ANSI SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .218 Appendix E: SQL Feature Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219 Notation Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219 Statements and Modifiers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219 Data Types and Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .277 Functions, Operators, and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .280Table of Contents SQL Reference: Fundamentals xi Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291Table of Contents xii SQL Reference: FundamentalsSQL Reference: Fundamentals 1 CHAPTER 1 Objects This chapter describes the objects you use to store, manage, and access data in the Teradata Database. Topics include: • Databases and Users • Tables • Columns • Data Types • Keys • Indexes • Views • Triggers • Macros • Stored Procedures and External Stored Procedures • User-Defined Functions • User-Defined Types (UDTs) and User-Defined Methods (UDMs) • Profiles • Roles Databases and Users Definitions A database is a collection of related tables, views, triggers, indexes, stored procedures, user-defined functions, and macros. A database also contains an allotment of space from which users can create and maintain their own objects, or other users or databases. A user is almost the same as a database, except that a user has a password and can log on to the system, whereas the database cannot. Defining Databases and Users Before you can create a database or user, you must have sufficient privileges granted to you. To create a database, use the CREATE DATABASE statement. You can specify the name of the database, the amount of storage to allocate, and other attributes.Chapter 1: Objects Tables 2 SQL Reference: Fundamentals To create a user, use the CREATE USER statement. The statement authorizes a new user identification (user name) for the database and specifies a password for user authentication. Because the system creates a database for each user, the CREATE USER statement is very similar to the CREATE DATABASE statement. Difference Between Users and Databases The difference between users and databases in the Teradata Database has important implications for matters related to access privileges, but neither the differences nor their implications are easy to understand. This is particularly true with respect to understanding fully the consequences of implicitly granted access privileges. Formally speaking, the difference between a user and a database is that a user has a password and a database does not. Users can also have default attributes such as time zone, date form, character set, role, and profile, while databases cannot. You might infer from this that databases are passive objects, while users are active objects. That is only true in the sense that databases cannot execute SQL statements. However, a query, macro, or stored procedure can execute using the privileges of the database. Tables Definitions A table is what is referred to in set theory terminology as a relation, from which the expression relational database is derived. Every relational table consists of one row of column headings (more commonly referred to as column names) and zero or more unique rows of data values. Formally speaking, each row represents what set theory calls a tuple. Each column represents what set theory calls an attribute. The number of rows (or tuples) in a table is referred to as its cardinality and the number of columns (or attributes) is referred to as its degree or arity. Defining Tables Use the CREATE TABLE statement to define base tables. The CREATE TABLE statement specifies a table name, one or more column names, and the attributes of each column. CREATE TABLE can also specify datablock size, percent freespace, and other physical attributes of the table. The CREATE/MODIFY USER and CREATE/MODIFY DATABASE statements provide options for creating permanent journal tables. Defining Indexes For a Table An index is a physical mechanism used to store and access the rows of a table. When you define a table, you can define a primary index and one or more secondary indexes. Chapter 1: Objects Tables SQL Reference: Fundamentals 3 All tables require a primary index. If you do not specify a column or set of columns as the primary index when you create a table, then CREATE TABLE specifies a primary index by default. For more information on indexes, see “Indexes” on page 17. Duplicate Rows in Tables Though both set theory and common sense prohibit duplicate rows in relational tables, the ANSI standard defines SQL based not on sets, but on bags, or multisets. A table defined not to permit duplicate rows is called a SET table because its properties are based on set theory, where set is defined as an unordered group of unique elements with no duplicates. A table defined to permit duplicate rows is called a MULTISET table because its properties are based on a multiset, or bag, model, where bag and multiset are defined as an unordered group of elements that may be duplicates. Temporary Tables Temporary tables are useful for temporary storage of data. Teradata Database supports three types of temporary tables. FOR more information on … SEE … rules for duplicate rows in a table CREATE TABLE in SQL Reference: Data Definition Statements. the result of an INSERT operation that would create a duplicate row INSERT in SQL Reference: Data Manipulation Statements. the result of an INSERT using a SELECT subquery that would create a duplicate row Type Usage Global temporary A global temporary table has a persistent table definition that is stored in the data dictionary. Any number of sessions can materialize and populate their own local copies that are retained until session logoff. Global temporary tables are useful for stroring temporary, intermediate results from multiple queries into working tables that are frequently used by applications. Global temporary tables are identical to ANSI global temporary tables. Volatile Like global temporary tables, the contents of volatile tables are only retained for the duration of a session. However, volatile tables do not have persistent definitions. To populate a volatile table, a session must first create the definition.Chapter 1: Objects Tables 4 SQL Reference: Fundamentals Materialized instances of a global temporary table share the following characteristics with volatile tables: • Private to the session that created them. • Contents cannot be shared by other sessions. • Optionally emptied at the end of each transaction using the ON COMMIT PRESERVE/DELETE rows option in the CREATE TABLE statement. • Activity optionally logged in the transient journal using the LOG/NO LOG option in the CREATE TABLE statement. • Dropped automatically when a session ends. For details about the individual characteristics of global temporary and volatile tables, see “Global Temporary Tables” on page 5 and “Volatile Tables” on page 9. Queue Tables Teradata Database supports queue tables, which are similar to ordinary base tables, with the additional unique property of behaving like an asynchronous first-in-first-out (FIFO) queue. Queue tables are useful for applications that want to submit queries that wait for data to be inserted into queue tables without polling. When you create a queue table, you must define a TIMESTAMP column with a default value of CURRENT_TIMESTAMP. The values in the column indicate the time the rows were inserted into the queue table, unless different, user-supplied values are inserted. You can then use a SELECT AND CONSUME statement, which operates like a FIFO pop: • Data is returned from the row with the oldest timestamp value in the specified queue table. • The row is deleted from the queue table, guaranteeing that the row is processed only once. If no rows are available, the transaction enters a delay state until one of the following occurs: • A row is inserted into the queue table. • The transaction aborts, either as a result of direct user intervention, such as the ABORT statement, or indirect user intervention, such as a DROP TABLE statement on the queue table. To perform a FIFO peek on a queue table, use a SELECT statement. Global temporary trace Global temporary trace tables are useful for debugging external routines (UDFs, UDMs, and external stored procedures). During execution, external routines can write trace output to columns in a global temporary trace table. Like global temporary tables, global temporary trace tables have persistent definitions, but do not retain rows across sessions. Type UsageChapter 1: Objects Global Temporary Tables SQL Reference: Fundamentals 5 Global Temporary Tables Introduction Global temporary tables allow you to define a table template in the database schema, providing large savings for applications that require well known temporary table definitions. The definition for a global temporary table is persistent and stored in the data dictionary. Space usage is charged to login user temporary space. Each user session can materialize as many as 2000 global temporary tables at a time. How Global Temporary Tables Work To create the base definition for a global temporary table, use the CREATE TABLE statement and specify the keywords GLOBAL TEMPORARY to describe the table type. Once created, the table exists only as a definition. It has no rows and no physical instantiation. When any application in a session accesses a table with the same name as the defined base table, and the table has not already been materialized in that session, then that table is materialized as a real relation using the stored definition. Because that initial invocation is generally due to an INSERT statement, a temporary table—in the strictest sense—is usually populated immediately upon its materialization. There are only two occasions when an empty global temporary table is materialized: • A CREATE INDEX statement is issued on the table. • A COLLECT STATISTICS statement is issued on the table. The following table summarizes this information. Note: Issuing a SELECT, UPDATE, or DELETE on a global temporary table that is not materialized produces the same result as issuing a SELECT, UPDATE, or DELETE on an empty global temporary table that is materialized. WHEN this statement is issued on a global temporary table that has not yet been materialized … THEN a local instance of the global temporary table is materialized and it is … INSERT populated with data upon its materialization. CREATE INDEX … ON TEMPORARY … COLLECT STATISTICS … ON TEMPORARY … not populated with data upon its materialization.Chapter 1: Objects Global Temporary Tables 6 SQL Reference: Fundamentals Example For example, suppose there are four sessions, Session 1, Session 2, Session 3, and Session 4 and two users, User_1 and User_2. Consider the scenario in the following two tables. Step Session … Does this … The result is this … 1 1 The DBA creates a global temporary table definition in the database scheme named globdb.gt1 according to the following CREATE TABLE statement: CREATE GLOBAL TEMPORARY TABLE globdb.gt1, LOG (f1 INT NOT NULL PRIMARY KEY, f2 DATE, f3 FLOAT) ON COMMIT PRESERVE ROWS; The global temporary table definition is created and stored in the database schema. 2 1 User_1 logs on an SQL session and references globdb.gt1 using the following INSERT statement: INSERT globdb.gt1 (1, 980101, 11.1); Session 1 creates a local instance of the global temporary table definition globdb.gt1. This is also referred to as a materialized temporary table. Immediately upon materialization, the table is populated with a single row having the following values. f1=1 f2=980101 f3=11.1 This means that the contents of this local instance of the global temporary table definition is not empty when it is created. From this point on, any INSERT/DELETE/UPDATE statement that references globdb.gt1 in Session 1 is mapped to this local instance of the table. 3 2 User_2 logs on an SQL session and issues the following SELECT statement. SELECT * FROM globdb.gt1; No rows are returned because Session 2 has not yet materialized a local instance of globdb.gt1.Chapter 1: Objects Global Temporary Tables SQL Reference: Fundamentals 7 User_1 and User_2 continue their work, logging onto two additional sessions as described in the following table. 4 2 User_2 issues the following INSERT statement: INSERT globdb.gt1 (2, 980202, 22.2); Session 2 creates a local instance of the global temporary table definition globdb.gt1. The table is populated, immediately upon materialization, with a single row having the following values. f1=2 f2=980202 f3=22.2 From this point on, any INSERT/DELETE/UPDATE statement that references globdb.gt1 in Session 2 is mapped to this local instance of the table. 5 2 User_2 logs again issues the following SELECT statement: SELECT * FROM globdb.gt1; A single row containing the data (2, 980202, 22.2) is returned to the application. 6 1 User_1 logs off from Session 1. The local instance of globdb.gt1 for Session 1 is dropped. 7 2 User_2 logs off from Session 2. The local instance of globdb.gt1 for Session 2 is dropped. Step Session … Does this … The result is this … 1 3 User_1 logs on another SQL session 3 and issues the following SELECT statement: SELECT * FROM globdb.gt1; No rows are returned because Session 3 has not yet materialized a local instance of globdb.gt1. 2 3 User_1 issues the following INSERT statement: INSERT globdb.gt1 (3, 980303, 33.3); Session 3 created a local instance of the global temporary table definition globdb.gt1. The table is populated, immediately upon materialization, with a single row having the following values. f1=3 f2=980303 f3=33.3 From this point on, any INSERT/DELETE/UPDATE statement that references globdb.gt1 in Session 3 maps to this local instance of the table. Step Session … Does this … The result is this …Chapter 1: Objects Global Temporary Tables 8 SQL Reference: Fundamentals With the exception of a few options (see “CREATE TABLE” in SQL Reference: Data Definition Statements for an explanation of the features not available for global temporary base tables), materialized temporary tables have the same properties as permanent tables. After a global temporary table definition is materialized in a session, all further references to the table are made to the materialized table. No additional copies of the base definition are materialized for the session. This global temporary table is defined for exclusive use by the session whose application materialized it. 3 3 User_1 again issues the following SELECT statement: SELECT * FROM globdb.gt1; A single row containing the data (3, 980303, 33.3) is returned to the application. 4 4 User_2 logs on Session 4 and issues the following CREATE INDEX statement: CREATE INDEX (f2) ON TEMPORARY globdb.gt1; An empty local global temporary table named globdb.gt1 is created for Session 4. This is one of only two cases in which a local instance of a global temporary table is materialized without data. The other would be a COLLECT STATISTICS statement—in this case, the following statement: COLLECT STATISTICS ON TEMPORARY globdb.gt1; 5 4 User_2 issues the following SELECT statement: SELECT * FROM globdb.gt1; No rows are returned because the local instance of globdb.gt1 for Session 4 is empty. 6 4 User_2 issues the following SHOW TABLE statement: SHOW TABLE globdb.gt1; CREATE SET GLOBAL TEMPORARY TABLE globdb.gt1, FALLBACK, LOG ( f1 INTEGER NOT NULL, f2 DATE FORMAT 'YYYY-MM-DD', f3 FLOAT) UNIQUE PRIMARY INDEX (f1) ON COMMIT PRESERVE ROWS; 7 4 User_2 issues the following SHOW TEMPORARY TABLE statement: SHOW TEMPORARY TABLE globdb.gt1; CREATE SET GLOBAL TEMPORARY TABLE globdb.gt1, FALLBACK, LOG ( f1 INTEGER NOT NULL, f2 DATE FORMAT 'YYYY-MM-DD', f3 FLOAT) UNIQUE PRIMARY INDEX (f1) INDEX (f2) ON COMMIT PRESERVE ROWS; Note that this report indicates the new index f2 that has been created for the local instance of the temporary table. Step Session … Does this … The result is this …Chapter 1: Objects Volatile Tables SQL Reference: Fundamentals 9 Materialized global temporary tables differ from permanent tables in the following ways: • They are always empty when first materialized. • Their contents cannot be shared by another session. • The contents can optionally be emptied at the end of each transaction. • The materialized table is dropped automatically at the end of each session. Limitations You cannot use the following CREATE TABLE options for global temporary tables: • WITH DATA • Permanent journaling • Referential integrity constraints This means that a temporary table cannot be the referencing or referenced table in a referential integrity constraint. References to global temporary tables are not permitted in FastLoad, MultiLoad, or FastExport. Archive, Restore, and TableRebuild operate on base global temporary tables only. Non-ANSI Extensions Transient journaling options on the global temporary table definition are permitted using the CREATE TABLE statement. You can modify the transient journaling and ON COMMIT options for base global temporary tables using the ALTER TABLE statement. Privileges Required To materialize a global temporary table, you must have the appropriate privilege on the base global temporary table or on the containing database or user as required by the statement that materializes the table. No access logging is performed on materialized global temporary tables, so no access log entries are generated. Volatile Tables Creating Volatile Tables Neither the definition nor the contents of a volatile table persist across a system restart. You must use CREATE TABLE with the VOLATILE keyword to create a new volatile table each time you start a session in which it is needed. Chapter 1: Objects Volatile Tables 10 SQL Reference: Fundamentals What this means is that you can create volatile tables as you need them. Being able to create a table quickly provides you with the ability to build scratch tables whenever you need them. Any volatile tables you create are dropped automatically as soon as your session logs off. Volatile tables are always created in the login user space, regardless of the current default database setting. That is, the database name for the table is the login user name. Space usage is charged to login user spool space. Each user session can materialize as many as 1000 volatile tables at a time. Limitations The following CREATE TABLE options are not permitted for volatile tables: • Permanent journaling • Referential integrity constraints This means that a volatile table cannot be the referencing or referenced table in a referential integrity constraint. • Check constraints • Compressed columns • DEFAULT clause • TITLE clause • Named indexes References to volatile tables are not permitted in FastLoad or MultiLoad. For more information, see “CREATE TABLE” in SQL Reference: Data Definition Statements. Non-ANSI Extensions Volatile tables are not defined in ANSI. Privileges Required To create a volatile table, you do not need any privileges. No access logging is performed on volatile tables, so no access log entries are generated. Volatile Table Maintenence Among Multiple Sessions Volatile tables are private to a session. This means that you can log on multiple sessions and create volatile tables with the same name in each session. However, at the time you create a volatile table, the name must be unique among all global and permanent temporary table names in the database that has the name of the login user.Chapter 1: Objects Volatile Tables SQL Reference: Fundamentals 11 For example, suppose you log on two sessions, Session 1 and Session 2. Assume the default database name is your login user name. Consider the following scenario. Stage In Session 1, you … In Session 2, you … The result is this … 1 Create a volatile table named VT1. Create a volatile table named VT1. Each session creates its own copy of volatile table VT1 using your login user name as the database. 2 Create a permanent table with an unqualified table name of VT2. Session 1 creates a permanent table named VT2 using your login user name as the database. 3 Create a volatile table named VT2. Session 2 receives a CREATE TABLE error, because there is already a permanent table with that name. 4 Create a volatile table named VT3. Session 1 creates a volatile table named VT3 using your login user name as the database. 5 Create a permanent table with an unqualified table name of VT3. Session 2 creates a permanent table named VT3 using your login user name as the database. Because a volatile table is known only to the session that creates it, a permanent table with the same name as the volatile table VT3 in Session 1 can be created as a permanent table in Session 2. 6 Insert into VT3. Session 1 references volatile table VT3. Note: Volatile tables take precedence over permanent tables in the same database in a session. Because Session 1 has a volatile table VT3, any reference to VT3 in Session 1 is mapped to the volatile table VT3 until it is dropped (see Step 10). On the other hand, in Session 2, references to VT3 remain mapped to the permanent table named VT3. 7 Create volatile table VT3. Session 2 receives a CREATE TABLE error for attempting to create the volatile table VT3 because of the existence of that permanent table. 8 Insert into VT3. Session 2 references permanent table VT3. 9 Drop VT3. Session 2 drops volatile table VT3. 10 Select from VT3. Session 1 references the permanent table VT3.Chapter 1: Objects Columns 12 SQL Reference: Fundamentals Columns Definition A column is a structural component of a table and has a name and a declared type. Each row in a table has exactly one value for each column. Each value in a row is a value in the declared type of the column. The declared type includes nulls and values of the declared type. A column value is the smallest unit of data that can be selected from or updated for a table. Defining Columns The column definition clause of the CREATE TABLE statement defines the table column elements. A name and a data type must be specified for each column defined for a table. Each column can be further defined with one or more attribute definitions. Here is an example that creates a table called employee with three columns: CREATE TABLE employee (deptno INTEGER ,name CHARACTER(23) ,hiredate DATE); The following optional subclauses are also elements of the SQL column definition clause: • Data type attribute declaration, such as NOT NULL, FORMAT, and TITLE • COMPRESS column storage attributes clause • Column constraint attributes clause, such as PRIMARY KEY • UNIQUE table-level definition clause • REFERENCES table-level definition clause • CHECK constraint table-level definition clause Related Topics FOR more information on … SEE … data types “Data Types” on page 13. CREATE TABLE and the column definition clause SQL Reference: Data Definition Statements.Chapter 1: Objects Data Types SQL Reference: Fundamentals 13 Data Types Introduction Every data value belongs to an SQL data type. For example, when you define a column in a CREATE TABLE statement, you must specify the data type of the column. The set of data values that a column defines can belong to one of the following data types: Numeric Data Types A numeric value is either an exact numeric number (integer or decimal) or an approximate numeric number (floating point). Use the following SQL data types to specify numeric values. Character Data Types Character data types represent characters that belong to a given character set. Use the following SQL data types to specify character data. • Numeric • Character • Datetime • Interval • Byte • UDT Type Description BIGINT Represents a signed, binary integer value from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. INTEGER Represents a signed, binary integer value from -2,147,483,648 to 2,147,483,647. SMALLINT Represents a signed binary integer value in the range -32768 to 32767. BYTEINT Represents a signed binary integer value in the range -128 to 127. REAL Represent a value in sign/magnitude form. DOUBLE PRECISION FLOAT DECIMAL [(n[,m])] Represent a decimal number of n digits, with m of those n digits to the right of the decimal point. NUMERIC [(n[,m])] Type Description CHAR Represents a fixed length character string for Teradata Database internal character storage. VARCHAR(n) Represents a variable length character string of length n for Teradata Database internal character storage.Chapter 1: Objects Data Types 14 SQL Reference: Fundamentals DateTime Data Types DateTime values represent dates, times, and timestamps. Use the following SQL data types to specify DateTime values. Interval Data Types An interval value is a span of time. There are two mutually exclusive interval type categories. LONG VARCHAR LONG VARCHAR specifies the longest permissible variable length character string for Teradata Database internal character storage. CLOB Represents a large character string. A character large object (CLOB) column can store character data, such as simple text, HTML, or XML documents. Type Description Type Description DATE Represents a date value that includes year, month, and day components. TIME Represents a time value that includes hour, minute, second, and fractional second components. TIMESTAMP Represents a timestamp value that includes year, month, day, hour, minute, second, and fractional second components. TIME WITH TIME ZONE Represents a time value that includes hour, minute, second, fractional second, and time zone components. TIMESTAMP WITH TIIME ZONE Represents a timestamp value that includes year, month, day, hour, minute, second, fractional second, and time zone components. Category Type Description Year-Month • INTERVAL YEAR • INTERVAL YEAR TO MONTH • INTERVAL MONTH Represent a time span that can include a number of years and months. Day-Time • INTERVAL DAY • INTERVAL DAY TO HOUR • INTERVAL DAY TO MINUTE • INTERVAL DAY TO SECOND • INTERVAL HOUR • INTERVAL HOUR TO MINUTE • INTERVAL HOUR TO SECOND • INTERVAL MINUTE • INTERVAL MINUTE TO SECOND • INTERVAL SECOND Represent a time span that can include a number of days, hours, minutes, or seconds.Chapter 1: Objects Data Types SQL Reference: Fundamentals 15 Byte Data Types Byte data types store raw data as logical bit streams. For any machine, BYTE, VARBYTE, and BLOB data is transmitted directly from the memory of the client system. BLOB is ANSI SQL-2003-compliant. BYTE and VARBYTE are Teradata extensions to the ANSI SQL-2003 standard. UDT Data Types UDT data types are custom data types that you define with the CREATE TYPE statement. Teradata Database supports distinct and structured UDTs. For more details on UDTs, including a synopsis of the steps you take to develop and use UDTs, see “User-Defined Types” on page 58. Related Topics For detailed information on data types, see SQL Reference: Data Types and Literals. Type Description BYTE Represents a fixed-length binary string. VARBYTE Represents a variable-length binary string. BLOB Represents a large binary string of raw bytes. A binary large object (BLOB) column can store binary objects, such as graphics, video clips, files, and documents. Type Description Distinct A UDT that is based on a single predefined data type, such as INTEGER or VARCHAR. Structured A UDT that is a collection of one or more fields called attributes, each of which is defined as a predefined data type or other UDT (which allows nesting).Chapter 1: Objects Keys 16 SQL Reference: Fundamentals Keys Definitions Keys and Referential Integrity Teradata Database uses primary and foreign keys to maintain referential integrity. For additional information, see “Referential Integrity” on page 36. Effect on Row Distribution Because Teradata Database uses a unique primary or secondary index to enforce a primary key, the primary key can affect how Teradata Database distributes and retrieves rows. For more information, see “Primary Indexes” on page 22 and “Secondary Indexes” on page 25. Differences Between Primary Keys and Primary Indexes The following table summarizes the differences between keys and indexes using the primary key and primary index for purposes of comparison. Term Definition Primary Key A primary key is a column, or combination of columns, in a table that uniquely identifies each row in the table. The values defining a primary key for a table: • Must be unique • Cannot change • Cannot be null Foreign Key A foreign key is a column, or combination of columns, in a table that is also the primary key in one or more additional tables in the same database. Foreign keys provide a mechanism to link related tables based on key values. Primary Key Primary Index Important element of logical data model. Not used in logical data model. Used to maintain referential integrity. Used to distribute and retrieve data. Must be unique to identify each row. Can be unique or nonunique. Values cannot change. Values can change. Cannot be null. Can be null. Does not imply access path. Defines the most common access path. Not required for physical table definition. Required for physical table definition.Chapter 1: Objects Indexes SQL Reference: Fundamentals 17 Indexes Definition An index is a mechanism that the SQL query optimizer can use to make table access more performant. Indexes enhance data access by providing a more-or-less direct path to stored data to avoid performing full table scans to locate the small number of rows you typically want to retrieve or update. The Teradata Database parallel architecture makes indexing an aid to better performance, not a crutch necessary to ensure adequate performance. Full table scans are not something to be feared in the Teradata Database environment. This means that the sorts of unplanned, ad hoc queries that characterize the data warehouse process, and that often are not supported by indexes, perform very effectively for Teradata Database using full table scans. The classic index for a relational database is itself a file made up of rows having two parts: • A (possibly unique) data field in the referenced table. • A pointer to the location of that row in the base table (if the index is unique) or a pointer to all possible locations of rows with that data field value (if the index is nonunique). Because the Teradata Database is a massively parallel architecture, it requires a more efficient means of distributing and retrieving its data. One such method is hashing. All Teradata Database indexes are based on row hash values rather than raw table column values, even though secondary, hash, and join indexes can be stored in order of their values to make them more useful for satisfying range conditions. Selectivity of Indexes An index that retrieves many rows is said to have weak selectivity. An index that retrieves few rows is said to be strongly selective. The more strongly selective an index is, the more useful it is. In some cases, it is possible to link together several weakly selective nonunique secondary indexes by bit mapping them. The result is effectively a strongly selective index and a dramatic reduction in the number of table rows that must be accessed. For more information on linking weakly selective secondary indexes into a strongly selective unit using bit mapping, see “NUSI Bit Mapping” on page 28. Row Hash and RowID Teradata Database table rows are self-indexing with respect to their primary index and so require no additional storage space. When a row is inserted into a table, the relational database manager stores the 32-bit row hash value of the primary index with it. Because row hash values are not necessarily unique, the relational database manager also generates a unique 32-bit numeric value (called the Uniqueness Value) that it appends to the row hash value, forming a unique RowID. This RowID makes each row in a table uniquely identifiable and ensures that hash collisions do not occur.Chapter 1: Objects Indexes 18 SQL Reference: Fundamentals If a table is defined with a partitioned primary index (PPI), the RowID also includes the partition number to which the row was assigned. For more information on PPIs, see “Partitioned and Non-Partitioned Primary Indexes” on page 20. The first row having a specific row hash value is always assigned a uniqueness value of 1, which becomes the highest current uniqueness value. Thereafter, each time another row having the same row hash value is inserted, the row is assigned the current high value incremented by 1, and that value becomes the current high value. Table rows having the same row hash value are stored on disk sorted in the ascending order of RowID. Uniqueness values are not reused except for the special case in which the highest valued row within a row hash is deleted from a table. A RowID for a row might change, for instance, when a primary index or partitioning column is changed, or when there is complex update of the table. Index Hash Mapping Rows are distributed across the AMPS using a hashing algorithm that computes a row hash value based on the primary index. The row hash is a 32-bit value. The higher-order 16 bits of a hash value determine an associated hash bucket. Teradata Database databases have 65536 hash buckets. The hash buckets are distributed as evenly as possible among the AMPs on a system. Teradata Database maintains a hash map—an index of which hash buckets live on which AMPs—that it uses to determine whether rows belong to an AMP based on their row hash values. Row assignment is performed in a manner that ensures as equal a distribution as possible among all the AMPs on a system. Advantages of Indexes The intent of indexes is to lessen the time it takes to retrieve rows from a database. The faster the retrieval, the better. Disadvantages of Indexes Perhaps not so obvious is the disadvantage of using indexes. • They must be updated every time a row is updated, deleted, or added to a table. This is only a consideration for indexes other than the primary index in the Teradata Database environment. The more indexes you have defined for a table, the bigger the potential update downside becomes. Because of this, secondary, join, and hash indexes are rarely appropriate for OLTP situations. • All Teradata Database secondary indexes are stored in subtables, and join and hash indexes are stored in separate tables, exerting a burden on system storage space.Chapter 1: Objects Indexes SQL Reference: Fundamentals 19 • When FALLBACK is defined for a table, a further storage space burden is created because secondary index subtables are always duplicated whenever FALLBACK is defined for a table. An additional burden on system storage space is exerted when FALLBACK is defined for join indexes or hash indexes or both. For this reason, it is extremely important to use the EXPLAIN modifier to determine optimum data manipulation statement syntax and index usage before putting statements and indexes to work in a production environment. For more information on EXPLAIN, see SQL Reference: Data Manipulation Statements. Teradata Database Index Types Teradata Database provides four different index types: • Primary index All Teradata Database tables require a primary index because the system distributes tables on their primary indexes. Primary indexes can be: • Unique or nonunique • Partitioned or non-partitioned • Secondary index Secondary indexes can be unique or nonunique. • Join index (JI) • Hash index Unique Indexes A unique index, like a primary key, has a unique value for each row in a table. Teradata Database defines two different types of unique index. • Unique primary index (UPI) UPIs provide optimal data distribution and are typically assigned to the primary key for a table. When a NUPI makes better sense for a table, then the primary key is frequently assigned to be a USI. • Unique secondary index (USI) USIs guarantee that each complete index value is unique, while ensuring that data access based on it is always a two-AMP operation. Nonunique Indexes A nonunique index does not require its values to be unique. There are occasions when a nonunique index is the best choice as the primary index for a table. NUSIs are also very useful for many decision support situations.Chapter 1: Objects Indexes 20 SQL Reference: Fundamentals Partitioned and Non-Partitioned Primary Indexes Primary indexes can be partitioned or non-partitioned. A non-partitioned primary index (NPPI) is the traditional primary index by which rows are assigned to AMPs. A partitioned primary index (PPI) allows rows to be partitioned, based on some set of columns, on the AMP to which they are distributed, and ordered by the hash of the primary index columns within the partition. A PPI can be used to improve query performance through partition elimination. A PPI provides a useful alternative to an NPPI for executing range queries against a table, while still providing efficient join and aggregation strategies on the primary index. Join Indexes A join index is an indexing structure containing columns from one or more base tables and is generally used to resolve queries and eliminate the need to access and join the base tables it represents. Teradata Database join indexes can be defined in the following general ways. • Simple or aggregate • Single- or multitable • Hash-ordered or value-ordered • Complete or sparse For details, see “Join Indexes” on page 30. Hash Indexes Hash indexes are used for the same purposes as are single-table join indexes, and are less complicated to define. However, a join index offers more choices. For additional information, see “Hash Indexes” on page 34. Creating Indexes For a Table Use the CREATE TABLE statement to define a primary index and one or more secondary indexes. You can define the primary index (and any secondary index) as unique, depending on whether duplicate values are to be allowed in the indexed column set. A partitioned primary index cannot be defined as unique if one or more partitioning columns are not included in the primary index. To create hash or join indexes, use the CREATE HASH INDEX and CREATE JOIN INDEX statements, respectively.Chapter 1: Objects Indexes SQL Reference: Fundamentals 21 Using EXPLAIN and Teradata Index Wizard to Determine the Usefulness of Indexes One important thing to remember is that the use of indexes by the optimizer is not under user control in a relational database management system. That is, the only references made to indexes in the SQL language concern their definition and not their use. The SQL data manipulation language statements do not provide for any specification of indexes. There are several implications of this behavior. • First, it is very important to collect statistics regularly to ensure that the optimizer has access to current information about how to best optimize any query or update made to the database. For additional information concerning collecting and maintaining accurate database statistics, see “COLLECT STATISTICS” in SQL Reference: Data Definition Statements. • Second, it is even more important to build your queries and updates in such a way that you know their performance will be most optimal. Apart from good logical database design, one way to ensure that you are accessing your data in the most efficient manner possible is to use the EXPLAIN modifier to try out various candidate queries or updates and to note which indexes are used by the optimizer in their execution (if any) as well as examining the relative length of time required to complete the operation. There are several methods you can use to determine optimal sets of secondary indexes tailored to particular application workloads: • Teradata Index Wizard • EXPLAIN reports The Teradata Index Wizard client utility provides a method of determining optimum secondary indexes for a given SQL statement workload automatically and then verifying that the proposed indexes actually produce the expected performance enhancements. See the following references for more information about the Teradata Index Wizard: • Teradata Index Wizard User Guide • SQL Reference: Statement and Transaction Processing You can produce and analyze EXPLAIN reports using either the Teradata Visual Explain client utility or the SQL EXPLAIN request modifier. For each statement in the request, EXPLAIN output provides you with the following basic information: • The step-by-step access method the optimizer would use to execute the specified data manipulation statement given the current set of table statistics it has to work with. • The relative time it would take to perform the data manipulation statement. While you cannot rely on the reported statement execution time as an absolute, you can rely on it as a relative means for comparison with other candidate data manipulation statements against the same tables with the same statistics defined.Chapter 1: Objects Primary Indexes 22 SQL Reference: Fundamentals Primary Indexes Introduction The primary index for a table controls the distribution and retrieval of the data for that table across the AMPs. Both distribution and retrieval of the data is controlled using the Teradata Database hashing algorithm (see “Row Hash and RowID” on page 17 and “Index Hash Mapping” on page 18). If the primary index is defined as a partitioned primary index (PPI), the data is partitioned, based on some set of columns, on each AMP, and ordered by the hash of the primary index columns within the partition. Data accessed based on a primary index is always a one-AMP operation because a row and its index are stored on the same AMP. This is true whether the primary index is unique or nonunique, and whether it is partitioned or non-partitioned. Tables Require a Primary Index All Teradata Database tables require a primary index. To create a primary index, use the CREATE TABLE statement. If you do not assign a primary index explicitly when you create a table, Teradata Database assigns a primary index, based on the following rules. FOR more information on … SEE … using the EXPLAIN request modifier SQL Reference: Data Manipulation Statements using the Teradata Visual Explain client utility Teradata Visual Explain User Guide additional performance-related information about how to use the access and join plan reports produced by EXPLAIN to optimize the performance of your databases • Database Design • Performance Management WHEN a CREATE TABLE statement defines a … THEN Teradata Database selects the … Primary Index Primary Key Unique Column Constraint No Yes No primary key column set to be a UPI. No No Yes first column or columns having a UNIQUE constraint to be a UPI. No Yes Yes primary key column set to be a UPI.Chapter 1: Objects Primary Indexes SQL Reference: Fundamentals 23 In general, the best practice is to specify a primary index instead of having Teradata Database select a default primary index. Uniform Distribution of Data and Optimal Access Considerations When choosing the primary index for a table, there are two essential factors to keep in mind: uniform distribution of the data and optimal access. With respect to uniform data distribution, consider the following factors: • The more distinct the primary index values, the better. • Rows having the same primary index value are distributed to the same AMP. • Parallel processing is more efficient when table rows are distributed evenly across the AMPs. With respect to optimal data access, consider the following factors: • Choose the primary index on the most frequently used access path. For example, if rows are generally accessed by a range query, consider defining a partitioned primary index on the table that creates a useful set of partitions. If the table is frequently joined with a specific set of tables, consider defining the primary index on the column set that is typically used as the join condition. • Primary index operations must provide the full primary index value. • Primary index retrievals on a single value are always one-AMP operations. While it is true that the columns you choose to be the primary index for a table are often the same columns that define the primary key, it is also true that primary indexes often comprise fields that are neither unique nor components of the primary key for the table. Unique and Nonunique Primary Index Considerations In addition to uniform distribution of data and optimal access considerations, other guidelines and performance considerations apply to selecting a unique or a nonunique column set as the primary index for a table. No No No first column defined for the table to be a NUPI. If the data type of the first column in the table is UDT or LOB, then the CREATE TABLE operation aborts and the system returns an error message. WHEN a CREATE TABLE statement defines a … THEN Teradata Database selects the … Primary Index Primary Key Unique Column ConstraintChapter 1: Objects Primary Indexes 24 SQL Reference: Fundamentals Generally, other considerations can include the following: • Primary and other alternate key column sets • The value range seen when using predicates in a WHERE clause • Whether access can involve multiple rows or a spool file or both For more information on criteria for selecting a primary index, see Database Design. Partitioning Considerations The decision to define a Partitioned Primary Index (PPI) for a table depends on how its rows are most frequently accessed. PPIs are designed to optimize range queries while also providing efficient primary index join strategies. For range queries, only rows of the qualified partitions need to be accessed. PPI increases query efficiency by avoiding full table scans without the overhead and maintenance costs of secondary indexes. Various partitioning strategies are possible: • For some applications, defining the partitions such that each has approximately the same number of rows might be an effective strategy. • For other applications, it might be desirable to have a varying number of rows per partition. For example, more frequently accessed data (such as for the current year) might be divided into finer partitions (such as weeks) but other data (such as previous years) may have coarser partitions (such as months or multiples of months). • Alternatively, it might be important to define each range with equal width, even if the number of rows per range varies. The most important factors for PPIs are accessibility and maximization of partition elimination. In all cases, it is critical for parallel efficiency to define a primary index that distributes the rows of the table fairly evenly across the AMPs. For more information on partitioning considerations, see Database Design. Primary Index Summary Teradata Database primary indexes have the following properties. • Defined with the CREATE TABLE data definition statement. CREATE INDEX is used only to create secondary indexes. • Modified with the ALTER TABLE data definition statement. Some modifications, such as partitioning and primary index columns, require an empty table. • Automatically assigned by CREATE TABLE if you do not explicitly define a primary index. However, the best practice is to always specify the primary index, because the default may not be appropriate for the table. • Can be composed of as many as 64 columns. • A maximum of one can be defined per table.Chapter 1: Objects Secondary Indexes SQL Reference: Fundamentals 25 • Can be partitioned or non-partitioned. Partitioned primary indexes are not automatically assigned. You must explicitly define a partitioned primary index. • Can be unique or non-unique. Note that a partitioned primary index can only be unique if all the partitioning columns are also included as primary index columns. If the primary index does not include all the partitioning columns, uniqueness on the primary index columns may be enforced with a unique secondary index on the same columns as the primary index. • Defined as non-unique if the primary index is not defined explicitly as unique or if the primary index is specified for a single column SET table. • Controls data distribution and retrieval using the Teradata hashing algorithm. • Improves performance when used correctly in the WHERE clause of an SQL data manipulation statement to perform the following actions. • Single-AMP retrievals • Joins between tables with identical primary indexes, the optimal scenario • Partition elimination when the primary index is partitioned Related Topics Consult the following books for more detailed information on using primary indexes to enhance the performance of your databases: • Database Design • Performance Management Secondary Indexes Introduction Secondary indexes are never required for Teradata Database tables, but they can often improve system performance. You create secondary indexes explicitly using the CREATE TABLE and CREATE INDEX statements. Teradata Database can implicitly create unique secondary indexes; for example, when you use a CREATE TABLE statement that specifies a primary index, Teradata Database implicitly creates unique secondary indexes on column sets that you specify using PRIMARY KEY or UNIQUE constraints. Creating a secondary index causes the Teradata Database to build a separate internal subtable to contain the index rows, thus adding another set of rows that requires updating each time a table row is inserted, deleted, or updated. Nonunique secondary indexes (NUSIs) can be specified as either hash-ordered or value-ordered. Value-ordered NUSIs are limited to a single numeric-valued (including DATE) sort key whose size is four or fewer bytes.Chapter 1: Objects Secondary Indexes 26 SQL Reference: Fundamentals Secondary index subtables are also duplicated whenever a table is defined with FALLBACK. After the table is created and usage patterns have developed, additional secondary indexes can be defined with the CREATE INDEX statement. Differences Between Unique and Nonunique Secondary Indexes Teradata Database processes USIs and NUSIs very differently. Consider the following statements that define a USI and a NUSI. The following table highlights differences in the build process for the preceding statements. Secondary Index Statement USI CREATE UNIQUE INDEX (customer_number) ON customer_table; NUSI CREATE INDEX (customer_name) ON customer_table; USI Build Process NUSI Build Process Each AMP accesses its subset of the base table rows. Each AMP accesses its subset of the base table rows. Each AMP copies the secondary index value and appends the RowID for the base table row. Each AMP builds a spool file containing each secondary index value found followed by the RowID for the row it came from. Each AMP creates a Row Hash on the secondary index value and puts all three values onto the BYNET. For hash-ordered NUSIs, each AMP sorts the RowIDs for each secondary index value into ascending order. For value-ordered NUSIs, the rows are sorted by NUSI value order. The appropriate AMP receives the data and creates a row in the index subtable. If the AMP receives a row with a duplicate index value, an error is reported. For hash-ordered NUSIs, each AMP creates a row hash value for each secondary index value on a local basis and creates a row in its portion of the index subtable. For value-ordered NUSIs, storage is based on NUSI value rather than the row hash value for the secondary index. Each row contains one or more RowIDs for the index value.Chapter 1: Objects Secondary Indexes SQL Reference: Fundamentals 27 Consider the following statements that access a USI and a NUSI. The following table identifies differences for the access process of the preceding statements. Note: The NUSI is not used if the estimated number of rows to be read in the base table is equal to or greater than the estimated number of data blocks in the base table; in this case, a full table scan is done, or, if appropriate, partition scans are done. NUSIs and Covering The Optimizer aggressively pursues NUSIs when they cover a query. Covered columns can be specified anywhere in the query, including the select list, the WHERE clause, aggregate functions, GROUP BY clauses, expressions, and so on. Presence of a WHERE condition on each indexed column is not a prerequisite for using a NUSI to cover a query. Value-Ordered NUSIs Value-ordered NUSIs are very efficient for range conditions, and more so when strongly selective or when combined with covering. Because the NUSI rows are sorted by data value, it is possible to search only a portion of the index subtable for a given range of key values. Secondary Index Statement USI SELECT * FROM customer_table WHERE customer_number=12; NUSI SELECT * FROM customer_table WHERE customer_name = 'SMITH'; USI Access Process NUSI Access Process The supplied index value hashes to the corresponding secondary index row. A message containing the secondary index value is broadcast to every AMP. The retrieved base table RowID is used to access the specific data row. For a hash-ordered NUSI, each AMP creates a local row hash and uses it to access its portion of the index subtable to see if a corresponding row exists. Value-ordered NUSI index subtable values are scanned only for the range of values specified by the query. The process is complete. This is typically a two-AMP operation. If an index row is found, the AMP uses the RowID or value order list to access the corresponding base table rows. The process is complete. This is always an all-AMP operation, with the exception of a NUSI that is defined on the same columns as the primary index.Chapter 1: Objects Secondary Indexes 28 SQL Reference: Fundamentals Value-ordered NUSIs have the following limitations. • The sort key is limited to a single numeric or DATE column. • The sort key column must be four or fewer bytes. The following query is an example of the sort of SELECT statement for which value-ordered NUSIs were designed. SELECT * FROM Orders WHERE o_date BETWEEN DATE '1998-10-01' AND DATE '1998-10-07'; Multiple Secondary Indexes and Composites Database designers frequently define multiple secondary indexes on a table. For example, the following statements define two secondary indexes on the EMPLOYEE table: CREATE INDEX (department_number) ON EMPLOYEE; CREATE INDEX (job_code) ON EMPLOYEE; The WHERE clause in the following query specifies the columns that have the secondary indexes defined on them: SELECT last_name, first_name, salary_amount FROM employee WHERE department_number = 500 AND job_code = 2147; Whether the Optimizer chooses to include one, all, or none of the secondary indexes in its query plan depends entirely on their individual and composite selectivity. NUSI Bit Mapping Bit mapping is a technique used by the Optimizer to effectively link several weakly selective indexes in a way that creates a result that drastically reduces the number of base rows that must be accessed to retrieve the desired data. The process determines common rowIDs among multiple NUSI values by means of the logical intersection operation. Bit mapping is significantly faster than the three-part process of copying, sorting, and comparing rowID lists. Additionally, the technique dramatically reduces the number of base table I/Os required to retrieve the requested rows. FOR more information on … SEE … multiple secondary index access Database Design composite secondary index access other aspects of index selectionChapter 1: Objects Secondary Indexes SQL Reference: Fundamentals 29 Secondary Index Summary Teradata SQL secondary indexes have the following properties. • Can enhance the speed of data retrieval. Because of this, secondary indexes are most useful in decision support applications. • Do not affect data distribution. • Can be a maximum of 32 defined per table. • Can be composed of as many as 64 columns. • For a value-ordered NUSI, only a single numeric or DATE column of four or fewer bytes may be specified for the sort key. • For a hash-ordered covering index, only a single column may be specified for the hash ordering. • Can be created or dropped dynamically as data usage changes or if they are found not to be useful for optimizing data retrieval performance. • Require additional disk space to store subtables. • Require additional I/Os on inserts and deletes. Because of this, secondary indexes might not be as useful in OLTP applications. • Should not be defined on columns whose values change frequently. • Should not include columns that do not enhance selectivity. • Should not use composite secondary indexes when multiple single column indexes and bit mapping might be used instead. • Composite secondary indexes is useful if it reduces the number of rows that must be accessed. • The Optimizer does not use composite secondary indexes unless there are explicit values for each column in the index. • Most efficient for selecting a small number of rows. • Can be unique or non-unique. • NUSIs can be hash-ordered r value-ordered, and can optionally include covering columns. • Cannot be partitioned, but can be defined on a table with a partitioned primary index. FOR more information on … SEE … when Teradata Database performs NUSI bit mapping Database Design how NUSI bit maps are computed using the EXPLAIN modifier to determine if bit mapping is being used for your indexes • Database Design • SQL Reference: Data Manipulation StatementsChapter 1: Objects Join Indexes 30 SQL Reference: Fundamentals Summary of USI and NUSI Properties Unique and nonunique secondary indexes have the following properties. For More Information About Secondary Indexes See “SQL Data Definition Language Statement Syntax” of SQL Reference: Data Definition Statements under “CREATE TABLE” and “CREATE INDEX” for more information. Also consult the following manuals for more detailed information on using secondary indexes to enhance the performance of your databases: • Database Design • Performance Management Join Indexes Introduction Join indexes are not indexes in the usual sense of the word. They are file structures designed to permit queries (join queries in the case of multitable join indexes) to be resolved by accessing the index instead of having to access and join their underlying base tables. You can use join indexes to: • Define a prejoin table on frequently joined columns (with optional aggregation) without denormalizing the database. • Create a full or partial replication of a base table with a primary index on a foreign key column table to facilitate joins of very large tables by hashing their rows to the same AMP as the large table. • Define a summary table without denormalizing the database. You can define a join index on one or several tables. Depending on how the index is defined, join indexes can also be useful for queries where the index structure contains only some of the columns referenced in the statement. This situation is referred to as a partial cover of the query. Unlike traditional indexes, join indexes do not implicitly store pointers to their associated base table rows. Instead, they are generally used as a fast path final access point that eliminates the USI NUSI • Guarantee that each complete index value is unique. • Any access using the index is a two-AMP operation. • Useful for locating rows having a specific value in the index. • Can be hash-ordered or value-ordered. Value-ordered NUSIs are particularly useful for enhancing the performance of range queries. • Can include covering columns. • Any access using the index is an all-AMP operation.Chapter 1: Objects Join Indexes SQL Reference: Fundamentals 31 need to access and join the base tables they represent. They substitute for rather than point to base table rows. The only exception to this is the case where an index partially covers a query. If the index is defined using either the ROWID keyword or the UPI or USI of its base table as one of its columns, then it can be used to join with the base table to cover the query. Defining Join Indexes To create a join index, use the CREATE JOIN INDEX statement. For example, suppose that a common task is to look up customer orders by customer number and date. You might create a join index like the following, linking the customer table, the order table, and the order detail table: CREATE JOIN INDEX cust_ord2 AS SELECT cust.customerid,cust.loc,ord.ordid,item,qty,odate FROM cust, ord, orditm WHERE cust.customerid = ord.customerid AND ord.ordid = orditm.ordid; Multitable Join Indexes A multitable join index stores and maintains the joined rows of two or more tables and, optionally, aggregates selected columns. Multitable join indexes are for join queries that are performed frequently enough to justify defining a prejoin on the joined columns. A multitable join index is useful for queries where the index structure contains all the columns referenced by one or more joins, thereby allowing the index to cover that part of the query, making it possible to retrieve the requested data from the index rather than accessing its underlying base tables. For obvious reasons, an index with this property is often referred to as a covering index. Single-Table Join Indexes Single-table join indexes are very useful for resolving joins on large tables without having to redistribute the joined rows across the AMPs. Single-table join indexes facilitate joins by hashing a frequently joined subset of base table columns to the same AMP as the table rows to which they are frequently joined. This enhanced geography eliminates BYNET traffic as well as often providing a smaller sized row to be read and joined. Aggregate Join Indexes When query performance is of utmost importance, aggregate join indexes offer an extremely efficient, cost-effective method of resolving queries that frequently specify the same aggregate operations on the same column or columns. When aggregate join indexes are available, the system does not have to repeat aggregate calculations for every query.Chapter 1: Objects Join Indexes 32 SQL Reference: Fundamentals You can define an aggregate join index on two or more tables, or on a single table. A single-table aggregate join index includes a summary table with: • A subset of columns from a base table • Additional columns for the aggregate summaries of the base table columns Sparse Join Indexes You can create join indexes that limit the number of rows in the index to only those that are accessed when, for example, a frequently run query references only a small, well known subset of the rows of a large base table. By using a constant expression to filter the rows included in the join index, you can create what is known as a sparse index. Any join index, whether simple or aggregate, multitable or single-table, can be sparse. To create a sparse index, use the WHERE clause in the CREATE JOIN INDEX statement. Effects of Join Indexes Join index limits affect the following Teradata Database functions and features. • Load Utilities MultiLoad and FastLoad utilities cannot be used to load or unload data into base tables that have a join index defined on them because join indexes are not maintained during the execution of these utilities. If an error occurs because of the join index, drop the join index and recreate it after loading data into that table. The TPump utility, which performs standard SQL row inserts and updates, can be used to load or unload data into base tables with join indexes because it properly maintains join indexes during execution. However, in some cases, performance may improve by dropping join indexes on the table prior to the load and recreating them after the load. • ARC (Archive and Recovery) Archive and Recovery cannot be used on a join index itself. Archiving is permitted on a base table or database that has an associated join index defined. Before a restore of such a base table or database, you must drop the existing join index definition. Before using any such index again in the execution of queries, you must recreate the join index definition. • Permanent Journal Recovery Using a permanent journal to recover a base table (that is, ROLLBACK or ROLLFORWARD) with an associated join index defined is permitted. The join index is not automatically rebuilt during the recovery process. Instead, it is marked as non-valid and it must be dropped and recreated before it can be used again in the execution of queries.Chapter 1: Objects Join Indexes SQL Reference: Fundamentals 33 Comparison of Join Indexes and Base Tables In most respects, a join index is similar to a base table. For example, you can do the following things to a join index: • Create nonunique secondary indexes on its columns. • Execute COLLECT STATISTICS, DROP STATISTICS, HELP, and SHOW statements. • Partition its primary index, if it is a non-compressed join index. Note: Unlike a base table that has a PPI, however, you cannot use COLLECT STATISTICS to collect PARTITION statistics on a non-compressed join index that has a PPI. Unlike base tables, you cannot do the following things with join indexes: • Query or update join index rows explicitly. • Store and maintain arbitrary query results such as expressions. Note: You can maintain aggregates or sparse indexes if you define the join index to do so. • Create explicit unique indexes on its columns. Related Topics FOR more information on … SEE … creating join indexes “CREATE JOIN INDEX” in SQL Reference: Data Definition Statements dropping join indexes “DROP JOIN INDEX” in SQL Reference: Data Definition Statements displaying the attributes of the columns defined by a join index “HELP JOIN INDEX” in SQL Reference: Data Definition Statements using join indexes to enhance the performance of your databases • Database Design • Performance Management • SQL Reference: Data Definition Statements • database design considerations for join indexes • improving join index performance Database DesignChapter 1: Objects Hash Indexes 34 SQL Reference: Fundamentals Hash Indexes Introduction Hash indexes are used for the same purposes as single-table join indexes. The following table lists the principal differences between hash indexes and single-table join indexes. Hash indexes are useful for creating a full or partial replication of a base table with a primary index on a foreign key column to facilitate joins of very large tables by hashing them to the same AMP. You can define a hash index on one table only. The functionality of hash indexes is a subset to that of single-table join indexes. Comparison of Hash and Single-Table Join Indexes The reasons for using hash indexes are similar to those for using single-table join indexes. Not only can hash indexes optionally be specified to be distributed in such a way that their rows are AMP-local with their associated base table rows, they also implicitly provide an alternate direct access path to those base table rows. This facility makes hash indexes somewhat similar to secondary indexes in function. Hash indexes are also useful for covering queries so that the base table need not be accessed at all. Hash Index Single-Table Join Index Column list cannot contain aggregate or ordered analytical functions. Column list can contain aggregate functions. Cannot have a secondary index. Can have a secondary index. Supports transparently added, system-defined columns that point to the underlying base table rows. Does not implicitly add underlying base table row pointers. Pointers to underlying base table rows can be created explicitly by defining one element of the column list using the ROWID keyword or the UPI or USI of the base table. FOR information on … SEE … using CREATE HASH INDEX to create a hash index SQL Reference: Data Definition Statements using DROP HASH INDEX to drop a hash index using HELP HASH INDEX to display the data types of the columns defined by a hash index database design considerations for hash indexes Database DesignChapter 1: Objects Hash Indexes SQL Reference: Fundamentals 35 The following list summarizes the similarities of hash and single-table join indexes: • Primary function of both is to improve query performance. • Both are maintained automatically by the system when the relevant columns of their base table are updated by a DELETE, INSERT, UPDATE, or MERGE statement. • Both can be the object of any of the following SQL statements: • COLLECT STATISTICS • DROP STATISTICS • HELP INDEX • SHOW • Both receive their space allocation from permanent space and are stored in distinct tables. • The storage organization for both supports a compressed format to reduce storage space, but for a hash index, Teradata Database makes this decision. • Both can be FALLBACK protected. • Neither can be queried or directly updated. • Neither can store an arbitrary query result. • Both share the same restrictions for use with the MultiLoad, FastLoad, and Archive/Recovery utilities. • A hash index implicitly defines a direct access path to base table rows. A join index may be explicitly specified to define a direct access path to base table rows. Effects of Hash Indexes Join index limits affect the following Teradata Database functions and features. • ARC (Archive and Recovery) Archive and Recovery cannot be used on a hash index itself. Archiving is permitted on a base table or database that has an associated hash index defined. During a restore of such a base table or database, the system does not rebuild the hash index. You must drop the existing hash index definition and create a new one before any such index can be used again in the execution of queries. • Load Utilities MultiLoad and FastLoad utilities cannot be used to load or unload data into base tables that have an associated hash index defined on them because hash indexes are not maintained during the execution of these utilities. The hash index must be dropped and recreated after that table has been loaded. The TPump utility, which performs standard SQL row inserts and updates, can be used because hash indexes are properly maintained during its execution. However, in some cases, performance may improve by dropping hash indexes on the table prior to the load and recreating them after the load. • Permanent Journal Recovery Using a permanent journal to recover a base table using ROLLBACK or ROLLFORWARD with an associated hash index defined is permitted. The hash index is not automatically Chapter 1: Objects Referential Integrity 36 SQL Reference: Fundamentals rebuilt during the recovery process. Instead, the hash index is marked as non-valid and it must be dropped and recreated before it can be used again in the execution of queries. Queries Using a Hash Index In most respects, a hash index is similar to a base table. For example, you can perform COLLECT STATISTICS, DROP STATISTICS, HELP, and SHOW statements on a hash index. Unlike base tables, you cannot do the following things with hash indexes: • Query or update hash index rows explicitly. • Store and maintain arbitrary query results such as expressions. • Create explicit unique indexes on its columns. • Partition the primary index of the hash index. For More Information About Hash Indexes Consult the following manuals for more detailed information on using hash indexes to enhance the performance of your databases: • Database Design • Performance Management • SQL Reference: Data Definition Statements Referential Integrity Introduction Referential integrity (RI) is defined as all the following notions. • The concept of relationships between tables, based on the definition of a primary key (or UNIQUE alternate key) and a foreign key. • A mechanism that provides for specification of columns within a referencing table that are foreign keys for columns in some other referenced table. Referenced columns must be defined as one of the following. • Primary key columns • Unique columns • A reliable mechanism for preventing accidental database corruption when performing inserts, updates, and deletes. Referential integrity requires that a row having a non-null value for a referencing column cannot exist in a table if an equal value does not exist in a referenced column.Chapter 1: Objects Referential Integrity SQL Reference: Fundamentals 37 Varieties of Referential Integrity Enforcement Supported by Teradata Database Teradata Database supports two forms of declarative SQL for enforcing referential integrity: • A standard method that enforces RI on a row-by-row basis • A batch method that enforces RI on a statement basis Both methods offer the same measure of integrity enforcement, but perform it in different ways. A third form is related to these because it provides a declarative definition for a referential relationship, but it does not enforce that relationship. Enforcement of the declared referential relationship is left to the user by any appropriate method. Referencing (Child) Table The referencing table is referred to as the child table, and the specified child table columns are the referencing columns. Note: Referencing columns must have the same numbers and types of columns, data types, and sensitivity as the referenced table keys. COMPRESS is not allowed on either referenced or referencing columns and column-level constraints are not compared. Referenced (Parent) Table A child table must have a parent, and the referenced table is referred to as the parent table. The parent key columns in the parent table are the referenced columns. Because the referenced columns are defined as unique constraints, they must be one of the following unique indexes. • A unique primary index (UPI), defined as NOT NULL • A unique secondary index (USI), defined as NOT NULL Terms Related to Referential Integrity The following terms are used to explain the concept of referential integrity. Term Definition Child Table A table where the referential constraints are defined. Child table and referencing table are synonyms. Parent Table The table referenced by a child table. Parent table and referenced table are synonyms. Primary Key A unique identifier for a row of a table. UNIQUE Alternate KeyChapter 1: Objects Referential Integrity 38 SQL Reference: Fundamentals Why Referential Integrity Is Important Consider the employee and payroll tables for any business. With referential integrity constraints, the two tables work together as one. When one table gets updated, the other table also gets updated. The following case depicts a useful referential integrity scenario. Looking for a better career, Mr. Clark Johnson leaves his company. Clark Johnson is deleted from the employee table. The payroll table, however, does not get updated because the payroll clerk simply forgets to do so. Consequently, Mr. Clark Johnson keeps getting paid. With good database design, referential integrity relationship would have been defined on these tables. They would have been linked and, depending on the defined constraints, the deletion of Clark Johnson from the employee table could not be performed unless it was accompanied by the deletion of Clark Johnson from the payroll table. Foreign Key A column set in the child table that is also the primary key (or a UNIQUE alternate key) in the parent table. Foreign keys can consist of as many as 64 different columns. Referential Constraint A constraint defined on a column set or a table to ensure referential integrity. For example, consider the following table definition: CREATE TABLE A (A1 CHAR(10) REFERENCES B (B1), A2 INTEGER FOREIGN KEY (A1,A2) REFERENCES C PRIMARY INDEX (A1)); This CREATE TABLE statement specifies the following referential integrity constraints. This constraint … Is defined at this level … 1 column. Implicit foreign key A1 references the parent key B1 in table B. 2 table. Explicit composite foreign key (A1, A2) implicitly references the UPI (or a USI) of parent table C, which must be two columns, the first typed CHAR(10) and the second typed INTEGER. Both parent table columns must also be defined as NOT NULL. Term DefinitionChapter 1: Objects Referential Integrity SQL Reference: Fundamentals 39 Besides data integrity and data consistency, referential integrity also has the benefits listed in the following table. Rules for Assigning Columns as FOREIGN KEYS The FOREIGN KEY columns in the referencing table must be identical in definition with the keys in the referenced table. Corresponding columns must have the same data type and case sensitivity. • The COMPRESS option is not permitted on either the referenced or referencing column(s). • Column level constraints are not compared. • A one-column FOREIGN KEY cannot reference a single column in a multi-column primary or unique key—the foreign and primary/unique key must contain the same number of columns. Circular References Are Allowed References can be defined as circular in that TableA can reference TableB, which can reference TableA. In this case, at least one set of FOREIGN KEYS must be defined on nullable columns. If the FOREIGN KEYS in TableA are on columns defined as nullable, then rows could be inserted into TableA with nulls for the FOREIGN KEY columns. Once the appropriate rows exist in TableB, the nulls of the FOREIGN KEY columns in TableA could then be updated to contain non-null values which match the TableB values. References Can Be to the Table Itself FOREIGN KEY references can also be to the same table that contains the FOREIGN KEY. The referenced columns must be different columns than the FOREIGN KEY, and both the referenced and referencing columns must subscribe to the referential integrity rules. Benefit Description Increases development productivity It is not necessary to code SQL statements to enforce referential constraints. The Teradata Database automatically enforces referential integrity. Requires fewer programs to be written All update activities are programmed to ensure that referential constraints are not violated. The Teradata Database enforces referential integrity in all environments. No additional programs are required. Improves performance The Teradata Database chooses the most efficient method to enforce the referential constraints. The Teradata Database can optimize queries based on the fact that there is referential integrity.Chapter 1: Objects Referential Integrity 40 SQL Reference: Fundamentals CREATE and ALTER TABLE Syntax Referential integrity affects the syntax and semantics of CREATE TABLE and ALTER TABLE. For more details, see “ALTER TABLE” and “CREATE TABLE” in SQL Reference: Data Definition Statements. Maintaining Foreign Keys Definition of a FOREIGN KEY requires that the Teradata Database maintain the integrity defined between the referenced and referencing table. The Teradata Database maintains the integrity of foreign keys as explained in the following table. FOR this data manipulation activity … The system verifies that … A row is inserted into a referencing table and foreign key columns are defined to be NOT NULL. a row exists in the referenced table with the same values as those in the foreign key columns. If such a row does not exist, then an error is returned. If the foreign key contains multiple columns, and if any one column value of the foreign key is null, then none of the foreign key values are validated. The values in foreign key columns are altered to be NOT NULL. a row exists in the referenced table that contains values equal to the altered values of all of the foreign key columns. If such a row does not exist, then an error is returned. A row is deleted from a referenced table. no rows exist in referencing tables with foreign key values equal to those of the row to be deleted. If such rows exist, then an error is returned. Before a referenced column in a referenced table is updated. no rows exist in a referencing table with foreign key values equal to those of the referenced columns. If such rows exist, then an error is returned. Before the structure of columns defined as foreign keys or referenced by foreign keys is altered. the change would not violate the rules for definition of a foreign key constraint. An ALTER TABLE or DROP INDEX statement attempting to change such a columns structure returns an error. A table referenced by another is dropped. the referencing table has dropped its foreign key reference to the referenced table.Chapter 1: Objects Referential Integrity SQL Reference: Fundamentals 41 Referential Integrity and the ARC Utility The Archive (ARC) utility archives and restores individual tables. It also copies tables from one database to another. When a table is restored or copied into a database, the dictionary definition of that table is also restored. The dictionary definitions of both the referenced (parent) and referencing (child) table contain the complete definition of a reference. By restoring a single table, it is possible to create an inconsistent reference definition in the Teradata Database. When either a parent or child table is restored, the reference is marked as inconsistent in the dictionary definitions. The ARC utility can validate these references once the restore is done. An ALTER TABLE statement adds a foreign key reference to a table. The same processes occur whether the reference is defined for standard or for soft referential integrity. all of the values in the foreign key columns are validated against columns in the referenced table. When the system parses ALTER TABLE, it defines an error table that: • Has the same columns and primary index as the target table of the ALTER TABLE statement. • Has a name that is the same as the target table name suffixed with the reference index number. A reference index number is assigned to each foreign key constraint for a table. To determine the number, use one of the following system views. • RI_Child_Tables • RI_Distinct_Children • RI_Distinct_Parents • RI_Parent_Tables • Is created under the same user or database as the table being altered. If a table already exists with the same name as that generated for the error table then an error is returned to the ALTER TABLE statement. Rows in the referencing table that contain values in the foreign key columns that cannot be found in any row of the referenced table are copied into the error table (the base data of the target table is not modified). It is your responsibility to: • Correct data values in the referenced or referencing tables so that full referential integrity exists between the two tables. Use the rows in the error table to define which corrections to make. • Maintain the error table. FOR this data manipulation activity … The system verifies that …Chapter 1: Objects Views 42 SQL Reference: Fundamentals While a table is marked as inconsistent, no updates, inserts, or deletes are permitted. The table is fully usable only when the inconsistencies are resolved (see below). This restriction is true for both hard and soft (Referential Constraint) referential integrity constraints. It is possible that the user either intends to or must revert to a definition of a table which results in an inconsistent reference on that table. The Archive and Restore operations are the most common cause of such inconsistencies. To remove inconsistent references from a child table that is archived and restored, follow these steps: 1 After archiving the child table, drop the parent table. 2 Restore the child table. When the child table is restored, the parent table no longer exists. The normal ALTER TABLE DROP FOREIGN KEY statement does not work, because the parent table references cannot be resolved. 3 Use the DROP INCONSISTENT REFERENCES option to remove these inconsistent references from a table. The syntax is: ALTER TABLE database_name.table_name DROP INCONSISTENT REFERENCES You must have DROP privileges on the target table of the statement to perform this option, which removes all inconsistent internal indexes used to establish references. For further information, see Teradata Archive/Recovery Utility Reference or Teradata ASF2 Tape Reader User Guide. Referential Integrity and the FastLoad and MultiLoad Utilities Foreign key references are not supported for any table that is the target table for a FastLoad or MultiLoad. For further details, see: • Database Design • Teradata FastLoad Reference • Teradata MultiLoad Reference Views Views and Tables A view can be compared to a window through which you can see selected portions of a database. Views are used to retrieve portions of one or more tables or other views. Views look like tables to a user, but they are virtual, not physical, tables. They display data in columns and rows and, in general, can be used as if they were physical tables. However, only the column definitions for a view are stored: views are not physical tables.Chapter 1: Objects Views SQL Reference: Fundamentals 43 A view does not contain data: it is a virtual table whose definition is stored in the data dictionary. The view is not materialized until it is referenced by a statement. Some operations that are permitted for the manipulation of tables are not valid for views, and other operations are restricted, depending on the view definition. Defining a View The CREATE VIEW statement defines a view. The statement names the view and its columns, defines a SELECT on one or more columns from one or more underlying tables and/or views, and can include conditional expressions and aggregate operators to limit the row retrieval. Why Use Views? The primary reason to use views is to simplify end user access to the Teradata database. Views provide a constant vantage point from which to examine and manipulate the database. Their perspective is altered neither by adding or nor by dropping columns from its component base tables unless those columns are part of the view definition. From an administrative perspective, views are useful for providing an easily maintained level of security and authorization. For example, users in a Human Resources department can access tables containing sensitive payroll information without being able to see salary and bonus columns. Views also provide administrators with an ability to control read and update privileges on the database with little effort. Restrictions on Views Some operations that are permitted on base tables are not permitted on views—sometimes for obvious reasons and sometimes not. The following set of rules outlines the restrictions on how views can be created and used. • You cannot create an index on a view. • A view definition cannot contain an ORDER BY clause. • Any derived columns in a view must explicitly specify view column names, for example by using an AS clause or by providing a column list immediately after the view name. • You cannot update tables from a view under the following circumstances: • The view is defined as a join view (defined on more than one table) • The view contains derived columns. • The view definition contains a DISTINCT clause. • The view definition contains a GROUP BY clause. • The view defines the same column more than once.Chapter 1: Objects Triggers 44 SQL Reference: Fundamentals Triggers Definition Triggers are active database objects associated with a subject table. A trigger essentially consists of a stored SQL statement or a block of SQL statements. Triggers execute when an INSERT, UPDATE, DELETE, or MERGE modifies a specified column or columns in the subject table. Typically, a stored trigger performs an UPDATE, INSERT, DELETE, MERGE, or other SQL operation on one or more tables, which may possibly include the subject table. Triggers in Teradata Database conform to the ANSI SQL-2003 standard, and also provide some additional features. Triggers have two types of granularity: • Row triggers fire once for each row of the subject table that is changed by the triggering event and that satisfies any qualifying condition included in the row trigger definition. • Statement triggers fire once upon the execution of the triggering statement. You can create, alter, and drop triggers. For details on creating, dropping, and altering triggers, see SQL Reference: Data Definition Statements. Process Flow for a Trigger The general process flow for a trigger is as follows. Note that this is a logical flow, not a physical re-enactment of how the Teradata Database processes a trigger. 1 The triggering event occurs on the subject table. 2 A determination is made as to whether triggers defined on the subject table are to become active upon a triggering event. 3 Qualified triggers are examined to determine the trigger action time, whether they are defined to fire before or after the triggering event. IF you want to … THEN use … define a trigger CREATE TRIGGER. • enable a trigger • disable a trigger • change the creation timestamp for a trigger ALTER TRIGGER. Disabling a trigger stops the trigger from functioning, but leaves the trigger definition in place as an object. This allows utility operations on a table that are not permitted on tables with enabled triggers. Enabling a trigger restores its active state. remove a trigger from the system permanently DROP TRIGGER.Chapter 1: Objects Triggers SQL Reference: Fundamentals 45 4 When multiple triggers qualify, then they fire normally in the ANSI-specified order of creation timestamp. To override the creation timestamp and specify a different execution order of triggers, you can use the ORDER clause, a Teradata extension. Even if triggers are created without the ORDER clause, you can redefine the order of execution by changing the trigger creation timestamp using the ALTER TRIGGER statement. 5 The triggered SQL statements (triggered action) execute. If the trigger definition uses a REFERENCING clause to specify that old, new, or both old and new data for the triggered action is to be collected under a correlation name (an alias), then that information is stored in transition tables or transition rows as follows: • OLD [ROW] or NEW [ROW] values, or both, under old (or new) values correlation name. • Entire set of rows as OLD TABLE or NEW TABLE under old (or new) values table alias. 6 The trigger passes control to the next trigger, if defined, in a cascaded sequence. The sequence can include recursive triggers. Otherwise, control passes to the next statement in the application. 7 If any of the actions involved in the triggering event or the triggered actions abort, then all of the actions are aborted. Restrictions on Using Triggers Most Teradata load utilities cannot access a table that has an active trigger. An application that uses triggers can use ALTER TRIGGER to disable the trigger and enable the load. The application must be sure that loading a table with disabled triggers does not result in a mismatch in a user defined relationship with a table referenced in the triggered action. The other restrictions on triggers include: • BEFORE statement triggers are not allowed. • BEFORE triggers cannot have data-changing statements as triggered action (triggered SQL statements). • BEFORE triggers cannot access OLD TABLE and NEW TABLE. • Triggers and hash indexes are mutually exclusive. You cannot define triggers on a table on which a hash index is already defined. • A positioned (updatable cursor) UPDATE or DELETE is not allowed to fire a trigger. An attempt to do so generates an error.Chapter 1: Objects Macros 46 SQL Reference: Fundamentals Related Topics Macros Introduction A frequently used SQL statement or series of statements can be incorporated into a macro and defined using the SQL CREATE MACRO statement. See “CREATE MACRO” in SQL Reference: Data Definition Statements. The statements in the macro are performed using the EXECUTE statement. See “EXECUTE (Macro Form)” in SQL Reference: Data Manipulation Statements. A macro can include an EXECUTE statement that executes another macro. Definition A macro consists of one or more statements that can be executed by performing a single statement. Each time the macro is performed, one or more rows of data can be returned. Performing a macro is similar to performing a multistatement request (see “Multistatement Requests” on page 121). Single-User and Multiuser Macros You can create a macro for your own use, or grant execution authorization to others. For example, your macro might enable a user in another department to perform operations on the data in the Teradata Database. When executing the macro, a user need not be aware of the database being accessed, the tables affected, or even the results. FOR detailed information on … SEE … • guidelines for creating triggers • conditions that cause triggers to fire • trigger action that occurs when a trigger fires • the trigger action time • when to use row triggers and when to use statement triggers CREATE TRIGGER in SQL Reference: Data Definition Statements. • temporarily disabling triggers • enabling triggers • changing the creation timestamp of a trigger ALTER TRIGGER in SQL Reference: Data Definition Statements. permanently removing triggers from the system DROP TRIGGER in SQL Reference: Data Definition Statements.Chapter 1: Objects Macros SQL Reference: Fundamentals 47 Multistatement Transactions Versus Macros Although you can enter a multistatement operation interactively using an explicit transaction (either BT/ET or COMMIT), a better practice is to define such an operation as a macro because an explicit transaction holds locks placed on objects by statements in the transaction until the statement sequence is completed with an END TRANSACTION or COMMIT statement. If you were to enter such a sequence interactively from BTEQ, items in the database would be locked to others while you typed and entered each statement. Contents of a Macro With the exception of CREATE AUTHORIZATION and REPLACE AUTHORIZATION, a data definition statement is allowed in macro if it is the only SQL statement in that macro. A data definition statement is not resolved until the macro is executed, at which time unqualified database object references are fully resolved using the default database of the user submitting the EXECUTE statement. If this is not the desired result, you must fully qualify all object references in a data definition statement in the macro body. A macro can contain parameters that are substituted with data values each time the macro is executed. It also can include a USING modifier, which allows the parameters to be filled with data from an external source such as a disk file. A COLON character prefixes references to a parameter name in the macro. Parameters cannot be used for data object names. Executing a Macro Regardless of the number of statements in a macro, the Teradata Database treats it as a single request. When you execute a macro, either all its statements are processed successfully or none are processed. If a macro fails, it is aborted, any updates are backed out, and the database is returned to its original state. Ways to Perform SQL Macros in Embedded SQL Macros in an embedded SQL program are performed in one of the following ways. IF the macro … THEN use … is a single statement, and that statement returns no data • the EXEC statement to specify static execution of the macro -or- • the PREPARE and EXECUTE statements to specify dynamic execution. Use DESCRIBE to verify that the single statement of the macro is not a data returning statement. • consists of multiple statements • returns data a cursor, either static or dynamic. The type of cursor used depends on the specific macro and on the needs of the application.Chapter 1: Objects Stored Procedures 48 SQL Reference: Fundamentals Static SQL Macro Execution in Embedded SQL Static SQL macro execution is associated with a macro cursor using the macro form of the DECLARE CURSOR statement. When you perform a static macro, you must use the EXEC form to distinguish it from the dynamic SQL statement EXECUTE. Dynamic SQL Macro Execution in Embedded SQL Define dynamic macro execution using the PREPARE statement with the statement string containing an EXEC macro_name statement rather than a single-statement request. The dynamic request is then associated with a dynamic cursor. See “DECLARE CURSOR (Macro Form)” in SQL Reference: Data Manipulation Statements for further information on the use of macros. Dropping, Replacing, Renaming, and Retrieving Information About a Macro For more information, see SQL Reference: Data Definition Statements. Stored Procedures Introduction Stored procedures are called Persistent Stored Modules in the ANSI SQL-2003 standard. They are written in SQL and consist of a set of control and condition handling statements that make SQL a computationally complete programming language. These features provide a server-based procedural interface to the Teradata Database for application programmers. Teradata stored procedure facilities are a subset of and conform to the ANSI SQL-2003 standards for semantics. IF you want to … THEN use the following statement … drop a macro DROP MACRO redefine an existing macro REPLACE MACRO rename a macro RENAME MACRO get the attributes for a macro HELP MACRO get the data definition statement most recently used to create, replace, or modify a macro SHOW MACROChapter 1: Objects Stored Procedures SQL Reference: Fundamentals 49 Elements of Stored Procedures The set of statements constituting the main tasks of the stored procedure is called the stored procedure body, which can consist of a single statement or a compound statement, or block. A single statement stored procedure body can contain one control statement, such as LOOP or WHILE, or one SQL DDL, DML, or DCL statement, including dynamic SQL. Some statements are not allowed, including: • Any declaration (local variable, cursor, or condition handler) statement • A cursor statement (OPEN, FETCH, or CLOSE) A compound statement stored procedure body consists of a BEGIN-END statement enclosing a set of declarations and statements, including: • Local variable declarations • Cursor declarations • Condition handler declaration statements • Control statements • SQL DML, DDL, and DCL statements supported by stored procedures, including dynamic SQL Compound statements can also be nested. For information about control statements, parameters, local variables, and labels, see SQL Reference: Stored Procedures and Embedded SQL. Privileges for Stored Procedures The security for stored procedures is similar to that for other Teradata database objects like tables, macros, views, and triggers. The rights to ALTER PROCEDURE, CREATE PROCEDURE, DROP PROCEDURE, and EXECUTE PROCEDURE can be granted using the GRANT statement and revoked using the REVOKE statement. Of these: • CREATE PROCEDURE is only a database-level privilege. • ALTER PROCEDURE, DROP PROCEDURE and EXECUTE PROCEDURE privileges can be granted at the object level and database or user level. • Only DROP PROCEDURE is an automatic privilege for all users. This is granted when a new user or database is created. • EXECUTE PROCEDURE is an automatic privilege only for the creator of a stored procedure, granted at the time of creation. Chapter 1: Objects Stored Procedures 50 SQL Reference: Fundamentals Creating Stored Procedures A stored procedure can be created from: • BTEQ utility using the COMPILE command • CLIv2 applications, ODBC, JDBC, and Teradata SQL Assistant (formerly called Queryman) using the SQL CREATE PROCEDURE or REPLACE PROCEDURE statement. The procedures are stored in the user database space as objects and are executed on the server. For the syntax of data definition statements related to stored procedures, including CREATE PROCEDURE and REPLACE PROCEDURE, see SQL Reference: Data Definition Statements. Note: The stored procedure definitions in the next examples are designed only to demonstrate the usage of the feature. They are not recommended for use. Example Assume you want to define a stored procedure NewProc to add new employees to the Employee table and retrieve the name of the department to which the employee belongs. You can also report an error, in case the row that you are trying to insert already exists, and handle that error condition. The CREATE PROCEDURE statement looks like this: CREATE PROCEDURE NewProc (IN name CHAR(12), IN number INTEGER, IN dept INTEGER, OUT dname CHAR(10), INOUT errstr VARCHAR(30)) BEGIN DECLARE CONTINUE HANDLER FOR SQLSTATE VALUE '23505' SET errstr = 'Duplicate Row.'; INSERT INTO Employee (EmpName, EmpNo, DeptNo ) VALUES (name, number, dept); SELECT DeptName INTO dname FROM Department WHERE DeptNo = dept; END; This stored procedure defines parameters that must be filled in each time it is called.Chapter 1: Objects Stored Procedures SQL Reference: Fundamentals 51 Modifying Stored Procedures You can modify a stored procedure definition using the REPLACE PROCEDURE statement. Example Assume you want to change the previous example to insert salary information to the Employee table for new employees. The REPLACE PROCEDURE statement looks like this: REPLACE PROCEDURE NewProc (IN name CHAR(12), IN number INTEGER, IN dept INTEGER, IN salary DECIMAL(10,2), OUT dname CHAR(10), INOUT errstr VARCHAR(30)) BEGIN DECLARE CONTINUE HANDLER FOR SQLSTATE VALUE '23505' SET errstr = 'Duplicate Row.'; INSERT INTO Employee (EmpName, EmpNo, DeptNo, Salary_Amount) VALUES (name, number, dept, salary); SELECT DeptName INTO dname FROM Department WHERE DeptNo = dept; END; Executing Stored Procedures You can execute a stored procedure from any supporting client utility or interface using the SQL CALL statement. You have to specify arguments for all the parameters contained in the stored procedure. The CALL statement for executing the procedure created in the CREATE PROCEDURE example looks like this: CALL NewProc (Jonathan, 1066, 34, dname); For details on executing stored procedures and on call arguments, see “CALL” in SQL Reference: Data Manipulation Statements. Recompiling Stored Procedures The ALTER PROCEDURE feature enables recompilation of stored procedures without having to execute SHOW PROCEDURE and REPLACE PROCEDURE statements. This feature provides the following benefits: • Stored procedures created in earlier releases of Teradata Database can be recompiled in Teradata Database release V2R5.0 and later to derive the benefits of new features and performance improvements. • Recompilation is also useful for cross-platform archive and restoration of stored procedures.Chapter 1: Objects Stored Procedures 52 SQL Reference: Fundamentals • ALTER PROCEDURE allows changes in the following compile-time attributes of a stored procedure: • SPL option • Warnings option Note: For stored procedures created in Teradata Database release V2R5.0 and later to work in earlier releases, they must be recompiled. Deleting Stored Procedures You can delete a stored procedure from a database using the DROP PROCEDURE statement. Assume you want to drop the NewProc procedure from the database. The DROP PROCEDURE statement looks like this: DROP PROCEDURE NewProc; Renaming Stored Procedures You can rename a stored procedure using the RENAME PROCEDURE statement. Assume you want to rename the NewProc stored procedure as NewEmp. The statement looks like this: RENAME PROCEDURE NewProc TO NewEmp; Getting Stored Procedure Information You can get information about the parameters specified in a stored procedure and their attributes using the HELP PROCEDURE statement. The output contains a list of all the parameters specified in the procedure and the attributes of each parameter. The statement to specify is: HELP PROCEDURE NewProc; To view the creation-time attributes of the stored procedure, specify the following statement: HELP PROCEDURE NewProc ATTRIBUTES; Archiving Procedures Stored procedures are archived and restored as part of a database archive and restoration. Individual stored procedures cannot be archived or restored using the ARCHIVE (DUMP) or RESTORE statements. Related Topics FOR details on … SEE … stored procedure control and condition handling statements SQL Reference: Stored Procedures and Embedded SQL invoking stored procedures the CALL statement in SQL Reference: Data Manipulation StatementsChapter 1: Objects External Stored Procedures SQL Reference: Fundamentals 53 External Stored Procedures Introduction External stored procedures are written in the C or C++ programming language, installed on the database, and then executed like stored procedures. Usage Here is a synopsis of the steps you take to develop, compile, install, and use external stored procedures: 1 If you are creating a new external stored procedure, then write, test, and debug the C or C++ code for the procedure. -orIf you are using a third party object or package, then skip to the next step. 2 Use CREATE PROCEDURE or REPLACE PROCEDURE for external stored procedures to identify the location of the source code, object, or package, and install it on the server. The external stored procedure is compiled, if the source code is submitted, linked to the dynamic linked library (DLL or SO) associated with the database in which the procedure resides, and distributed to all Teradata Database nodes in the system. 3 Use GRANT to grant privileges to users who are authorized to use the external stored procedure. 4 Invoke the procedure using the CALL statement. Differences Between Stored Procedures and External Stored Procedures Using external stored procedures is very similar to using stored procedures, except for the following: • Unlike stored procedures, external stored procedures cannot contain any embedded SQL statements. To call a stored procedure, an external stored procedure can call the FNC_CallSP library function. • Invoking an external stored procedure from a client application does not affect the nesting limit for stored procedures. creating or replacing stored procedures SQL Reference: Data Definition Statements dropping stored procedures renaming stored procedures FOR details on … SEE …Chapter 1: Objects User-Defined Functions 54 SQL Reference: Fundamentals • The CREATE PROCEDURE statement for external stored procedures is different from the CREATE PROCEDURE statement for stored procedures. In addition to syntax differences, you do not have to use the COMPILE command in BTEQ or BTEQWIN. • To install an external stored procedure on a database, you must have the CREATE EXTERNAL PROCEDURE privilege on the database. Related Topics User-Defined Functions Introduction SQL provides a set of useful functions, but they might not satisfy all of the particular requirements you have to process your data. User-defined functions (UDFs) allow you to extend SQL by writing your own functions in the C or C++ programming language, installing them on the database, and then using them like standard SQL functions. You can also install UDF objects or packages from third-party vendors, without providing the source code. UDF Types Teradata Database supports three types of UDFs. FOR details on … SEE … external stored procedure programming SQL Reference: UDF, UDM, and External Stored Procedure Programming invoking external stored procedures the CALL statement in SQL Reference: Data Manipulation Statements installing external stored procedures on the server the CREATE/REPLACE PROCEDURE statement in SQL Reference: Data Definition Statements UDF Type Description Scalar Scalar functions take input parameters and return a single value result. Examples of standard SQL scalar functions are CHARACTER_LENGTH, POSITION, and TRIM. Aggregate Aggregate functions produce summary results. They differ from scalar functions in that they take grouped sets of relational data, make a pass over each group, and return one result for the group. Some examples of standard SQL aggregate functions are AVG, SUM, MAX, and MIN. Table A table function is invoked in the FROM clause of a SELECT statement and returns a table to the statement.Chapter 1: Objects Profiles SQL Reference: Fundamentals 55 Usage Here is a synopsis of the steps you take to develop, compile, install, and use a UDF: 1 If you are creating a new UDF, then write, test, and debug the C or C++ code for the UDF. -orIf you are using a third party UDF object or package, then skip to the next step. 2 Use CREATE FUNCTION or REPLACE FUNCTION to identify the location of the source code, object, or package, and install it on the server. The function is compiled, if the source code is submitted, linked to the dynamic linked library (DLL or SO) associated with the database in which the function resides, and distributed to all Teradata Database nodes in the system. 3 Use GRANT to grant privileges to users who are authorized to use the UDF. 4 Call the function. Related Topics Profiles Definition Profiles define values for the following system parameters: • Default database • Spool space • Temporary space • Default account and alternate accounts • Password security attributes An administrator can define a profile and assign it to a group of users who share the same settings. FOR more information on … SEE … writing, testing, and debugging source code for a UDF SQL Reference: UDF, UDM, and External Stored Procedure Programming data definition statements related to UDFs, including CREATE FUNCTION and REPLACE FUNCTION SQL Reference: Data Definition StatementsChapter 1: Objects Profiles 56 SQL Reference: Fundamentals Advantages of Using Profiles Use profiles to: • Simplify system administration. Administrators can create a profile that contains system parameters and assign the profile to a group of users. To change a parameter, the administrator updates the profile instead of each individual user. • Control password security. A profile can define password attributes such as the number of: • Days before a password expires • Days before a password can be used again • Minutes to lock out a user after a certain number of failed logon attempts Administrators can assign the profile to an individual user or to a group of users. Usage The following steps describe how to use profiles to manage a common set of parameters for a group of users. 1 Define a user profile. A CREATE PROFILE statement defines a profile, and lets you set: • Account identifiers to charge for the space used and a default account identifier • Default database • Space to allocate for spool files • Space to allocate for temporary tables • Number of days before the password expires • Minimum and maximum number of characters in a password string • Whether or not to allow digits and special characters in a password string • Number of incorrect logon attempts to allow before locking a user • Number of minutes before unlocking a locked user • Number of days before a password can be used again 2 Assign the profile to users. Use the CREATE USER or MODIFY USER statement to assign a profile to a user. Profile settings override the values set for the user. 3 If necessary, change any of the system parameters for a profile. Use the MODIFY PROFILE statement to change a profile. Related Topics For information on the syntax and usage of profiles, see SQL Reference: Data Definition Statements.Chapter 1: Objects Roles SQL Reference: Fundamentals 57 Roles Definition Roles define access privileges on database objects. A user who is assigned a role can access all the objects that the role has privileges to. Roles simplify management of user access rights. A database administrator can create different roles for different job functions and responsibilities, grant specific privileges on database objects to the roles, and then grant membership to the roles to users. Advantages of Using Roles Use roles to: • Simplify access rights administration. A database administrator can grant rights on database objects to a role and have the rights automatically applied to all users assigned to that role. When a user’s function within an organization changes, changing the user’s role is far easier than deleting old rights and granting new rights to go along with the new function. • Reduce dictionary disk space. Maintaining rights on a role level rather than on an individual level makes the size of the DBC.AccessRights table much smaller. Instead of inserting one row per user per right on a database object, the Teradata Database inserts one row per role per right in DBC.AccessRights, and one row per role member in DBC.RoleGrants. Usage The following steps describe how to manage user access privileges using roles. 1 Define a role. A CREATE ROLE statement defines a role. A newly created role does not have any associated privileges. 2 Add access privileges to the role. Use the GRANT statement to grant privileges to roles on databases, tables, views, macros, columns, triggers, stored procedures, join indexes, hash indexes, and user-defined functions. 3 Grant the role to users or other roles. Use the GRANT statement to grant a role to users or other roles.Chapter 1: Objects User-Defined Types 58 SQL Reference: Fundamentals 4 Assign default roles to users. Use the DEFAULT ROLE option of the CREATE USER or MODIFY USER statement to specify the default role for a user, where: At logon time, the default role of the user becomes the current role for the session. Rights validation uses the active roles for a user, which include the current role and all nested roles. 5 If necessary, change the current role for a session. Use the SET ROLE statement to change the current role for a session. Managing role-based access rights requires sufficient privileges. For example, the CREATE ROLE statement is only authorized to users who have the CREATE ROLE system privilege. Related Topics For information on the syntax and usage of roles, see SQL Reference: Data Definition Statements. User-Defined Types Introduction SQL provides a set of predefined data types, such as INTEGER and VARCHAR, that you can use to store the data that your application uses, but they might not satisfy all of the requirements you have to model your data. User-defined types (UDTs) allow you to extend SQL by creating your own data types and then using them like predefined data types. DEFAULT ROLE = … Specifies … role_name the name of one role to assign as the default role for a user. NONE NULL that the user does not have a default role. ALL the default role to be all roles that are directly or indirectly granted to the user.Chapter 1: Objects User-Defined Types SQL Reference: Fundamentals 59 UDT Types Teradata Database supports distinct and structured UDTs. Distinct and structured UDTs can define methods that operate on the UDT. For example, a distinct UDT named euro can define a method that converts the value to a US dollar amount. Similarly, a structured UDT named circle can define a method that computes the area of the circle using the radius attribute. Using a Distinct UDT Here is a synopsis of the steps you take to develop and use a distinct UDT: 1 Use the CREATE TYPE statement to create a distinct UDT that is based on a predefined data type, such as INTEGER or VARCHAR. The Teradata Database automatically generates functionality for the UDT that allows you to import and export the UDT between the client and server, use the UDT in a table, perform comparison operations between two UDTs, and perform data type conversions between the UDT and the predefined data type on which the definition is based. 2 If the UDT defines methods, write, test, and debug the C or C++ code for the methods, and then use CREATE METHOD or REPLACE METHOD to identify the location of the source code and install it on the server. The methods are compiled, linked to the dynamic linked library (DLL or SO) associated with the SYSUDTLIB database, and distributed to all Teradata Database nodes in the system. 3 Use GRANT to grant privileges to users who are authorized to use the UDT. 4 Use the UDT as the data type of a column in a table definition. UDT Type Description Example Distinct A UDT that is based on a single predefined data type, such as INTEGER or VARCHAR. A distinct UDT named euro that is based on a DECIMAL(8,2) data type can store monetary data. Structured A UDT that is a collection of one or more fields called attributes, each of which is defined as a predefined data type or other UDT (which allows nesting). A structured UDT named circle can consist of x-coordinate, y-coordinate, and radius attributes.Chapter 1: Objects User-Defined Types 60 SQL Reference: Fundamentals Using a Structured UDT Here is a synopsis of the steps you take to develop and use a structured UDT: 1 Use the CREATE TYPE statement to create a structured UDT and specify attributes, constructor methods, and instance methods. Teradata Database automatically generates the following functionality: • A default constructor function that you can use to construct a new instance of the structured UDT and initialize the attributes to NULL • Observer methods for each attribute that you can use to get the attribute values • Mutator methods for each attribute that you can use to set the attribute values 2 Follow these steps to implement, install, and register cast functionality for the UDT (Teradata Database does not automatically generate cast functionality for structured UDTs): a Write, test, and debug C or C++ code that implements cast functionality that allows you to perform data type conversions between the UDT and other data types, including other UDTs. b Identify the location of the source code and install it on the server: The source code is compiled, linked to the dynamic linked library (DLL or SO) associated with the SYSUDTLIB database, and distributed to all Teradata Database nodes in the system. c Use the CREATE CAST or REPLACE CAST statement to register the method or function as a cast routine for the UDT. d Repeat Steps a through c for all methods or functions that provide cast functionality. 3 Follow these steps to implement, install, and register ordering functionality for the UDT (Teradata Database does not automatically generate ordering functionality for structured UDTs): a Write, test, and debug C or C++ code that implements ordering functionality that allows you to perform comparison operations between two UDTs. b Identify the location of the source code and install it on the server: IF you write the source code as a … THEN use one of the following statements … method CREATE METHOD or REPLACE METHOD function CREATE FUNCTION or REPLACE FUNCTION IF you write the source code as a … THEN use one of the following statements … method CREATE METHOD or REPLACE METHOD function CREATE FUNCTION or REPLACE FUNCTIONChapter 1: Objects User-Defined Types SQL Reference: Fundamentals 61 The source code is compiled, linked to the dynamic linked library (DLL or SO) associated with the SYSUDTLIB database, and distributed to all Teradata Database nodes in the system. c Use the CREATE ORDERING or REPLACE ORDERING statement to register the method or function as an ordering routine for the UDT. 4 Follow these steps to implement, install, and register transform functionality for the UDT (Teradata Database does not automatically generate transform functionality for structured UDTs): a Write, test, and debug C or C++ code that implements transform functionality that allows you to import and export the UDT between the client and server. b Identify the location of the source code and install it on the server: The source code is compiled, linked to the dynamic linked library (DLL or SO) associated with the SYSUDTLIB database, and distributed to all Teradata Database nodes in the system. c Repeat Steps a through b. d Use the CREATE TRANSFORM or REPLACE TRANSFORM statement to register the transform routines for the UDT. 5 If the UDT defines constructor methods or instance methods, write, test, and debug the C or C++ code for the methods, and then use CREATE METHOD or REPLACE METHOD to identify the location of the source code and install it on the server. IF the source code implements transform functionality for … THEN … importing the UDT to the server you must write the source code as a UDF and use CREATE FUNCTION or REPLACE FUNCTION to identify the location of the source code and install it on the server. exporting the UDT from the server IF you write the source code as a … THEN use one of the following statements to identify the location of the source code and install it on the server … method CREATE METHOD or REPLACE METHOD function CREATE FUNCTION or REPLACE FUNCTION IF you took Steps a through b to implement and install this transform functionality … THEN repeat Steps a through b to implement and install this transform functionality … importing the UDT to the server exporting the UDT from the server exporting the UDT from the server importing the UDT to the serverChapter 1: Objects User-Defined Types 62 SQL Reference: Fundamentals The methods are compiled, linked to the dynamic linked library (DLL or SO) associated with the SYSUDTLIB database, and distributed to all Teradata Database nodes in the system. 6 Use GRANT to grant privileges to users who are authorized to use the UDT. 7 Use the UDT as the data type of a column in a table definition. Related Topics FOR more information on … SEE … • CREATE TYPE • CREATE METHOD and REPLACE METHOD • CREATE FUNCTION and REPLACE FUNCTION • CREATE CAST and REPLACE CAST • CREATE ORDERING and REPLACE ORDERING • CREATE TRANSFORM and REPLACE TRANSFORM SQL Reference: Data Definition Statements writing, testing, and debugging source code for a constructor method or instance method SQL Reference: UDF, UDM, and External Stored Procedure ProgrammingSQL Reference: Fundamentals 63 CHAPTER 2 Basic SQL Syntax and Lexicon This chapter explains the syntax and lexicon for Teradata SQL, a single, unified, nonprocedural language that provides capabilities for queries, data definition, data modification, and data control of the Teradata Database. Topics include: • Structure of an SQL statement • Keywords • Expressions • Names • Literals • Operators • Functions • Delimiters • Separators • Comments • Terminators • Null statements Structure of an SQL Statement Syntax The following diagram indicates the basic structure of an SQL statement. FF07D232 statement_keyword ; expressions functions keywords clauses , phrasesChapter 2: Basic SQL Syntax and Lexicon Structure of an SQL Statement 64 SQL Reference: Fundamentals where: Typical SQL Statement A typical SQL statement consists of a statement keyword, one or more column names, a database name, a table name, and one or more optional clauses introduced by keywords. For example, in the following single-statement request, the statement keyword is SELECT: SELECT deptno, name, salary FROM personnel.employee WHERE deptno IN(100, 500) ORDER BY deptno, name ; The select list for this statement is made up of the names: • Deptno, name, and salary (the column names) • Personnel (the database name) • Employee (the table name) The search condition, or WHERE clause, is introduced by the keyword WHERE. WHERE deptno IN(100, 500) The sort order, or ORDER BY, clause is introduced by the keywords ORDER BY. ORDER BY deptno, name This syntax element … Specifies … statement_keyword the name of the statement. expressions literals, name references, or operations using names and literals. functions the name of a function and its arguments, if any. keywords special values introducing clauses or phrases or representing special objects, such as NULL. Most keywords are reserved words and cannot be used in names. clauses subordinate statement qualifiers. phrases data attribute phrases. ; the Teradata SQL statement separator and request terminator. The semicolon separates statements in a multistatement request and terminates a request when it is the last non-blank character on an input line in BTEQ. Note that the request terminator is required for a request defined in the body of a macro. For a discussion of macros and their use, see “Macros” on page 46.Chapter 2: Basic SQL Syntax and Lexicon SQL Lexicon Characters SQL Reference: Fundamentals 65 Related Topics The pages that follow provide details on the elements that appear in an SQL statement. SQL Lexicon Characters Client Character Data The characters that make up the SQL lexicon can be represented on the client system in ASCII, EBCDIC, UTF8, UTF16, or in an installed user-defined character set. If the client system character data is not ASCII, then it is converted by the Teradata Database to an internal form for processing and storage. Data returned to the client system is converted to the client character set. Server Character Data The internal forms used for character support are described in International Character Set Support. The notation used for Japanese characters is described in: • “Character Shorthand Notation Used In This Book” • Appendix A: “Notation Conventions.” Case Sensitivity See the following topics in SQL Reference: Data Types and Literals: • “Defining Case Sensitivity for Table Columns” • “CASESPECIFIC Phrase” • “UPPERCASE Phrase” • "Character Data Literals" FOR more information on … SEE … statement_keyword “Keywords” on page 66 keywords expressions “Expressions” on page 67 functions “Functions” on page 92 separators “Separators” on page 94 terminators “Terminators” on page 96Chapter 2: Basic SQL Syntax and Lexicon Keywords 66 SQL Reference: Fundamentals See the following topics in SQL Reference: Functions and Operators: • “LOWER Function” • “UPPER Function” Keywords Introduction Keywords are words that have special meanings in SQL statements. There are two types of keywords: reserved and non-reserved. You cannot use reserved keywords to name database objects. Although you can use non-reserved keywords as object names, you usually should not because of possible confusion resulting from their use. Statement Keyword The statement keyword, the first keyword in an SQL statement, is usually a verb. For example, in the INSERT statement, the first keyword is INSERT. Keywords Other keywords appear throughout a statement as modifiers (for example, DISTINCT, PERMANENT), or as words that introduce clauses (for example, IN, AS, AND, TO, WHERE). In this book, keywords appear entirely in uppercase letters, though SQL does not discriminate between uppercase and lowercase letters in a keyword. For example, SQL interprets the following SELECT statements to be identical: Select Salary from Employee where EmpNo = 10005; SELECT Salary FROM Employee WHERE EmpNo = 10005; select Salary FRom Employee WherE EmpNo = 10005; All keywords must be from the ASCII repertoire. Fullwidth letters are not valid regardless of the character set being used. For a list of Teradata SQL keywords, see Appendix B: “Restricted Words for V2R6.2.” Keywords and Object Names Note that you cannot use reserved keywords to name database objects. Because new keywords are frequently added to new releases of the Teradata Database, you may experience a problem with database object names that were valid in prior releases but which become nonvalid in a new release. The workaround for this is to do one of the following things: • Put the newly nonvalid name in double quotes. • Rename the object. In either case you must change your applications.Chapter 2: Basic SQL Syntax and Lexicon Expressions SQL Reference: Fundamentals 67 Expressions Introduction An expression specifies a value. An expression can consist of literals (or constants), name references, or operations using names and literals. Scalar Expressions A scalar expression, or value expression, produces a single number, character string, byte string, date, time, timestamp, or interval. A value expression has exactly one declared type, common to every possible result of evaluation. Implicit type conversion rules apply to expressions. Query Expressions Query expressions operate on table values and produce rows and tables of data. Every query expression includes at least one FROM clause, which operates on a table reference and returns a single table value. Related Topics Names Introduction In Teradata SQL, various database objects such as tables, views, stored procedures, macros, columns, and collations are identified by a name. The set of valid names depends on whether the system is enabled for Japanese language support. FOR more information on … SEE … • CASE expresssions • arithmetic expressions • logical expressions • datetime expressions • interval expressions • character expresssions • byte expressions SQL Reference: Functions and Operators. data type conversions SQL Reference: Functions and Operators. query expressions SQL Reference: Data Manipulation Statements.Chapter 2: Basic SQL Syntax and Lexicon Names 68 SQL Reference: Fundamentals Rules The rules for naming Teradata Database database objects on systems enabled for standard language support are as follows. • You must define and reference each object, such as user, database, or table, by a name. • In general, names consist of 1 to 30 characters. • Names can appear as a sequence of characters within double quotes and as a quoted hexadecimal string followed by the key letters XN. Such names have fewer restrictions on the characters that can be included. The restrictions are described in “QUOTATION MARKS Characters and Names” on page 69 and “Internal Hexadecimal Representation of a Name” on page 70. • Unquoted names have the following syntactic restrictions: • They may only include the following characters: • Uppercase or lowercase letters (A to Z and a to z) • Digits (0 through 9) • The special characters DOLLAR SIGN ($), NUMBER SIGN (#), and LOW LINE ( _ ) • They must not begin with a digit. • They must not be a keyword. • Systems that are enabled for Japanese language support allow various Japanese characters to be used for names, but determining the maximum number of characters allowed in a name becomes much more complex (see “Name Validation on Systems Enabled with Japanese Language Support” on page 77). • Names having any of the following characteristics are not ANSI SQL-2003 compliant: • Contains lower case letters. • Contains either a $ or a #. • Begins with an underscore. • Has more than 18 characters. • Names that define databases and objects must observe the following rules. • Databases, users, and profiles must have unique names. • Tables, views, stored procedures, join or hash indexes, triggers, user-defined functions, or macros can take the same name as the database or user in which they are created, but cannot take the same name as another of these objects in the same database or user. • Roles can have the same name as a profile, table, column, view, macro, trigger, table function, user-defined function, external stored procedure, or stored procedure; however, role names must be unique among users and databases. • Table and view columns must have unique names. • Parameters defined for a macro or stored procedure must have unique names. • Secondary indexes on a table must have unique names.Chapter 2: Basic SQL Syntax and Lexicon Names SQL Reference: Fundamentals 69 • Named constraints on a table must have unique names. • Secondary indexes and constraints can have the same name as the table they are associated with. • CHECK constraints, REFERENCE constraints, and INDEX objects can also have assigned names. Names are optional for these objects. • Names are not case-specific (see “Case Sensitivity and Names” on page 71). QUOTATION MARKS Characters and Names Enclosing names in QUOTATION MARKS characters (U+0022) greatly increases the valid set of characters for defining names. Pad characters and special characters can also be included. For example, the following strings are both valid names. • “Current Salary” • “D’Augusta” The QUOTATION MARKS characters are not part of the name, but they are required, if the name is not valid otherwise. For example, these two names are identical, even though one is enclosed within QUOTATION MARKS characters. • This_Name • “This_Name” On systems enabled for standard language support, any character translatable to the LATIN server character set can appear in an object name, with the following exceptions: • The NULL character (U+0000) is not allowed in any names, including quoted names. • The object name must not consist entirely of blank characters. In this context, a blank character is any of the following: • NULL (U+0000) • CHARACTER TABULATION (U+0009) • LINE FEED (U+000A) • LINE TABULATION (U+000B) • FORM FEED (U+000C) • CARRIAGE RETURN (U+000D) • SPACE (U+0020) • The code point 0x1A, which represents the error character for KANJI1 and LATIN server character sets, cannot be translated between character sets and must not appear in object names. All of the following examples are valid names. • Employee • job_title Chapter 2: Basic SQL Syntax and Lexicon Names 70 SQL Reference: Fundamentals • CURRENT_SALARY • DeptNo • Population_of_Los_Angeles • Totaldollars • “Table A” • “Today’s Date” Note: If you use quoted names, the QUOTATION MARKS characters that delineate the names are not counted in the length of the name and are not stored in Dictionary tables used to track name usage. If a Dictionary view is used to display such names, they are displayed without the double quote characters, and if the resulting names are used without adding double quotes, the likely outcome is an error report. For example, “D’Augusta” might be the name of a column in the Dictionary view DBC.Columns, and the HELP statements that return column names return the name as D’Augusta (without being enclosed in QUOTATION MARKS characters). Internal Hexadecimal Representation of a Name You can also create and reference object names by their internal hexadecimal representation in the Data Dictionary using the following syntax: where: The key letters XN specify that the string is a hexadecimal name. On systems enabled for standard language support, any character translatable to the LATIN server character set can appear in an object name, with the same exceptions listed in the preceding section, “QUOTATION MARKS Characters and Names” on page 69. For more information on using internal hexadecimal representations of names, see “Using the Internal Hexadecimal Representation of a Name” on page 82. Syntax element … Specifies … 'hexadecimal_digits' a quoted hexadecimal string representation of the Teradata Database internal encoding. HH01A099 'hexadecimal_digit(s)' XNChapter 2: Basic SQL Syntax and Lexicon Standard Form for Data in Teradata Database SQL Reference: Fundamentals 71 Case Sensitivity and Names Names are not case-dependent—a name cannot be used twice by changing its case. Any mix of uppercase and lowercase can be used when referencing symbolic names in a request. For example, the following statements are identical. SELECT Salary FROM Employee WHERE EmpNo = 10005; SELECT SALARY FROM EMPLOYEE WHERE EMPNO = 10005; SELECT salary FROM employee WHERE eMpNo = 10005; The case in which a column name is defined can be important. The column name is the default title of an output column, and symbolic names are returned in the same case in which they were defined. For example, assume that the columns in the SalesReps table are defined as follows: CREATE TABLE SalesReps ( last_name VARCHAR(20) NOT NULL, first_name VARCHAR(12) NOT NULL, ... In response to a query that does not define a TITLE phrase, such as the following example, the column names are returned exactly as defined they were defined, for example, last_name, then first_name. SELECT Last_Name, First_Name FROM SalesReps ORDER BY Last_Name; You can use the TITLE phrase to specify the case, wording, and placement of an output column heading either in the column definition or in an SQL statement. For more information, see SQL Reference: Data Manipulation Statements. Standard Form for Data in Teradata Database Introduction Data in Teradata Database is presented to a user according to the relational model, which models data as two dimensional tables with rows and columns. Each row of a table is composed one or more columns identified by column name. Each column contains a data item (or a null) having a single data type. Syntax for Referencing a Column table_name. FF07D238 column_name database_name.Chapter 2: Basic SQL Syntax and Lexicon Standard Form for Data in Teradata Database 72 SQL Reference: Fundamentals where: Definition: Fully Qualified Column Name A fully qualified name consists of a database name, table name, and column name. For example, a fully qualified reference for the Name column in the Employee table of the Personnel database is: Personnel.Employee.Name Column Alias In addition to referring to a column by name, an SQL query can reference a column by an alias. Column aliases are used for join indexes when two columns have the same name. However, an alias can be used for any column when a pseudonym is more descriptive or easier to use. Using an alias to name an expression allows a query to reference the expression. You can specify a column alias with or without the keyword AS on the first reference to the column in the query. The following example creates and uses aliases for the first two columns. SELECT departnumber AS d, employeename e, salary FROM personnel.employee WHERE d IN(100, 500) ORDER BY d, e ; Alias names must meet the same requirements as names of other database objects. For details, see “Names” on page 67. The scope of alias names is confined to the query. Syntax element … Specifies … database_name a qualifying name for the database in which the table and column being referenced is stored. Depending on the ambiguity of the reference, database_name might or might not be required. See “Unqualified Object Names” on page 73. table_name a qualifying name for the table in which the column being referenced is stored. Depending on the ambiguity of the reference, table_name might or might not be required. See “Unqualified Object Names” on page 73. column_name one of the following: • The name of the column being referenced • The alias of the column being referenced • The keyword PARTITION See “Column Alias” on page 72.Chapter 2: Basic SQL Syntax and Lexicon Unqualified Object Names SQL Reference: Fundamentals 73 Referencing All Columns in a Table An asterisk references all columns in a row simultaneously, for example, the following SELECT statement references all columns in the Employee table. A list of those fully qualified column names follows the query. SELECT * FROM Employee; Personnel.Employee.EmpNo Personnel.Employee.Name Personnel.Employee.DeptNo Personnel.Employee.JobTitle Personnel.Employee.Salary Personnel.Employee.YrsExp Personnel.Employee.DOB Personnel.Employee.Sex Personnel.Employee.Race Personnel.Employee.MStat Personnel.Employee.EdLev Personnel.Employee.HCap Unqualified Object Names Definition An unqualified object name is a table, column, trigger, macro, or stored procedure reference that is not fully qualified. For example, the WHERE clause in the following statement uses “DeptNo” as an unqualified column name: SELECT * FROM Personnel.Employee WHERE DeptNo = 100 ; Unqualified Column Names You can omit database and table name qualifiers when you reference columns as long as the reference is not ambiguous. For example, the WHERE clause in the following statement: SELECT Name, DeptNo, JobTitle FROM Personnel.Employee WHERE Personnel.Employee.DeptNo = 100 ; can be written as: WHERE DeptNo = 100 ; because the database name and table name can be derived from the Personnel.Employee reference in the FROM clause.Chapter 2: Basic SQL Syntax and Lexicon Unqualified Object Names 74 SQL Reference: Fundamentals Omitting Database Names When you omit the database name qualifier, Teradata Database looks in the following databases to find the unqualified table, view, trigger, or macro name: • The default database, which is established by a DATABASE, CREATE USER, MODIFY USER, CREATE PROFILE, or MODIFY PROFILE statement • Other databases, if any, referenced by the SQL statement • The login user database for a volatile table, if the unqualified object name is a table name The search must find the table name in only one of those databases. An ambiguous name error message results if the name exists in more than one of those databases. For example, if your login user database has no volatile tables named Employee and you have established Personnel as your default database, you can omit the Personnel database name qualifier from the preceding sample query. Rules for Name Resolution The following rules govern name resolution: • Name resolution is performed statement by statement. • When an INSERT statement contains a subquery, names are resolved in the subquery first. • Names in a view are resolved when the view is created. • Names in a macro data manipulation statement are resolved when the macro is created. • Names in a macro data definition statement are resolved when the macro is performed using the default database of the user submitting the EXECUTE statement. Therefore, you should fully qualify all names in a macro data definition statement, unless you specifically intend for the user’s default to be used. • Names in stored procedure statements are resolved when the procedure is created. All unqualified object names acquire the current default database name. • An ambiguous unqualified name returns an error to the requestor. Related Topics FOR more information on … SEE … default databases “Default Database” on page 75. the DATABASE statement “SQL Data Definition Language Statement Syntax” in SQL Reference: Data Definition Statements. the CREATE USER statement the MODIFY USER statementChapter 2: Basic SQL Syntax and Lexicon Default Database SQL Reference: Fundamentals 75 Default Database Definition The default database is a Teradata extension to SQL that defines a database that Teradata Database uses to look for unqualified table, view, trigger, or macro names in SQL statements. The default database is not the only database that Teradata Database uses to find an unqualified table, view, trigger, or macro name in an SQL statement, however; Teradata Database also looks for the name in: • Other databases, if any, referenced by the SQL statement • The login user database for a volatile table, if the unqualified object name is a table name If the unqualified object name exists in more than one of the databases in which Teradata Database looks, the SQL statement produces an ambiguous name error. Establishing a Permanent Default Database You can establish a permanent default database that is invoked each time you log on. For example, the following statement automatically establishes Personnel as the default database for Marks at the next logon: MODIFY USER marks AS DEFAULT DATABASE = personnel ; After you assign a default database, Teradata Database uses that database as one of the databases to look for all unqualified object references. To obtain information from a table, view, trigger, or macro in another database, fully qualify the table reference by specifying the database name, a FULLSTOP character, and the table name. TO … USE one of the following SQL Data Definition statements … define a permanent default database • CREATE USER, with a DEFAULT DATABASE clause. • CREATE USER, with a PROFILE clause that specifies a profile that defines the default database. change your permanent default database definition • MODIFY USER, with a DEFAULT DATABASE clause. • MODIFY USER, with a PROFILE clause. • MODIFY PROFILE, with a DEFAULT DATABASE clause. add a default database when one had not been established previouslyChapter 2: Basic SQL Syntax and Lexicon Default Database 76 SQL Reference: Fundamentals Establishing a Default Database for a Session You can establish a default database for the current session that Teradata Database uses to look for unqualified table, view, trigger, or macro names in SQL statements. For example, after entering the following SQL statement: DATABASE personnel ; you can enter a SELECT statement as follows: SELECT deptno (TITLE 'Org'), name FROM employee ; which has the same results as: SELECT deptno (TITLE 'Org'), name FROM personnel.employee; To establish a default database, you must have some privilege on a database, macro, stored procedure, table, user, or view in that database. Once defined, the default database remains in effect until the end of a session or until it is replaced by a subsequent DATABASE statement. Related Topics TO … USE … establish a default database for a session the DATABASE statement. FOR more information on … SEE … the DATABASE statement SQL Reference: Data Definition Statements. the CREATE USER statement the MODIFY USER statement fully-qualified names “Standard Form for Data in Teradata Database” on page 71. “Unqualified Object Names” on page 73. using profiles to define a default database “Profiles” on page 55.Chapter 2: Basic SQL Syntax and Lexicon Name Validation on Systems Enabled with Japanese Language Support SQL Reference: Fundamentals 77 Name Validation on Systems Enabled with Japanese Language Support Introduction A system that is enabled with Japanese language support allows thousands of additional characters to be used for names, but also introduces additional restrictions. Rules for Unquoted Names Unquoted names can use the following characters when Japanese language support is enabled: • Any character valid in an unquoted name under standard language support: • Uppercase or lowercase letters (A to Z and a to z) • Digits (0 through 9) • The special characters DOLLAR SIGN ($), NUMBER SIGN (#), and LOW LINE ( _ ) • The fullwidth (zenkaku) versions of the characters valid for names under standard language support: • Fullwidth uppercase or lowercase letters (A to Z and a to z) • Fullwidth digits (0 through 9) • The special characters fullwidth DOLLAR SIGN ($), fullwidth NUMBER SIGN (#), and fullwidth LOW LINE ( _ ) • Fullwidth (zenkaku) and halfwidth (hankaku) Katakana characters and sound marks. • Hiragana characters. • Kanji characters from JIS-x0208. The length of a name is restricted in a complex fashion. Charts of the supported Japanese character sets, the Teradata Database internal encodings, the valid character ranges for Japanese object names and data, and the non-valid character ranges for Japanese data and object names are documented in International Character Set Support. Rules for Quoted Names and Internal Hexadecimal Representation of Names As described in “QUOTATION MARKS Characters and Names” on page 69 and “Internal Hexadecimal Representation of a Name” on page 70, names can also appear as a sequence of characters within double quotes or as a quoted hexadecimal string followed by the key letters XN. Such names have fewer restrictions on the characters that can be included. The following restrictions that apply to systems enabled for standard language support also apply to systems enabled for Japanese language support: • The NULL character (U+0000) is not allowed. • The code point 0x1A, which represents the error character for KANJI1 and LATIN server character sets, cannot be translated between character sets and must not appear in object names.Chapter 2: Basic SQL Syntax and Lexicon Name Validation on Systems Enabled with Japanese Language Support 78 SQL Reference: Fundamentals • The object name must not consist entirely of blank characters. In this context, a blank character is any of the following: • NULL (U+0000) • LINE FEED (U+000A) • LINE TABULATION (U+000B) • FORM FEED (U+000C) • CARRIAGE RETURN (U+000D) • SPACE (U+0020) • CHARACTER TABULATION (U+0009) Additional rules apply to sessions using non-Japanese client character sets on systems enabled with Japanese language support. Here are some examples of predefined non-Japanese client character sets (you can also define your own site-defined client character sets): For sessions using non-Japanese client character sets on systems where Japanese language support is enabled, object names can only have characters in the following inclusive ranges: • U+0001 through U+000D • U+0015 through U+005B • U+005D through U+007D • U+007F REVERSE SOLIDUS (U+005C) and TILDE (U+007E) are not allowed. Cross-Platform Integrity If you need to access objects from heterogeneous clients, the best practice is to restrict the object names to those allowed under standard language support. Calculating the Length of a Name The length of a name is measured by the physical bytes of its internal representation, not by the number of viewable characters. Under the KanjiEBCDIC character sets, the Shift-Out and Shift-In characters that delimit a multibyte character string are included in the byte count. • EBCDIC • EBCDIC037_0E • ASCII • LATIN1_0A • LATIN9_0A • LATIN1252_0A • UTF8 • UTF16 • SCHEBCDIC935_2IJ • TCHEBCDIC937_3IB • HANGULEBCDIC933_1II • SCHGB2312_1T0 • TCHBIG5_1R0 • HANGULKSC5601_2R4 • SCHGB2312_1T0 • TCHBIG5_1R0 • HANGULKSC5601_2R4Chapter 2: Basic SQL Syntax and Lexicon Name Validation on Systems Enabled with Japanese Language Support SQL Reference: Fundamentals 79 For example, the following table name contains six logical characters of mixed single byte characters/multibyte characters, defined during a KanjiEBCDIC session: QR All single byte characters, including the Shift-Out/Shift-In characters, are translated into the Teradata Database internal encoding, based on JIS-x0201. Under the KanjiEBCDIC character sets, all multibyte characters remain in the client encoding. Thus, the processed name is stored as a string of twelve bytes, padded on the right with the single byte space character to a total of 30 bytes. The internal representation is as follows: 0E 42E3 42C1 42C2 42F1 0F 51 52 20 20 20 20 20 20 20 20 20 20 20 20 ... < T A B 1 > Q R To ensure upgrade compatibility, an object name created under one character set cannot exceed 30 bytes in any supported character set. For example, a single Katakana character occupies 1 byte in KanjiShift-JIS. However, when KanjiShift-JIS is converted to KanjiEUC, each Katakana character occupies two bytes. Thus, a 30-byte Katakana name in KanjiShift-JIS would expand in KanjiEUC to 60 bytes, which is illegal. The formula for calculating the correct length of an object name is as follows: Length = ASCII + (2*KANJI) + MAX (2*KATAKANA, (KATAKANA + 2*S2M + 2*M2S)) where: How Validation Occurs Name validation occurs when the object is created or renamed, as follows: • User names, database names, and account names are verified during the CREATE/ MODIFY USER and CREATE/MODIFY DATABASE statements. • Names of work tables and error tables are validated by the MultiLoad and FastLoad client utilities. • Table names and column names are verified during the CREATE/ALTER TABLE and RENAME TABLE statements. View and macro names are verified during the CREATE/ RENAME VIEW and CREATE/RENAME MACRO statements. This variable … Represents the number of … ASCII single-byte ASCII characters in the name. KATAKANA single-byte Hankaku Katakana characters in the name. KANJI double-byte characters in the name from the JIS-x0208 standard. S2M transitions from ASCII or KATAKANA to JIS-x0208. M2S transitions from JIS-x0208 to ASCII or KATAKANA.Chapter 2: Basic SQL Syntax and Lexicon Name Validation on Systems Enabled with Japanese Language Support 80 SQL Reference: Fundamentals Stored procedure names are verified during the execution of CREATE/RENAME/ REPLACE PROCEDURE statements. • Alias object names used in the SELECT, UPDATE, and DELETE statements are verified. The validation occurs only when the SELECT statement is used in a CREATE/REPLACE VIEW statement, and when the SELECT, UPDATE, or DELETE TABLE statement is used in a CREATE/REPLACE MACRO statement. Examples of Validating Japanese Object Names The following tables illustrate valid and non-valid object names under the Japanese character sets: KanjiEBCDIC, KanjiEUC, and KanjiShift-JIS. The meanings of ASCII, KATAKANA, KANJI, S2M, M2S, and LEN are defined in “Calculating the Length of a Name” on page 78. KanjiEBCDIC Object Name Examples KanjiEUC Object Name Examples Name ASCII Katakana Kanji S2M M2S LEN Result 0 0 14 1 1 32 Not valid because LEN > 30. kl 2 0 12 2 2 34 Not valid because LEN > 30. kl<> 2 0 10 2 2 30 Not valid because consecutive SO and SI characters are not allowed. 0 0 11 2 2 30 Not valid because consecutive SI and SO characters are not allowed. ABCDEFGHIJKLMNO 0 15 0 0 0 30 Valid. KLMNO 0 5 10 1 1 30 Valid. 0 0 1 1 1 6 Not valid because the double byte space is not allowed. Name ASCII Katakana Kanji S2M M2S LEN Result ABCDEFGHIJKLM 6 0 7 3 3 32 Not valid because LEN > 30 bytes. ABCDEFGHIJKLM 6 0 7 2 2 28 Valid. ss 2ABCDEFGHIJKL 0 1 11 1 1 27 Valid. Ass 2 BCDEFGHIJKL 0 1 11 2 2 31 Not valid because LEN > 30 bytes. ss 3C 0 0 0 1 1 4 Not valid because characters from code set 3 are not allowed.Chapter 2: Basic SQL Syntax and Lexicon Object Name Translation and Storage SQL Reference: Fundamentals 81 KanjiShift-JIS Object Name Examples Related Topics For charts of the supported Japanese character sets, the Teradata Database internal encodings, the valid character ranges for Japanese object names and data, and the non-valid character ranges for Japanese data and object names, see International Character Set Support. Object Name Translation and Storage Object names are stored in the dictionary tables using the following translation conventions. Both the ASCII character set and the EBCDIC character set are stored on the server as ASCII. Name ASCII Katakana Kanji S2M M2S LEN Result ABCDEFGHIJKLMNOPQR 6 7 5 1 1 30 Valid. ABCDEFGHIJKLMNOPQR 6 7 5 2 2 31 Not valid because LEN > 30 bytes. Character Type Description Single byte All single byte characters in a name, including the KanjiEBCDIC Shift-Out/ShiftIn characters, are translated into the Teradata Database internal representation (based on JIS-x0201 encoding). Multibyte Multibyte characters in object names are handled according to the character set in effect for the current session, as follows. Multibyte Character Set Description KanjiEBCDIC Each multibyte character within the Shift-Out/ShiftIn delimiters is stored without translation; that is, it remains in the client encoding. The name string must have matched (but not consecutive) Shift-Out and Shift-In delimiters. KanjiEUC Under code set 1, each multibyte character is translated from KanjiEUC to KanjiShift-JIS. Under code set 2, byte ss2 (0x8E) is translated to 0x80; the second byte is left unmodified. This translation preserves the relative ordering of code set 0, code set 1, and code set 2. KanjiShift-JIS Each multibyte character is stored without translation; it remains in the client encoding.Chapter 2: Basic SQL Syntax and Lexicon Object Name Comparisons 82 SQL Reference: Fundamentals Object Name Comparisons Comparison Rules In comparing two names, the following rules apply: • A simple Latin lowercase letter is equivalent to its corresponding simple Latin uppercase letter. For example, 'a' is equivalent to 'A'. • Multibyte characters that have the same logical presentation but have different physical encodings under different character sets do not compare as equivalent. • Two names compare as identical when their internal hexadecimal representations are the same, even if their logical meanings are different under the originating character sets. Note that identical characters on keyboards connected to different clients are not necessarily identical in their internal encoding in the Teradata Database. The Teradata Database could interpret two logically identical names as different names if the character sets under which they were created are not the same. For example, the following strings illustrate the internal representation of two names, both of which were defined with the same logical multibyte characters. However, the first name was created under KanjiEBCDIC, and the second name was created under KanjiShift-JIS. KanjiEBCDIC: 0E 42E3 42C1 42C2 42F1 0F 51 52 KanjiShift-JIS: 8273 8260 8261 8250 D8 D9 To ensure upgrade compatibility, you must avoid semantically duplicate object names in situations where duplicate object names would not normally be allowed. Also, two different character sets might have the same internal encoding for two logically different multibyte characters. Thus, two names might compare as identical even if their logical meanings are different. Using the Internal Hexadecimal Representation of a Name The Teradata Database knows an object name by its internal hexadecimal representation, and this is how it is stored in the various system tables of the Data Dictionary. The encoding of the internal representation of an object name depends on the components of the name string (are there single byte characters, multibyte characters, or both; are there Shift Out/Shift In (SO/SI) characters, and so on) and the character set in effect when the name was created. Suppose that a user under one character set needs to reference an object created by a user under a different character set. If the current user attempts to reference the name with the actual characters (that is, by typing the characters or by selecting non-specific entries from a dictionary table), the access could fail or the returned name could be meaningless. For example, assume that User_1 invokes a session under KanjiEBCDIC and creates a table name with multibyte characters. User_2 invokes a session under KanjiEUC and issues the following statement.Chapter 2: Basic SQL Syntax and Lexicon Object Name Comparisons SQL Reference: Fundamentals 83 SELECT TableName FROM DBC.Tables The result returns the KanjiEBCDIC characters in KanjiEUC presentation, which probably does not make sense. You can avoid this problem by creating objects and specifying object names in the following ways: • Create objects using names that contain only simple single byte Latin letters (A...Z, a...z) digits, and the DOLLAR SIGN ($), NUMBER SIGN (#), and LOW LINE ( _ ) symbols. Because these characters always translate to the same internal representation, they display exactly the same presentation to any session, regardless of the client or the character set. • Use the following syntax to reference a name by its internal representation. where: The key letters XN specify that the string is a hexadecimal name. Example The following table name, which contains mixed single byte characters and multibyte characters, was created under a KanjiEBCDIC character set: KAN The client encoding in which this name was received is as follows: 0E 42E3 42C1 42C2 42F1 0F D2 C1 D5 < T A B 1 > K A N The single byte characters (the letters K, A, and N, and the SO/SI characters) were translated into internal JIS-x0201 encoding. The multibyte characters were not translated and remained in the host encoding. The resulting internal string by which the name was stored is as follows: 0E 42E3 42C1 42C2 42F1 0F 4B 41 4E < T A B 1 > K A N To access this table from a KanjiShift-JIS or KanjiEUC character set, you could use the following Teradata SQL statement: SELECT * FROM '0E42E342C142C242F10F4B414E'XN; The response would be all rows from table KAN. Syntax element … Specifies … ’hexadecimal_digits’ a quoted hexadecimal string representation of the Teradata Database internal encoding. HH01A099 'hexadecimal_digit(s)' XNChapter 2: Basic SQL Syntax and Lexicon Finding the Internal Hexadecimal Representation for Object Names 84 SQL Reference: Fundamentals Finding the Internal Hexadecimal Representation for Object Names Introduction The CHAR2HEXINT function converts a character string to its internal hexadecimal representation. You can use this function to find the internal representation of any Teradata Database name. For more information on CHAR2HEXINT, see SQL Reference: Functions and Operators. Example 1 For example, to find the internal representation of all Teradata Database table names, issue the following Teradata SQL statement. SELECT CHAR2HEXINT(T.TableName) (TITLE 'Internal Hex Representation of TableName'),T.TableName (TITLE 'TableName') FROM DBC.Tables T WHERE T.TableKind = 'T' ORDER BY T.TableName; This statement selects all rows from the DBC.Tables view where the value of the TableKind column is T. For each row selected, both the internal hexadecimal representation and the character format of the value in the TableName column are returned, sorted alphabetically. An example of a portion of the output from this statement is shown below. In this example, the first name (double byte-A) was created using the KanjiEBCDIC character set. Internal Hex Representation of TableName TableName ------------------------------------------------------------ ----------- 0E42C10F2020202020202020202020202020202020202020202020202020 416363657373526967687473202020202020202020202020202020202020 AccessRights 4163634C6F6752756C6554626C2020202020202020202020202020202020 AccLogRuleTb 4163634C6F6754626C202020202020202020202020202020202020202020 AccLogTbl 4163636F756E747320202020202020202020202020202020202020202020 Accounts 416363746720202020202020202020202020202020202020202020202020 Acctg 416C6C202020202020202020202020202020202020202020202020202020 All 4368616E676564526F774A6F75726E616C20202020202020202020202020 ChangedRowJo 636865636B5F7065726D2020202020202020202020202020202020202020 check_perm 436F70496E666F54626C2020202020202020202020202020202020202020 CopInfoTbl Note that the first name, , cannot be interpreted. To obtain a printable version of a name, you must log onto a session under the same character set under which the name was created.Chapter 2: Basic SQL Syntax and Lexicon Finding the Internal Hexadecimal Representation for Object Names SQL Reference: Fundamentals 85 Example 2 You can use the same syntax to obtain the internal hexadecimal representations of all views or all macros. To do this, modify the WHERE condition to TableKind=’V’ for views and to TableKind=’M’ for macros. To obtain the internal hexadecimal representation of all database names, you can issue the following statement: SELECT CHAR2HEXINT(D.DatabaseName)(TITLE 'Internal Hex Representation of DatabaseName'),D.DatabaseName (TITLE 'DatabaseName') FROM DBC.Databases D ORDER BY D.DatabaseName; This statement selects every DatabaseName from DBC.Databases. For each DatabaseName, it returns the internal hexadecimal representation and the name in character format, sorted by DatabaseName. An example of the output from this statement is as follows: Internal Hex Representation of DatabaseName DatabaseName ------------------------------------------------------------ ------------ 416C6C202020202020202020202020202020202020202020202020202020 All 434F4E534F4C452020202020202020202020202020202020202020202020 CONSOLE 437261736864756D70732020202020202020202020202020202020202020 Crashdumps 444243202020202020202020202020202020202020202020202020202020 DBC 44656661756C742020202020202020202020202020202020202020202020 Default 5055424C4943202020202020202020202020202020202020202020202020 PUBLIC 53797341646D696E20202020202020202020202020202020202020202020 SysAdmin 53797374656D466520202020202020202020202020202020202020202020 SystemFe Example 3 Note that these statements return the padded hexadecimal name. The value 0x20 represents a space character in the internal representation. You can use the TRIM function to obtain the hexadecimal values without the trailing spaces, as follows. SELECT CHAR2HEXINT(TRIM(T.TableName)) (TITLE 'Internal Hex Representation of TableName'),T.TableName (TITLE 'TableName') FROM DBC.Tables T WHERE T.TableKind = 'T' ORDER BY T.TableName;Chapter 2: Basic SQL Syntax and Lexicon Specifying Names in a Logon String 86 SQL Reference: Fundamentals Specifying Names in a Logon String Purpose Identifies a user to the Teradata Database and, optionally, permits the user to specify a particular account to log onto. Syntax where: The Teradata Database does not support the hexadecimal representation of a username, a password, or an accountname in a logon string. For example, if you attempt to log on as user DBC by entering '444243'XN, the logon is not successful and an error message is generated. Passwords The password format options allows the site administrator to change the minimum and maximum number of characters allowed in the password string, and control the use of digits and special characters. Password string rules are identical to those for naming objects. See “Name Validation on Systems Enabled with Japanese Language Support” on page 77. The password formatting feature does not apply to multibyte client character sets on systems enabled with Japanese language support. Syntax element … Specifies … tdp_id/username the client TDP the user wishes to use to communicate with the Teradata Database and the name by which the Teradata Database knows the user. The username parameter can contain mixed single byte and multibyte characters if the current character set permits them. password an optional (depending on how the user is defined) password required to gain access to the Teradata Database. The password parameter can contain mixed single byte and multibyte characters if the current character set permits them. accountname an optional account name or account string that specifies a user account or account and performance-related variable parameters the user can use to tailor the session being logged onto. The accountname parameter can contain mixed single byte and multibyte characters if the current character set permits them. tdpid/username HH01A079 ,password ,accountnameChapter 2: Basic SQL Syntax and Lexicon Literals SQL Reference: Fundamentals 87 Literals Literals, or constants, are values coded directly in the text of an SQL statement, view or macro definition text, or CHECK constraint definition text. In general, the system is able to determine the data type of a literal by its form. Numeric Literals A numeric literal (also referred to as a constant) is a character string of 1 to 40 characters selected from the following: • digits 0 through 9 • plus sign • minus sign • decimal point There are three types of numeric literals: integer, decimal, and floating point. Hexadecimal Literals A hexadecimal literal specifies a string of 0 to 62000 hexadecimal digits that can represent a byte, character, or integer value. A hexadecimal digit is a character from 0 to 9, a to f, or A to F. Type Description Integer Literal An integer literal declares literal strings of integer numbers. Integer literals consist of an optional sign followed by a sequence of up to 10 digits. A numeric literal that is outside the range of values of an integer literal is considered a decimal literal. Decimal Literal A decimal literal declares literal strings of decimal numbers. Decimal literals consist of the following components, reading from left-to-right: an optional sign, an optional sequence of up to 38 digits (mandatory only when no digits appear after the decimal point), an optional decimal point, an optional sequence of digits (mandatory only when no digits appear before the decimal point). The scale and precision of a decimal literal are determined by the total number of digits in the literal and the number of digits to the right of the decimal point, respectively. Floating Point Literal A floating point literal declares literal strings of floating point numbers. Floating point literals consist of the following components, reading from left-toright: an optional sign, an optional sequence of digits (mandatory only when no digits appear after the decimal point) representing the whole number portion of the mantissa, an optional decimal point, an optional sequence of digits (mandatory only when no digits appear before the decimal point) representing the fractional portion of the mantissa, the literal character E, an optional sign, a sequence of digits representing the exponent. Chapter 2: Basic SQL Syntax and Lexicon Literals 88 SQL Reference: Fundamentals DateTime Literals Date and time literals declare date, time, or timestamp values in a SQL expression, view or macro definition text, or CONSTRAINT definition text. Date and time literals are introduced by keywords. For example: DATE '1969-12-23' There are three types of DateTime literals: DATE, TIME, and TIMESTAMP. Interval Literals Interval literals provide a means for declaring spans of time. Interval literals are introduced and followed by keywords. For example: INTERVAL '200' HOUR There are two mutually exclusive categories of interval literals: Year-Month and Day-Time. Type Description DATE Literal A date literal declares a date value in ANSI DATE format. ANSI DATE literal is the preferred format for DATE constants. All DATE operations accept this format. TIME Literal A time literal declares a time value and an optional time zone offset. TIMESTAMP Literal A timestamp literal declares a timestamp value and an optional time zone offset. Category Type Description Year-Month • YEAR • YEAR TO MONTH • MONTH Represent a time span that can include a number of years and months. Day-Time • DAY • DAY TO HOUR • DAY TO MINUTE • DAY TO SECOND • HOUR • HOUR TO MINUTE • HOUR TO SECOND • MINUTE • MINUTE TO SECOND • SECOND Represent a time span that can include a number of days, hours, minutes, or seconds.Chapter 2: Basic SQL Syntax and Lexicon Literals SQL Reference: Fundamentals 89 Character Literals A character literal declares a character value in an expression, view or macro definition text, or CHECK constraint definition text. Character literals consist of 0 to 31000 bytes delimited by a matching pair of single quotes. A zero-length character literal is represented by two consecutive single quotes (''). Graphic Literals A graphic literal specifies multibyte characters within the graphic repertoire. Built-In Functions The built-in functions, or special register functions, which are niladic (have no arguments), return various information about the system and can be used like other literals within SQL expressions. In an SQL query, the appropriate system value is substituted by the Parser after optimization but prior to executing a query using a cachable plan. Available built-in functions include all of the following: • ACCOUNT • CURRENT_DATE • CURRENT_TIME • CURRENT_TIMESTAMP • DATABASE • DATE • PROFILE • ROLE • SESSION • TIME • USER Related Topics FOR more information on … SEE … • numeric literals • DateTime literals • interval literals • character literals • graphic literals • hexadecimal literals SQL Reference: Data Types and Literals. built-in functions SQL Reference: Functions and Operators.Chapter 2: Basic SQL Syntax and Lexicon NULL Keyword as a Literal 90 SQL Reference: Fundamentals NULL Keyword as a Literal Null A null represents any of three things: • An empty column • An unknown value • An unknowable value Nulls are neither values nor do they signify values; they represent the absence of value. A null is a place holder indicating that no value is present. NULL Keyword The keyword NULL represents null, and is sometimes available as a special construct similar to, but not identical with, a literal. ANSI Compliance NULL is ANSI SQL-2003-compliant with extensions. Using NULL as a Literal Use NULL as a literal in the following ways: • A CAST source operand, for example: SELECT CAST (NULL AS DATE); • A CASE result, for example. SELECT CASE WHEN orders = 10 THEN NULL END FROM sales_tbl; • An insert item specifying a null is to be placed in a column position on INSERT. • An update item specifying a null is to be placed in a column position on UPDATE. • A default column definition specification, for example: CREATE TABLE European_Sales (Region INTEGER DEFAULT 99 ,Sales Euro_Type DEFAULT NULL); • An explicit SELECT item, for example: SELECT NULL This is a Teradata extension to ANSI. • An operand of a function, for example: SELECT TYPE(NULL) This is a Teradata extension to ANSI. Data Type of NULL When you use NULL as an explicit SELECT item or as the operand of a function, its data type is INTEGER. In all other cases NULL has no data type because it has no value.Chapter 2: Basic SQL Syntax and Lexicon Operators SQL Reference: Fundamentals 91 For example, if you perform SELECT TYPE(NULL), then INTEGER is returned as the data type of NULL. To avoid type issues, cast NULL to the desired type. Related Topics For information on the behavior of nulls and how to use them in data manipulation statements, see “Manipulating Nulls” on page 134. Operators Introduction SQL operators are used to express logical and arithmetic operations. Operators of the same precedence are evaluated from left to right. See “SQL Operations and Precedence” on page 91 for more detailed information. Parentheses can be used to control the order of precedence. When parentheses are present, operations are performed from the innermost set of parentheses outward. Definitions The following definitions apply to SQL operators. SQL Operations and Precedence SQL operations, and the order in which they are performed when no parentheses are present, appear in the following table. Operators of the same precedence are evaluated from left to right. Term Definition numeric Any literal, data reference, or expression having a numeric value. string Any character string or string expression. logical A Boolean expression (resolves to TRUE, FALSE, or unknown). value Any numeric, character, or byte data item. set A collection of values returned by a subquery, or a list of values separated by commas and enclosed by parentheses. Precedence Result Type Operation highest numeric + numeric (unary plus) - numeric (unary minus)Chapter 2: Basic SQL Syntax and Lexicon Functions 92 SQL Reference: Fundamentals Functions Scalar Functions Scalar functions take input parameters and return a single value result. Some examples of standard SQL scalar functions are CHARACTER_LENGTH, POSITION, and SUBSTRING. Aggregate Functions Aggregate functions produce summary results. They differ from scalar functions in that they take grouped sets of relational data, make a pass over each group, and return one result for the group. Some examples of standard SQL aggregate functions are AVG, SUM, MAX, and MIN. intermediate numeric numeric ** numeric (exponentiation) numeric numeric * numeric (multiplication) numeric / numeric (division) numeric MOD numeric (modulo operator) numeric numeric + numeric (addition) numeric - numeric (subtraction) string concatenation operator logical value EQ value value NE value value GT value value LE value value LT value value GE value value IN set value NOT IN set value BETWEEN value AND value character value LIKE character value logical NOT logical logical logical AND logical lowest logical logical OR logical Precedence Result Type OperationChapter 2: Basic SQL Syntax and Lexicon Delimiters SQL Reference: Fundamentals 93 Related Topics For the names, parameters, return values, and other details of scalar and aggregate functions, see SQL Reference: Functions and Operators. Delimiters Introduction Delimiters are special characters having meanings that depend on context. The function of each delimiter appears in the following table. Delimiter Name Purpose ( ) LEFT PARENTHESIS RIGHT PARENTHESIS Group expressions and define the limits of various phrases. , COMMA Separates and distinguishes column names in the select list, or column names or parameters in an optional clause, or DateTime fields in a DateTime type. : COLON Prefixes reference parameters or client system variables. Also separates DateTime fields in a DateTime type. . FULLSTOP • Separates database names from table, trigger, UDF, UDT, and stored procedure names, such as personnel.employee. • Separates table names from a particular column name, such as employee.deptno). • In numeric constants, the period is the decimal point. • Separates DateTime fields in a DateTime type. • Separates a method name from a UDT expression in a method invocation. ; SEMICOLON • Separates statements in multi-statement requests. • Separates statements in a stored procedure body. • Separates SQL procedure statements in a triggered SQL statement in a trigger definition. • Terminates requests submitted via utilities such as BTEQ. • Terminates embedded SQL statements in C or PL/I applications. ’ APOSTROPHE • Defines the boundaries of character string constants. • To include an APOSTROPHE character or show possession in a title, double the APOSTROPHE characters. • Also separates DateTime fields in a DateTime type.Chapter 2: Basic SQL Syntax and Lexicon Separators 94 SQL Reference: Fundamentals Example In the following statement submitted through BTEQ, the FULLSTOP separates the database name (Examp and Personnel) from the table name (Profile and Employee), and, where reference is qualified to avoid ambiguity, it separates the table name (Profile, Employee) from the column name (DeptNo). UPDATE Examp.Profile SET FinGrad = 'A' WHERE Name = 'Phan A' ; SELECT EdLev, FinGrad,JobTitle, YrsExp FROM Examp.Profile, Personnel.Employee WHERE Profile.DeptNo = Employee.DeptNo ; The first SEMICOLON separates the UPDATE statement from the SELECT statement. The second SEMICOLON terminates the entire multistatement request. The semicolon is required in Teradata SQL to separate multiple statements in a request and to terminate a request submitted through BTEQ. Separators Lexical Separators A lexical separator is a character string that can be placed between words, literals, and delimiters without changing the meaning of a statement. Valid lexical separators are any of the following. • Comments For an explanation of comment lexical separators, see “Comments” on page 95. • Pad characters (several pad characters are treated as a single pad character except in a string literal) • RETURN characters (X’0D’) Statement Separators The SEMICOLON is a Teradata SQL statement separator. “ QUOTATION MARK Defines the boundaries of nonstandard names. / SOLIDUS Separates DateTime fields in a DateTime type. B b Uppercase B Lowercase b - HYPHENMINUS Delimiter Name PurposeChapter 2: Basic SQL Syntax and Lexicon Comments SQL Reference: Fundamentals 95 Each statement of a multistatement request must be separated from any subsequent statement with a semicolon. The following multistatement request illustrates the use of the semicolon as a statement separator. SHOW TABLE Payroll_Test ; INSERT INTO Payroll_Test (EmpNo, Name, DeptNo) VALUES ('10044', 'Jones M', '300') ; INSERT INTO ... For statements entered using BTEQ, a request terminates with an input line-ending semicolon unless that line has a comment, beginning with two dashes (- -). Everything to the right of the - - is a comment. In this case, the semicolon must be on the following line. The SEMICOLON as a statement separator in a multistatement request is a Teradata extension to the ANSI SQL-2003 standard. Comments Introduction You can embed comments within an SQL request anywhere a blank can occur. The SQL parser and the preprocessor recognize the following types of embedded comments: • Simple • Bracketed Simple Comments The simple form of a comment is delimited by two consecutive HYPHEN-MINUS (U+002D) characters (--) at the beginning of the comment and the newline character at the end of the comment. The newline character is implementation-specific, but is typed by pressing the Enter (non- 3270 terminals) or Return (3270 terminals) key. Simple SQL comments cannot span multiple lines. Example The following SELECT statement illustrates the use of a simple comment: SELECT EmpNo, Name FROM Payroll_Test ORDER BY Name -- Alphabetic order ; 1101E231 - - comment_text new_line_characterChapter 2: Basic SQL Syntax and Lexicon Terminators 96 SQL Reference: Fundamentals Bracketed Comments A bracketed comment is a text string of unrestricted length that is delimited by the beginning comment characters SOLIDUS (U+002F) and ASTERISK (U+002A) /* and the end comment characters ASTERISK and SOLIDUS */. Bracketed comments can begin anywhere on an input line and can span multiple lines. Example The following CREATE TABLE statement illustrates the use of a bracketed comment. CREATE TABLE Payroll_Test /* This is a test table set up to process actual payroll data on a test basis. The data generated from this table will be compared with the existing payroll system data for 2 months as a parallel test. */ (EmpNo INTEGER NOT NULL FORMAT ’ZZZZ9’, Name VARCHAR(12) NOT NULL, DeptNo INTEGER FORMAT ’ZZZZ9’, . . . Comments With Multibyte Character Set Strings You can include multibyte character set strings in both simple and bracketed comments. When using mixed mode in comments, you must have a properly formed mixed mode string, which means that a Shift-In (SI) must follow its associated Shift-Out (SO). If an SI does not follow the multibyte string, the results are unpredictable. When using bracketed comments that span multiple lines, the SI must be on the same line as its associated SO. If the SI and SO are not on the same line, the results are unpredictable. You must specify the bracketed comment delimiters (/* and */) as single byte characters. Terminators Definition The SEMICOLON is a Teradata SQL request terminator when it is the last non-blank character on an input line in BTEQ unless that line has a comment beginning with two dashes. In this case, the SEMICOLON request terminator should be on the following line, after the comment line. /* comment_text */ 1101E230Chapter 2: Basic SQL Syntax and Lexicon Terminators SQL Reference: Fundamentals 97 A request is considered complete when either the “End of Text” character or the request terminator character is detected. ANSI Compliance The SEMICOLON as a request terminator is Teradata extension to the ANSI SQL-2003 standard. Example For example, on the following input line: SELECT * FROM Employee ; the SEMICOLON terminates the single-statement request “SELECT * FROM Employee”. BTEQ uses SEMICOLONs to terminate multistatement requests. A request terminator is mandatory for request types that are: • In the body of a macro • Triggered action statements in a trigger definition • Entered using the BTEQ interface • Entered using other interfaces that require BTEQ Example 1: Macro Request The following statement illustrates the use of a request terminator in the body of a macro. CREATE MACRO Test_Pay (number (INTEGER), name (VARCHAR(12)), dept (INTEGER) AS ( INSERT INTO Payroll_Test (EmpNo, Name, DeptNo) VALUES (:number, :name, :dept) ; UPDATE DeptCount SET EmpCount = EmpCount + 1 ; SELECT * FROM DeptCount ; ) Example 2: BTEQ Request When entered through BTEQ, the entire CREATE MACRO statement must be terminated. CREATE MACRO Test_Pay (number (INTEGER), name (VARCHAR(12)), dept (INTEGER) AS (INSERT INTO Payroll_Test (EmpNo, Name, DeptNo) VALUES (:number, :name, :dept) ; UPDATE DeptCount SET EmpCount = EmpCount + 1 ; SELECT * FROM DeptCount ; ) ;Chapter 2: Basic SQL Syntax and Lexicon Null Statements 98 SQL Reference: Fundamentals Null Statements Introduction A null statement is a statement that has no content except for optional pad characters or SQL comments. Example 1 The semicolon in the following request is a null statement. /* This example shows a comment followed by a semicolon used as a null statement */ ; UPDATE Pay_Test SET ... Example 2 The first SEMICOLON in the following request is a null statement. The second SEMICOLON is taken as statement separator: /* This example shows a semicolon used as a null statement and as a statement separator */ ; UPDATE Payroll_Test SET Name = 'Wedgewood A' WHERE Name = 'Wedgewood A' ; SELECT ... -- This example shows the use of an ANSI component -- used as a null statement and statement separator ; Example 3 A SEMICOLON that precedes the first (or only) statement of a request is taken as a null statement. ;DROP TABLE temp_payroll;SQL Reference: Fundamentals 99 CHAPTER 3 SQL Data Definition, Control, and Manipulation This chapter describes the functional families of the SQL language. Topics include: • SQL Functional Families and Binding Styles • Data Definition Language • Data Control Language • Data Manipulation Language • Query and Workload Analysis Statements • Help and Database Object Definition Tools SQL Functional Families and Binding Styles Introduction The SQL language can be characterized in several different ways. This chapter is organized around functional groupings of the components of the language with minor emphasis on binding styles. Definition: Functional Family SQL provides facilities for defining database objects, for defining user access to those objects, and for manipulating the data stored within them. The following list describes the principal functional families of the SQL language. • SQL Data Definition Language (DDL) • SQL Data Control Language (DCL) • SQL Data Manipulation Language (DML) • Query and Workload Analysis Statements • Help and Database Object Definition Tools Some classifications of SQL group the data control language statements with the data definition language statements.Chapter 3: SQL Data Definition, Control, and Manipulation Embedded SQL 100 SQL Reference: Fundamentals Definition: Binding Style The ANSI SQL standards do not define the term binding style. The expression refers to a possible method by which an SQL statement can be invoked. Teradata Database supports the following SQL binding styles: • Direct, or interactive • Embedded SQL • Stored procedure • SQL Call Level Interface (as ODBC) • JDBC The direct binding style is usually not qualified in this manual set because it is the default style. Embedded SQL and stored procedure binding styles are always clearly specified, either explicitly or by context. Related Topics You can find more information on binding styles in the SQL Reference set and in other books. Embedded SQL You can execute SQL statements from within client application programs. The expression embedded SQL refers to SQL statements executed or declared from within a client application. An embedded Teradata SQL client program consists of the following: • Client programming language statements • One or more embedded SQL statements • Depending on the host language, one or more embedded SQL declare sections SQL declare sections are optional in COBOL and PL/I, but must be used in C. FOR more information on … SEE … embedded SQL • “Embedded SQL” on page 100 • Teradata Preprocessor2 for Embedded SQL Programmer Guide • SQL Reference: Stored Procedures and Embedded SQL stored procedures • “Stored Procedures” on page 48 • SQL Reference: Stored Procedures and Embedded SQL ODBC ODBC Driver for Teradata User Guide JDBC Teradata Driver for the JDBC Interface User GuideChapter 3: SQL Data Definition, Control, and Manipulation Data Definition Language SQL Reference: Fundamentals 101 A special prefix, EXEC SQL, distinguishes the SQL language statements embedded into the application program from the host programming language. Embedded SQL statements must follow the rules of the host programming language concerning statement continuation and termination, construction of variable names, and so forth. Aside from these rules, embedded SQL is host language-independent. Details of Teradata Database support for embedded SQL are described in SQL Reference: Stored Procedures and Embedded SQL. Data Definition Language Definition The SQL Data Definition Language (DDL) is a subset of the SQL language and consists of all SQL statements that support the definition of database objects. Purpose of Data Definition Language Statements Data definition language statements perform the following functions: • Create, drop, rename, and alter tables • Create, drop, rename, and replace stored procedures, user-defined functions, views, and macros • Create, drop, and alter user-defined types • Create, drop, and replace user-defined methods • Create and drop indexes • Create, drop, and modify users and databases • Create, drop, alter, rename, and replace triggers • Create, drop, and set roles • Create, drop, and modify profiles • Collect statistics on a column set or index • Establish a default database • Comment on database objects • Set a different collation sequence, account priority, DateForm, time zone, and database for the session • Begin and end logging Rules on Entering DDL Statements A DDL statement can be entered as: • A single statement request. • The solitary statement, or the last statement, in an explicit transaction (in Teradata mode, one or more requests enclosed by user-supplied BEGIN TRANSACTION and END Chapter 3: SQL Data Definition, Control, and Manipulation Data Definition Language 102 SQL Reference: Fundamentals TRANSACTION statement, or in ANSI mode, one or more requests ending with the COMMIT keyword). • The solitary statement in a macro. DDL statements cannot be entered as part of a multistatement request. Successful execution of a DDL statement automatically creates and updates entries in the Data Dictionary. SQL Data Definition Statements DDL statements include the following: Related Topics For detailed information about the function, syntax, and usage of Teradata SQL Data Definition statements, see SQL Reference: Data Definition Statements. • ALTER FUNCTION • ALTER METHOD • ALTER PROCEDURE • ALTER REPLICATION GROUP • ALTER TABLE • ALTER TRIGGER • ALTER TYPE • BEGIN LOGGING • COMMENT • CREATE AUTHORIZATION • CREATE CAST • CREATE DATABASE • CREATE FUNCTION • CREATE HASH INDEX • CREATE INDEX • CREATE JOIN INDEX • CREATE MACRO • CREATE METHOD • CREATE ORDERING • CREATE PROCEDURE • CREATE PROFILE • CREATE REPLICATION GROUP • CREATE ROLE • CREATE TABLE • CREATE TRANSFORM • CREATE TRIGGER • CREATE TYPE • CREATE USER • CREATE VIEW • DATABASE • DELETE DATABASE • DELETE USER • DROP AUTHORIZATION • DROP CAST • DROP DATABASE • DROP FUNCTION • DROP HASH INDEX • DROP INDEX • DROP JOIN INDEX • DROP MACRO • DROP ORDERING • DROP PROCEDURE • DROP PROFILE • DROP REPLICATION GROUP • DROP ROLE • DROP TABLE • DROP TRANSFORM • DROP TRIGGER • DROP TYPE • DROP USER • DROP VIEW • END LOGGING • MODIFY DATABASE • MODIFY PROFILE • MODIFY USER • RENAME FUNCTION • RENAME MACRO • RENAME PROCEDURE • RENAME TABLE • RENAME TRIGGER • RENAME VIEW • REPLACE CAST • REPLACE FUNCTION • REPLACE MACRO • REPLACE METHOD • REPLACE ORDERING • REPLACE PROCEDURE • REPLACE TRANSFORM • REPLACE TRIGGER • REPLACE VIEW • SET ROLE • SET SESSION • SET TIME ZONEChapter 3: SQL Data Definition, Control, and Manipulation Altering Table Structure and Definition SQL Reference: Fundamentals 103 Altering Table Structure and Definition Introduction You may need to change the structure or definition of an existing table or temporary table. In many cases, you can use ALTER TABLE and RENAME to make the changes. Some changes, however, may require you to use CREATE TABLE to recreate the table. How to Make Changes Use the RENAME TABLE statement to change the name of a table or temporary table. Use the ALTER TABLE statement to perform any of the following functions: • Add or drop columns on an existing table or temporary table • Add column default control, FORMAT, and TITLE attributes on an existing table or temporary table • Add or remove journaling options on an existing table or temporary table • Add or remove the FALLBACK option on an existing table or temporary table • Change the DATABLOCKSIZE or percent FREESPACE on an existing table or temporary table • Add or drop column and table level constraints on an existing table or temporary table • Change the LOG and ON COMMIT options for a global temporary table • Modify referential constraints • Change the properties of the primary index for a table (some cases require an empty table) • Change the partitioning properties of the primary index for a table, including modifications to the partitioning expression defined for use by a partitioned primary index (some cases require an empty table) • Regenerate table headers and optionally validate and correct the partitioning of PPI table rows • Define, modify, or delete the COMPRESS attribute for an existing column • Change column attributes (that do not affect stored data) on an existing table or temporary table Restrictions apply to many of the preceding modifications. For a complete list of rules and restrictions on using ALTER TABLE to change the structure or definition of an existing table, see SQL Reference: Data Definition Statements. To perform any of the following functions, use CREATE TABLE to recreate the table: • Redefine the primary index or its partitioning for a non-empty table when not allowed for ALTER TABLE • Change a data type attribute that affects existing data • Add a column that would exceed the maximum column count Interactively, the SHOW TABLE statement can call up the current table definition, which can then be modified and resubmitted to create a new table.Chapter 3: SQL Data Definition, Control, and Manipulation Dropping and Renaming Objects 104 SQL Reference: Fundamentals If the stored data is not affected by incompatible data type changes, an INSERT... SELECT statement can be used to transfer data from the existing table to the new table. Dropping and Renaming Objects Dropping Objects To drop an object, use the appropriate DDL statement. Renaming Objects Teradata SQL provides RENAME statements that you can use to rename some objects. To rename objects that do not have associated RENAME statements, you must first drop them and then recreate them with a new name, or, in the case of primary indexes, use ALTER TABLE. To drop this type of database object … Use this SQL statement … Hash Index DROP HASH INDEX Join Index DROP JOIN INDEX Macro DROP MACRO Profile DROP PROFILE Role DROP ROLE Secondary Index DROP INDEX Stored procedure DROP PROCEDURE Table DROP TABLE Global temporary table or volatile table Primary index Trigger DROP TRIGGER User-Defined Function DROP FUNCTION User-Defined Method ALTER TYPE User-Defined Type DROP TYPE View DROP VIEW To rename this type of database object … Use … Hash index DROP HASH INDEX and then CREATE HASH INDEX Join index DROP JOIN INDEX and then CREATE JOIN INDEXChapter 3: SQL Data Definition, Control, and Manipulation Data Control Language SQL Reference: Fundamentals 105 Related Topics For further information on these statements, including rules that apply to usage, see SQL Reference: Data Definition Statements. Data Control Language Definition The SQL Data Control Language (DCL) is a subset of the SQL language and consists of all SQL statements that support the definition of security authorization for accessing database objects. Purpose of Data Control Statements Data control statements perform the following functions: • Grant and revoke privileges • Give ownership of a database to another user Rules on Entering Data Control Statements A data control statement can be entered as: • A single statement request • The solitary statement, or as the last statement, in an “explicit transaction” (one or more requests enclosed by user-supplied BEGIN TRANSACTION and END TRANSACTION Macro RENAME MACRO Primary index ALTER TABLE Profile DROP PROFILE and then CREATE PROFILE Role DROP ROLE and then CREATE ROLE Secondary index DROP INDEX and then CREATE INDEX Stored procedure RENAME PROCEDURE Table RENAME TABLE Global temporary table or volatile table Trigger RENAME TRIGGER User-Defined Function RENAME FUNCTION User-Defined Method ALTER TYPE and then CREATE METHOD User-Defined Type DROP TYPE and then CREATE TYPE View RENAME VIEW To rename this type of database object … Use …Chapter 3: SQL Data Definition, Control, and Manipulation Data Manipulation Language 106 SQL Reference: Fundamentals statement in Teradata mode, or in ANSI mode, one or more requests ending with the COMMIT keyword). • The solitary statement in a macro A data control statement cannot be entered as part of a multistatement request. Successful execution of a data control statement automatically creates and updates entries in the Data Dictionary. Teradata SQL Data Control Statements Data control statements include the following: • GIVE • GRANT • GRANT LOGON • REVOKE • REVOKE LOGON Related Topics For detailed information about the function, syntax, and usage of Teradata SQL Data Control statements, see “SQL Data Control Language Statement Syntax” in SQL Reference: Data Definition Statements. Data Manipulation Language Definition The SQL Data Manipulation Language (DML) is a subset of the SQL language and consists of all SQL statements that support the manipulation or processing of database objects. Selecting Columns The SELECT statement returns information from the tables in a relational database. SELECT specifies the table columns from which to obtain the data, the corresponding database (if not defined by default), and the table (or tables) to be accessed within that database. For example, to request the data from the name, salary, and jobtitle columns of the Employee table, type: SELECT name, salary, jobtitle FROM employee ; The response might be something like the following results table. Chapter 3: SQL Data Definition, Control, and Manipulation Data Manipulation Language SQL Reference: Fundamentals 107 Note: The left-to-right order of the columns in a result table is determined by the order in which the column names are entered in the SELECT statement. Columns in a relational table are not ordered logically. As long as a statement is otherwise constructed properly, the spacing between statement elements is not important as long as at least one pad character separates each element that is not otherwise separated from the next. For example, the SELECT statement in the above example could just as well be formulated like this: SELECT name, salary,jobtitle FROM employee; Notice that there are multiple pad characters between most of the elements and that a comma only (with no pad characters) separates column name salary from column name jobtitle. To select all the data in the employee table, you could enter the following SELECT statement: SELECT * FROM employee ; The asterisk specifies that the data in all columns (except system-derived columns) of the table is to be returned. Selecting Rows The SELECT statement retrieves stored data from a table. All rows, specified rows, or specific columns of all or specified rows can be retrieved. The FROM, WHERE, ORDER BY, DISTINCT, WITH, GROUP BY, HAVING, and TOP clauses provide for a fine detail of selection criteria. To obtain data from specific rows of a table, use the WHERE clause of the SELECT statement. That portion of the clause following the keyword WHERE causes a search for rows that satisfy the condition specified. For example, to get the name, salary, and title of each employee in Department 100, use the WHERE clause: SELECT name, salary, jobtitle FROM employee WHERE deptno = 100 ; Name Salary JobTitle Newman P 28600.00 Test Tech Chin M 38000.00 Controller Aquilar J 45000.00 Manager Russell S 65000.00 President Clements D 38000.00 SalespersonChapter 3: SQL Data Definition, Control, and Manipulation Data Manipulation Language 108 SQL Reference: Fundamentals The response appears in the following table. To obtain data from a multirow result table in embedded SQL, declare a cursor for the SELECT statement and use it to fetch individual result rows for processing. To obtain data from the row with the oldest timestamp value in a queue table, use the SELECT AND CONSUME statement, which also deletes the row from the queue table. Zero-Table SELECT Zero-table SELECT statements return data but do not access tables. For example, the following SELECT statement specifies an expression after the SELECT keyword that does not require a column reference or FROM clause: SELECT 40000.00 / 52.; The response is one row: (40000.00/52.) ----------------- 769.23 Here is another example that specifies an attribute function after the SELECT keyword: SELECT TYPE(sales_table.region); Because the argument to the TYPE function is a column reference that specifies the table name, a FROM clause is not required and the query does not access the table. The response is one row that might be something like the following: Type(region) --------------------------------------- INTEGER Adding Rows Use the INSERT statement to add rows to a table. One statement is required for each new row, except in the case of an INSERT...SELECT statement. For more details on this, see SQL Reference: Data Manipulation Statements. Defaults and constraints defined by the CREATE TABLE statement affect an insert operation in the following ways. Name Salary JobTitle Chin M 38000.00 Controller Greene W 32500.00 Payroll Clerk Moffit H 35000.00 Recruiter Peterson J 25000.00 Payroll ClerkChapter 3: SQL Data Definition, Control, and Manipulation Data Manipulation Language SQL Reference: Fundamentals 109 Updating Rows To modify data in one or more rows of a table, use the UPDATE statement. In the UPDATE statement, you specify the column name of the data to be modified along with the new value. You can also use a WHERE clause to qualify the rows to change. Attributes specified in the CREATE TABLE statement affect an update operation in the following ways: • When an update supplies a value that violates some defined constraint on a column or columns, the update operation is rejected and an error message is returned. • When an update supplies the value NULL and a NULL is allowed, any existing data is removed from the column. • If the result of an UPDATE will violate uniqueness constraints or create a duplicate row in a table which does not allow duplicate rows, an error message is returned. To update rows in a multirow result table in embedded SQL, declare a cursor for the SELECT statement and use it to fetch individual result rows for processing, then use a WHERE CURRENT OF clause in a positioned UPDATE statement to update the selected rows. The Teradata Database supports a special form of UPDATE, called the upsert form, which is a single SQL statement that includes both UPDATE and INSERT functionality. The specified update operation performs first, and if it fails to find a row to update, then the specified insert operation performs automatically. Alternatively, use the MERGE statement. Deleting Rows The DELETE statement allows you to remove an entire row or rows from a table. A WHERE clause qualifies the rows that are to be deleted. WHEN an INSERT statement … THEN the system … attempts to add a duplicate row • for any unique index • to a table defined as SET (not to allow duplicate rows) returns an error, with one exception. The system silently ignores duplicate rows that an INSERT … SELECT would create when the: • table is defined as SET • mode is Teradata omits a value for a column for which a default value is defined stores the default value for that column. omits a value for a column for which both of the following statements are true: • NOT NULL is specified • no default is specified rejects the operation and returns an error message. supplies a value that does not satisfy the constraints specified for a column or violates some defined constraint on a column or columnsChapter 3: SQL Data Definition, Control, and Manipulation Subqueries 110 SQL Reference: Fundamentals To delete rows in a multirow result table in embedded SQL, use the following process: 1 Declare a cursor for the SELECT statement. 2 Fetch individual result rows for processing using the cursor you declared. 3 Use a WHERE CURRENT OF clause in a positioned DELETE statement to delete the selected rows. Merging Rows The MERGE statement merges a source row into a target table based on whether any target rows satisfy a specified matching condition with the source row. The MERGE statement is a single SQL statement that includes both UPDATE and INSERT functionality. Subqueries Introduction Subqueries are nested SELECT statements. They can be used to ask a series of questions to arrive at a single answer. Three Level Subqueries: Example The following subqueries, nested to three levels, are used to answer the question “Who manages the manager of Marston?” SELECT Name FROM Employee WHERE EmpNo IN (SELECT MgrNo FROM Department WHERE DeptNo IN (SELECT DeptNo FROM Employee WHERE Name = 'Marston A') ) ; The subqueries that pose the questions leading to the final answer are inverted: • The third subquery asks the Employee table for the number of Marston’s department. • The second subquery asks the Department table for the employee number (MgrNo) of the manager associated with this department number. • The first subquery asks the Employee table for the name of the employee associated with this employee number (MgrNo). IF the source and target rows … THEN the merge operation is an … satisfy the matching condition update based on the specified WHEN MATCHED THEN UPDATE clause. do not satisfy the matching condition insert based on the specified WHEN NOT MATCHED THEN INSERT clause.Chapter 3: SQL Data Definition, Control, and Manipulation Recursive Queries SQL Reference: Fundamentals 111 The result table looks like the following: Name -------- Watson L This result can be obtained using only two levels of subquery, as the following example shows. SELECT Name FROM Employee WHERE EmpNo IN (SELECT MgrNo FROM Department, Employee WHERE Employee.Name = 'Marston A' AND Department.DeptNo = Employee.DeptNo) ; In this example, the second subquery defines a join of Employee and Department tables. This result could also be obtained using a one-level query that uses correlation names, as the following example shows. SELECT M.Name FROM Employee M, Department D, Employee E WHERE M.EmpNo = D.MgrNo AND E.Name = 'Marston A' AND D.DeptNo = E.DeptNo; In some cases, as in the preceding example, the choice is a style preference. In other cases, correct execution of the query may require a subquery. For More Information For more information, see SQL Reference: Data Manipulation Statements. Recursive Queries Introduction A recursive query is a way to query hierarchies of data, such as an organizational structure, bill-of-materials, and document hierarchy. Recursion is typically characterized by three steps: 1 Initialization 2 Recursion, or repeated iteration of the logic through the hierarchy 3 Termination Similarly, a recursive query has three execution phases: 1 Create an initial result set. 2 Recursion based on the existing result set. 3 Final query to return the final result set.Chapter 3: SQL Data Definition, Control, and Manipulation Recursive Queries 112 SQL Reference: Fundamentals Two Ways to Specify a Recursive Query You can specify a recursive query by: • Preceding a query with the WITH RECURSIVE clause • Creating a permanent view using the RECURSIVE clause in a CREATE VIEW statement Using the WITH RECURSIVE Clause Consider the following employee table: CREATE TABLE employee (employee_number INTEGER ,manager_employee_number INTEGER ,last_name CHAR(20) ,first_name VARCHAR(30)); The table represents an organizational structure containing a hierarchy of employee-manager data. The following figure depicts what the employee table looks like hierarchically. The following recursive query retrieves the employee numbers of all employees who directly or indirectly report to the manager with employee_number 801: WITH RECURSIVE temp_table (employee_number) AS ( SELECT root.employee_number FROM employee root WHERE root.manager_employee_number = 801 UNION ALL SELECT indirect.employee_number FROM temp_table direct, employee indirect 1101A285 employee # = 801 manager employee # = NULL employee # = 1003 manager employee # = 801 employee # = 1010 manager employee # = 1003 employee # = 1012 manager employee # = 1004 employee # = 1002 manager employee # = 1004 employee # = 1015 manager employee # = 1004 employee # = 1001 manager employee # = 1003 employee # = 1004 manager employee # = 1003 employee # = 1008 manager employee # = 1019 employee # = 1006 manager employee # = 1019 employee # = 1014 manager employee # = 1019 employee # = 1011 manager employee # = 1019 employee # = 1019 manager employee # = 801 employee # = 1016 manager employee # = 801Chapter 3: SQL Data Definition, Control, and Manipulation Recursive Queries SQL Reference: Fundamentals 113 WHERE direct.employee_number = indirect.manager_employee_number ) SELECT * FROM temp_table ORDER BY employee_number; In the example, temp_table is a temporary named result set that can be referred to in the FROM clause of the recursive statement. The initial result set is established in temp_table by the non-recursive, or seed, statement and contains the employees that report directly to the manager with an employee_number of 801: SELECT root.employee_number FROM employee root WHERE root.manager_employee_number = 801 The recursion takes place by joining each employee in temp_table with employees who report to the employees in temp_table. The UNION ALL adds the results to temp_table. SELECT indirect.employee_number FROM temp_table direct, employee indirect WHERE direct.employee_number = indirect.manager_employee_number Recursion stops when no new rows are added to temp_table. The final query is not part of the recursive WITH clause and extracts the employee information out of temp_table: SELECT * FROM temp_table ORDER BY employee_number; Here are the results of the recursive query: employee_number --------------- 1001 1002 1003 1004 1006 1008 1010 1011 1012 1014 1015 1016 1019 Using the RECURSIVE Clause in a CREATE VIEW Statement Creating a permanent view using the RECURSIVE clause is similar to preceding a query with the WITH RECURSIVE clause. Consider the employee table that was presented in “Using the WITH RECURSIVE Clause” on page 112. The following statement creates a view named hierarchy_801 using a recursive query that retrieves the employee numbers of all employees who directly or indirectly report to the manager with employee_number 801: CREATE RECURSIVE VIEW hierarchy_801 (employee_number) AS ( SELECT root.employee_number FROM employee rootChapter 3: SQL Data Definition, Control, and Manipulation Recursive Queries 114 SQL Reference: Fundamentals WHERE root.manager_employee_number = 801 UNION ALL SELECT indirect.employee_number FROM hierarchy_801 direct, employee indirect WHERE direct.employee_number = indirect.manager_employee_number ); The seed statement and recursive statement in the view definition are the same as the seed statement and recursive statement in the previous recursive query that uses the WITH RECURSIVE clause, except that the hierarchy_801 view name is different from the temp_table temporary result name. To extract the employee information, use the following SELECT statement on the hierarchy_801 view: SELECT * FROM hierarchy_801 ORDER BY employee_number; Here are the results: employee_number --------------- 1001 1002 1003 1004 1006 1008 1010 1011 1012 1014 1015 1016 1019 Depth Control to Avoid Infinite Recursion If the hierarchy is cyclic, or if the recursive statement specifies a bad join condition, a recursive query can produce a runaway query that never completes with a finite result. The best practice is to control the depth of the recursion as follows: • Specify a depth control column in the column list of the WITH RECURSIVE clause or recursive view • Initialize the column value to 0 in the seed statements • Increment the column value by 1 in the recursive statements • Specify a limit for the value of the depth control column in the join condition of the recursive statements Here is an example that modifies the previous recursive query that uses the WITH RECURSIVE clause of the employee table to limit the depth of the recursion to five cycles: WITH RECURSIVE temp_table (employee_number, depth) AS ( SELECT root.employee_number, 0 AS depth FROM employee root WHERE root.manager_employee_number = 801 Chapter 3: SQL Data Definition, Control, and Manipulation Query and Workload Analysis Statements SQL Reference: Fundamentals 115 UNION ALL SELECT indirect.employee_number, direct.depth+1 AS newdepth FROM temp_table direct, employee indirect WHERE direct.employee_number = indirect.manager_employee_number AND newdepth <= 5 ) SELECT * FROM temp_table ORDER BY employee_number; Related Topics Query and Workload Analysis Statements Data Collection and Analysis Collected data can be used in several ways, for example: • By the Optimizer, to produce the best query plans possible. • To populate user-defined Query Capture Database (QCD) tables with data used by various utilities to analyze query workloads as part of the ongoing process to reengineer the database design process. For example, the Teradata Index Wizard determines optimal secondary index sets to support the query workloads you ask it to analyze. Index Analysis and Target Level Emulation Teradata also provides diagnostic statements that support the Teradata Index Wizard and the sample-based components of the target level emulation facility used to emulate a production environment on a test system: • DIAGNOSTIC DUMP SAMPLES • DIAGNOSTIC HELP SAMPLES FOR details on … SEE … recursive queries “WITH RECURSIVE” in SQL Reference: Data Manipulation Statements. recursive views “CREATE VIEW” in SQL Reference: Data Definition Statements. Teradata provides the following SQL statements for collecting and analyzing query and data demographics and statistics: • BEGIN QUERY LOGGING • COLLECT DEMOGRAPHICS • COLLECT STATISTICS • DROP STATISTICS • DUMP EXPLAIN • END QUERY LOGGING • INITIATE INDEX ANALYSIS • INSERT EXPLAIN • RESTART INDEX ANALYSISChapter 3: SQL Data Definition, Control, and Manipulation Help and Database Object Definition Tools 116 SQL Reference: Fundamentals • DIAGNOSTIC SET SAMPLES • DIAGNOSTIC “Validate Index” After configuring the test environment and enabling it with the appropriate production system statistical and demographic data, you can perform various workload analyses to determine optimal sets of secondary indexes to support those workloads in the production environment. Related Topics For more information on query and workload analysis statements, see SQL Reference: Data Definition Statements. Help and Database Object Definition Tools Introduction Teradata SQL provides several powerful tools to get help about database object definitions and summaries of database object definition statement text. HELP Statements The various HELP statements return reports about the current column definitions for named database objects. The reports returned by these statements can be useful to database designers who need to fine tune index definitions, column definitions (for example, changing data typing to eliminate the necessity of ad hoc conversions), and so on. IF you want to get … THEN use … the attributes of a column, including whether it is a single-column primary or secondary index and, if so, whether it is unique HELP COLUMN the attributes for a specific named constraint on a table HELP CONSTRAINT the attributes, sorted by object name, for all tables, views, join and hash indexes, stored procedures, user-defined functions, and macros in a specified database HELP DATABASE and HELP USER the specific function name, list of parameters, data types of the parameters, and any comments associated with the parameters of a userdefined function HELP FUNCTION the data types of the columns defined by a particular hash index HELP HASH INDEX the attributes for the indexes defined for a table or join index HELP INDEX the attributes of the columns defined by a particular join index HELP JOIN INDEX the attributes for the specified macro HELP MACRO the specific name, list of parameters, data types of the parameters, and any comments associated with the parameters of a user-defined method HELP METHODChapter 3: SQL Data Definition, Control, and Manipulation Help and Database Object Definition Tools SQL Reference: Fundamentals 117 SHOW Statements A SHOW statement returns a CREATE statement indicating the last data definition statement performed against the named database object. These statements are particularly useful for application developers who need to develop exact replicas of existing objects for purposes of testing new software. the attributes for the specified join index or table HELP TABLE the attribute and format parameters for each parameter of the procedure or just the creation time attributes for the specified procedure HELP PROCEDURE the attributes of the specified replication group and its member tables HELP REPLICATION GROUP the attributes for the specified trigger HELP TRIGGER information on the type, attributes, methods, cast, ordering, and transform of the specified user-defined type HELP TYPE the attributes for a specified view HELP VIEW the attributes for the requested volatile table HELP VOLATILE TABLE IF you want to get … THEN use … IF you want to get the data definition statement most recently used to create, replace, or modify a specified … THEN use … hash index SHOW HASH INDEX join index SHOW JOIN INDEX macro SHOW MACRO stored procedure or external stored procedure SHOW PROCEDURE table SHOW TABLE trigger SHOW TRIGGER user-defined function SHOW FUNCTION user-defined method SHOW METHOD user-defined type SHOW TYPE view SHOW VIEWChapter 3: SQL Data Definition, Control, and Manipulation Help and Database Object Definition Tools 118 SQL Reference: Fundamentals Example Consider the following definition for a table named department: CREATE TABLE department, FALLBACK (department_number SMALLINT ,department_name CHAR(30) NOT NULL ,budget_amount DECIMAL(10,2) ,manager_employee_number INTEGER ) UNIQUE PRIMARY INDEX (department_number) ,UNIQUE INDEX (department_name); To get the attributes for the table, use the HELP TABLE statement: HELP TABLE department; The HELP TABLE statement returns: Column Name Type Comment ------------------------------ ---- ------------------------- department_number I2 ? department_name CF ? budget_amount D ? manager_employee_number I ? To get the CREATE TABLE statement that defines the department table, use the SHOW TABLE statement: SHOW TABLE department; The SHOW TABLE statement returns: CREATE SET TABLE TERADATA_EDUCATION.department, FALLBACK, NO BEFORE JOURNAL, NO AFTER JOURNAL, CHECKSUM = DEFAULT (department_number SMALLINT, department_name CHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL, budget_amount DECIMAL(10,2), manager_employee_number INTEGER) UNIQUE PRIMARY INDEX ( department_number ) UNIQUE INDEX ( department_name ); Related Topics For more information, see SQL Reference: Data Definition Statements.SQL Reference: Fundamentals 119 CHAPTER 4 SQL Data Handling This chapter describes the fundamentals of Teradata Database data handling. Topics include: • Requests • Transactions • Event processing • Session Parameters • Session Management • Return Codes Invoking SQL Statements Introduction One of the primary issues that motivated the development of relational database management systems was the perceived need to create database management systems that could be queried not just by predetermined, hard-coded requests but also interactively by well-formulated ad hoc queries. SQL addresses this issue by offering four ways to invoke an executable statement: • Interactively from a terminal • Embedded within an application program • Dynamically performed from within an embedded application • Embedded within a stored procedure Executable SQL Statements An executable SQL statement is one that performs an action. The action can be on data or on a transaction or some other entity at a higher level than raw data. Some examples of executable SQL statements are the following: • SELECT • CREATE TABLE • COMMIT • CONNECT • PREPAREChapter 4: SQL Data Handling Requests 120 SQL Reference: Fundamentals Most, but not all, executable SQL statements can be performed interactively from a terminal using an SQL query manager like BTEQ or Teradata SQL Assistant (formerly called Queryman). Types of executable SQL commands that cannot be performed interactively are the following: • Cursor control and declaration statements • Dynamic SQL control statements • Stored procedure control statements and condition handlers • Connection control statements • Special forms of SQL statements such as SELECT INTO These statements can only be used within an embedded SQL or stored procedure application. Nonexecutable SQL Statements A nonexecutable SQL statement is one that declares an SQL statement, object, or host or local variable to the preprocessor or stored procedure compiler. Nonexecutable SQL statements are not processed during program execution. Some examples of nonexecutable SQL statements for embedded SQL applications include: • DECLARE CURSOR • BEGIN DECLARE SECTION • END DECLARE SECTION • EXEC SQL Examples of nonexecutable SQL statements for stored procedures include: • DECLARE CURSOR • DECLARE Requests Introduction A request to the Teradata Database consists of one or more SQL statements and can span any number of input lines. Teradata Database can receive and perform SQL statements that are: • Embedded in a client application program that is written in a procedural language. • Embedded in a stored procedure. • Entered interactively through BTEQ or Teradata SQL Assistant interfaces. • Submitted in a BTEQ script as a batch job. • Submitted through other supported methods (such as CLIv2, ODBC, and JDBC).Chapter 4: SQL Data Handling Requests SQL Reference: Fundamentals 121 Single Statement Requests A single statement request consists of a statement keyword followed by one or more expressions, other keywords, clauses, and phrases. A single statement request is treated as a solitary unit of work. Single Statement Syntax Multistatement Requests A multistatement request consists of two or more statements separated by SEMICOLON characters. Multistatement requests are non-ANSI standard. For more information, see “Multistatement Requests” on page 124. Multistatement Syntax Iterated Requests An iterated request is a single DML statement with multiple data records. Iterated requests do not directly impact the syntax of SQL statements. They provide a more performant way of processing DML statements that specify the USING row descriptor to import or export data from the Teradata Database. For more information, see “Iterated Requests” on page 127. ANSI Session Mode If an error is found in a request, then that request is aborted, but not the entire transaction. Note: Some failures will abort the entire transaction. Teradata Session Mode A multistatement request is treated as an implicit transaction. That is, if an error is found in any statement in the request, then the entire transaction is aborted. HH01A003 ; statement HH01A004 ; statement ;Chapter 4: SQL Data Handling Transactions 122 SQL Reference: Fundamentals Abort processing proceeds as follows: 1 Backs out any changes made to the database as a result of any preceding statements. 2 Deletes any associated spooled output. 3 Releases any associated locks. 4 Bypasses any remaining statements in the transaction. Complete Requests A request is considered complete when either an End of Text character or the request terminator is encountered. The request terminator is a SEMICOLON character. It is the last nonpad character on an input line. A request terminator is optional except when the request is embedded in an SQL macro or trigger or when it is entered through BTEQ. In a stored procedure, each SQL statement is treated as a request. Stored procedures do not support multistatement requests. Transactions Introduction A transaction is a logical unit of work where the statements nested within the transaction either execute successfully as a group or do not execute. Transaction Processing Mode You can perform transaction processing in either of the following session modes: • ANSI • Teradata In ANSI session mode, transaction processing adheres to the rules defined by the ANSI SQL specification. In Teradata session mode, transaction processing follows the rules defined by Teradata Database over years of evolution. To set the transaction processing mode, use the: • SessionMode field of the DBS Control Record • BTEQ command .SET SESSION TRANSACTION • Preprocessor2 TRANSACT() option • ODBC SessionMode option in the .odbc.ini file • JDBC TeraDataSource.setTransactMode() methodChapter 4: SQL Data Handling Transaction Processing in ANSI Session Mode SQL Reference: Fundamentals 123 Related Topics The next few pages highlight some of the differences between transaction processing in ANSI session mode and transaction processing in Teradata session mode. For detailed information on statement and transaction processing, see SQL Reference: Statement and Transaction Processing. Transaction Processing in ANSI Session Mode Introduction Transactions are always implicit in ANSI session mode. A transaction initiates when one of the following happens: • The first SQL statement in a session executes • The first statement following the close of a transaction executes The COMMIT or ROLLBACK/ABORT statements close a transaction. If a transaction includes a DDL statement, it must be the last statement in the transaction. Note that DATABASE and SET SESSION are DDL statements. See “Rollback Processing” in SQL Reference: Statement and Transaction Processing. If a session terminates with an open transaction, then any effects of that transaction are rolled back. Two-Phase Commit (2PC) Sessions in ANSI session mode do not support 2PC. If an attempt is made to use the 2PC protocol in ANSI session mode, the Logon process aborts and an error returns to the requestor. Transaction Processing in Teradata Session Mode Introduction A Teradata SQL transaction can be a single Teradata SQL statement, or a sequence of Teradata SQL statements, treated as a single unit of work. Each request is processed as one of the following transaction types: • Implicit • Explicit • Two-phase commit (2PC)Chapter 4: SQL Data Handling Multistatement Requests 124 SQL Reference: Fundamentals Implicit Transactions An implicit transaction is a request that does not include the BEGIN TRANSACTION and END TRANSACTION statements. The implicit transaction starts and completes all within the SQL request: it is self-contained. An implicit transaction can be one of the following: • A single DML statement that affects one or more rows of one or more tables • A macro or trigger containing one or more statements • A request containing multiple statements separated by SEMICOLON characters. Each SEMICOLON character can appear anywhere in the input line. The Parser interprets a SEMICOLON character at the end of an input line as a transaction terminator. DDL statements are not valid in an implicit multistatement transaction. Explicit Transactions In Teradata session mode, an explicit transaction contains one or more statements enclosed by BEGIN TRANSACTION and END TRANSACTION statements. The first BEGIN TRANSACTION initiates a transaction and the last END TRANSACTION terminates the transaction. When multiple statements are included in an explicit transaction, you can only specify a DDL statement if it is the last statement in the series. Two-Phase Commit (2PC) Rules Two-phase commit (2PC) protocol is supported in Teradata session mode: • A 2PC transaction contains one or more DML statements that affect multiple databases and are coordinated externally using the 2PC protocol. • A DDL statement is not valid in a two-phase commit transaction. Multistatement Requests Definition An atomic request containing more than one SQL statement, each terminated by a SEMICOLON character. Syntax HH01A004 ; statement ;Chapter 4: SQL Data Handling Multistatement Requests SQL Reference: Fundamentals 125 ANSI Compliance Multistatement requests are non-ANSI SQL-2003 standard. Rules The Teradata Database imposes restrictions on the use of multistatement requests: • Only one USING row descriptor is permitted per request, so only one USING row descriptor can be used per multistatement request. This rule applies to interactive SQL only. Embedded SQL and stored procedures do not permit the USING row descriptor. • A multistatement request cannot include a DDL statement. • The keywords BEGIN REQUEST and END REQUEST must delimit a multistatement request in a stored procedure. Power of Multistatement Requests The multistatement request is application-independent. It improves performance for a variety of applications that can package more than one SQL statement at a time. BTEQ, CLI, and the SQL preprocessor all support multistatement requests. Multistatement requests improve system performance by reducing processing overhead. By performing a series of statements as one request, performance for the client, the Parser, and the Database Manager are all enhanced. Because of this reduced overhead, using multistatement requests also decreases response time. A multistatement request that contains 10 SQL statements could be as much as 10 times more efficient than the 10 statements entered separately (depending on the types of statements submitted). Multistatement Requests Treated as Transaction In a multistatement request, treated as a single unit of work, either all statements in the request complete successfully, or the entire request is aborted. In ANSI session mode, the request is rolled back if aborted. In Teradata session mode, any updates to the database up to that point for the transaction are rolled back. Parallel Step Processing Teradata Database can perform some requests in parallel (see “Parallel Steps” on page 126). This capability applies both to implicit transactions, such as macros and multistatement requests, and to Teradata-style transactions explicitly defined by BEGIN/END TRANSACTION statements. Statements in a multistatement request are broken down by the Parser into one or more steps that direct the execution performed by the AMPs. It is these steps, not the actual statements, that are executed in parallel.Chapter 4: SQL Data Handling Multistatement Requests 126 SQL Reference: Fundamentals A handshaking protocol between the PE and the AMP allows the AMP to determine when the PE can dispatch the next parallel step. Up to twenty parallel steps can be processed per request if channels are not required, such as a request with an equality constraint based on a primary index value. Up to ten channels can be used for parallel processing when a request is not constrained to a primary index value. For example, if an INSERT step and a DELETE step are allowed to run in parallel, the AMP informs the PE that the DELETE step has progressed to the point where the INSERT step will not impact it adversely. This handshaking protocol also reduces the chance of a deadlock. “Parallel Steps” on page 126 illustrates the following process: 1 The statements in a multistatement request are broken down into a series of steps. 2 The Optimizer determines which steps in the series can be executed in parallel. 3 The steps are processed. Each step undergoes some preliminary processing before it is executed, such as placing locks on the objects involved. These preliminary processes are not performed in parallel with the steps. Parallel Steps Time Step 1 2 3 4 5 6 7 8 9 (1) Time (2) Time Step 1 2 5 6 9 7 8 (3) FF02A001 Step 1 2 3 4 5 6 7 8 9 3 4Chapter 4: SQL Data Handling Iterated Requests SQL Reference: Fundamentals 127 Iterated Requests Definition A single DML statement with multiple data records. Usage An iterated request is an atomic request consisting of a single SQL DML statement with multiple sets (records) of data. Iterated requests do not directly impact the syntax of SQL statements. They provide an efficient way to execute the same single-statement DML operation on multiple data records, like the way that ODBC applications execute parameterized statements for arrays of parameter values, for example. Several Teradata Database client tools and interfaces provide facilities to pack multiple data records in a single buffer with a single DML statement. For example, suppose you use BTEQ to import rows of data into table ptable using the following INSERT statement and USING row descriptor: USING (pid INTEGER, pname CHAR(12)) INSERT INTO ptable VALUES(:pid, :pname); To repeat the request as many times as necessary to read up to 200 data records and pack a maximum of 100 data records with each request, precede the INSERT statement with the following BTEQ command: .REPEAT RECS 200 PACK 100 Note: The PACK option is ignored if the database being used does not support iterated requests or if the request that follows the REPEAT command is not a DML statement supported by iterated requests. For details, see “Rules” on page 128. The following tools and interfaces provide facilities that you can use to execute iterated requests. Tool/Interface Facility CLIv2 for network-attached systems using_data_count field in the DBCAREA data area CLIv2 for channel-attached systems Using-data-count field in the DBCAREA data area ODBC Parameter arrays JDBC type 4 driver Batch operations OLE DB Provider for Teradata Parameter sets BTEQ • .REPEAT command • .SET PACK commandChapter 4: SQL Data Handling Iterated Requests 128 SQL Reference: Fundamentals Rules The following rules apply to iterated requests: • The iterated request must consist of a single DML statement from the following list: • ABORT • DELETE (excluding the positioned form of DELETE) • EXECUTE macro_name The fully-expanded macro must be equivalent to a single DML statement that is qualified to be in an iterated request. • INSERT • MERGE • ROLLBACK • SELECT • UPDATE (including atomic UPSERT, but excluding the positioned form of UPDATE) • The DML statement must reference user-supplied input data, either as named fields in a USING row descriptor or as '?' parameter markers in a parameterized request. • All the data records in a given request must use the same record layout. This restriction applies by necessity to requests where the record layout is given by a single USING row descriptor in the request text itself; but note that the restriction also applies to parameterized requests, where the request text has no USING descriptor and does not fully specify the input record. • The server processes the iterated request as if it were a single multi-statement request, with each iteration and its response associated with a corresponding statement number. Related Topics FOR more information on … SEE … iterated request processing SQL Reference: Statement and Transaction Processing which DML statements can be specified in an iterated request SQL Reference: Data Manipulation Statements CLIv2 • Teradata Call-Level Interface Version 2 Reference for Channel-Attached Systems • Teradata Call-Level Interface Version 2 Reference for Network-Attached Systems ODBC parameter arrays ODBC Driver for Teradata User Guide JDBC driver batch operations Teradata Driver for the JDBC Interface User Guide OLE DB Provider for Teradata parameter sets OLE DB Provider for Teradata Installation and User Guide BTEQ PACK command Basic Teradata Query ReferenceChapter 4: SQL Data Handling Dynamic and Static SQL SQL Reference: Fundamentals 129 Dynamic and Static SQL Definitions ANSI Compliance Dynamic SQL is ANSI SQL-2003-compliant. The ANSI SQL standard does not define the expression static SQL, but relational database management commonly uses it to contrast with the ANSI-defined expression dynamic SQL. Ad Hoc and Hard-Coded Invocation of SQL Statements Perhaps the best way to think of dynamic SQL is to contrast it with ad hoc SQL statements created and executed from a terminal and with preprogrammed SQL statements created by an application programmer and executed by an application program. In the case of the ad hoc query, everything legal is available to the requester: choice of SQL statements and clauses, variables and their names, databases, tables, and columns to manipulate, and literals. In the case of the application programmer, the choices are made in advance and hard-coded into the source code of the application. Once the program is compiled, nothing can be changed short of editing and recompiling the application. Dynamic Invocation of SQL Statements Dynamic SQL offers a compromise between these two extremes. By choosing to code dynamic SQL statements in the application, the programmer has the flexibility to allow an end user to select not only the variables to be manipulated at run time, but also the SQL statement to be executed. As you might expect, the flexibility that dynamic SQL offers a user is offset by more work and increased attention to detail on the part of the application programmer, who needs to set up additional dynamic SQL statements and manipulate information in the SQLDA to ensure a correct result. This is done by first preparing, or compiling, an SQL text string containing placeholder tokens at run time and then executing the prepared statement, allowing the application to prompt the user for values to be substituted for the placeholders. Term Definition Dynamic SQL Dynamic SQL is a method of invoking an SQL statement by compiling and performing it at runtime from within an embedded SQL application program or a stored procedure. The specification of data to be manipulated by the statement is also determined at runtime. Static SQL Static SQL is, by default, any method of invoking an SQL statement that is not dynamic.Chapter 4: SQL Data Handling Dynamic SQL in Stored Procedures 130 SQL Reference: Fundamentals SQL Statements to Set Up and Invoke Dynamic SQL The embedded SQL statements for preparing and executing an SQL statement dynamically are: • PREPARE • EXECUTE • EXECUTE IMMEDIATE. EXECUTE IMMEDIATE is a special form that combines PREPARE and EXECUTE into one statement. EXECUTE IMMEDIATE can only be used in the case where there are no input host variables. This description applies directly to all executable SQL statements except SELECT, which requires additional handling. Note that SELECT INTO cannot be invoked dynamically. For details, see SQL Reference: Stored Procedures and Embedded SQL. Related Topics Dynamic SQL in Stored Procedures Overview The way stored procedures support dynamic SQL statements is different from the way embedded SQL does. Use the following statement to set up and invoke dynamic SQL in a stored procedure: CALL DBC.SysExecSQL(string_expression) where string_expression is any valid string expression that builds an SQL statement. The string expression is composed of string literals, status variables, local variables, input (IN and INOUT) parameters, and for-loop aliases. Dynamic SQL statements are not validated at compile time. The resulting SQL statement cannot have status variables, local variables, parameters, for-loop aliases, or a USING or EXPLAIN modifier. For more information on … See … examples of dynamic SQL code in C, COBOL, and PL/I Teradata Preprocessor2 for Embedded SQL Programmer Guide.Chapter 4: SQL Data Handling Using SELECT With Dynamic SQL SQL Reference: Fundamentals 131 Example The following example uses dynamic SQL within stored procedure source text: CREATE PROCEDURE new_sales_table( my_table VARCHAR(30), my_database VARCHAR(30)) BEGIN DECLARE sales_columns VARCHAR(128) DEFAULT '(item INTEGER, price DECIMAL(8,2), sold INTEGER)'; CALL DBC.SysExecSQL('CREATE TABLE ' || my_database || '.' || my_table || sales_columns); END; Any number of calls to SysExecSQL can be made in a stored procedure and the request text in the string expression can specify a multistatement request. Because the request text of dynamic SQL statements can vary from execution to execution, dynamic SQL provides more usability and conciseness to the stored procedure definition. Restrictions Dynamic SQL statements can be specified in a stored procedure only when the creator is the same as the immediate "owner" of the stored procedure. The following SQL statements cannot be specified as dynamic SQL in stored procedures: Related Topics For rules and usage examples of dynamic SQL statements in stored procedures, see SQL Reference: Stored Procedures and Embedded SQL. Using SELECT With Dynamic SQL Unlike other executable SQL statements, SELECT returns information beyond statement responses and return codes to the requester. DESCRIBE Statement Because the requesting application needs to know how much (if any) data will be returned by a dynamically prepared SELECT, you must use an additional SQL statement, DESCRIBE, to make the application aware of the demographics of the data to be returned by the SELECT statement (see “DESCRIBE” in SQL Reference: Stored Procedures and Embedded SQL). • CALL • DATABASE • HELP • SELECT • SET SESSION ACCOUNT • SET SESSION DATEFORM • SHOW • CREATE PROCEDURE • EXPLAIN • REPLACE PROCEDURE • SELECT - INTO • SET SESSION COLLATION • SET TIME ZONEChapter 4: SQL Data Handling Using SELECT With Dynamic SQL 132 SQL Reference: Fundamentals DESCRIBE writes this information to the SQLDA declared for the SELECT statement as follows. General Procedure An application must use the following general procedure to set up, execute, and retrieve the results of a SELECT statement invoked as dynamic SQL. 1 Declare a dynamic cursor for the SELECT in the form: DECLARE cursor_name CURSOR FOR sql_statement_name 2 Declare the SQLDA, preferably using an INCLUDE SQLDA statement. 3 Build and PREPARE the SELECT statement. 4 Issue a DESCRIBE statement in the form: DESCRIBE sql_statement_name INTO SQLDA DESCRIBE performs the following actions: a Interrogate the database for the demographics of the expected results. b Write the addresses of the target variables to receive those results to the SQLDA. This step is bypassed if any of the following occurs: • The request does not return any data. • An INTO clause was present in the PREPARE statement. • The statement returns known columns and the INTO clause is used on the corresponding FETCH statement. • The application code defines the SQLDA. 5 Allocate storage for target variables to receive the returned data based on the demographics reported by DESCRIBE. 6 Retrieve the result rows using the following SQL cursor control statements: • OPEN cursor_name • FETCH cursor_name USING DESCRIPTOR SQLDA • CLOSE cursor_name Note that in step 6, results tables are examined one row at a time using the selection cursor. This is because client programming languages do not support data in terms of sets, but only as individual records. THIS information … IS written to this field of SQLDA … number of values to be returned SQLN column name or label of n th value SQLVAR (n th row in the SQLVAR(n) array) column data type of n th value column length of n th valueChapter 4: SQL Data Handling Event Processing Using Queue Tables SQL Reference: Fundamentals 133 Event Processing Using Queue Tables Introduction Teradata Database provides queue tables that you can use for event processing. Queue tables are base tables with first-in-first-out (FIFO) queue properties. When you create a queue table, you define a timestamp column. You can query the queue table to retrieve data from the row with the oldest timestamp. Usage An application can perform FIFO push, pop, and peek operations on queue tables. Here is an example of how an application can process events using queue tables: • Internally, you can define a trigger on a base table to insert a row into the queue table when the trigger fires. • Externally, your application can submit a SELECT AND CONSUME statement that waits for data in the queue table. • When data arrives in the queue table, the waiting SELECT AND CONSUME statement returns a result to the external application, which processes the event. Additionally, the row is deleted from the queue table. Related Topics TO perform a FIFO … USE the … push INSERT statement pop SELECT AND CONSUME statement peek SELECT statement FOR more information on … SEE … creating queue tables the CREATE/REPLACE TABLE statement in SQL Reference: Data Definition Statements SELECT AND CONSUME SQL Reference: Data Manipulation StatementsChapter 4: SQL Data Handling Manipulating Nulls 134 SQL Reference: Fundamentals Manipulating Nulls Introduction A null represents any of three things: • An empty field • An unknown value • An unknowable value Nulls are neither values nor do they signify values; they represent the absence of value. A null is a place holder indicating that no value is present. You cannot solve for the value of a null because, by definition, it has no value. For example, the expression NULL = NULL has no meaning and therefore can never be true. A query that specifies the predicate WHERE NULL = NULL is not valid because it can never be true. The meaning of the comparison it specifies is not only unknown, but unknowable. These properties make the use and interpretation of nulls in SQL problematic. The following sections outline the behavior of nulls for various SQL operations to help you to understand how to use them in data manipulation statements and to interpret the results those statements affect. NULL Literals See “NULL Keyword as a Literal” on page 90 for information on how to use the NULL keyword as a literal. Nulls and DateTime and Interval Data A DateTime or Interval value is either atomically null or it is not null. For example, you cannot have an interval of YEAR TO MONTH in which YEAR is null and MONTH is not. Result of Expressions That Contain Nulls Here are some general rules for the result of expressions that contain nulls: • When any component of a value expression is null, then the result is null. • The result of a conditional expression that has a null component is unknown. • If an operand of any arithmetic operator (such as + or -) or function (such as ABS or SQRT) is null, then the result of the operation or function is null with the exception of ZEROIFNULL. If the argument to ZEROIFNULL is NULL, then the result is 0. • COALESCE, a special shorthand variant of the CASE expression, returns NULL if all its arguments evaluate to null. Otherwise, COALESCE returns the value of the fist non-null argument. For more rules on the result of expressions containing nulls, see the sections that follow and SQL Reference: Functions and Operators.Chapter 4: SQL Data Handling Manipulating Nulls SQL Reference: Fundamentals 135 Nulls and Comparison Operators If either operand of a comparison operator is null, then the result is unknown. If either operand is the keyword NULL, an error is returned that recommends using IS NULL or IS NOT NULL instead. The following examples indicate this behavior. 5 = NULL 5 <> NULL NULL = NULL NULL <> NULL 5 = NULL + 5 Note that if the argument of the NOT operator is unknown, the result is also unknown. This translates to FALSE as a final boolean result. Instead of using comparison operators, use the IS NULL operator to search for fields that contain nulls and the IS NOT NULL operator to search for fields that do not contain nulls. For details, see “Searching for Nulls” on page 135 and “Excluding Nulls” on page 135. Using IS NULL is different from using the comparison operator =. When you use an operator like =, you specify a comparison between values or value expressions, whereas when you use the IS NULL operator, you specify an existence condition. Nulls and CASE Expressions The following rules apply to nulls and CASE expressions: • CASE and its related expressions COALESCE and NULLIF can return a null. • NULL and null expressions are valid as the CASE test expression in a valued CASE expression. • When testing for NULL, it is best to use a searched CASE expression using the IS NULL or IS NOT NULL operators in the WHEN clause. • NULL and null expressions are valid as THEN clause conditions. For details on the rules for nulls in CASE, NULLIF, and COALESCE expressions, see SQL Reference: Functions and Operators. Excluding Nulls To exclude nulls from the results of a query, use the operator IS NOT NULL. For example, to search for the names of all employees with a value other than null in the jobtitle column, enter the statement. SELECT name FROM employee WHERE jobtitle IS NOT NULL ; Searching for Nulls To search for columns that contain nulls, use the operator IS NULL. The IS NULL operator tests row data for the presence of nulls. Chapter 4: SQL Data Handling Manipulating Nulls 136 SQL Reference: Fundamentals For example, to search for the names of all employees who have a null in the deptno column, you could enter the statement: SELECT name FROM employee WHERE deptno IS NULL ; This query produces the names of all employees with a null in the deptno field. Searching for Nulls and Non-Nulls Together To search for nulls and non-nulls in the same statement, the search condition for nulls must be separate from any other search conditions. For example, to select the names of all employees with the job title of Vice Pres, Manager, or null, enter the following SELECT statement. SELECT name, jobtitle FROM employee WHERE jobtitle IN ('Manager', 'Vice Pres') OR jobtitle IS NULL ; Including NULL in the IN list has no effect because NULL never equals NULL or any value. Null Sorts as the Lowest Value in a Collation When you use an ORDER BY clause to sort records, Teradata Database sorts null as the lowest value. Sorting nulls can vary from RDBMS to RDBMS. Other systems may sort null as the highest value. If any row has a null in the column being grouped, then all rows having a null are placed into one group. NULL and Unique Indexes For unique indexes, Teradata Database treats nulls as if they are equal rather than unknown (and therefore false). For single-column unique indexes, only one row may have null for the index value; otherwise a uniqueness violation error occurs. For multi-column unique indexes, no two rows can have nulls in the same columns of the index and also have non-null values that are equal in the other columns of the index. For example, consider a two-column index. Rows can occur with the following index values: An attempt to insert a row that matches any of these rows will result in a uniqueness violation. Value of First Column in Index Value of Second Column in Index 1 null null 1 null nullChapter 4: SQL Data Handling Manipulating Nulls SQL Reference: Fundamentals 137 Teradata Database Replaces Nulls With Values on Return to Client in Record Mode When the Teradata Database returns information to a client system in record mode, nulls must be replaced with some value for the underlying column because client system languages do not recognize nulls. The following table shows the values returned for various column data types. The substitute values returned for nulls are not, by themselves, distinguishable from valid non-null values. Data from CLI is normally accessed in IndicData mode, in which additional identifying information that flags nulls is returned to the client. BTEQ uses the identifying information, for example, to determine whether the values it receives are values or just aliases for nulls so it can properly report the results. Note that BTEQ displays nulls as ?, which are not by themselves distinguishable from a CHAR or VARCHAR value of '?'. Nulls and Aggregate Functions With the important exception of COUNT(*), aggregate functions ignore nulls in their arguments. This treatment of nulls is very different from the way arithmetic operators and functions treat them. This behavior can result in apparent nontransitive anomalies. For example, if there are nulls in either column A or column B (or both), then the following expression is virtually always true. SUM(A) + (SUM B) <> SUM (A+B) Data Type Substitute Value Returned for Null CHARACTER(n) DATE (ANSI) TIME TIMESTAMP INTERVAL Pad character (or n pad characters for CHARACTER(n), where n > 1) BYTE[(n)] Binary zero byte if n omitted else n binary zero bytes VARBYTE(n) 0-length byte string VARCHARACTER(n) 0-length character string DATE (Teradata) 0 BIGINT INTEGER SMALLINT BYTEINT FLOAT DECIMAL REAL DOUBLE PRECISION NUMERIC 0Chapter 4: SQL Data Handling Session Parameters 138 SQL Reference: Fundamentals In other words, for the case of SUM, the result is never a simple iterated addition if there are nulls in the data being summed. The only exception to this is the case in which the values for columns A and B are both null in the same rows, because in those cases the entire row is disregarded in the aggregation. This is a trivial case that does not violate the general rule. The same is true, the necessary changes being made, for all the aggregate functions except COUNT(*). If this property of nulls presents a problem, you can always do either of the following workarounds, each of which produces the desired result of the aggregate computation SUM(A) + SUM(B) = SUM(A+B). • Always define NUMERIC columns as NOT NULL DEFAULT 0. • Use the ZEROIFNULL function within the aggregate function to convert any nulls to zeros for the computation, for example SUM(ZEROIFNULL(x) + ZEROIFNULL(y)) which produces the same result as this: SUM(ZEROIFNULL(x) + ZEROIFNULL(y)). COUNT(*) does include nulls in its result. For details, see SQL Reference: Functions and Operators. RANGE_N and CASE_N Functions Nulls have special considerations in the RANGE_N and CASE_N functions. For details, see SQL Reference: Functions and Operators. Session Parameters Introduction The following session parameters can be controlled with keywords or predefined system variables. Parameter Valid Keywords or System Variables SQL Flagger ON OFF Transaction Mode ANSI (COMMIT) Teradata (BTET)Chapter 4: SQL Data Handling Session Parameters SQL Reference: Fundamentals 139 SQL Flagger When enabled, the SQL Flagger assists SQL programmers by notifying them of the use of nonANSI and non-entry level ANSI SQL syntax. Enabling the SQL Flagger can be done regardless of whether you are in ANSI or Teradata session mode. Session Collation ASCII EBCDIC MULTINATIONAL HOST CHARSET_COLL JIS_COLL Account and Priority Account and reprioritization. Within the account identifier, you can specify a performance group or use one of the following predefined performance groups: • $R • $H • $M • $L Date Form ANSIDATE INTEGERDATE Character Set Indicates the character set being used by the client. You can view site-installed client character sets from DBC.CharSets or DBC.CharTranslations. The following client character sets are permanently enabled: • ASCII • EBCDIC • UTF8 • UTF16 For more information on character sets, see International Character Set Support. Express Logon (for networkattached clients) ENABLE DISABLE Parameter Valid Keywords or System VariablesChapter 4: SQL Data Handling Session Parameters 140 SQL Reference: Fundamentals To set the SQL Flagger on or off for interactive SQL, use the .SET SESSION command in BTEQ. For more detail on using the SQL Flagger, see “SQL Flagger” on page 217. To set the SQL Flagger on or off for embedded SQL, use the SQLCHECK or -sc and SQLFLAGGER or -sf options when you invoke the preprocessor. If you are using SQL in other application programs, see the reference manual for that application for instructions on enabling the SQL Flagger. Transaction Mode You can run transactions in either Teradata or ANSI session modes and these modes can be set or changed. To set the transaction mode, use the .SET SESSION command in BTEQ. For more detail on transaction semantics, see “Transaction Processing” in SQL Reference: Statement and Transaction Processing. If you are using SQL in other application programs, see the reference manual for that application for instructions on setting or changing the transaction mode. Session Collation Collation of character data is an important and complex option. The Teradata Database provides several named collations. The MULTINATIONAL and CHARSET_COLL collations allow the system administrator to provide collation sequences tailored to the needs of the site. The collation for the session is determined at logon from the defined default collation for the user. You can change your collation any number of times during the session using the SET SESSION COLLATION statement, but you cannot change your default logon in this way. Your default collation is assigned via the COLLATION option of the CREATE USER or MODIFY USER statement. This has no effect on any current session, only new logons. To set this level of flagging … Set the flag variable to this value … None SQLFLAG NONE Entry level SQLFLAG ENTRY Intermediate level SQLFLAG INTERMEDIATE To run transactions in this mode … Set the variable to this value … Teradata TRANSACTION BTET ANSI TRANSACTION ANSIChapter 4: SQL Data Handling Session Parameters SQL Reference: Fundamentals 141 Each named collation can be CASESPECIFIC or NOT CASESPECIFIC. NOT CASESPECIFIC collates lowercase data as if it were converted to uppercase before the named collation is applied. For details, see “SET SESSION COLLATION” in SQL Reference: Data Definition Statements. Account and Priority You can dynamically downgrade or upgrade the performance group priority for your account. Collation Name Description ASCII Character data is collated in the order it would appear if converted for an ASCII session, and a binary sort performed. EBCDIC Character data is collated in the order it would appear if converted for an EBCDIC session, and a binary sort performed. MULTINATIONAL The default MULTINATIONAL collation is a two-level collation based on the Unicode collation standard. Your system administrator can redefine this collation to any two-level collation of characters in the LATIN repertoire. For backward compatibility, the following are true: • MULTINATIONAL collation of KANJI1 data is single level. • The system administrator can redefine single byte character collation. This definition is not compatible with MULTINATIONAL collation of nonKANJI1 data. CHARSET_COLL collation is usually a better solution for KANJI1 data. See “ORDER BY Clause” in SQL Reference: Data Manipulation Statements. For information on setting up the MULTINATIONAL collation sequence, see “Collation Sequences” in International Character Set Support. HOST The default. HOST collation defaults are as follows: • EBCDIC collation for channel-connected systems. • ASCII collation for all others. CHARSET_COLL Character data is collated in the order it would appear if converted to the current client character set and then sorted in binary order. CHARSET_COLL collation is a system administrator-defined collation. JIS_COLL Character data is collated based on the Japanese Industrial Standards (JIS). JIS characters collate in the following order: 1 JIS X 0201-defined characters in standard order 2 JIS X 0208-defined characters in standard order 3 JIS X 0212-defined characters in standard order 4 KanjiEBCDIC-defined characters not defined in JIS X 0201, JIS X 0208, or JIS X 0212 in standard order 5 All remaining characters in Unicode standard orderChapter 4: SQL Data Handling Session Parameters 142 SQL Reference: Fundamentals Priorities can be downgraded or upgraded at either the session or the request level. For more information, see “SET SESSION ACCOUNT” in SQL Reference: Data Definition Statements. Note that changing the performance group for your account changes the account name for accounting purposes because a performance group is part of an account name. Date Form You can change the format in which DATE data is imported or exported in your current session. DATE data can be set to be treated either using the ANSI date format (DATEFORM=ANSIDATE) or using the Teradata date format (DATEFORM=INTEGERDATE). For details, see “SET SESSION DATEFORM” in SQL Reference: Data Definition Statements. Character Set To set the client character set, use one of the following: • From BTEQ, use the BTEQ [.] SET SESSION CHARSET ‘name’ command. • In a CLIv2 application, call CHARSET name. • In the URL for selecting a Teradata JDBC driver connection to a Teradata Database, use the CHARSET=name database connection parameter. where the ‘name’ or name value is ASCII, EBCDIC, UTF8, UTF16, or a name assigned to the translation codes that define an available character set. If not explicitly requested, the session default is the character set associated with the logon client. This is either the standard client default, or the character set assigned to the client by the database administrator. Express Logon Express Logon improves the logon response time for network-attached, NCR UNIX MP-RAS clients and is especially useful in the OLTP environment where sessions are short-lived. Express Logon allows the gateway to choose the fast path when logging users onto the Teradata Database. Enable or disable this mode from the Gateway Global Utility, from the XGTWGLOBAL interface: In this mode … Use this command to enable or disable Express Logon … Terminal ENABLE EXLOGON DISABLE EXLOGON Window EXLOGON button (via the LOGON dialog box)Chapter 4: SQL Data Handling Session Management SQL Reference: Fundamentals 143 The feature can be enabled or disabled for a particular host group, or for all host groups. For details on this feature, see the Utilities book. For channel-attached clients, see “Session Pools” on page 143. HELP SESSION The HELP SESSION statement identifies the transaction mode, character set, collation sequence, and date form in effect for the current session. See “HELP SESSION” in SQL Reference: Data Definition Statements for details. Session Management Introduction Each session is logged on and off via calls to CLIv2 routines or through ODBC or JDBC, which offer a one-step logon-connect function. Sessions are internally managed by dividing the session control functions into a series of single small steps that are executed in sequence to implement multi-threaded tasking. This provides concurrent processing of multiple logon and logoff events, which can be any combination of individual users, and one or more concurrent sessions established by one or more users and applications. Once connected and active, a session can be viewed as a work stream consisting of a series of requests between the client and server. Session Pools For channel-connected applications, you can establish session pools, which are collections of sessions that are logged on to the Teradata Database in advance (generally at the time of TDP initialization) for use by applications that require a ‘fast path’ logon. This capability is particularly advantageous for transaction processing in which interaction with the Teradata Database consists of many single, short transactions. TDP identifies each session with a unique session number. Teradata Database identifies a session with a session number, the username of the initiating user, and the logical host identification number of the connection (LAN or mainframe channel) associated with the controlling TDP or mTDP. For network-connected, UNIX MP-RAS applications that require fast path logons, use the Express Logon feature. For details, see “Express Logon” on page 142. Session Reserve Use the ENABLE SESSION RESERVE command from an OS/390 or VM client to reserve session capacity in the event of a PE failure. To release reserved session capacity, use the DISABLE SESSION RESERVE command. Chapter 4: SQL Data Handling Return Codes 144 SQL Reference: Fundamentals See Teradata Tools and Utilities Installation Guide for IBM OS/390 and z/OS and Teradata Tools and Utilities Installation Guide for IBM VM for further information. Session Control The major functions of session control are session logon and logoff. Upon receiving a session request, the logon function verifies authorization and returns a yes or no response to the client. The logoff function terminates any ongoing activity and deletes the session context. Requests and Responses Requests are sent to a server to initiate an action. Responses are sent by a server to reflect the results of that action. Both requests and responses are associated with an established session. A request consists of the following components: • One or more Teradata SQL statements • Control information • Optional USING data If any operation specified by an initiating request fails, the request is backed out, along with any change that was made to the database. In this case, a failure response is returned to the application. Return Codes Introduction SQL return codes provide information about the status of a completed executable SQL DML statement. Status Variables for Receiving SQL Return Codes ANSI SQL defines two status variables for receiving return codes: • SQLSTATE • SQLCODE SQLCODE is not ANSI SQL-compliant. The ANSI SQL-92 standard explicitly deprecates SQLCODE, and the ANSI SQL-99 standard does not define SQLCODE. The ANSI SQL committee recommends that new applications use SQLSTATE in place of SQLCODE. Teradata Database defines a third status variable for receiving the number of rows affected by an SQL statement in a stored procedure: • ACTIVITY_COUNT Teradata SQL defines a non-ANSI SQL Communications Area (SQLCA) that also has a field named SQLCODE for receiving return codes. Chapter 4: SQL Data Handling Return Codes SQL Reference: Fundamentals 145 Exception and Completion Conditions ANSI SQL defines two categories of conditions that issue return codes: • Exception conditions • Completion conditions Exception Conditions An exception condition indicates a statement failure. A statement that raises an exception condition does nothing more than return that exception condition to the application. There are as many exception condition return codes as there are specific exception conditions. For more information about exception conditions, see “Failure Response” on page 150 and “Error Response (ANSI Session Mode Only)” on page 149. For a complete list of exception condition codes, see the Messages book. Completion Conditions A completion condition indicates statement success. There are three categories of completion conditions: • Successful completion • Warnings • No data found For more information, see: • “Statement Responses” on page 147 • “Success Response” on page 148 • “Warning Response” on page 149 A statement that raises a completion condition can take further action such as querying the database and returning results to the requesting application, updating the database, initiating an SQL transaction, and so on. For information on … See … • SQLSTATE • SQLCODE • ACTIVITY_COUNT “Result Code Variables” in SQL Reference: Stored Procedures and Embedded SQL SQLCA “SQL Communications Area (SQLCA)” in SQL Reference: Stored Procedures and Embedded SQLChapter 4: SQL Data Handling Return Codes 146 SQL Reference: Fundamentals Return Codes for Stored Procedures The return code values are different in the case of SQL control statements in stored procedures. The return codes for stored procedures appear in the following table. How an Application Uses SQL Return Codes An application program or stored procedure tests the status of a completed executable SQL statement to determine its status. FOR this type of completion condition … The value for this return code is … SQLSTATE SQLCODE Success '00000' 0 Warning '01901' 901 '01800' to '01841' 901 '01004' 902 No data found '02000' 100 FOR this type of condition … The value for this return code is … SQLSTATE SQLCODE Successful completion '00000' 0 Warning SQLSTATE value corresponding to the warning code. the Teradata Database warning code. No data found or any other Exception SQLSTATE value corresponding to the error code. the Teradata Database error code. IF the statement raises this type of condition … THEN the application or condition handler takes the following remedial action … Successful completion none. Warning the statement execution continues. If a warning condition handler is defined in the application, the handler executes.Chapter 4: SQL Data Handling Statement Responses SQL Reference: Fundamentals 147 Statement Responses Response Types The Teradata Database responds to an SQL request with one of the following condition responses: • Success response, with optional warning • Failure response • Error response (ANSI session mode only) Depending on the type of statement, the Teradata Database also responds with one or more rows of data. Multistatement Responses A response to a request that contains more than one statement, such as a macro, is not returned to the client until all statements in the request are successfully executed. How a Response Is Returned to the User The manner in which the response is returned depends on the interface that is being used. For example, if an application is using a language preprocessor, then the activity count, warning code, error code, and fields from a selected row are returned directly to the program through its appropriately declared variables. If the application is a stored procedure, then the activity count is returned directly in the ACTIVITY_COUNT status variable. If you are using BTEQ, then a success, error, or failure response is displayed automatically. Response Condition Codes SQL statements also return condition codes that are useful for handling errors and warnings in embedded SQL and stored procedure applications. No data found or any other exception whatever appropriate action is required by the exception. If an EXIT handler has been defined for the exception, the statement execution terminates. If a CONTINUE handler has been defined, execution continues after the remedial action. IF the statement raises this type of condition … THEN the application or condition handler takes the following remedial action …Chapter 4: SQL Data Handling Success Response 148 SQL Reference: Fundamentals For information about SQL response condition codes, see the following in SQL Reference: Stored Procedures and Embedded SQL: • SQLSTATE • SQLCODE • ACTIVITY_COUNT Success Response Definition A success response contains an activity count that indicates the total number of rows involved in the result. For example, the activity count for a SELECT statement is the total number of rows selected for the response. For a SELECT, COMMENT, or ECHO statement, the activity count is followed by the data that completes the response. An activity count is meaningful for statements that return a result set, for example: • SELECT • INSERT • UPDATE • DELETE • HELP • SHOW • EXPLAIN • CREATE PROCEDURE • REPLACE PROCEDURE For other SQL statements, activity count is meaningless. Example The following interactive SELECT statement returns the successful response message. SELECT AVG(f1) FROM Inventory; *** Query completed. One row found. One column returned. *** Total elapsed time was 1 second. Average(f1) ----------- 14Chapter 4: SQL Data Handling Warning Response SQL Reference: Fundamentals 149 Warning Response Definition A success or OK response with a warning indicates either that an anomaly has occurred or informs the user about the anomaly and indicates how it can be important to the interpretation of the results returned. Example Assume the current session is running in ANSI session mode. If nulls are included in the data for column f1, then the following interactive query returns the successful response message with a warning about the nulls. SELECT AVG(f1) FROM Inventory; *** Query completed. One row found. One column returned. *** Warning: 2892 Null value eliminated in set function. *** Total elapsed time was 1 second. Average(f1) ----------- 14 This warning response is not generated if the session is running in Teradata session mode. Error Response (ANSI Session Mode Only) Definition An error response occurs when a query anomaly is severe enough to prevent the correct processing of the request. In ANSI session mode, an error for a request causes the request to rollback, and not the entire transaction. Example 1 The following command returns the error message immediately following. .SET SESSION TRANS ANSI; *** Error: You must not be logged on .logoff to change the SQLFLAG or TRANSACTION settings. Example 2 Assume that the session is running in ANSI session mode, and the following table is defined: CREATE MULTISET TABLE inv, FALLBACK, NO BEFORE JOURNAL, NO AFTER JOURNALChapter 4: SQL Data Handling Failure Response 150 SQL Reference: Fundamentals ( item INTEGER CHECK ((item >=10) AND (item <= 20) )) PRIMARY INDEX (item); You insert a value of 12 into the item column of the inv table. This is valid because the defined integer check specifies that any integer between 10 and 20 (inclusive) is valid. INSERT INTO inv (12); The following results message returns. *** Insert completed. One row added.... You insert a value of 9 into the item column of the inv table. This is not valid because the defined integer check specifies that any integer with a value less than 10 is not valid. INSERT INTO inv (9); The following error response returns: ***Error 5317 Check constraint violation: Check error in field inv.item. You commit the current transaction: COMMIT; The following results message returns: *** COMMIT done. ... You select all rows from the inv table: SELECT * FROM inv; The following results message returns: *** Query completed. One row found. One column returned. item ------- 12 Failure Response Definition A failure response is a severe error. The response includes a statement number, an error code, and an associated text string describing the cause of the failure. Teradata Session Mode In Teradata session mode, a failure causes the system to roll back the entire transaction. If one statement in a macro fails, a single failure response is returned to the client, and the results of any previous statements in the transaction are backed out.Chapter 4: SQL Data Handling Failure Response SQL Reference: Fundamentals 151 ANSI Session Mode In ANSI session mode, a failure causes the system to roll back the entire transaction, for example, when the current request: • Results in a deadlock • Performs a DDL statement that aborts • Executes an explicit ROLLBACK or ABORT statement Example 1 The following SELECT statement SELECT * FROM Inventory:; in BTEQ, returns the failure response message: *** Failure 3709 Syntax error, replace the ':' that follows the name with a ';'. Statement# 1, Info =20 *** Total elapsed time was 1 second. Example 2 Assume that the session is running in ANSI session mode, and the following table is defined: CREATE MULTISET TABLE inv, FALLBACK, NO BEFORE JOURNAL, NO AFTER JOURNAL ( item INTEGER CHECK ((item >=10) AND (item <= 20) )) PRIMARY INDEX (item); You insert a value of 12 into the item column of the inv table. This is valid because the defined integer check specifies that any integer between 10 and 20 (inclusive) is valid. INSERT INTO inv (12); The following results message returns. *** Insert completed. One row added.... You commit the current transaction: COMMIT; The following results message returns: *** COMMIT done. ... You insert a valid value of 15 info the item column of the inv table: INSERT INTO inv (15); The following results message returns. *** Insert completed. One row added....Chapter 4: SQL Data Handling Failure Response 152 SQL Reference: Fundamentals You can use the ABORT statement to cause the system to roll back the transaction: ABORT; The following failure message returns: *** Failure 3514 User-generated transaction ABORT. Statement# 1, Info =0 You select all rows from the inv table: SELECT * FROM inv; The following results message returns: *** Query completed. One row found. One column returned. item ------- 12SQL Reference: Fundamentals 153 CHAPTER 5 Query Processing This chapter discusses query processing, including single AMP requests and all AMP requests, and table access methods available to the Optimizer. Topics include: • Query processing • Table access methods • Full-table scans • Collecting statistics Query Processing Introduction An SQL query (the definition for “query” here includes DELETE, INSERT, MERGE, and UPDATE as well as SELECT) can affect one AMP, several AMPs, or all AMPs in the configuration. IF a query … THEN … involving a single table uses a unique primary index (UPI) the row hash can be used to identify a single AMP. At most one row can be returned. involving a single table uses a nonunique primary index (NUPI) the row hash can be used to identify a single AMP. Any number of rows can be returned. uses a unique secondary index (USI) one or two AMPs are affected (one AMP if the subtable and base table are on the same AMP). At most one row can be returned. uses a nonunique secondary index (NUSI) if the table has a partitioned primary index (PPI) and the NUSI is the same column set as a NUPI, the query affects one AMP. Otherwise, all AMPs take part in the operation and any number of rows can be returned.Chapter 5: Query Processing Query Processing 154 SQL Reference: Fundamentals The SELECT statements in subsequent examples reference the following table data. Single AMP Request Assume that a PE receives the following SELECT statement: SELECT last_name FROM Employee WHERE employee_number = 1008; Because a unique primary index value is used as the search condition (the column employee_number is the primary index for the Employee table), PE1 generates a single AMP step requesting the row for employee 1008. The AMP step, along with the PE identification, is put into a message, and sent via the BYNET to the relevant AMP (processor). This process is illustrated by the graphic under “Flow Diagram of a Single AMP Request” on page 155. Only one BYNET is shown to simplify the illustration. Abbreviation Meaning PK Primary Key FK Foreign Key UPI Unique Primary Index Employee Employee Number Manager Employee Number Dept. Number Job Code Last Name First Name Hire Date Birth Date Salary Amount PK/UPI FK FK FK 1006 1019 301 312101 Stein John 76105 531015 2945000 1008 1019 301 312102 Kanieski Carol 770201 580517 2925000 1005 0801 403 431100 Ryan Loretta 761015 550910 3120000 1004 1003 401 412101 Johnson Darlene 761015 460423 3630000 1007 1005 403 432101 Villegas Arnando 770102 370131 4970000 1003 0801 401 411100 Trader James 760731 470619 3755000 1016 0801 302 321100 Rogers Nora 780310 590904 5650000 1012 1005 403 432101 Hopkins Paulene 770315 420218 3790000 1019 0801 301 311100 Kubic Ron 780801 421211 5770000 1023 1017 501 512101 Rabbit Peter 790301 621029 2650000 1083 0801 619 414221 Kimble George 910312 410330 3620000 1017 0801 501 511100 Runyon Irene 780501 511110 6600000 1001 1003 401 412101 Hoover William 760818 500114 2552500Chapter 5: Query Processing Query Processing SQL Reference: Fundamentals 155 Flow Diagram of a Single AMP Request Assuming that AMP2 has the row, it accepts the message. As illustrated by the graphic under “Single AMP Response to Requesting PE” on page 156, AMP2 retrieves the row from its DSU (disk storage unit), includes the row and the PE identification in a return message, and sends the message back to PE1 via the BYNET. PE1 accepts the message and returns the response row to the requesting application. For an illustration of a single AMP request with partition elimination, see “Single AMP Request With Partition Elimination” on page 160. BYNET PE1 PE2 AMP1 AMP2 AMP3 AMP4 1006 STEIN 1008 KANIESKI 1023 RABBIT 1004 JOHNSON 1101C002 AMP STEP DSU DSU DSUChapter 5: Query Processing Query Processing 156 SQL Reference: Fundamentals Single AMP Response to Requesting PE All AMP Request Assume PE1 receives a SELECT statement that specifies a range of primary index values as a search condition as shown in the following example: SELECT last_name, employee_number FROM employee WHERE employee_number BETWEEEN 1001 AND 1010 ORDER BY last_name; In this case, each value hashes differently, and all AMPs must search for the qualifying rows. PE1 first parses the request and creates the following AMP steps: • Retrieve rows between 1001 and 1010 • Sort ascending on last_name • Merge the sorted rows to form the answer set PE1 then builds a message for each AMP step and puts that message onto the BYNET. Typically, each AMP step is completed before the next one begins; note, however, that some queries can generate parallel steps. When PE1 puts the message for the first AMP step on the BYNET, that message is broadcast to all processors as illustrated by “Figure 1: Flow Diagram for an All AMP Request” on page 157. BYNET ROW 1008 PE1 PE2 AMP1 AMP2 AMP3 AMP4 1006 Stein 1008 Kanieski 1023 Rabbit 1004 Johnson 1101C003Chapter 5: Query Processing Query Processing SQL Reference: Fundamentals 157 Figure 1: Flow Diagram for an All AMP Request The process is as follows: 1 All AMPs accept the message, but the PEs do not. 2 Each AMP checks for qualifying rows on its disk storage units. 3 If any qualifying rows are found, the data in the requested columns is converted to the client format and copied to a spool file. 4 Each AMP completes the step, whether rows were found or not, and puts a completion message on the BYNET. The completion messages flow across the BYNET to PE1. 5 When all AMPs have returned a completion message, PE1 transmits a message containing AMP Step 2 to the BYNET. Upon receipt of Step 2, the AMPs sort their individual answer sets into ascending sequence by last_name (see “Figure 2: Flow Diagram for an AMP Sort” on page 158). Note: If partitioned on employee_number, the scan may be limited to a few partitions based on partition elimination. PE1 PE2 AMP1 AMP2 AMP3 AMP4 1006 STEIN 1008 KANIESKI 1004 JOHNSON 1007 VILLEGAS 1003 TRADER 1001 HOOVER 1005 RYAN BYNET FF02A004 DATA SPOOLChapter 5: Query Processing Query Processing 158 SQL Reference: Fundamentals Figure 2: Flow Diagram for an AMP Sort 6 Each AMP sorts its answer set, then puts a completion message on the BYNET. 7 When PE1 has received all completion messages for Step 2, it sends a message containing AMP Step 3. 8 Upon receipt of Step 3, each AMP copies the first block from its sorted spool to the BYNET. Because there can be multiple AMPs on a single node, each node might be required to handle sort spools from multiple AMPs (see “Figure 3: Flow Diagram for a BYNET Merge” on page 159). PE1 PE2 AMP1 AMP2 AMP3 AMP4 1006 STEIN 1008 KANIESKI 1004 JOHNSON 1007 VILLEGAS 1003 TRADER 1001 HOOVER 1005 RYAN BYNET FF02A005 1004 JOHNSON 1008 KANIESKI 1006 STEIN 1003 TRADER 1007 VILLEGAS 1001 HOOVER 1005 RYAN DATA SPOOL SORT SPOOLChapter 5: Query Processing Query Processing SQL Reference: Fundamentals 159 Figure 3: Flow Diagram for a BYNET Merge 9 Nodes that contain multiple AMPs must first perform an intermediate sort of the spools generated by each of the local AMPs. When the local sort is complete on each node, the lowest sorting row from each node is sent over the BYNET to PE1. From this point on, PE1 acts as the Merge coordinator among all the participating nodes. 10 The Merge continues with PE1 building a globally sorted buffer. When this buffer fills, PE1 forwards it to the application and begins building subsequent buffers. 11 When a participant node has exhausted its sort spool, it sends a Done message to PE1. This causes PE1 to prune this node from the set of Merge participants. When there are no remaining Merge participants, PE1 sends the final buffer to the application along with an End Of File message. Partition Elimination A PPI can increase query efficiency via partition elimination. The degree of partition elimination depends on the: • Partition expression for the primary index of the table • Conditions in the query • Capability of the Optimizer to detect partition elimination It is not always required that all values of the partitioning columns be specified in a query to have partition elimination occur. HD03A005 Global Sort Buffer Local Sort Tree Sort Spools Node 1 Local Sort Tree Sort Spools Node 3 Sort Spools Node 2 Local Sort Tree Local Sort Tree Sort Spools Node 4 PE1 PE2 AMP AMP AMP AMP PE5 PE6 AMP AMP AMP AMP PE7 PE8 AMP AMP AMP AMP PE3 PE4 AMP AMP AMP AMP BYNETChapter 5: Query Processing Query Processing 160 SQL Reference: Fundamentals Single AMP Request With Partition Elimination If a SELECT specifies values for all the primary index columns, the AMP where the rows reside can be determined and only a single AMP is accessed. If conditions are also specified on the partitioning columns, partition elimination may reduce the number of partitions to be probed on that AMP. IF a SELECT … THEN … specifies values for all the primary index columns the AMP where the rows reside can be determined and only a single AMP is accessed. IF conditions are … THEN … not specified on the partitioning columns each partition can be probed to find the rows based on the hash value. also specified on the partitioning columns partition elimination may reduce the number of partitions to be probed on that AMP. For an illustration, see “Single AMP Request With Partition Elimination” on page 160. does not specify the values for all the primary index columns an all-AMP full file scan is required for a table with an NPPI. However, with a PPI, if conditions are specified on the partitioning columns, partition elimination may reduce an all-AMP full file scan to an all-AMP scan of only the non-eliminated partitions.Chapter 5: Query Processing Table Access SQL Reference: Fundamentals 161 The following diagram illustrates this process. The AMP Step includes the list of partitions (P) to access. Partition elimination reduces access to the partitions that satisfy the query requirements. In each partition, look for rows with a given row hash value (RH) of the PI. Table Access Teradata Database uses indexes and partitions to access the rows of a table. If indexed or partitioned access is not suitable for a query, the result is a full-table scan. Access Methods The following table access methods are available to the Optimizer: BYNET PE1 PE2 AMP1 AMP2 AMP3 AMP4 Table P P 1101A094 AMP STEP DSU DSU DSU RH RH RH RH • Unique Primary Index • Unique Partitioned Primary Index • Nonunique Primary Index • Nonunique Partitioned Primary Index • Unique Secondary Index • Nonunique Secondary Index • Join Index • Hash Index • Full-Table Scan • Partition ScanChapter 5: Query Processing Table Access 162 SQL Reference: Fundamentals Effects of Conditions in WHERE Clause Whether the system can use row hashing, or do a table scan with partition elimination, or whether it must do a full-table scan depends on the predicates or conditions that appear in the WHERE clause associated with an UPDATE, DELETE, or SELECT statement. The following functions are applied to rows identified by the WHERE clause, and have no effect on the selection of rows from the base table: Statements that specify any of the following WHERE clause conditions result in full-table scans (FTS). If the table has a PPI, partition elimination might reduce the FTS access to only the affected partitions. The type of table access that the system uses when statements specify any of the following WHERE clause conditions depends on whether the column or columns are indexed, the type of index, and its selectivity: • GROUP BY • HAVING • INTERSECT • MINUS/EXCEPT • ORDER BY • QUALIFY • SAMPLE • UNION • WITH ... BY • WITH • nonequality comparisons • column_name IS NOT NULL • column_name NOT IN (explicit list of values) • column_name NOT IN (subquery) • column_name BETWEEN ... AND ... • condition_1 OR condition_2 • NOT condition_1 • column_name LIKE • column_1 || column_2 = value • table1.column_x = table1.column_y • table1.column_x [computation] = value • table1.column_x [computation] - table1.column_y • INDEX (column_name) • SUBSTR (column_name) • SUM • MIN • MAX • AVG • DISTINCT • COUNT • ANY • ALL • missing WHERE clause • column_name = value or constant expression • column_name IS NULL • column_name IN (explicit list of values) • column_name IN (subquery) • condition_1 AND condition_2 • different data types • table1.column_x = table2.column_xChapter 5: Query Processing Full-Table Scans SQL Reference: Fundamentals 163 In summary, a query influences processing choices as follows: • A full-table scan (possibly with partition elimination if the table has a PPI) is required if the query includes an implicit range of values, such as in the following WHERE examples. Note that when a small BETWEEN range is specified, the optimizer can use row hashing rather than a full-table scan. ... WHERE column_name [BETWEEN <, >, <>, <=, >=] ... WHERE column_name [NOT] IN (SELECT...) ... WHERE column_name NOT IN (val1, val2 [,val3]) • Row hashing can be used if the query includes an explicit value, as shown in the following WHERE examples: ... WHERE column_name = val ... WHERE column_name IN (val1, val2, [,val3]) Related Topics Full-Table Scans Introduction A full-table scan is a retrieval mechanism that touches all rows in a table. If you do not specify a WHERE clause in your query, then the Teradata Database always uses a full-table scan to access the data. Even when results are qualified using a WHERE clause, indexed or partitioned access may not be suitable for a query, and a full-table scan may result. A full-table scan is always an all-AMP operation, and should be avoided when possible. Fulltable scans may generate spool files that can have as many rows as the base table. Full-table scans are not something to fear, however. The architecture that the Teradata Database uses makes a full-table scan an efficient procedure, and optimization is scalable based on the number of AMPs defined for the system. The sorts of unplanned, ad hoc queries that characterize the data warehouse process, and that often are not supported by indexes, perform very effectively for Teradata Database using full-table scans. FOR more information on … SEE … the efficiency, number of AMPs used, and the number of rows accessed by all table access methods Database Design strengths and weaknesses of table access methods Introduction to Teradata Warehouse full-table scans “Full-Table Scans” on page 163 index access “Indexes” on page 17Chapter 5: Query Processing Collecting Statistics 164 SQL Reference: Fundamentals How a Full-Table Scan Accesses Rows Because full-table scans necessarily touch every row on every AMP, they do not use the following mechanisms for locating rows. • Hashing algorithm and hash map • Primary indexes • Secondary indexes or their subtables • Partitioning Instead, a full-table scan uses the file system tables known as the Master Index and Cylinder Index to locate each data block. Each row within a data block is located by a forward scan. Because rows from different tables are never mixed within the same data block and because rows never span blocks, an AMP can scan up to 128K bytes of the table on each block read, making a full-table scan a very efficient operation. Data block read-ahead and cylinder reads can also increase efficiency. Related Topics Collecting Statistics The COLLECT STATISTICS (Optimizer form) statement collects demographic data for one or more columns of a base table, hash index, or join index, computes a statistical profile of the collected data, and stores the synopsis in the data dictionary. The Optimizer uses the synopsis data when it generates its table access and join plans. Usage You should collect statistics on newly created, empty data tables. An empty collection defines the columns, indexes, and synoptic data structure for loaded collections. You can easily collect statistics again after the table is populated for prototyping, and again when it is in production. FOR more information on … SEE … full-table scans Database Design cylinder reads Database Administration data-block read ahead • Performance Management • DBS Control Utility in UtilitiesChapter 5: Query Processing Collecting Statistics SQL Reference: Fundamentals 165 You can collect statistics on a: • Unique index, which can be: • Primary or secondary • Single or multiple column • Partitioned or non-partitioned • Non-unique index, which can be: • Primary or secondary • Single or multiple column • Partitioned or non-partitioned • With or without COMPRESS fields • Non-indexed column or set of columns, which can be: • Partitioned or non-partitioned • With or without COMPRESS fields • Join index • Hash index • Temporary table • If you specify the TEMPORARY keyword but a materialized table does not exist, the system first materializes an instance based on the column names and indexes you specify. This means that after a true instance is created, you can update (re-collect) statistics on the columns by entering COLLECT STATISTICS and the TEMPORARY keyword without having to specify the desired columns and index. • If you omit the TEMPORARY keyword but the table is a temporary table, statistics are collected for an empty base table rather than the materialized instance. • Sample (system-selected percentage) of the rows of a data table or index, to detect data skew and dynamically increase the sample size when found. • The SAMPLE option is not supported for global temporary tables, join indexes, or hash indexes. • The system does not store both sampled and defined statistics for the same index or column set. Once sampled statistics have been collected, implicit re-collection hits the same columns and indexes, and operates in the same mode. To change this, specify any keywords or options and name the columns and/or indexes.Chapter 5: Query Processing Collecting Statistics 166 SQL Reference: Fundamentals Related Topics FOR more information on … SEE … using the COLLECT STATISTICS statement SQL Reference: Data Definition Statements collecting statistics on a join index Database Design collecting statistics on a hash index when to collect statistics on base table columns instead of hash index columns database administration and collecting statistics Database AdministrationSQL Reference: Fundamentals 167 APPENDIX A Notation Conventions This appendix describes the notation conventions used in this book. Throughout this book, three conventions are used to describe the SQL syntax and code: • Syntax diagrams, used to describe SQL syntax form, including options. See “Syntax Diagram Conventions” on page 167. • Square braces in the text, used to represent options. The indicated parentheses are required when you specify options. For example: • DECIMAL [(n[,m])] means the decimal data type can be defined optionally: • without specifying the precision value n or scale value mspecifying precision (n) only • specifying both values (n,m) • you cannot specify scale without first defining precision. • CHARACTER [(n)] means that use of (n) is optional. The values for n and m are integers in all cases • Japanese character code shorthand notation, used to represent unprintable Japanese characters. See “Character Shorthand Notation Used In This Book” on page 171. Symbols from the predicate calculus are also used occasionally to describe logical operations. See “Predicate Calculus Notation Used in This Book” on page 172. Syntax Diagram Conventions Notation Conventions The following table defines the notation used in this section: Item Definition / Comments Letter An uppercase or lowercase alphabetic character ranging from A through Z. Number A digit ranging from 0 through 9. Do not use commas when entering a number with more than three digits.Appendix A: Notation Conventions Syntax Diagram Conventions 168 SQL Reference: Fundamentals Paths The main path along the syntax diagram begins at the left, and proceeds, left to right, to the vertical bar, which marks the end of the diagram. Paths that do not have an arrow or a vertical bar only show portions of the syntax. The only part of a path that reads from right to left is a loop. Paths that are too long for one line use continuation links. Continuation links are small circles with letters indicating the beginning and end of a link: When you see a circled letter in a syntax diagram, go to the corresponding circled letter and continue. Word Variables and reserved words. IF a word is shown in … THEN it represents … UPPERCASE LETTERS a keyword. Syntax diagrams show all keywords in uppercase, unless operating system restrictions require them to be in lowercase. If a keyword is shown in uppercase, you may enter it in uppercase or mixed case. lowercase letters a keyword that you must enter in lowercase, such as a UNIX command. lowercase italic letters a variable such as a column or table name. You must substitute a proper value. lowercase bold letters a variable that is defined immediately following the diagram that contains it. UNDERLINED LETTERS the default value. This applies both to uppercase and to lowercase words. Spaces Use one space between items, such as keywords or variables. Punctuation Enter all punctuation exactly as it appears in the diagram. Item Definition / Comments FE0CA002 A AAppendix A: Notation Conventions Syntax Diagram Conventions SQL Reference: Fundamentals 169 Required Items Required items appear on the main path: If you can choose from more than one item, the choices appear vertically, in a stack. The first item appears on the main path: Optional Items Optional items appear below the main path: If choosing one of the items is optional, all the choices appear below the main path: You can choose one of the options, or you can disregard all of the options. Abbreviations If a keyword or a reserved word has a valid abbreviation, the unabbreviated form always appears on the main path. The shortest valid abbreviation appears beneath. In the above syntax, the following formats are valid: • SHOW CONTROLS • SHOW CONTROL Loops A loop is an entry or a group of entries that you can repeat one or more times. Syntax diagrams show loops as a return path above the main path, over the item or items that you can repeat. FE0CA003 SHOW FE0CA005 SHOW VERSIONS CONTROLS FE0CA004 SHOW CONTROLS FE0CA006 SHOW CONTROLS VERSIONS FE0CA042 SHOW CONTROL CONTROLSAppendix A: Notation Conventions Syntax Diagram Conventions 170 SQL Reference: Fundamentals The following rules apply to loops: Excerpts Sometimes a piece of a syntax phrase is too large to fit into the diagram. Such a phrase is indicated by a break in the path, marked by | terminators on either side of the break. A name for the excerpted piece appears between the break marks in boldface type. The named phrase appears immediately after the complete diagram, as illustrated by the following example. IF … THEN … there is a maximum number of entries allowed the number appears in a circle on the return path. In the example, you may enter cname a maximum of 4 times. there is a minimum number of entries required the number appears in a square on the return path. In the example, you must enter at least 3 groups of column names. a separator character is required between entries the character appears on the return path. If the diagram does not show a separator character, use one blank space. In the example, the separator character is a comma. a delimiter character is required around entries the beginning and end characters appear outside the return path. Generally, a space is not needed between delimiter characters and entries. In the example, the delimiter characters are the left and right parentheses. JC01B012 ( , 4 cname ) , 3 LOCKING excerpt where_cond A cname excerpt JC01A014 A HAVING con , col_pos ,Appendix A: Notation Conventions Character Shorthand Notation Used In This Book SQL Reference: Fundamentals 171 Character Shorthand Notation Used In This Book Introduction This book uses the UNICODE naming convention for characters. For example, the lowercase character ‘a’ is more formally specified as either LATIN SMALL LETTER A or U+0041. The U+xxxx notation refers to a particular code point in the Unicode standard, where xxxx stands for the hexadecimal representation of the 16-bit value defined in the standard. In parts of the book, it is convenient to use a symbol to represent a special character, or a particular class of characters. This is particularly true in discussion of the following Japanese character encodings. • KanjiEBCDIC • KanjiEUC • KanjiShift-JIS These encodings are further defined in the International Character Set Support book. Symbols The symbols, and the character sets with which they are used, are defined in the following table. Symbol Encoding Meaning a..z A..Z 0..9 Any Any single byte Latin letter or digit. a..z A..Z 0..9 Unicode compatibility zone Any fullwidth Latin letter or digit. < KanjiEBCDIC Shift Out [SO] (0x0E). Indicates transition from single to multibyte character in KanjiEBCDIC. > KanjiEBCDIC Shift In [SI] (0x0F). Indicates transition from multibyte to single byte KanjiEBCDIC. T Any Any multibyte character. Its encoding depends on the current character set. For KanjiEUC, “ss 3 ” sometimes precedes code set 3 characters. I Any Any single byte Hankaku Katakana character. In KanjiEUC, it must be preceded by “ss 2 ”, forming an individual multibyte character. ? Any Represents the graphic pad character.Appendix A: Notation Conventions Predicate Calculus Notation Used in This Book 172 SQL Reference: Fundamentals For example, string “TEST”, where each letter is intended to be a fullwidth character, is written as TEST. Occasionally, when encoding is important, hexadecimal representation is used. For example, the following mixed single byte/multibyte character data in KanjiEBCDIC character set LMNQRS is represented as: D3 D4 D5 0E 42E3 42C5 42E2 42E3 0F D8 D9 E2 Pad Characters The following table lists the pad characters for the various server character sets. Predicate Calculus Notation Used in This Book Relational databases are based on the theory of relations as developed in set theory. Predicate calculus is often the most unambiguous way to express certain relational concepts. Occasionally this book uses the following predicate calculus notation to explain concepts. ? Any Represents either a single or multibyte pad character, depending on context. ss 2 KanjiEUC Represents the EUC code set 2 introducer (0x8E). ss 3 KanjiEUC Represents the EUC code set 3 introducer (0x8F). Symbol Encoding Meaning Server Character Set Pad Character Name Pad Character Value LATIN SPACE 0x20 UNICODE SPACE U+0020 GRAPHIC IDEOGRAPHIC SPACE U+3000 KANJISJIS SPACE 0x20 KANJI1 SPACE 0x20 This symbol … Represents this phrase … iff If and only if ? For all ? There existsSQL Reference: Fundamentals 173 APPENDIX B Restricted Words for V2R6.2 This appendix details restrictions for Release V2R6.2 on the use of certain terminology in SQL queries and in other user application programs that interface with the Teradata Database. The following sections are described: • A current listing of Teradata reserved keywords, non-reserved keywords, those words reserved for future use, and ANSI SQL-2003 reserved and non-reserved keywords. • Statements about the varying usage restrictions of each type of word. Reserved Words and Keywords for V2R6.2 The following list contains all classes of restricted words for Teradata Database Release V2R6.2, and uses these conventions: • Abbreviations and the full words they represent appear separately, except in cases where the abbreviation is the only common usage, such as ASCII. • The following definitions apply to the Teradata Database Status column: Type Explanation Reserved Teradata Database reserved word that cannot be used as an identifier to name host variables, correlations, local variables in stored procedures, objects, such as databases, tables, columns, or stored procedures, or parameters, such as macro or stored procedure parameters, because Teradata Database already uses the word and might misinterpret it. Future Word reserved for future Teradata Database use and cannot be used as an identifier. NonReserved Teradata Database non-reserved keyword that is permitted as an identifier but discouraged because of possible confusion that may result. empty If the keyword does not have a Teradata Database status, the word is permitted as an identifier but discouraged because it is an SQL-2003 reserved or non-reserved word.Appendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 174 SQL Reference: Fundamentals • The following definitions apply to the SQL-2003 Status column: Type Explanation Reserved ANSI SQL-2003 reserved word. If the Teradata Database Status is Reserved or Future, an SQL-2003 reserved word cannot be used as an identifier. If the Teradata Database Status is Non-Reserved or empty, the word is permitted as an identifier but discouraged because of possible confusion that may result. NonReserved ANSI SQL-2003 non-reserved word. If the Teradata Database Status is Reserved or Future, an SQL-2003 non-reserved word cannot be used as an identifier. If the Teradata Database Status is Non-Reserved or empty, the word is permitted as an identifier, but discouraged because of the possible confusion that may result. Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReserved A X ABORT X ABORTSESSION X ABS X X ABSOLUTE X ACCESS X ACCESS_LOCK X ACCOUNT X ACOS X ACOSH X ACTION X ADA X ADD X X ADD_MONTHS X ADMIN X X AFTER X X AG X AGGREGATE XAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 SQL Reference: Fundamentals 175 ALIAS X ALL X X ALLOCATE X ALLOCATION X ALTER X X ALWAYS X X AMP X ANALYSIS X AND X X ANSIDATE X ANY X X ARE X ARGLPAREN X ARRAY X AS X X ASC X X ASCII X ASENSITIVE X ASIN X ASINH X ASSERTION X ASSIGNMENT X X ASYMMETRIC X AT X X ATAN X ATAN2 X ATANH X ATOMIC X X ATTR X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 176 SQL Reference: Fundamentals ATTRIBUTE X X ATTRIBUTES X X ATTRS X AUTHORIZATION X X AVE X AVERAGE X AVG X X BEFORE X X BEGIN X X BERNOULLI X BETWEEN X X BIGINT X X BINARY X X BLOB X X BOOLEAN X BOTH X X BREADTH X BT X BUT X BY X X BYTE X BYTEINT X BYTES X C X X CALL X X CALLED X X CARDINALITY X CASCADE X CASCADED X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 SQL Reference: Fundamentals 177 CASE X X CASE_N X CASESPECIFIC X CAST X X CATALOG X CATALOG_NAME X CD X CEIL X CEILING X CHAIN X CHANGERATE X CHAR X X CHAR_LENGTH X X CHAR2HEXINT X CHARACTER X X CHARACTER_LENGTH X X CHARACTER_SET_CATALOG X CHARACTER_SET_NAME X CHARACTER_SET_SCHEMA X CHARACTERISTICS X X CHARACTERS X X CHARS X CHARSET_COLL X CHECK X X CHECKED CHECKPOINT X CHECKSUM X CLASS X CLASS_ORIGIN X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 178 SQL Reference: Fundamentals CLOB X X CLOSE X X CLUSTER X CM X COALESCE X X COBOL X COLLATE X COLLATION X X COLLATION_CATALOG X COLLATION_NAME X COLLATION_SCHEMA X COLLECT X X COLUMN X X COLUMN_NAME X COLUMNSPERINDEX X COLUMNSPERJOININDEX X COMMAND_FUNCTION X COMMAND_FUNCTION_CODE X COMMENT X COMMIT X X COMMITTED X COMPARISON X COMPILE X COMPRESS X CONDITION X CONDITION_NUMBER X CONNECT X CONNECTION X CONNECTION_NAME X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 SQL Reference: Fundamentals 179 CONSTRAINT X X CONSTRAINT_CATALOG X CONSTRAINT_NAME X CONSTRAINT_SCHEMA X CONSTRAINTS X CONSTRUCTOR X X CONSUME X CONTAINS X CONTINUE X X CONVERT X CONVERT_TABLE_HEADER X CORR X X CORRESPONDING X COS X COSH X COSTS X COUNT X X COVAR_POP X X COVAR_SAMP X X CPP X CPUTIME X CREATE X X CROSS X X CS X CSUM X CT X CUBE X X CUME_DIST X CURRENT X X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 180 SQL Reference: Fundamentals CURRENT_DATE X X CURRENT_DEFAULT_TRANSFORM_GROUP X CURRENT_PATH X CURRENT_ROLE X CURRENT_TIME X X CURRENT_TIMESTAMP X X CURRENT_TRANSFORM_GROUP_FOR_TYPE X CURRENT_USER X CURSOR X X CURSOR_NAME X CV X CYCLE X X DATA X X DATABASE X DATABLOCKSIZE X DATE X X DATEFORM X DATETIME_INTERVAL_CODE X DATETIME_INTERVAL_PRECISION X DAY X X DBC X DEALLOCATE X DEBUG X DEC X X DECIMAL X X DECLARE X X DEFAULT X X DEFAULTS X DEFERRABLE X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 SQL Reference: Fundamentals 181 DEFERRED X X DEFINED X DEFINER X X DEGREE X DEGREES X DEL X DELETE X X DEMOGRAPHICS X DENIALS X DENSE_RANK X DEPTH X DEREF X DERIVED X DESC X X DESCRIBE X DESCRIPTOR X X DETERMINISTIC X X DIAGNOSTIC X DIAGNOSTICS X DIGITS X DISABLED X DISCONNECT X DISPATCH X DISTINCT X X DO X DOMAIN X X DOUBLE X X DR X DROP X X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 182 SQL Reference: Fundamentals DUAL X DUMP X DYNAMIC X DYNAMIC_FUNCTION X DYNAMIC_FUNCTION_CODE X EACH X X EBCDIC X ECHO X ELEMENT X ELSE X X ELSEIF X ENABLED X ENCRYPT X END X X END-EXEC X EQ X EQUALS X X ERROR X ERRORFILES X ERRORTABLES X ESCAPE X X ET X EVERY X EXCEPT X X EXCEPTION X EXCL X EXCLUDE X EXCLUDING X EXCLUSIVE X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 SQL Reference: Fundamentals 183 EXEC X X EXECUTE X X EXISTING EXISTS X X EXIT X EXP X X EXPIRE X EXPLAIN X EXTERNAL X X EXTRACT X X FALLBACK X FALSE X FASTEXPORT X FETCH X X FILTER X FINAL X X FIRST X X FLOAT X X FLOOR X FOLLOWING X X FOR X X FOREIGN X X FORMAT X FORTRAN X FOUND X X FREE X FREESPACE X FROM X X FULL X X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 184 SQL Reference: Fundamentals FUNCTION X X FUSION X G X X GE X GENERAL X GENERATED X X GET X GIVE X GLOBAL X X GO X X GOTO X X GRANT X X GRANTED X GRAPHIC X GROUP X X GROUPING X X GT X HANDLER X HASH X HASHAMP X HASHBAKAMP X HASHBUCKET X HASHROW X HAVING X X HELP X HIERARCHY X HIGH X HOLD X HOST X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 SQL Reference: Fundamentals 185 HOUR X X IDENTITY X X IF X IFP X IMMEDIATE X X IMPLEMENTATION X IN X X INCLUDING X INCONSISTENT X INCREMENT X X INDEX X INDEXESPERTABLE X INDEXMAINTMODE X INDICATOR X X INITIALLY X INITIATE X INNER X X INOUT X X INPUT X X INS X INSENSITIVE X INSERT X X INSTANCE X X INSTANTIABLE X X INSTEAD X INT X X INTEGER X X INTEGERDATE X INTERSECT X X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 186 SQL Reference: Fundamentals INTERSECTION X INTERVAL X X INTO X X INVOKER X X IOCOUNT X IS X X ISOLATION X X ITERATE X JAVA X JIS_COLL X JOIN X X JOURNAL X K X X KANJI1 X KANJISJIS X KBYTE X KBYTES X KEEP X KEY X X KEY_MEMBER X KEY_TYPE X KILOBYTES X KURTOSIS X LANGUAGE X X LARGE X X LAST X X LATERAL X LATIN X LE X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 SQL Reference: Fundamentals 187 Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReserved LEADING X X LEAVE X LEFT X X LENGTH X LEVEL X X LIKE X X LIMIT X LN X X LOADING X LOCAL X X LOCALTIME X LOCALTIMESTAMP X LOCATOR X X LOCK X LOCKEDUSEREXPIRE X LOCKING X LOG X LOGGING X LOGON X LONG X LOOP X LOW X LOWER X X LT X M X X MACRO X MAP X X MATCH XAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 188 SQL Reference: Fundamentals MATCHED X X MAVG X MAX X X MAXCHAR X MAXIMUM X MAXLOGONATTEMPTS X MAXVALUE X X MCHARACTERS X MDIFF X MEDIUM X MEMBER X MERGE X X MESSAGE_LENGTH X MESSAGE_OCTET_LENGTH X MESSAGE_TEXT X METHOD X X MIN X X MINCHAR X MINDEX X MINIMUM X MINUS X MINUTE X X MINVALUE X X MLINREG X MLOAD X MOD X X MODE X MODIFIED X MODIFIES X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 SQL Reference: Fundamentals 189 MODIFY X MODULE X MONITOR X MONRESOURCE X MONSESSION X MONTH X X MORE X MSUBSTR X MSUM X MULTINATIONAL X MULTISET X X MUMPS X NAME X X NAMED X NAMES X NATIONAL X NATURAL X X NCHAR X NCLOB X NE X NESTING X NEW X X NEW_TABLE X NEXT X X NO X X NONE X X NORMALIZE X NORMALIZED X NOT X X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 190 SQL Reference: Fundamentals NOWAIT X NULL X X NULLABLE X NULLIF X X NULLIFZERO X NULLS X NUMBER X NUMERIC X X OA X OBJECT X X OBJECTS X OCTET_LENGTH X X OCTETS X OF X X OFF X OLD X X OLD_TABLE X ON X X ONLY X X OPEN X X OPTION X X OPTIONS X OR X X ORDER X X ORDERED_ANALYTIC X ORDERING X X ORDINALITY X OTHERS X OUT X X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 SQL Reference: Fundamentals 191 OUTER X X OUTPUT X OVER X X OVERLAPS X X OVERLAY X OVERRIDE X OVERRIDING X PAD X PARAMETER X X PARAMETER_MODE X PARAMETER_NAME X PARAMETER_ORDINAL_POSITION X PARAMETER_SPECIFIC_CATALOG X PARAMETER_SPECIFIC_NAME X PARAMETER_SPECIFIC_SCHEMA X PARTIAL X PARTITION X X PARTITIONED X PASCAL X PASSWORD X PATH X PERCENT X PERCENT_RANK X X PERCENTILE_CONT X PERCENTILE_DISC X PERM X PERMANENT X PLACING X PLI X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 192 SQL Reference: Fundamentals POSITION X X POWER X PRECEDING X X PRECISION X X PREPARE X X PRESERVE X X PRIMARY X X PRINT X PRIOR X PRIVATE X PRIVILEGES X X PROCEDURE X X PROFILE X PROTECTED X PROTECTION X PUBLIC X X QUALIFIED X QUALIFY X QUANTILE X QUEUE X QUERY X RADIANS X RANDOM X RANDOMIZED X RANGE X X RANGE_N X RANK X X READ X X READS X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 SQL Reference: Fundamentals 193 REAL X X RECALC X RECURSIVE X X REF X REFERENCES X X REFERENCING X X REGR_AVGX X X REGR_AVGY X X REGR_COUNT X X REGR_INTERCEPT X X REGR_R2 X X REGR_SLOPE X X REGR_SXX X X REGR_SXY X X REGR_SYY X X RELATIVE X X RELEASE X X RENAME X REPEAT X REPEATABLE X REPLACE X REPLACEMENT X REPLCONTROL X REPLICATION X REQUEST X RESTART X X RESTORE X RESTRICT X RESULT X X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 194 SQL Reference: Fundamentals RESUME X RET X RETAIN X RETRIEVE X RETURN X RETURNED_CARDINALITY X RETURNED_LENGTH X RETURNED_OCTET_LENGTH X RETURNED_SQLSTATE X RETURNS X X REUSE X REVALIDATE X REVOKE X X RIGHT X X RIGHTS X ROLE X X ROLLBACK X X ROLLFORWARD X ROLLUP X X ROUTINE X ROUTINE_CATALOG X ROUTINE_NAME X ROUTINE_SCHEMA X ROW X X ROW_COUNT X ROW_NUMBER X X ROWID X ROWS X X RU X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 SQL Reference: Fundamentals 195 SAMPLE X SAMPLEID X SAMPLES X SAVEPOINT X SCALE X SCHEMA X SCHEMA_NAME X SCOPE X SCOPE_CATALOG X SCOPE_NAME X SCOPE_SCHEMA X SCROLL X X SEARCH X SEARCHSPACE X SECOND X X SECTION X SECURITY X X SEED X SEL X SELECT X X SELF X X SENSITIVE X SEQUENCE X SERIALIZABLE X X SERVER_NAME X SESSION X X SESSION_USER X SET X X SETRESRATE X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 196 SQL Reference: Fundamentals SETS X X SETSESSRATE X SHARE X SHOW X SIMILAR X SIMPLE X SIN X SINH X SIZE X SKEW X SMALLINT X X SOME X X SOUNDEX X SOURCE X X SPACE X SPECCHAR X SPECIFIC X X SPECIFIC_NAME X SPECIFICTYPE X SPL X SPOOL X SQL X X SQLEXCEPTION X X SQLSTATE X X SQLTEXT X SQLWARNING X X SQRT X X SR X SS X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 SQL Reference: Fundamentals 197 START X X STARTUP X STAT X STATE X STATEMENT X X STATIC X STATISTICS X STATS X STDDEV_POP X X STDDEV_SAMP X X STEPINFO X STRING_CS X STRUCTURE X STYLE X X SUBCLASS_ORIGIN X SUBLIST SUBMULTISET X SUBSCRIBER X SUBSTR X SUBSTRING X X SUM X X SUMMARY X SUMMARYONLY X SUSPEND X SYMMETRIC X SYSTEM X X SYSTEM_USER X SYSTEMTEST X TABLE X X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 198 SQL Reference: Fundamentals TABLE_NAME X TABLESAMPLE X TAN X TANH X TARGET X TBL_CS X TD_GENERAL X TD_INTERNAL X TEMPORARY X X TERMINATE X TEXT X THAN THEN X X THRESHOLD X TIES X X TIME X X TIMESTAMP X X TIMEZONE_HOUR X X TIMEZONE_MINUTE X X TITLE X TO X X TOP X TPA X TOP_LEVEL_COUNT X TRACE X TRAILING X X TRANSACTION X X TRANSACTION_ACTIVE X TRANSACTIONS_COMMITTED X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 SQL Reference: Fundamentals 199 TRANSACTIONS_ROLLED_BACK X TRANSFORM X X TRANSFORMS X TRANSLATE X X TRANSLATE_CHK X TRANSLATION X TREAT X TRIGGER X X TRIGGER_CATALOG X TRIGGER_NAME X TRIGGER_SCHEMA X TRIM X X TRUE X TYPE X X UC X UDTCASTAS X UDTCASTLPAREN X UDTMETHOD X UDTTYPE X UDTUSAGE X UESCAPE X UNBOUNDED X X UNCOMMITTED X X UNDEFINED X UNDER X UNDO X UNICODE X UNION X X UNIQUE X X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 200 SQL Reference: Fundamentals UNKNOWN X X UNNAMED X UNNEST X UNTIL X UPD X UPDATE X X UPPER X X UPPERCASE X USAGE X USE X USER X X USER_DEFINED_TYPE_CATALOG X USER_DEFINED_TYPE_CODE X USER_DEFINED_TYPE_NAME X USER_DEFINED_TYPE_SCHEMA X USING X X VALUE X X VALUES X X VAR_POP X X VAR_SAMP X X VARBYTE X VARCHAR X X VARGRAPHIC X VARYING X X VIEW X X VOLATILE X WAIT X WARNING X WHEN X X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 SQL Reference: Fundamentals 201 WHENEVER X WHERE X X WHILE X WIDTH_BUCKET X X WINDOW X WITH X X WITHIN X WITHOUT X WORK X X WRITE X X YEAR X X ZEROIFNULL X ZONE X X Keyword Teradata Database Status SQL-2003 Status Reserved Future NonReserved Reserved NonReservedAppendix B: Restricted Words for V2R6.2 Reserved Words and Keywords for V2R6.2 202 SQL Reference: FundamentalsSQL Reference: Fundamentals 203 APPENDIX C Teradata Database Limits This appendix provides the following Teradata Database limits: • System limits • Database limits • Session limitsAppendix C: Teradata Database Limits System Limits 204 SQL Reference: Fundamentals System Limits The system specifications in the following table apply to an entire Teradata Database configuration. Parameter Value Maximum number of databases and users 4.2 x 10 9 Total data capacity • Expressed as a base 10 value: • 1.39 TB/AMP (1.39 x 10 12 bytes/AMP) • Expressed as a base 2 value: • 1.26 TB/AMP (1.26 x 10 12 bytes/AMP) Maximum number of active concurrent transactions 2048 Maximum data format descriptor size 30 characters Maximum error message text size in failure parcel 255 bytes Maximum number of sectors per datablock 255 a Maximum data block size 130560 bytes Datablock header size Depends on several factors: FOR a datablock that is … The datablock header size is this many bytes … new or has been updated 72 on a 64-bit system and has not been updated 40 on a 32-bit system and has not been updated 36 Maximum number of sessions per PE 120 Maximum number of gateways per node 1 Maximum number of sessions per Gateway Tunable. b 1200 maximum certified Maximum number of parcels in one message 256Appendix C: Teradata Database Limits System Limits SQL Reference: Fundamentals 205 Maximum message size Approximately 65000 bytes Note: This limit applies to messages to/from host systems and to some internal Teradata Database messages. Maximum number of PEs per system 1024 Maximum number of AMPs per system 16383 c More generally, the maximum number of AMPs per system depends on the number of PEs in the configuration. The following equation provides the most general solution: Maximum number of AMP and PE vprocs, in any combination, per system 16384 Number of hash buckets per system 65536 d Bucket numbers range from 0 to 65535. Number of hash values per system 4.2 x 10 9 Maximum number of external routine protected mode server tasks per PE or AMP 20 e Maximum number of external routine secure mode server tasks per PE or AMP 20 c Amount of private disk swap space required per protected or secure mode server per PE or AMP vproc 256 KB a. The increase in datablock header size from 36 or 40 bytes to 64 bytes increases the size of roughly 6 percent of the datablocks by one sector (see “Datablock header size” on page 204). b. See Utilities for details. c. This value is derived by subtracting 1 from the maximum total of PE and AMP vprocs per system (because each system must have at least one PE), which is 16384. This is obviously not a practical configuration. d. This value is fixed. The system assigns its 65536 hash buckets to AMPs as evenly as possible. For example, a system with 1000 AMPs has 65 hash buckets on some AMPs and 66 hash buckets on others. In this particular case, the AMPs having 66 hash buckets also perform 1.5 percent more work than those with 65 hash buckets. The work per AMP imbalance increases as a function of the number of AMPs in the system for those cases where 65536 is not evenly divisible by the total number of AMPs. e. The valid range is 0 to 20, inclusive. The limit is 20 servers for each server type, not 20 combined for both. See Utilities for details. Parameter Value 16384 number_of_PEs –Appendix C: Teradata Database Limits Database Limits 206 SQL Reference: Fundamentals Database Limits The database specifications in the following table apply to a single database. The values presented are maxima for their respective parameters individually and not in combination. Parameter Maximum Value Number of journal tables per database 1 Number of data tables per database 4.2 x 10 9 Database, user, table, view, macro, index, constraint, userdefined function, stored procedure, user-defined method, user-defined type, replication group, or column name size 30 bytes Tables and Views Number of columns per base table or view 2048 Number of UDT columns per base table or view Approximately 1600 g,h,i Number of LOB type columns per base table 32 j Number of columns created over the life of a base table 2560 Number of rows per base table Limited by disk capacity Number of bytes per table header a • Approximately 64000 bytes -or- • Approximately 128000 bytes Row size Approximately 65536 bytes Logical row size b 67106816000 bytes k Number of secondary c , hash, and join indexes, in any combination, per table 32 Non-LOB column size • 65522 bytes (NPPI table) l • 65520 bytes (PPI table)m Number of columns per primary or secondary index 64 SQL title size 60 characters Size of the queue table FIFO runtime cache per PE • 100 queue table entries • 1 MB Size of the queue table FIFO runtime cache per table 2211 row entries Number of primary indexes per table 1 Number of partitions for a partitioned primary index 65535 Number of table-level constraints per table 100Appendix C: Teradata Database Limits Database Limits SQL Reference: Fundamentals 207 Number of referential constraints per table 64 Number of columns in foreign and parent keys 64 Number of compressed values per column 255 plus nulls Predefined and User-Defined Types BLOB object size 2097088000 bytes CLOB object size • 2097088000 single-byte characters • 1048544000 double-byte characters Structured UDT size. d • 65521 bytes (NPPI table) • 65519 bytes (PPI table) Number of characters in a string constant 32000 Number of attributes that can be specified for a structured UDT per CREATE TYPE or ALTER TYPE statement 300 - 512 n Number of attributes that can be defined for a structured UDT Approximately 4000 o Number of nested attributes in a structured UDT 512 Number of methods associated with a UDT Approximately 500 p Macros, Stored Procedures, and External Routines Expanded text size for macros and views 2 MB Length of external name string for an external routine. e 1000 characters Package path length for an external routine 256 characters SQL request size in a stored procedure 64 KB Number of parameters specified in a UDF 128 Number of parameters specified in a UDM 128 Number of parameters specified in a macro 2048 Number of parameters in a stored procedure 256 Number of nested CALL statements 15 Number of open cursors 16 for embedded SQL, 15 for a stored procedure Queries, Requests, and Responses SQL request size 1 MB (Includes SQL statement text, USING data, and parcel overhead) Parameter Maximum ValueAppendix C: Teradata Database Limits Database Limits 208 SQL Reference: Fundamentals SQL response size 1 MB (Includes SQL result and parcel overhead) Number of columns per DML statement ORDER BY clause 16 Number of tables that can be joined per query block 64 Number of subquery nesting levels per query 64 Number of fields in a USING row descriptor 2550 SQL activity count size 2 32 -1 rows Number of SELECT AND CONSUME statements in a delayed state per PE 24 Number of partitions for a hash join operation 50 Query and Workload Analysis Size of the Index Wizard workload cache 256 MB q Number of indexes on which statistics can be collected and maintained at one time 32 This limit is independent of the number of pseudoindexes on which statistics can be collected and maintained. Number of pseudoindexes f on which multicolumn statistics can be collected and maintained at one time 32 This limit is independent of the number of indexes on which statistics can be collected and maintained. Number of columns and indexes on which statistics can be recollected for a table 512 Hash and Join Indexes Number of columns referenced per single table in a hash or join index 64 Number of columns referenced in the fixed part of a compressed hash or join index 64 Number of columns referenced in the repeating part of a compressed hash or join index 64 Number of columns in an uncompressed join index 2048 Number of columns in a compressed join index 128 Parameter Maximum ValueAppendix C: Teradata Database Limits Database Limits SQL Reference: Fundamentals 209 Replication Row size permitted for a replication operation Approximately 25000 bytes For details, see Teradata Replication Solutions Overview and “CREATE REPLICATION GROUP” in SQL Reference: Data Definition Statements. Number of replication groups per system 100 Number of tables that can be copied simultaneously with a replication operation 15 Number of columns that can be defined for a replicated table 1000 Character column data size permitted for a replication operation • CHARACTER(10000) • VARCHAR(10000) For UTF16, this translates to a maximum of 5000 characters. a. A table header that is large enough to require more than ~64000 bytes uses two 64Kbyte rows. A table header that requires 64000 or fewer bytes does not use the second row that is required to contain a table header of ~128000 bytes. b. A logical row is defined as a base table row plus the sum of the bytes stored in a LOB subtable for that row. c. A NUSI defined with an ORDER BY clause counts as two indexes in this calculation. d. Based on a table having a 1 byte (BYTEINT) primary index. Because a UDT column cannot be part of any index definition, there must be at least one non-UDT column in the table for its primary index. Row header overhead consumes 14 bytes in an NPPI table and 16 bytes in a PPI table, so the maximum structured UDT size is derived by subtracting 15 bytes (for an NPPI table) or 17 bytes (for a PPI table) from the row maximum of 65 536 bytes. e. An external routine is the portion of a UDF, external stored procedure, or method that is written in C or C++. This is the code that defines the semantics for the UDF, procedure, or method. f. A pseudoindex is a file structure that allows you to collect statistics on a composite, or multicolumn, column set in the same way you collect statistics on a composite index. g. The absolute limit is 2048, and the realizable number varies as a function of the number of other features declared for a table that occupy table header space. h. The figure of 1600 UDT columns assumes a FAT table header. i. This limit is true whether the UDT is a distinct or a structured type. j. This includes both predefined type LOB columns and UDT LOB columns. A UDT LOB column counts as one LOB column even if the UDT is a structured type that has multiple LOB attributes. k. This value is derived by multiplying the maximum number of LOB columns per base table (32) times the maximum size of a LOB field (2 097 088 000 8-bit bytes). Remember that each LOB column consumes 39 bytes of Object ID from the base table, so 1 248 of those 67 106 816 000 bytes cannot be used for data. l. Based on subtracting the minimum row overhead value for an NPPI table row (14 bytes) from the system-defined maximum row length (65 536 bytes). Parameter Maximum ValueAppendix C: Teradata Database Limits Database Limits 210 SQL Reference: Fundamentals m. Based on subtracting the minimum row overhead value for a PPI table row (16 bytes) from the system-defined maximum row length (65 536 bytes). n. The maximum is platform-dependent. o. While you can specify no more than 300 to 512 attributes for a structured UDT per CREATE TYPE or ALTER TYPE statement, you can submit any number of ALTER TYPE statements with the ADD ATTRIBUTE option specified as necessary to add additional attributes to the type up to the upper limit of approximately 4000. p. There is no absolute limit on the number of methods that can be associated with a given UDT. Methods can have a variable number of parameters, and the number of parameters directly affects the limit, which is due to parser memory restrictions. There is a workaround for this issue. See the documentation for ALTER TYPE in SQL Reference: Data Manipulation Statements for details. q. The default is 48 megabytes and the minimum is 32 megabytes.Appendix C: Teradata Database Limits Session Limits SQL Reference: Fundamentals 211 Session Limits The session specifications in the following table apply to a single session. Parameter Value Active request result spool files 16 Parallel steps Parallel steps can be used to process a request submitted within a transaction (which may be either explicit or implicit). The maximum number of steps generated per request is determined as follows: • Per request, if no channels Note: Channels are not required for a primary index request with an equality constraint. 20 steps • A request that involves redistribution of rows to other AMPs, such as a join or an INSERT-SELECT Requires 4 channels • A request that does not involve row distribution Requires 2 channels Number of materialized global temporary tables per session 2000 Number of volatile tables per session 1000Appendix C: Teradata Database Limits Session Limits 212 SQL Reference: FundamentalsSQL Reference: Fundamentals 213 APPENDIX D ANSI SQL Compliance This appendix describes the ANSI SQL standard, Teradata compliance with the ANSI SQL standard, and terminology differences between ANSI SQL and Teradata SQL. Topics include: • ANSI SQL Standard • Terminology Differences Between ANSI SQL and Teradata • SQL Flagger • Differences Between Teradata and ANSI SQL ANSI SQL Standard Introduction The American National Standards Institute (ANSI) SQL standard, formally titled International Standard ISO/IEC 9075:2003, Database Language SQL, defines a version of Structured Query Language that all vendors of relational database management systems support to a greater or lesser degree. Motivation Behind an SQL Standard Teradata, like most vendors of relational database management systems, had its own dialect of the SQL language for many years prior to the development of the SQL standard. You might ask several questions like the following: • Why should there be an industry-wide SQL standard? • Why should any vendor with an entrenched user base consider modifying its SQL dialect to conform with the ANSI SQL standard? Why an SQL Standard? National and international standards abound in the computer industry. As anyone who has worked in the industry for any length of time knows, standardization offers both advantages and disadvantages both to users and to vendors. The principal advantages of having an SQL standard are the following: • Open systems The overwhelming trend in computer technology has been toward open systems with publicly defined standards to facilitate third party and end user access and development using the standardized products.Appendix D: ANSI SQL Compliance ANSI SQL Standard 214 SQL Reference: Fundamentals The ANSI SQL standard provides an open definition for the SQL language. • Less training for transfer and new employees A programmer trained in ANSI-standard SQL can move from one SQL programming environment to another with no need to learn a new SQL dialect. When a core dialect of the language is the lingua franca for SQL programmers, the need for retraining is significantly reduced. • Application portability When there is a standardized public definition for a programming language, users can rest assured that any applications they develop to the specifications of that standard are portable to any environment that supports the same standard. This is an extremely important budgetary consideration for any large scale end user application development project. • Definition and manipulation of heterogeneous databases is facilitated Many user data centers support multiple merchant databases across different platforms. A standard language for communicating with relational databases, irrespective of the vendor offering the database management software, is an important factor in reducing the overhead of maintaining such an environment. • Intersystem communication is facilitated It is common for an enterprise to exchange applications and data among different merchant databases. Common examples of this appear below. • Two-phase commit transactions where rows are written to multiple databases simultaneously. • Bulk data import and export between different vendor databases. These operations are made much cleaner and simpler when there is no need to translate data types, database definitions, and other component definitions between source and target databases. Teradata Compliance With the ANSI Standard Conformance to a standard presents problems for any vendor that produces an evolved product and supports a large user base. Teradata, in its historical development, has produced any number of innovative SQL language elements that do not conform to the ANSI SQL standard, a standard that did not exist when those features were conceived. The existing Teradata user base had invested substantial time, effort, and capital into developing applications using that Teradata SQL dialect. At the same time, new customers demand that vendors conform to open standards for everything from chip sets to operating systems to application programming interfaces. Meeting these divergent requirements presents a challenge that Teradata SQL solves by following the multipronged policy outlined in the following table.Appendix D: ANSI SQL Compliance ANSI SQL Standard SQL Reference: Fundamentals 215 WHEN … THEN … a new feature or feature enhancement is added to Teradata SQL that feature conforms to the ANSI SQL standard. the difference between the Teradata SQL dialect and the ANSI SQL standard for a language feature is slight the ANSI SQL is added to the Teradata Database feature as an option. the difference between the Teradata SQL dialect and the ANSI SQL standard for a language feature is significant both syntaxes are offered and the user has the choice of operating in either Teradata or ANSI mode or of turning off SQL Flagger. The mode can be defined in the following ways: • Persistently Use the SessionMode field of the DBS Control Record to define session mode characteristics. • For a session Use the BTEQ .SET SESSION TRANSACTION command to control transaction semantics. Use the BTEQ .SET SESSION SQLFLAG command to control use of the SQL Flagger. Use the SQL statement SET SESSION DATEFORM to control how data typed as DATE is handled. a new feature or feature enhancement is added to Teradata SQL and that feature is not defined by the ANSI SQL standard that feature is designed using the following criteria: IF other vendors … THEN Teradata designs the new feature … offer a similar feature or feature extension to broadly comply with other solutions, but consolidates the best ideas from all and, where necessary, creates its own, cleaner solution. do not offer a similar feature or feature extension • as cleanly and generically as possible with an eye toward creating a language element that will not be subject to major revisions to comply with future updates to the ANSI SQL standard. • in a way that offers the most power to users without violating any of the basic tenets of the ANSI SQL standard.Appendix D: ANSI SQL Compliance Terminology Differences Between ANSI SQL and Teradata 216 SQL Reference: Fundamentals Terminology Differences Between ANSI SQL and Teradata The ANSI SQL standard and Teradata occasionally use different terminology. The following table lists the more important variances. Note: 1) In the ANSI SQL standard, the term table has the following definitions: • A base table • A viewed table (view) • A derived table ANSI Teradata Base table Table 1 Binding style Not defined, but implicitly includes the following: • Interactive SQL • Embedded SQL • ODBC • CLIv2 Authorization ID User ID Catalog Dictionary CLI ODBC 2 Direct SQL Interactive SQL Domain Not defined External routine function User-defined function (UDF) Module Not defined Persistent stored module Stored procedure Schema User Database SQL database Relational database Viewed table View Not defined Explicit transaction 3 Not defined CLIv2 4 Not defined Macro 5Appendix D: ANSI SQL Compliance SQL Flagger SQL Reference: Fundamentals 217 2) ANSI CLI is not exactly equivalent to ODBC, but the ANSI standard is heavily based on the ODBC definition. 3) ANSI transactions are always implicit, beginning with an executable SQL statement and ending with either a COMMIT or a ROLLBACK statement. 4) Teradata CLIv2 is an implementation-defined binding style. 5) The function of Teradata Database macros is similar to that of ANSI persistent stored modules without having the loop and branch capabilities stored modules offer. SQL Flagger Function The SQL Flagger, when enabled, reports the use of non-standard SQL. The SQL Flagger always permits statements flagged as non-entry-level or noncompliant ANSI SQL to execute. Its task is not to enforce the standard, but rather to return a warning message to the requestor noting the noncompliance. The analysis includes syntax checking as well as some dictionary lookup, particularly the implicit assignment and comparison of different data types (where ANSI requires use of the CAST function to convert the types explicitly) as well as some semantic checks. The SQL Flagger does not check or detect every condition for noncompliance; thus, a statement that is not flagged does not necessarily mean it is compliant. Enabling and Disabling the SQL Flagger Flagging is enabled by a client application before a session is logged on and generally is used only to assist in checking for ANSI compliance in code that must be portable across multiple vendor environments. The SQL Flagger is disabled by default. You can enable or disable it using any of the following procedures, depending on your application. FOR this software … USE these commands or options … TO turn the SQL Flagger … BTEQ .[SET] SESSION SQLFLAG ENTRY to entry-level ANSI .[SET] SESSION SQLFLAG NONE off See Basic Teradata Query Reference for more detail on using BTEQ commands. Preprocessor2 SQLFLAGGER(ENTRY) to entry-level ANSI SQLFLAGGER(NONE) off See Teradata Preprocessor2 for Embedded SQL Programmer Guide for details on setting Preprocessor options.Appendix D: ANSI SQL Compliance Differences Between Teradata and ANSI SQL 218 SQL Reference: Fundamentals Differences Between Teradata and ANSI SQL For a complete list of SQL features in this release, see Appendix E. The list identifies which features are ANSI SQL compliant and which features are Teradata extensions. The list of features includes SQL statements and options, functions and operators, data types and literals. CLI set lang_conformance = ‘2’ set lang_conformance to ‘2’ to entry-level ANSI set lang_conformance = ‘N’ off See Teradata Call-Level Interface Version 2 Reference for Channel-Attached Systems and Teradata Call-Level Interface Version 2 Reference for NetworkAttached Systems for details on setting the conformance field. FOR this software … USE these commands or options … TO turn the SQL Flagger …SQL Reference: Fundamentals 219 APPENDIX E SQL Feature Summary This appendix details the differences in SQL between this release and previous releases. • “Statements and Modifiers” on page 219 • “Data Types and Literals” on page 277 • “Functions, Operators, and Expressions” on page 280 The intent of this appendix is to provide a way to readily identify new SQL in this release and previous releases of Teradata Database. It is not meant as a Teradata SQL reference. Notation Conventions The following table describes the conventions used in this appendix. Statements and Modifiers The following table lists SQL statements and modifiers for this version and previous versions of Teradata Database. The following type codes appear in the ANSI Compliance column. This notation … Means … UPPERCASE a keyword italics a variable, such as a column or table name [ n ] that the use of n is optional | n | that option n is described separately in this appendix Code Definition A ANSI SQL-2003 compliant T Teradata extensionAppendix E: SQL Feature Summary Statements and Modifiers 220 SQL Reference: Fundamentals Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0 ABORT T X X X Options FROM option T X X X WHERE condition T X X X ALTER FUNCTION ALTER SPECIFIC FUNCTION T X X X Options EXECUTE PROTECTED/ EXECUTE NOT PROTECTED T X X X COMPILE/ COMPILE ONLY T X X X ALTER METHOD ALTER CONSTRUCTOR METHOD ALTER INSTANCE METHOD ALTER SPECIFIC METHOD T X X Options EXECUTE PROTECTED/ EXECUTE NOT PROTECTED/ COMPILE/ COMPILE ONLY T X X ALTER PROCEDURE (external form) T X X X Options LANGUAGE C/ LANGUAGE CPP T X X X COMPILE/ COMPILE ONLY/ EXECUTE PROTECTED/ EXECUTE NOT PROTECTED T X X XAppendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 221 ALTER PROCEDURE (internal form) T X X X Options COMPILE T X X X WITH PRINT/ WITH NO PRINT T X X X WITH SPL/ WITH NO SPL T X X X WITH WARNING/ WITH NO WARNING T X X X ALTER REPLICATION GROUP T X X X Options ADD table_name/ ADD database_name.table_name T X X X DROP table_name/ DROP database_name.table_name T X X X ALTER TABLE A, T X X X Options ADD column_name |Data Type| |Data Type Attributes| A X X X ADD column_name |Column Storage Attributes| T X X X ADD column_name NO COMPRESS T X ADD column_name |Column Constraint Attributes| T X X X ADD column_name |Table Constraint Attributes| T X X X ADD |Table Constraint Attributes| A X X X ADD column_name NULL T X X X AFTER JOURNAL/ NO AFTER JOURNAL/ DUAL AFTER JOURNAL/ LOCAL AFTER JOURNAL/ NOT LOCAL AFTER JOURNAL T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 222 SQL Reference: Fundamentals ALTER TABLE, continued Options BEFORE JOURNAL/ JOURNAL/ NO BEFORE JOURNAL/ DUAL BEFORE JOURNAL T X X X DATABLOCKSIZE IMMEDIATE/ MINIMUM DATABLOCKSIZE/ MAXIMUM DATABLOCKSIZE/ DEFAULT DATABLOCKSIZE T X X X CHECKSUM = DEFAULT/ CHECKSUM = NONE/ CHECKSUM = LOW/ CHECKSUM = MEDIUM/ CHECKSUM = HIGH/ CHECKSUM = ALL T X X X DROP column_name A X X X DROP CHECK/ DROP column_name CHECK/ DROP CONSTRAINT name CHECK T X X X DROP CONSTRAINT T X X X DROP FOREIGN KEY REFERENCES T X X X WITH CHECK OPTION/ WITH NO CHECK OPTION T X X X DROP INCONSISTENT REFERENCES T X X X FALLBACK PROTECTION/ NO FALLBACK PROTECTION T X X X FREESPACE/ DEFAULT FREESPACE T X X X LOG/NO LOG T X X X MODIFY CHECK/ MODIFY column_name CHECK/ MODIFY CONSTRAINT name CHECK T X X X MODIFY [[NOT] UNIQUE] PRIMARY INDEX index [(column)]/ MODIFY [[NOT] UNIQUE] PRIMARY INDEX NOT NAMED [(column)] T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 223 ALTER TABLE, continued Options NOT PARTITIONED/ PARTITION BY expression/ DROP RANGE WHERE expression [ADD RANGE ranges]/ DROP RANGE ranges [ADD RANGE ranges]/ ADD RANGE ranges T X X X ON COMMIT DELETE ROWS/ ON COMMIT PRESERVE ROWS T X X X RENAME column_name T X X X REVALIDATE PRIMARY INDEX/ REVALIDATE PRIMARY INDEX WITH DELETE/ REVALIDATE PRIMARY INDEX WITH INSERT [INTO] table_name T X X X WITH JOURNAL TABLE T X X X ALTER TRIGGER T X X X Options ENABLED/DISABLED / T X X X TIMESTAMP T X X X ALTER TYPE A,T X X Options ADD ATTRIBUTE/ DROP ATTRIBUTE/ ADD METHOD/ ADD INSTANCE METHOD/ ADD CONSTRUCTOR METHOD/ ADD SPECIFIC METHOD/ DROP METHOD/ DROP INSTANCE METHOD/ DROP CONSTRUCTOR METHOD/ DROP SPECIFIC METHOD A,T X X BEGIN DECLARE SECTION A X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 224 SQL Reference: Fundamentals BEGIN LOGGING T X X X Options DENIALS T X X X WITH TEXT T X X X FIRST/ LAST/ FIRST AND LAST/ EACH T X X X BY database_name T X X X ON ALL/ ON operation/ ON GRANT T X X X ON DATABASE/ ON USER/ ON TABLE/ ON VIEW/ ON MACRO/ ON PROCEDURE/ ON FUNCTION/ T X X X ON TYPE T X X BEGIN QUERY LOGGING T X X X Options WITH ALL/ WITH OBJECTS/ WITH SQL/ WITH STEPINFO/ T X X X WITH COSTS T X X X LIMIT SQLTEXT [=n] [AND …]/ LIMIT SUMMARY = n1, n2, n3 [AND …]/ LIMIT THRESHOLD [=n] [AND …]/ T X X X LIMIT MAXCPU [=n] [AND …] T X X X ON ALL/ ON user_name/ ON user_name ACCOUNT = 'account_name'/ ON user_name ACCOUNT = ('account_name' [ … ,'account_name']) T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 225 BEGIN TRANSACTION/ BT T X X X CALL A X X X Options stored_procedure_name/ A X X X external_stored_procedure_name A X X X CHECKPOINT T X X X Options NAMED checkpoint T X X X INTO host_variable_name T X X X [INDICATOR] :host_indicator_name T X X X CLOSE A X X X COLLECT DEMOGRAPHICS T X X X Options FOR table_name/ FOR (table_name [ … ,table_name]) T X X X ALL/ WITH NO INDEX T X X X COLLECT STATISTICS/ COLLECT STATS/ COLLECT STAT (QCD form) T X X X Options PERCENT T X X X SET QUERY query_ID T X X SAMPLEID statistics_ID T X X UPDATE MODIFIED T X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 226 SQL Reference: Fundamentals COLLECT STATISTICS (QCD form), continued Options INDEX (column_name [ … , column_name])/ INDEX index_name/ COLUMN (column_name [ … ,column_name])/ COLUMN column_name/ T X X X COLUMN (column_name [ … , column_name], PARTITION [ … , column_name])/ COLUMN (PARTITION [ … , column_name])/ COLUMN PARTITION T X X COLLECT STATISTICS/ COLLECT STATS/ COLLECT STAT (optimizer form) T X X X Options USING SAMPLE T X X X [ON] [TEMPORARY] table_name/ [ON] join_index_name/ [ON] hash_index_name T X X X INDEX (column_name [ … , column_name])/ INDEX index_name/ COLUMN (column_name [ … ,column_name])/ COLUMN column_name/ T X X X COLUMN (column_name [ … , column_name], PARTITION [ … , column_name])/ COLUMN (PARTITION [ … , column_name])/ COLUMN PARTITION T X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 227 COLLECT STATISTICS/ COLLECT STATS/ COLLECT STAT (optimizer form, CREATE INDEX-style syntax) T X X X Options USING SAMPLE T X X X [UNIQUE] INDEX [index_name] [ALL] (column_name [ … , column_name]) [ORDER BY [VALUES]] (column_name)/ [UNIQUE] INDEX [index_name] [ALL] (column_name [ … , column_name]) [ORDER BY [HASH]] (column_name)/ COLUMN column_name/ COLUMN (column_name [ … , column_name])/ T X X X COLUMN (column_name [ … , column_name], PARTITION [ … , column_name])/ COLUMN (PARTITION [ … , column_name])/ COLUMN PARTITION T X X ON [TEMPORARY] table_name/ ON hash_index_name/ ON join_index_name T X X X COMMENT T X X X Options [ON] COLUMN object_name/ [ON] DATABASE object_name/ [ON] FUNCTION object_name/ [ON] MACRO object_name/ [ON] PROCEDURE object_name/ [ON] TABLE object_name/ [ON] TRIGGER object_name/ [ON] USER object_name/ [ON] VIEW object_name/ [ON] PROFILE object_name/ [ON] ROLE object_name/ T X X X [ON] GROUP group_name/ T X X X [ON] METHOD object_name/ [ON] TYPE object_name T X X AS 'comment'/ IS 'comment' T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 228 SQL Reference: Fundamentals COMMENT (embedded SQL) T X X X Options [ON] COLUMN object_reference/ [ON] DATABASE object_reference/ [ON] FUNCTION object_name/ [ON] MACRO object_reference/ [ON] PROCEDURE object_reference/ [ON] TABLE object_reference/ [ON] TRIGGER object_reference/ [ON] USER object_reference/ [ON] VIEW object_reference/ [ON] PROFILE object_name/ [ON] ROLE object_name/ T X X X [ON] GROUP group_name T X X X INTO host_variable_name T X X X [INDICATOR] :host_indicator_name T X X X COMMIT A, T X X X Options WORK A X X X RELEASE T X X X CONNECT (embedded SQL) T X X X Options IDENTIFIED BY passwordvar/ IDENTIFIED BY :passwordvar T X X X AS connection_name/ AS :namevar T X X X CREATE AUTHORIZATION T X X Options [AS] DEFINER/ [AS] DEFINER DEFAULT/ [AS] INVOKER T X X DOMAIN 'domain_name' T X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 229 CREATE CAST A X X Options WITH SPECIFIC METHOD specific_method_name/ WITH METHOD method_name/ WITH INSTANCE METHOD method_name/ WITH SPECIFIC FUNCTION specific_function_name/ WITH FUNCTION function_name A X X AS ASSIGNMENT A X X CREATE DATABASE T X X X Options PERMANENT = n [BYTES] T X X X SPOOL = n [BYTES] T X X X TEMPORARY = n [BYTES] T X X X ACCOUNT T X X X FALLBACK [PROTECTION]/ NO FALLBACK [PROTECTION] T X X X BEFORE JOURNAL/ JOURNAL/ NO JOURNAL NO BEFORE JOURNAL/ DUAL JOURNAL DUAL BEFORE JOURNAL T X X X AFTER JOURNAL/ NO AFTER JOURNAL/ DUAL AFTER JOURNAL/ LOCAL AFTER JOURNAL/ NOT LOCAL AFTER JOURNAL T X X X DEFAULT JOURNAL TABLE T X X X CREATE FUNCTION A, T X X X Options RETURNS data_type/ RETURNS data_type CAST FROM data_type A X X X LANGUAGE C/ A X X X LANGUAGE CPP A X X X NO SQL A X X X SPECIFIC [database_name.] function_name A X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 230 SQL Reference: Fundamentals CREATE FUNCTION, continued Options CLASS AGGREGATE/ CLASS AG T X X X PARAMETER STYLE SQL/ PARAMETER STYLE TD_GENERAL A X X X DETERMINISTIC/ NOT DETERMINISTIC A X X X CALLED ON NULL INPUT/ RETURNS NULL ON NULL INPUT A X X X EXTERNAL/ EXTERNAL NAME function_name/ EXTERNAL NAME function_name PARAMETER STYLE SQL/ EXTERNAL NAME function_name PARAMETER STYLE TD_GENERAL/ EXTERNAL PARAMETER STYLE SQL/ EXTERNAL PARAMETER STYLE TD_GENERAL/ EXTERNAL NAME '[F delimiter function_name] [D] [SI delimiter name delimiter include_name] [CI delimiter name delimiter include_name] [SL delimiter library_name] [SO delimiter name delimiter object_name ] [CO delimiter name delimiter object_name] [SP delimiter package_name] [SS delimiter name delimiter source_name] [CS delimiter name delimiter source_name]' A X X X EXTERNAL SECURITY DEFINER/ EXTERNAL SECURITY DEFINER authorization_name/ EXTERNAL SECURITY INVOKER A X X CREATE FUNCTION (table function form) T X X X Options RETURNS TABLE ( column_name data_type [ … , column_name data_type ] ) T X X X LANGUAGE C/ LANGUAGE CPP T X X X NO SQL T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 231 CREATE FUNCTION (table function form), continued Options SPECIFIC [database_name.] function_name T X X X PARAMETER STYLE SQL T X X X DETERMINISTIC/ NOT DETERMINISTIC T X X X CALLED ON NULL INPUT/ RETURNS NULL ON NULL INPUT T X X X EXTERNAL/ EXTERNAL NAME function_name/ EXTERNAL NAME function_name PARAMETER STYLE SQL/ EXTERNAL PARAMETER STYLE SQL/ EXTERNAL NAME '[F delimiter function_name] [D] [SI delimiter name delimiter include_name] [CI delimiter name delimiter include_name] [SL delimiter library_name] [SO delimiter name delimiter object_name ] [CO delimiter name delimiter object_name] [SP delimiter package_name] [SS delimiter name delimiter source_name] [CS delimiter name delimiter source_name]' T X X X EXTERNAL SECURITY DEFINER/ EXTERNAL SECURITY DEFINER authorization_name/ EXTERNAL SECURITY INVOKER T X X CREATE HASH INDEX T X X X Options FALLBACK PROTECTION/ NO FALLBACK PROTECTION T X X X ORDER BY VALUES/ ORDER BY HASH T X X X CHECKSUM = DEFAULT/ CHECKSUM = NONE/ CHECKSUM = LOW/ CHECKSUM = MEDIUM/ CHECKSUM = HIGH/ CHECKSUM = ALL T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 232 SQL Reference: Fundamentals CREATE INDEX CREATE UNIQUE INDEX T X X X Options ALL T X X X ORDER BY VALUES/ ORDER BY HASH T X X X TEMPORARY T X X X CREATE JOIN INDEX T X X X Options FALLBACK PROTECTION/ NO FALLBACK PROTECTION T X X X CHECKSUM = DEFAULT/ CHECKSUM = NONE/ CHECKSUM = LOW/ CHECKSUM = MEDIUM/ CHECKSUM = HIGH/ CHECKSUM = ALL T X X X ROWID T X X X EXTRACT YEAR FROM/ EXTRACT MONTH FROM T X X X SUM numeric_expression T X X X COUNT column_expression T X X X FROM table_name/ FROM table_name correlation_name/ FROM table_name AS correlation_name T X X X FROM (joined_table) T X X X FROM table JOIN table/ FROM table INNER JOIN table/ FROM table LEFT JOIN table/ FROM table LEFT OUTER JOIN table/ FROM table RIGHT JOIN table/ FROM table RIGHT OUTER JOIN table T X X X |WHERE statement modifier| A X X X |GROUP BY statement modifier| T X X X |ORDER BY statement modifier| A, T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 233 CREATE JOIN INDEX, continued Options INDEX [index_name] [ALL] (column_list)/ INDEX [index_name] [ALL] (column_list) ORDER BY HASH [(column_name)]/ INDEX [index_name] [ALL] (column_list) ORDER BY VALUES [(column_name)]/ UNIQUE INDEX [index_name] (column_list)/ PRIMARY INDEX [index_name] (column_list)/ T X X X PRIMARY INDEX [index_name] (column_list) PARTITION BY expression T X CREATE MACRO/ CM T X X X Options AS statement T X X X USING modifier T X X X |LOCKING statement modifier| T X X X CREATE METHOD CREATE INSTANCE METHOD CREATE CONSTRUCTOR METHOD A X X Options EXTERNAL/ EXTERNAL NAME method_name/ EXTERNAL NAME '[F delimiter function_entry_name] [D] [SI delimiter name delimiter include_name] [CI delimiter name delimiter include_name] [SL delimiter library_name] [SO delimiter name delimiter object_name ] [CO delimiter name delimiter object_name] [SP delimiter package_name] [SS delimiter name delimiter source_name] [CS delimiter name delimiter source_name]' A X X EXTERNAL SECURITY DEFINER/ EXTERNAL SECURITY DEFINER authorization_name/ EXTERNAL SECURITY INVOKER T X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 234 SQL Reference: Fundamentals CREATE ORDERING A X X Options MAP WITH SPECIFIC METHOD specific_method_name/ MAP WITH METHOD method_name/ MAP WITH INSTANCE METHOD method_name/ MAP WITH SPECIFIC FUNCTION specific_function_name/ MAP WITH FUNCTION function_name A X X CREATE PROCEDURE (external stored procedure form) A X X X Options parameter_name data_type/ IN parameter_name data_type/ OUT parameter_name data_type/ INOUT parameter_name data_type A X X X LANGUAGE C/ LANGUAGE CPP A X X X NO SQL A X X X PARAMETER STYLE SQL/ PARAMETER STYLE TD_GENERAL A X X X EXTERNAL/ EXTERNAL NAME procedure_name/ EXTERNAL NAME procedure_name PARAMETER STYLE SQL/ EXTERNAL NAME procedure_name PARAMETER STYLE TD_GENERAL/ EXTERNAL PARAMETER STYLE SQL/ EXTERNAL PARAMETER STYLE TD_GENERAL/ EXTERNAL NAME '[F delimiter function_entry_name] [D] [SI delimiter name delimiter include_name] [CI delimiter name delimiter include_name] [SL delimiter library_name] [SO delimiter name delimiter object_name ] [CO delimiter name delimiter object_name] [SP delimiter package_name] [SS delimiter name delimiter source_name] [CS delimiter name delimiter source_name]' A X X X EXTERNAL SECURITY DEFINER/ EXTERNAL SECURITY DEFINER authorization_name/ EXTERNAL SECURITY INVOKER A X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 235 CREATE PROCEDURE (stored procedure form) A, T X X X Options parameter_name data_type/ IN parameter_name data_type/ OUT parameter_name data_type/ INOUT parameter_name data_type A X X X NOT ATOMIC T X X X DECLARE variable-name data-type [DEFAULT literal] DECLARE variable-name data-type [DEFAULT NULL] A X X X DECLARE cursor_name [SCROLL] CURSOR FOR cursor_specification [FOR READ ONLY]/ DECLARE cursor_name [SCROLL] CURSOR FOR cursor_specification [FOR UPDATE]/ DECLARE cursor_name [NO SCROLL] CURSOR FOR cursor_specification [FOR READ ONLY]/ DECLARE cursor_name [NO SCROLL] CURSOR FOR cursor_specification [FOR UPDATE]/ A X X X DECLARE CONTINUE HANDLER DECLARE EXIT HANDLER A X X X FOR SQLSTATE sqlstate/ FOR SQLSTATE VALUE sqlstate A X X X FOR SQLEXCEPTION/ FOR SQLWARNING/ FOR NOT FOUND A X X X SET assignment_target = assignment_source A X X X IF expression THEN statement [ELSEIF expression THEN statement] [ELSE statement] END IF A X X X CASE operand1 WHEN operand2 THEN statement [ELSE statement] END CASE A X X X CASE WHEN expression THEN statement [ELSE statement] END CASE A X X X ITERATE label_name A X X X LEAVE label_name A X X X PRINT string_literal/ PRINT print_variable_name T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 236 SQL Reference: Fundamentals CREATE PROCEDURE, continued Options SQL_statement A X X X CALL procedure_name A X X X OPEN cursor_name A X X X CLOSE cursor_name A X X X FETCH [[NEXT] FROM] cursor_name INTO local_variable_name [ … , local_variable_name]/ FETCH [[FIRST] FROM] cursor_name INTO local_variable_name [ … , local_variable_name]/ FETCH [[NEXT] FROM] cursor_name INTO parameter_reference [ … , parameter_reference]/ FETCH [[FIRST] FROM] cursor_name INTO parameter_reference [ … , parameter_reference] A X X X WHILE expression DO statement END WHILE A X X X LOOP statement END LOOP A X X X FOR for_loop_variable AS [cursor_name CURSOR FOR] SELECT column_name [AS correlation_name] FROM table_name [WHERE clause] [SELECT clause] DO statement_list END FOR/ FOR for_loop_variable AS [cursor_name CURSOR FOR] SELECT expression [AS correlation_name] FROM table_name [WHERE clause] [SELECT clause] DO statement_list END FOR A X X X REPEAT statement_list UNTIL conditional_expression END REPEAT A X X X CREATE PROFILE T X X X Options ACCOUNT = ‘account_id’/ ACCOUNT = (‘account_id’ [ … ,’account_id’])/ ACCOUNT = NULL T X X X DEFAULT DATABASE = database_name/ DEFAULT DATABASE = NULL T X X X SPOOL = n [BYTES]/ SPOOL = NULL T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 237 CREATE PROFILE, continued Options TEMPORARY = n [BYTES]/ TEMPORARY = NULL T X X X PASSWORD [ATTRIBUTES] = ( EXPIRE = n, EXPIRE = NULL, MINCHAR = n, MINCHAR = NULL, MAXCHAR = n, MAXCHAR = NULL, DIGITS = n, DIGITS = NULL, SPECCHAR = c, SPECCHAR = NULL, MAXLOGONATTEMPTS = n, MAXLOGONATTEMPTS = NULL, LOCKEDUSEREXPIRE = n, LOCKEDUSEREXPIRE = NULL, REUSE = n, REUSE = NULL) PASSWORD [ATTRIBUTES] = NULL T X X X CREATE REPLICATION GROUP A X X X CREATE ROLE A X X X CREATE TABLE/ CT A, T X X X Options SET/ MULTISET T X X X GLOBAL TEMPORARY A X X X GLOBAL TEMPORARY TRACE T X X X VOLATILE T X X X QUEUE T X X X FALLBACK [PROTECTION]/ NO FALLBACK [PROTECTION] T X X X WITH JOURNAL TABLE = name T X X X LOG/ NO LOG T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 238 SQL Reference: Fundamentals CREATE TABLE, continued Options [BEFORE] JOURNAL/ NO [BEFORE] JOURNAL/ DUAL [BEFORE] JOURNAL/ T X X X AFTER JOURNAL/ NO AFTER JOURNAL/ DUAL AFTER JOURNAL/ LOCAL JOURNAL/ NOT LOCAL JOURNAL T X X X FREESPACE = integer PERCENT T X X X DATABLOCKSIZE = integer/ DATABLOCKSIZE = integer BYTES/ DATABLOCKSIZE = integer KBYTES/ DATABLOCKSIZE = integer KILOBYTES T X X X MINIMUM DATABLOCKSIZE/ MAXIMUM DATABLOCKSIZE T X X X CHECKSUM = DEFAULT/ CHECKSUM = NONE/ CHECKSUM = LOW/ CHECKSUM = MEDIUM/ CHECKSUM = HIGH/ CHECKSUM = ALL T X X X QUEUE/ NO QUEUE T X X X column_name |Data Type| |Data Type Attributes| A X X X column_name |Data Type| |Column Storage Attributes| T X X X column_name |Data Type| |Column Constraint Attributes| A X X X GENERATED ALWAYS AS IDENTITY/ GENERATED BY DEFAULT AS IDENTITY A X X X |Column Constraint Attributes| T X X X |Table Constraint Attributes| T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 239 CREATE TABLE, continued Options [UNIQUE] [PRIMARY] INDEX [name] [ALL] (column_name) T X X X [UNIQUE] PRIMARY INDEX [name] (column) PARTITION BY expression T X X X INDEX [name] [ALL] (column_name) ORDER BY VALUES (name)/ INDEX [name] [ALL] (column_name) ORDER BY HASH (name) T X X X ON COMMIT DELETE ROWS/ ON COMMIT PRESERVE ROWS A X X X AS source_table_name WITH [NO] DATA/ A X X X AS source_table_name WITH [NO] DATA AND [NO] STATISTICS/ AS source_table_name WITH [NO] DATA AND [NO] STATS/ AS source_table_name WITH [NO] DATA AND [NO] STAT/ T X AS (query_expression) WITH [NO] DATA/ A X X X AS (query_expression) WITH [NO] DATA AND [NO] STATISTICS/ AS (query_expression) WITH [NO] DATA AND [NO] STATS/ AS (query_expression) WITH [NO] DATA AND [NO] STAT T X CREATE TRANSFORM A X X Options TO SQL WITH SPECIFIC METHOD specific_method_name/ TO SQL WITH METHOD method_name/ TO SQL WITH INSTANCE METHOD method_name/ TO SQL WITH SPECIFIC FUNCTION specific_function_name/ TO SQL WITH FUNCTION function_name A X X FROM SQL WITH SPECIFIC METHOD specific_method_name/ FROM SQL WITH METHOD method_name/ FROM SQL WITH INSTANCE METHOD method_name/ FROM SQL WITH SPECIFIC FUNCTION specific_function_name/ FROM SQL WITH FUNCTION function_name A X X CREATE TRIGGER A, T X X X Options ENABLED/ DISABLED T X X X BEFORE/ AFTER A X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 240 SQL Reference: Fundamentals CREATE TRIGGER, continued Options INSERT ON table_name [ORDER integer]/ DELETE ON table_name [ORDER integer]/ UPDATE [OF (column_list)] ON table_name [ORDER integer] A X X X REFERENCING OLD_TABLE [AS] identifier [NEW_TABLE [AS] identifier]/ T X X X REFERENCING OLD [AS] identifier [NEW [AS] identifier]/ REFERENCING OLD TABLE [AS] identifier [NEW TABLE [AS] identifier]/ REFERENCING OLD [ROW] [AS] identifier [NEW [ROW] [AS] identifier] A X X X FOR EACH ROW/ FOR EACH STATEMENT A X X X WHEN (search_condition) A X X X (SQL_proc_statement ;)/ SQL_proc_statement / BEGIN ATOMIC (SQL_proc_statement;) END/ BEGIN ATOMIC SQL_proc_statement ; END A,T X X X CREATE TYPE (distinct form) A, T X X Options CHARACTER SET server_character_set T X X METHOD [SYSUDTLIB.]method_name/ INSTANCE METHOD [SYSUDTLIB.]method_name A, T X X RETURNS predefined_data_type/ RETURNS predefined_data_type AS LOCATOR/ RETURNS predefined_data_type [AS LOCATOR] CAST FROM predefined_data_type [AS LOCATOR]/ RETURNS predefined_data_type CAST FROM [SYSUDTLIB.]UDT_name [AS LOCATOR]/ RETURNS [SYSUDTLIB.]UDT_name/ RETURNS [SYSUDTLIB.]UDT_name AS LOCATOR/ RETURNS [SYSUDTLIB.]UDT_name [AS LOCATOR] CAST FROM predefined_data_type [AS LOCATOR]/ RETURNS [SYSUDTLIB.]UDT_name CAST FROM [SYSUDTLIB.]UDT_name [AS LOCATOR] A, T X X LANGUAGE C/ LANGUAGE CPP A X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 241 CREATE TYPE (distinct form), continued Options NO SQL A X X SPECIFIC [SYSUDTLIB.] specific_method_name A, T X X SELF AS RESULT A X X PARAMETER STYLE SQL/ PARAMETER STYLE TD_GENERAL A X X DETERMINISTIC/ NOT DETERMINISTIC A X X CALLED ON NULL INPUT/ RETURNS NULL ON NULL INPUT A X X CREATE TYPE (structured form) A, T X X Options AS (attribute_name predefined_data_type)/ AS (attribute_name predefined_data_type CHARACTER SET server_character_set)/ AS (attribute_name predefined_data_type [CHARACTER SET server_character_set] […, attribute_name predefined_data_type [CHARACTER SET server_character_set]] […, attribute_name UDT_name])/ AS (attribute_name predefined_data_type [CHARACTER SET server_character_set] […, attribute_name UDT_name] […, attribute_name predefined_data_type [CHARACTER SET server_character_set]])/ AS (attribute_name UDT_name)/ AS (attribute_name UDT_name […, attribute_name UDT_name] […, attribute_name predefined_data_type [CHARACTER SET server_character_set]])/ AS (attribute_name UDT_name […, attribute_name predefined_data_type [CHARACTER SET server_character_set]] […, attribute_name UDT_name]) A X X INSTANTIABLE A X X METHOD [SYSUDTLIB.]method_name/ INSTANCE METHOD [SYSUDTLIB.]method_name CONSTRUCTOR METHOD [SYSUDTLIB.]method_name A, T X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 242 SQL Reference: Fundamentals CREATE TYPE (structured form), continued Options RETURNS predefined_data_type/ RETURNS predefined_data_type AS LOCATOR/ RETURNS predefined_data_type [AS LOCATOR] CAST FROM predefined_data_type [AS LOCATOR]/ RETURNS predefined_data_type CAST FROM [SYSUDTLIB.]UDT_name [AS LOCATOR]/ RETURNS [SYSUDTLIB.]UDT_name/ RETURNS [SYSUDTLIB.]UDT_name AS LOCATOR/ RETURNS [SYSUDTLIB.]UDT_name [AS LOCATOR] CAST FROM predefined_data_type [AS LOCATOR]/ RETURNS [SYSUDTLIB.]UDT_name CAST FROM [SYSUDTLIB.]UDT_name [AS LOCATOR] A, T X X LANGUAGE C/ LANGUAGE CPP A X X NO SQL A X X SPECIFIC [SYSUDTLIB.] specific_method_name A, T X X SELF AS RESULT A X X PARAMETER STYLE SQL/ PARAMETER STYLE TD_GENERAL A X X DETERMINISTIC/ NOT DETERMINISTIC A X X CALLED ON NULL INPUT/ RETURNS NULL ON NULL INPUT A X X CREATE USER T X X X Options FROM database_name T X X X PERMANENT = number [BYTES]/ PERM = number [BYTES] T X X X PASSWORD = password/ PASSWORD = NULL T X X X STARTUP = ‘string;’ T X X X TEMPORARY = n [bytes] T X X X SPOOL = n [BYTES] T X X X DEFAULT DATABASE = database_name T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 243 CREATE USER, continued Options COLLATION = collation_sequence T X X X ACCOUNT = ‘acct_ID’/ ACCOUNT = (‘acct_ID’ [ … ,’acct_ID’]) T X X X [NO] FALLBACK [PROTECTION] T X X X [BEFORE] JOURNAL/ NO [BEFORE] JOURNAL/ DUAL [BEFORE] JOURNAL T X X X AFTER JOURNAL/ NO AFTER JOURNAL/ DUAL AFTER JOURNAL/ LOCAL AFTER JOURNAL/ NOT LOCAL AFTER JOURNAL T X X X DEFAULT JOURNAL TABLE = table_name T X X X TIME ZONE = LOCAL/ TIME ZONE = [sign] quotestring/ TIME ZONE = NULL T X X X DATEFORM = INTEGERDATE/ DATEFORM = ANSIDATE T X X X DEFAULT CHARACTER SET data_type T X X X DEFAULT ROLE = role_name/ DEFAULT ROLE = NONE/ DEFAULT ROLE = NULL/ DEFAULT ROLE = ALL T X X X PROFILE = profile_name/ PROFILE = NULL T X X X CREATE VIEW A, T X X X Options (column_name [ … , column_name]) A X X X AS [ |LOCKING statement modifier| ] query_expression A, T X X X WITH CHECK OPTION A X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 244 SQL Reference: Fundamentals CREATE RECURSIVE VIEW A X X X Options (column_name [ … , column_name]) A X X X AS (seed_statement [UNION ALL recursive_statement)] [ … [UNION ALL seed_statement] [ … UNION ALL recursive_statement]) A X X X DATABASE T X X X DECLARE CURSOR (selection form) A, T X X X Options FOR SELECT A X X X FOR COMMENT/ FOR EXPLAIN/ FOR HELP/ FOR SHOW T X X X DECLARE CURSOR (request form) A X X X Options FOR 'request_specification' A X X X DECLARE CURSOR (macro form) T X X X Options FOR EXEC macro_name T X X X DECLARE CURSOR (dynamic SQL form) A X X X Options FOR statement_name A X X X DECLARE STATEMENT T X X X DECLARE TABLE T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 245 DELETE (basic/searched form)/ DEL A, T X X X Options [FROM] table_name A X X X [AS] alias_name A X X X WHERE condition A X X X ALL T X X X DELETE (implied join condition form)/ DEL A, T X X X Options delete_table_name T X X X [FROM] table_name [ … ,[FROM] table_name] T X X X [AS] alias_name A X X X WHERE condition A X X X ALL T X X X DELETE (positioned form)/ DEL A X X X Options FROM table_name A X X X WHERE CURRENT OF cursor_name A X X X DELETE DATABASE DELETE USER T X X X Option ALL T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 246 SQL Reference: Fundamentals DESCRIBE T X X X Options INTO descriptor_area T X X X USING NAMES/ USING ANY/ USING BOTH/ USING LABELS T X X X FOR STATEMENT statement_number/ FOR STATEMENT [:] num_var T X X X DIAGNOSTIC "validate index" T X X X Option ON/ NOT ON T X X X DIAGNOSTIC DUMP SAMPLES T X X X DIAGNOSTIC HELP SAMPLES T X X X DIAGNOSTIC SET SAMPLES T X X X Options ON/ NOT ON T X X X FOR SESSION/ FOR SYSTEM T X X X DROP AUTHORIZATION T X X DROP CAST A X X DROP DATABASE DROP USER T X X X DROP FUNCTION DROP SPECIFIC FUNCTION A X X X DROP HASH INDEX T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 247 DROP INDEX T X X X Options TEMPORARY T X X X ORDER BY (column_name)/ ORDER BY VALUES (column_name)/ ORDER BY HASH (column_name) T X X X DROP JOIN INDEX T X X X DROP MACRO T X X X DROP ORDERING A X X DROP PROCEDURE A X X X DROP PROFILE T X X X DROP REPLICATION GROUP T X X X DROP ROLE A X X X DROP STATISTICS/ DROP STATS/ DROP STAT (optimizer form) T X X X Options [FOR] [UNIQUE] INDEX index_name/ [FOR] [UNIQUE] INDEX [index_name] (col_name) [ORDER BY col_name]/ [FOR] [UNIQUE] INDEX [index_name] (col_name) [ORDER BY VALUES (col_name)]/ [FOR] [UNIQUE] INDEX [index_name] (col_name) [ORDER BY HASH (col_name)]/ [FOR] COLUMN column_name/ [FOR] COLUMN (column_name [ … , column_name])/ T X X X [FOR] COLUMN (column_name [ … , column_name], PARTITION [ … , column_name])/ [FOR] COLUMN (PARTITION [ … , column_name])/ [FOR] COLUMN PARTITION T X X ON T X X X TEMPORARY T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 248 SQL Reference: Fundamentals Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0 DROP STATISTICS/ DROP STATS/ DROP STAT (QCD form) T X X X Options INDEX (column_name [ … , column_name])/ INDEX index_name/ COLUMN (column_name [ … ,column_name])/ COLUMN column_name/ T X X X COLUMN (column_name [ … , column_name], PARTITION [ … , column_name])/ COLUMN (PARTITION [ … , column_name])/ COLUMN PARTITION T X X DROP TABLE A, T X X X Options TEMPORARY A X X X ALL A X X X OVERRIDE A X X X DROP TRANSFORM A X X DROP TRIGGER T X X X DROP TYPE A X X DROP VIEW T X X X DUMP EXPLAIN T X X X Options AS query_plan_name T X X X LIMIT/ LIMIT SQL/ LIMIT SQL = n T X X X CHECK STATISTICS T X X ECHO T X X X END DECLARE SECTION T X X X END-EXEC A X X XAppendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 249 END LOGGING T X X X Options DENIALS T X X X WITH TEXT T X X X ALL/ operation/ GRANT T X X X BY database_name T X X X ON DATABASE name/ ON FUNCTION/ ON MACRO name/ ON PROCEDURE name/ ON TABLE name/ ON TRIGGER name/ ON USER name/ ON VIEW name T X X X END QUERY LOGGING T X X X Options ON ALL/ ON user_name/ ON user_name ACCOUNT = 'account_name'/ ON user_name ACCOUNT = ('account_name' [ … ,'account_name']) T X X X END TRANSACTION/ ET T X X X EXECUTE macro_name/ EXEC macro_name T X X X EXECUTE statement_name A X X X Options USING [:] host_variable_name A X X X [INDICATOR] :host_indicator_name A X X X USING DESCRIPTOR [:] descriptor_area A X X X EXECUTE IMMEDIATE A X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 250 SQL Reference: Fundamentals FETCH A X X X Options INTO [:] host_variable_name A X X X [INDICATOR] :host_indicator_name A X X X USING DESCRIPTOR [:] descriptor_area A X X X GET CRASH (embedded SQL) T X X X GIVE T X X X Options database_name TO recipient_name/ user_name TO recipient_name T X X X GRANT A, T X X X Options ALL/ A X X X ALL PRIVILEGES/ ALL BUT T X X X DELETE/ EXECUTE/ INSERT/ REFERENCES/ SELECT/ UPDATE/ A X X X ALTER/ CHECKPOINT/ CREATE/ DROP/ DUMP/ INDEX/ RESTORE/ T X X X REPLCONTROL/ T X X X UDTMETHOD/ UDTTYPE/ UDTUSAGE T X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 251 GRANT, continued Options ON database_name/ ON database_name.object_name/ ON object_name/ ON PROCEDURE identifier/ ON SPECIFIC FUNCTION specific_function_name/ ON FUNCTION function_name/ A X X X ON TYPE UDT_name/ ON TYPE SYSUDTLIB.UDT_name A X X TO user_name/ TO ALL user_name/ T X X X TO PUBLIC A X X X WITH GRANT OPTION A X X X GRANT LOGON T X X X Options ON host_id/ ON ALL T X X X AS DEFAULT/ TO database_name/ FROM database_name T X X X WITH NULL PASSWORD T X X X GRANT MONITOR/ GRANT monitor_privilege T X X X Options PRIVILEGES/ BUT NOT monitor_privilege T X X X TO [ALL] user_name/ TO PUBLIC T X X X WITH GRANT OPTION T X X X GRANT ROLE A X X X Options WITH ADMIN OPTION A X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 252 SQL Reference: Fundamentals HELP T X X X Options CAST [database_name.] UDT_name/ CAST [database_name.] UDT_name SOURCE/ CAST [database_name.] UDT_name TARGET T X X COLUMN column_name FROM table_name/ COLUMN * FROM table_name/ COLUMN table_name.column_name/ COLUMN table_name.*/ COLUMN expression T X X X CONSTRAINT [database_name.] table_name.name T X X X DATABASE database_name T X X X FUNCTION function_name [(data_type [ … , data_type])]/ SPECIFIC FUNCTION specific_function_name T X X X HASH INDEX hash_index_name T X X X [TEMPORARY] INDEX table_name [(column_name)]/ [TEMPORARY] INDEX join_index_name [(column_name)] T X X X JOIN INDEX join_index_name T X X X MACRO macro_name T X X X METHOD [database_name.] method_name/ INSTANCE METHOD [database_name.] method_name/ CONSTRUCTOR METHOD [database_name.] method_name/ SPECIFIC METHOD [database_name.] specific_method_name T X X PROCEDURE [database_name.] procedure_name/ PROCEDURE [database_name.] procedure_name ATTRIBUTES/ PROCEDURE [database_name.] procedure_name ATTR/ PROCEDURE [database_name.] procedure_name ATTRS T X X X REPLICATION GROUP T X X X SESSION T X X X TABLE table_name/ TABLE join_index_name T X X X TRANSFORM [database_name.] UDT_name T X X TRIGGER [database_name.] trigger_name/ TRIGGER [database_name.] table_name T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 253 HELP, continued Options TYPE [database_name.] UDT_name/ TYPE [database_name.] UDT_name ATTRIBUTE/ TYPE [database_name.] UDT_name METHOD T X X USER user_name T X X X VIEW view_name T X X X VOLATILE TABLE T X X X HELP STATISTICS/ HELP STATS/ HELP STAT (optimizer form) T X X X Option INDEX (column_name [ … , column_name])/ INDEX index_name/ COLUMN (column_name [ … ,column_name])/ COLUMN column_name/ T X X X COLUMN (column_name [ … , column_name], PARTITION [ … , column_name])/ COLUMN (PARTITION [ … , column_name])/ COLUMN PARTITION T X X HELP STATISTICS/ HELP STATS/ HELP STAT (QCD form) T X X X Options INDEX (column_name [ … , column_name])/ INDEX index_name/ COLUMN (column_name [ … ,column_name])/ COLUMN column_name/ T X X X COLUMN (column_name [ … , column_name], PARTITION [ … , column_name])/ COLUMN (PARTITION [ … , column_name])/ COLUMN PARTITION T X X FOR QUERY query_ID T X X SAMPLEID statistics_ID T X X UPDATE MODIFIED T X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 254 SQL Reference: Fundamentals INCLUDE A X X X INCLUDE SQLCA T X X X INCLUDE SQLDA T X X X INITIATE INDEX ANALYSIS T X X X Options ON table_name [ … , table_name] T X X X SET IndexesPerTable = value [, SearchSpace = value] [, ChangeRate = value] [, ColumnsPerIndex = value] T X X X [, JoinIndexesPerTable = value] [, ColumnsPerJoinIndex = value] [, IndexMaintMode = value] T X X KEEP INDEX T X X X USE MODIFIED STATISTICS/ USE MODIFIED STATS/ USE MODIFIED STAT T X X X WITH INDEX TYPE number/ WITH INDEX TYPE number [ … , number]/ WITH NO INDEX TYPE number/ WITH NO INDEX TYPE number [ … , number] T X X X CHECKPOINT checkpoint_trigger T X X X INSERT/ INS A T X X X Options [VALUES] (expression [ … , expression]) A X X X (column_name [ … , column_name]) VALUES (expression [ … , expression]) A X X X [(column_name [ … , column_name])] subquery A X X X DEFAULT VALUES A X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 255 INSERT EXPLAIN T X X X Options WITH [NO] STATISTICS T X X X AND DEMOGRAPHICS T X X X USING SAMPLE percentage/ USING SAMPLE percentage PERCENT T X X FOR table_name [ … , table_name] T X X X AS query_plan_name T X X X LIMIT/ LIMIT SQL/ LIMIT SQL = n T X X X FOR frequency T X X X LOGOFF (embedded SQL) T X X X Options CURRENT/ ALL/ connection_name/ :host_variable_name T X X X LOGON (embedded SQL) T X X X Options AS connection_name/ AS :namevar T X X X MERGE A X X X Options INTO A X X X AS correlation_name A X X X VALUES using_expression/ (subquery) A X X X ON match_condition A X X X WHEN MATCHED THEN UPDATE SET/ WHEN NOT MATCHED THEN INSERT A X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 256 SQL Reference: Fundamentals MODIFY DATABASE T X X X Options PERMANENT = number [BYTES]/ PERM = number [BYTES] T X X X TEMPORARY = number [bytes] T X X X SPOOL = number [BYTES] T X X X ACCOUNT = ‘account_ID’ T X X X [NO] FALLBACK [PROTECTION] T X X X [BEFORE] JOURNAL/ NO [BEFORE] JOURNAL/ DUAL [BEFORE] JOURNAL T X X X AFTER JOURNAL/ NO AFTER JOURNAL/ DUAL AFTER JOURNAL/ LOCAL AFTER JOURNAL/ NOT LOCAL AFTER JOURNAL T X X X DEFAULT JOURNAL TABLE = table_name T X X X DROP DEFAULT JOURNAL TABLE [= table_name] T X X X MODIFY PROFILE T X X X Options ACCOUNT = ‘account_id’/ ACCOUNT = (‘account_id’ [ … ,’account_id’])/ ACCOUNT = NULL T X X X DEFAULT DATABASE = database_name/ DEFAULT DATABASE = NULL T X X X SPOOL = n [BYTES]/ SPOOL = NULL T X X X TEMPORARY = n [BYTES]/ TEMPORARY = NULL T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 257 MODIFY PROFILE, continued Options PASSWORD [ATTRIBUTES] = ( EXPIRE = n, EXPIRE = NULL, MINCHAR = n, MINCHAR = NULL, MAXCHAR = n, MAXCHAR = NULL, DIGITS = n, DIGITS = NULL, SPECCHAR = c, SPECCHAR = NULL, MAXLOGONATTEMPTS = n, MAXLOGONATTEMPTS = NULL, LOCKEDUSEREXPIRE = n, LOCKEDUSEREXPIRE = NULL, REUSE = n, REUSE = NULL) PASSWORD [ATTRIBUTES] = NULL T X X X MODIFY USER T X X X Options PERMANENT = number [BYTES]/ PERM = number [BYTES] T X X X PASSWORD = password [FOR USER] T X X X STARTUP = ‘string;’/ STARTUP = NULL T X X X RELEASE PASSWORD LOCK T X X X TEMPORARY = n [bytes] T X X X SPOOL = n [BYTES] T X X X ACCOUNT = ‘acct_ID’ ACCOUNT = (‘acct_ID’ [ … ,’acct_ID’]) T X X X DEFAULT DATABASE = database_name T X X X COLLATION = collation_sequence T X X X [NO] FALLBACK [PROTECTION] T X X X [BEFORE] JOURNAL/ NO [BEFORE] JOURNAL/ DUAL [BEFORE] JOURNAL T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 258 SQL Reference: Fundamentals MODIFY USER, continued Options AFTER JOURNAL/ NO AFTER JOURNAL/ DUAL AFTER JOURNAL/ LOCAL AFTER JOURNAL/ NOT LOCAL AFTER JOURNAL T X X X DEFAULT JOURNAL TABLE = table_name T X X X DROP DEFAULT JOURNAL TABLE [= table_name] T X X X TIME ZONE = LOCAL/ TIME ZONE = [sign] quotestring/ TIME ZONE = NULL T X X X DATEFORM = INTEGERDATE/ DATEFORM = ANSIDATE T X X X DEFAULT CHARACTER SET data_type T X X X DEFAULT ROLE T X X X PROFILE T X X X OPEN A X X X Options USING [:] host_variable_name A X X X [INDICATOR] :host_indicator_name A X X X USING DESCRIPTOR [:] descriptor_area A X X X POSITION A X X X Options TO NEXT/ TO [STATEMENT] statement_number/ TO [STATEMENT] [:] numvar A X X X PREPARE A X X X Options INTO [:] descriptor_area A X X X USING NAMES/ USING ANY/ USING BOTH/ USING LABELS A X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 259 PREPARE, continued Options FOR STATEMENT statement_number/ FOR STATEMENT [:] numvar A X X X FROM statement_string/ FROM [:] statement_string_var A X X X RENAME FUNCTION T X X X RENAME MACRO T X X X RENAME PROCEDURE T X X X RENAME TABLE T X X X RENAME TRIGGER T X X X RENAME VIEW T X X X REPLACE CAST T X X Options WITH SPECIFIC METHOD specific_method_name/ WITH METHOD method_name/ WITH INSTANCE METHOD method_name/ WITH SPECIFIC FUNCTION specific_function_name/ WITH FUNCTION function_name T X X AS ASSIGNMENT T X X REPLACE FUNCTION T X X X Options RETURNS data_type/ RETURNS data_type CAST FROM data_type A X X X LANGUAGE C/ LANGUAGE CPP A X X X NO SQL A X X X SPECIFIC [database_name.] function_name A X X X CLASS AGGREGATE/ CLASS AG T X X X PARAMETER STYLE SQL/ PARAMETER STYLE TD_GENERAL A X X X DETERMINISTIC/ NOT DETERMINISTIC A X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 260 SQL Reference: Fundamentals REPLACE FUNCTION, continued Options CALLED ON NULL INPUT/ RETURNS NULL ON NULL INPUT A X X X EXTERNAL/ EXTERNAL NAME function_name/ EXTERNAL NAME function_name PARAMETER STYLE SQL/ EXTERNAL NAME function_name PARAMETER STYLE TD_GENERAL/ EXTERNAL PARAMETER STYLE SQL/ EXTERNAL PARAMETER STYLE TD_GENERAL/ EXTERNAL NAME '[F delimiter function_name] [D] [SI delimiter name delimiter include_name] [CI delimiter name delimiter include_name] [SL delimiter library_name] [SO delimiter name delimiter object_name ] [CO delimiter name delimiter object_name] [SP delimiter package_name] [SS delimiter name delimiter source_name] [CS delimiter name delimiter source_name]' A X X X EXTERNAL SECURITY DEFINER/ EXTERNAL SECURITY DEFINER authorization_name/ EXTERNAL SECURITY INVOKER A X X REPLACE FUNCTION (table function form) T X X X Options RETURNS TABLE ( column_name data_type [ … , column_name data_type ] ) T X X X LANGUAGE C/ LANGUAGE CPP T X X X NO SQL T X X X SPECIFIC [database_name.] function_name T X X X PARAMETER STYLE SQL T X X X DETERMINISTIC/ NOT DETERMINISTIC T X X X CALLED ON NULL INPUT/ RETURNS NULL ON NULL INPUT T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 261 REPLACE FUNCTION (table function form), continued Options EXTERNAL/ EXTERNAL NAME function_name/ EXTERNAL NAME function_name PARAMETER STYLE SQL/ EXTERNAL PARAMETER STYLE SQL/ EXTERNAL NAME '[F delimiter function_name] [D] [SI delimiter name delimiter include_name] [CI delimiter name delimiter include_name] [SL delimiter library_name] [SO delimiter name delimiter object_name ] [CO delimiter name delimiter object_name] [SP delimiter package_name] [SS delimiter name delimiter source_name] [CS delimiter name delimiter source_name]' T X X X EXTERNAL SECURITY DEFINER/ EXTERNAL SECURITY DEFINER authorization_name/ EXTERNAL SECURITY INVOKER T X X REPLACE MACRO T X X X Options AS T X X X USING T X X X REPLACE METHOD REPLACE CONSTRUCTOR METHOD REPLACE INSTANCE METHOD REPLACE SPECIFIC METHOD T X X Options parameter_name data_type/ parameter_name UDT_name T X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 262 SQL Reference: Fundamentals REPLACE METHOD, continued Options EXTERNAL/ EXTERNAL NAME method_name/ EXTERNAL NAME '[F delimiter function_entry_name] [D] [SI delimiter name delimiter include_name] [CI delimiter name delimiter include_name] [SL delimiter library_name] [SO delimiter name delimiter object_name ] [CO delimiter name delimiter object_name] [SP delimiter package_name] [SS delimiter name delimiter source_name] [CS delimiter name delimiter source_name]' T X X EXTERNAL SECURITY DEFINER/ EXTERNAL SECURITY DEFINER authorization_name/ EXTERNAL SECURITY INVOKER T X X REPLACE ORDERING A X X Options MAP WITH SPECIFIC METHOD specific_method_name/ MAP WITH METHOD method_name/ MAP WITH INSTANCE METHOD method_name/ MAP WITH SPECIFIC FUNCTION specific_function_name/ MAP WITH FUNCTION function_name A X X REPLACE PROCEDURE (external stored procedure form) A X X X Options parameter_name data_type/ IN parameter_name data_type/ OUT parameter_name data_type/ INOUT parameter_name data_type A X X X LANGUAGE C/ LANGUAGE CPP A X X X NO SQL A X X X PARAMETER STYLE SQL/ PARAMETER STYLE TD_GENERAL A X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 263 REPLACE PROCEDURE (external stored procedure form), continued Options EXTERNAL/ EXTERNAL NAME procedure_name/ EXTERNAL NAME procedure_name PARAMETER STYLE SQL/ EXTERNAL NAME procedure_name PARAMETER STYLE TD_GENERAL/ EXTERNAL PARAMETER STYLE SQL/ EXTERNAL PARAMETER STYLE TD_GENERAL/ EXTERNAL NAME '[F delimiter function_entry_name] [D] [SI delimiter name delimiter include_name] [CI delimiter name delimiter include_name] [SL delimiter library_name] [SO delimiter name delimiter object_name ] [CO delimiter name delimiter object_name] [SP delimiter package_name] [SS delimiter name delimiter source_name] [CS delimiter name delimiter source_name]' A X X X EXTERNAL SECURITY DEFINER/ EXTERNAL SECURITY DEFINER authorization_name/ EXTERNAL SECURITY INVOKER A X X REPLACE PROCEDURE (stored procedure form) T X X X Options parameter_name data_type/ IN parameter_name data_type/ OUT parameter_name data_type/ INOUT parameter_name data_type T X X X NOT ATOMIC T X X X DECLARE variable-name data-type [DEFAULT literal] DECLARE variable-name data-type [DEFAULT NULL] T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 264 SQL Reference: Fundamentals REPLACE PROCEDURE (stored procedure form), continued Options DECLARE cursor_name [SCROLL] CURSOR FOR cursor_specification [FOR READ ONLY]/ DECLARE cursor_name [SCROLL] CURSOR FOR cursor_specification [FOR UPDATE]/ DECLARE cursor_name [NO SCROLL] CURSOR FOR cursor_specification [FOR READ ONLY]/ DECLARE cursor_name [NO SCROLL] CURSOR FOR cursor_specification [FOR UPDATE]/ T X X X DECLARE CONTINUE HANDLER/ DECLARE EXIT HANDLER T X X X FOR SQLSTATE sqlstate/ FOR SQLSTATE VALUE sqlstate T X X X FOR SQLEXCEPTION/ FOR SQLWARNING/ FOR NOT FOUND T X X X SET assignment_target = assignment_source T X X X IF expression THEN statement [ELSEIF expression THEN statement] [ELSE statement] END IF T X X X CASE operand1 WHEN operand2 THEN statement [ELSE statement] END CASE T X X X CASE WHEN expression THEN statement [ELSE statement] END CASE T X X X ITERATE label_name T X X X LEAVE label_name T X X X PRINT string_literal/ PRINT print_variable_name T X X X SQL_statement T X X X CALL procedure_name T X X X OPEN cursor_name T X X X CLOSE cursor_name T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 265 REPLACE PROCEDURE (stored procedure form), continued Options FETCH [[NEXT] FROM] cursor_name INTO local_variable_name [ … , local_variable_name]/ FETCH [[FIRST] FROM] cursor_name INTO local_variable_name [ … , local_variable_name]/ FETCH [[NEXT] FROM] cursor_name INTO parameter_reference [ … , parameter_reference]/ FETCH [[FIRST] FROM] cursor_name INTO parameter_reference [ … , parameter_reference] T X X X WHILE expression DO statement END WHILE T X X X LOOP statement END LOOP T X X X FOR for_loop_variable AS [cursor_name CURSOR FOR] SELECT column_name [AS correlation_name] FROM table_name [WHERE clause] [SELECT clause] DO statement_list END FOR/ FOR for_loop_variable AS [cursor_name CURSOR FOR] SELECT expression [AS correlation_name] FROM table_name [WHERE clause] [SELECT clause] DO statement_list END FOR T X X X REPEAT statement_list UNTIL conditional_expression END REPEAT T X X X REPLACE TRANSFORM T X X Options TO SQL WITH SPECIFIC METHODspecific_method_name/ TO SQL WITH METHOD method_name/ TO SQL WITH INSTANCE METHOD method_name/ TO SQL WITH SPECIFIC FUNCTION specific_function_name/ TO SQL WITH FUNCTION function_name T X X FROM SQL WITH SPECIFIC METHOD specific_method_name/ FROM SQL WITH METHOD method_name/ FROM SQL WITH INSTANCE METHOD method_name/ FROM SQL WITH SPECIFIC FUNCTION specific_function_name/ FROM SQL WITH FUNCTION function_name T X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 266 SQL Reference: Fundamentals REPLACE TRIGGER T X X X Options ENABLED/ DISABLED T X X X BEFORE/ AFTER T X X X INSERT/ DELETE/ UPDATE [OF (column_list)] T X X X ORDER integer T X X X REFERENCING OLD_TABLE [AS] identifier [NEW_TABLE [AS] identifier]/ REFERENCING OLD [AS] identifier [NEW [AS] identifier]/ REFERENCING OLD TABLE [AS] identifier [NEW TABLE [AS] identifier]/ REFERENCING OLD [ROW] [AS] identifier [NEW [ROW] [AS] identifier] T X X X FOR EACH ROW/ FOR EACH STATEMENT T X X X WHEN (search_condition) T X X X (SQL_proc_statement ;)/ SQL_proc_statement / BEGIN ATOMIC (SQL_proc_statement;) END/ BEGIN ATOMIC SQL_proc_statement ; END T X X X REPLACE VIEW A, T X X X Options (column_name [ … , column_name]) T X X X AS [ |LOCKING statement modifier| ] query_expression A, T X X X WITH CHECK OPTION A X X X RESTART INDEX ANALYSIS T X X X REVOKE A, T X X X Options GRANT OPTION FOR A X X X ALL/ ALL PRIVILEGES/ ALL BUT operation A X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 267 REVOKE, continued Options DELETE/ INSERT/ SELECT/ REFERENCES/ UPDATE/ A X X X ALTER/ CHECKPOINT/ CREATE/ DROP/ DUMP/ EXECUTE/ INDEX/ RESTORE/ T X X X REPLCONTROL/ T X X X UDTMETHOD/ UDTTYPE/ UDTUSAGE T X X ON database_name/ ON database_name.object_name/ ON object_name/ ON PROCEDURE procedure_name/ ON SPECIFIC FUNCTION specific_function_name/ ON FUNCTION function_name/ A X X X ON TYPE UDT_name/ ON TYPE SYSUDTLIB.UDT_name A X X TO [ALL] user_name/ TO PUBLIC/ FROM [ALL] user_name/ FROM PUBLIC T X X X REVOKE LOGON T X X X Options ON host_id/ ON ALL T X X X AS DEFAULT/ TO database_name/ FROM database_name T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 268 SQL Reference: Fundamentals REVOKE MONITOR/ REVOKE monitor_privilege T X X X Options GRANT OPTION FOR T X X X PRIVILEGES/ BUT NOT monitor_privilege T X X X TO [ALL] user_name/ TO PUBLIC/ FROM [ALL] user_name/ FROM PUBLIC T X X X REVOKE ROLE A X X X Options ADMIN OPTION FOR A X X X REWIND T X X X ROLLBACK A, T X X X Options WORK A X X X WORK RELEASE T X X X 'abort_message' T X X X FROM_clause T X X X WHERE_clause T X X X SELECT/ SEL A, T X X X Options |WITH [RECURSIVE] statement modifier| A X X X DISTINCT/ ALL A X X X TOP integer [WITH TIES]/ TOP integer PERCENT [WITH TIES]/ TOP decimal [WITH TIES]/ TOP decimal PERCENT [WITH TIES] T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 269 SELECT, continued Options */ expression/ expression [AS] alias_name/ table_name.*/ A X X X *.ALL/ table_name.*.ALL/ column_name.ALL T X X SAMPLEID T X X X FROM table_name/ FROM table_name [AS] alias_name/ FROM join_table_name JOIN joined_table ON search_condition/ FROM join_table_name INNER JOIN joined_table ON search_condition/ FROM join_table_name LEFT JOIN joined_table ON search_condition/ FROM join_table_name LEFT OUTER JOIN joined_table ON search_condition/ FROM join_table_name RIGHT JOIN joined_table ON search_condition/ FROM join_table_name RIGHT OUTER JOIN joined_table ON search_condition/ FROM join_table_name FULL JOIN joined_table ON search_condition/ FROM join_table_name FULL OUTER JOIN joined_table ON search_condition/ FROM join_table_name CROSS JOIN/ FROM (subquery) [AS] derived_table_name/ FROM (subquery) [AS] derived_table_name (column_name)/ FROM TABLE (function_name([expression [ … , expression]])) [AS] derived_table_name/ FROM TABLE (function_name([expression [ … , expression]])) [AS] derived_table_name (column_name [ … , column_name]) A X X X |WHERE statement modifier| A X X X |GROUP BY statement modifier| A, T X X X |HAVING statement modifier| A X X X |QUALIFY statement modifier| T X X X |SAMPLE statement modifier| T X X X |ORDER BY statement modifier| A, T X X X |WITH statement modifier| T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 270 SQL Reference: Fundamentals SELECT AND CONSUME TOP 1 T X X X Options FROM queue_table_name T X X X SELECT … INTO/ SEL … INTO A, T X X X Options DISTINCT/ ALL A X X X AND CONSUME TOP 1 T X X X expression/ expression [AS] alias_name A X X X FROM table_name/ FROM table_name [AS] alias_name/ FROM join_table_name JOIN joined_table ON search_condition/ FROM join_table_name INNER JOIN joined_table ON search_condition/ FROM join_table_name LEFT JOIN joined_table ON search_condition/ FROM join_table_name LEFT OUTER JOIN joined_table ON search_condition/ FROM join_table_name RIGHT JOIN joined_table ON search_condition/ FROM join_table_name RIGHT OUTER JOIN joined_table ON search_condition/ FROM join_table_name FULL JOIN joined_table ON search_condition/ FROM join_table_name FULL OUTER JOIN joined_table ON search_condition/ FROM join_table_name CROSS JOIN/ FROM (subquery) [AS] derived_table_name/ FROM (subquery) [AS] derived_table_name (column_name) A X X X |WHERE statement modifier| A X X X SET BUFFERSIZE (embedded SQL) T X X X SET CHARSET (embedded SQL) T X X X SET CONNECTION (embedded SQL) T X X X SET CRASH (embedded SQL) T X X X Options WAIT_NOTELL/ NOWAIT_TELL T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 271 SET ROLE A, T X X X Options role_name/ NONE/ A X X X NULL/ ALL/ EXTERNAL T X X X SET SESSION ACCOUNT/ SS ACCOUNT T X X X Options FOR SESSION/ FOR REQUEST T X X X SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL/ SS CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL A X X Options RU/ READ UNCOMMITTED/ SR/ SERIALIZABLE A X X SET SESSION COLLATION/ SS COLLATION T X X X SET SESSION DATABASE/ SS DATABASE T X X X SET SESSION DATEFORM/ SS DATEFORM T X X X Options ANSIDATE/ INTEGERDATE T X X X SET SESSION FUNCTION TRACE/ SS FUNCTION TRACE T X X X Options OFF/ USING mask FOR TABLE table_name/ USING mask FOR TRACE TABLE table_name T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 272 SQL Reference: Fundamentals SET SESSION OVERRIDE REPLICATION/ SS OVERRIDE REPLICATION T X X X Options OFF/ ON T X X X SET TIME ZONE T X X X Options LOCAL/ INTERVAL offset HOUR TO MINUTE/ USER T X X X SHOW T X X X Options QUALIFIED T X X X SHOW CAST T X SHOW FUNCTION SHOW SPECIFIC FUNCTION T X X X SHOW HASH INDEX T X X X SHOW JOIN INDEX T X X X SHOW MACRO T X X X SHOW METHOD SHOW CONSTRUCTOR METHOD SHOW INSTANCE METHOD SHOW SPECIFIC METHOD T X X SHOW PROCEDURE T X X X SHOW REPLICATION GROUP T X X X SHOW [TEMPORARY] TABLE T X X X SHOW TRIGGER T X X X SHOW TYPE T X X SHOW VIEW T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 273 TEST T X X X Options async_statement_identifier/ :namevar T X X X COMPLETION T X X X UPDATE/ UPD (searched form) A, T X X X Options table_name A X X X [AS] alias_name/ FROM table_name [[AS] alias_name] [ … , table_name [[AS] alias_name]] A, T X X X SET column_name=expression [ … , column_name=expression]/ A X X X SET column_name=expression [ … , column_name=expression] [ … , column_name.mutator_name=expression]/ SET column_name.mutator_name=expression [ … , column_name.mutator_name=expression] [ … , column_name=expression] A X X ALL T X X X |WHERE statement modifier| A X X X UPDATE/ UPD (positioned form) A X X X Options table_name [alias_name] A X X X SET column_name=expression [ … , column_name=expression] A X X X WHERE CURRENT OF cursor_name A X X X UPDATE/ UPD (upsert form) T X X X Options table_name_1 T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 274 SQL Reference: Fundamentals UPDATE (upsert form), continued Options SET column_name=expression [ … , column_name=expression]/ T X X X SET column_name=expression [ … , column_name=expression] [ … , column_name.mutator_name=expression]/ SET column_name.mutator_name=expression [ … , column_name.mutator_name=expression] [ … , column_name=expression] T X X |WHERE statement modifier| T X X X ELSE INSERT [INTO] table_name_2/ ELSE INS [INTO] table_name_2 T X X X [(column_name [ … , column_name])] VALUES (expression)/ DEFAULT VALUES T X X X WAIT T X X X Options async_statement_identifier COMPLETION/ ALL COMPLETION/ ANY COMPLETION INTO [:] stmtvar, [:] sessvar T X X X WHENEVER A, T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers SQL Reference: Fundamentals 275 Request Modifier EXPLAIN T X X X Statement Modifiers ASYNC T X X X EXEC SQL A X X X GROUP BY clause A, T X X X Options CUBE/ GROUPING SETS/ ROLLUP A X X X HAVING clause A X X X LOCKING/ LOCK T X X X Options DATABASE database_name/ TABLE table_name/ VIEW view_name/ ROW T X X X FOR/ IN T X X X ACCESS/ EXCLUSIVE/ EXCL/ SHARE/ WRITE/ CHECKSUM/ READ/ READ OVERRIDE T X X X MODE T X X X NOWAIT T X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Statements and Modifiers 276 SQL Reference: Fundamentals Statement Modifiers, continued ORDER BY clause A, T X X X Options expression T X X X column_name/ column_position A X X X ASC/ DESC A X X X QUALIFY clause T X X X SAMPLE clause T X X X Options WITH REPLACEMENT T X X X RANDOMIZED ALLOCATION T X X X USING row descriptor T X X X Options AS DEFERRED/ AS LOCATOR T X X X WHERE clause A X X X WITH clause T X X X Options expression_1 T X X X BY expression_2 T X X X ASC/ DESC T X X X WITH [RECURSIVE] clause A X X X Options (column_name [ … , column_name]) A X X X AS (seed_statement [UNION ALL recursive_statement)] [ … [UNION ALL seed_statement] [ … UNION ALL recursive_statement]) A X X X Statement ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Data Types and Literals SQL Reference: Fundamentals 277 Data Types and Literals The following list contains all SQL data types and literals for this version and previous versions of Teradata Database. The following type codes appear in the ANSI Compliance column. Code Definition A ANSI T Teradata extension Data Type / Literal ANSI Compliance V2R6.2 V2R6.1 V2R6.0 Data Types BIGINT A X BINARY LARGE OBJECT, BLOB A X X X BYTE T X X X BYTEINT T X X X CHAR, CHARACTER A X X X CHAR VARYING, CHARACTER VARYING A X X X CHARACTER LARGE OBJECT, CLOB A X X X DATE A, T X X X DEC, DECIMAL A X X X DOUBLE PRECISION A X X X FLOAT A X X X GRAPHIC T X X X INT, INTEGER A X X X INTERVAL DAY A X X X INTERVAL DAY TO HOUR A X X X INTERVAL DAY TO MINUTE A X X X INTERVAL DAY TO SECOND A X X X INTERVAL HOUR A X X X INTERVAL HOUR TO MINUTE A X X XAppendix E: SQL Feature Summary Data Types and Literals 278 SQL Reference: Fundamentals Data Types, continued INTERVAL HOUR TO SECOND A X X X INTERVAL MINUTE A X X X INTERVAL MINUTE TO SECOND A X X X INTERVAL MONTH A X X X INTERVAL SECOND A X X X INTERVAL YEAR A X X X INTERVAL YEAR TO MONTH A X X X LONG VARCHAR T X X X LONG VARGRAPHIC T X X X NUMERIC A X X X REAL A X X X SMALLINT A X X X TIME A X X X TIME WITH TIMEZONE A X X X TIMESTAMP A X X X TIMESTAMP WITH TIMEZONE A X X X user-defined type (UDT) A X X VARBYTE T X X X VARCHAR A X X X VARGRAPHIC T X X X Literals Character data A X X X DATE A X X X Decimal A X X X Floating point A X X X Graphic T X X X Hexadecimal T X X X Integer A X X X Data Type / Literal ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Data Types and Literals SQL Reference: Fundamentals 279 Literals, continued Interval A X X X TIME A X X X TIMESTAMP A X X X Data Type Attributes AS output format phrase A X X X CASESPECIFIC/NOT CASESPECIFIC phrase/ CS/NOT CS phrase T X X X CHARACTER SET A X X X CHECK table constraint attribute A X X X COMPRESS/ COMPRESS NULL/ COMPRESS string/ COMPRESS value column storage attribute T X X X COMPRESS (value_list) column storage attribute T X X X CONSTRAINT/ CONSTRAINT CHECK/ CONSTRAINT PRIMARY KEY/ CONSTRAINT REFERENCES/ CONSTRAINT UNIQUE column constraint attribute T X X X DEFAULT constant_value/ DEFAULT DATE quotestring/ DEFAULT INTERVAL quotestring/ DEFAULT TIME quotestring/ DEFAULT TIMESTAMP quotestring default value control phrase A X X X FOREIGN KEY table constraint attribute A X X X FORMAT output format phrase T X X X NAMED output format phrase T X X X NOT NULL default value control phrase A X X X PRIMARY KEY table constraint attribute A X X X REFERENCES table constraint attribute A X X X TITLE output format phrase T X X X UC, UPPERCASE phrase T X X X Data Type / Literal ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Functions, Operators, and Expressions 280 SQL Reference: Fundamentals Functions, Operators, and Expressions The following list contains all SQL functions, operators, and expressions for this version and previous versions of Teradata Database. The following type codes appear in the ANSI Compliance column: Data Type Attributes, continued UNIQUE table constraint attribute A X X X WITH CHECK OPTION/ WITH NO CHECK OPTION column constraint attribute T X X X WITH DEFAULT default value control phrase T X X X Data Type / Literal ANSI Compliance V2R6.2 V2R6.1 V2R6.0 Code Definition A ANSI P Partially ANSI-compliant T Teradata extension Function / Operator / Expression ANSI Compliance V2R6.2 V2R6.1 V2R6.0 - (subtract) A X X X - (unary minus) A X X X * (multiply) A X X X ** (exponentiate) T X X X / (divide) A X X X ^= (inequality) T X X X + (add) A X X X + (unary plus) A X X X < (less than) A X X X <= (less than or equal) A X X X <> (inequality) A X X XAppendix E: SQL Feature Summary Functions, Operators, and Expressions SQL Reference: Fundamentals 281 = (equality) A X X X > (greater than) A X X X >= (greater than or equal) A X X X ABS T X X X ACCOUNT T X X X ACOS T X X X ACOSH T X X X ADD_MONTHS T X X X ALL A X X X AND A X X X ANY A X X X ASIN T X X X ASINH T X X X ATAN T X X X ATAN2 T X X X ATANH T X X X AVE/ AVERAGE/ T X X X AVG A X X X Options OVER A X X X PARTITION BY value_expression A X X X ORDER BY value_expression A X X X ROWS window_frame_extent A X X X BETWEEN NOT BETWEEN A X X X BYTE/ BYTES T X X X CASE A X X X CASE_N T X X X CAST A, T X X X Function / Operator / Expression ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Functions, Operators, and Expressions 282 SQL Reference: Fundamentals CHAR/ CHARACTERS/ CHARS T X X X CHAR_LENGTH/ CHARACTER_LENGTH A X X X CHAR2HEXINT T X X X COALESCE A X X X CORR A X X X COS T X X X COSH T X X X COUNT A X X X Options OVER A X X X PARTITION BY value_expression A X X X ORDER BY value_expression A X X X ROWS window_frame_extent A X X X COVAR_POP A X X X COVAR_SAMP A X X X CSUM T X X X CURRENT_DATE A X X X CURRENT_TIME A X X X CURRENT_TIMESTAMP A X X X DATABASE T X X X DATE T X X X DEFAULT A, T X EQ T X X X EXCEPT A, T X X X Options ALL T X X X EXISTS NOT EXISTS A X X X Function / Operator / Expression ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Functions, Operators, and Expressions SQL Reference: Fundamentals 283 EXP T X X X EXTRACT P X X X FORMAT T X X X GE T X X X GROUPING A X X X GT T X X X HASHAMP T X X X HASHBAKAMP T X X X HASHBUCKET T X X X HASHROW T X X X IN NOT IN A X X X INDEX T X X X INTERSECT A, T X X X Options ALL T X X X IS NULL IS NOT NULL A X X X KURTOSIS A X X X LE T X X X LIKE NOT LIKE A X X X LN T X X X LOG T X X X LOWER A X X X LT T X X X MAVG T X X X Function / Operator / Expression ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Functions, Operators, and Expressions 284 SQL Reference: Fundamentals MAX/ MAXIMUM A T X X X Options OVER A X X X PARTITION BY value_expression A X X X ORDER BY value_expression A X X X ROWS window_frame_extent A X X X MCHARACTERS T X X X MDIFF T X X X MIN/ MINIMUM A T X X X Options OVER A X X X PARTITION BY value_expression A X X X ORDER BY value_expression A X X X ROWS window_frame_extent A X X X MINUS T X X X Options ALL T X X X MLINREG T X X X MOD T X X X MSUM T X X X NE T X X X NEW P X X NOT A X X X NOT= T X X X NULLIF A X X X NULLIFZERO T X X X OCTET_LENGTH A X X X OR A X X X Function / Operator / Expression ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Functions, Operators, and Expressions SQL Reference: Fundamentals 285 OVERLAPS A X X X PERCENT_RANK A X X X Options OVER A X X X PARTITION BY value_expression A X X X ORDER BY value_expression A X X X POSITION A X X X PROFILE T X X X QUANTILE T X X X RANDOM T X X X RANGE_N T X X X RANK T X X X RANK A X X X Options OVER A X X X PARTITION BY value_expression A X X X ORDER BY value_expression A X X X REGR_AVGX A X X X REGR_AVGY A X X X REGR_COUNT A X X X REGR_INTERCEPT A X X X REGR_R2 A X X X REGR_SLOPE A X X X REGR_SXX A X X X REGR_SXY A X X X REGR_SYY A X X X ROLE T X X X Function / Operator / Expression ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Functions, Operators, and Expressions 286 SQL Reference: Fundamentals ROW_NUMBER A X X X Options OVER A X X X PARTITION BY value_expression A X X X ORDER BY value_expression A X X X SESSION T X X X SIN T X X X SINH T X X X SKEW A X X X SOME A X X X SOUNDEX T X X X SQRT T X X X STDDEV_POP A X X X STDDEV_SAMP A X X X SUBSTR T X X X SUBSTRING A X X X SUM A X X X Options OVER A X X X PARTITION BY value_expression A X X X ORDER BY value_expression A X X X ROWS window_frame_extent A X X X TAN T X X X TANH T X X X TIME T X X X TITLE T X X X TRANSLATE A X X X TRANSLATE_CHK T X X X TRIM P X X X Function / Operator / Expression ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Functions, Operators, and Expressions SQL Reference: Fundamentals 287 TYPE T X X X UNION A, T X X X Options ALL T X X X UPPER A X X X USER A X X X VAR_POP A X X X VAR_SAMP A X X X VARGRAPHIC T X X X WIDTH_BUCKET A X X X ZEROIFNULL T X X X Function / Operator / Expression ANSI Compliance V2R6.2 V2R6.1 V2R6.0Appendix E: SQL Feature Summary Functions, Operators, and Expressions 288 SQL Reference: FundamentalsSQL Reference: Fundamentals 289 Glossary AMP Access Module Processor vproc ANSI American National Standards Institute BLOB Binary Large Object BTEQ Basic TEradata Query facility BYNET Banyan Network - High speed interconnect CJK Chinese, Japanese, and Korean CLIv2 Call Level Interface Version 2 CLOB Character Large Object cs0, cs1, cs2, cs3 Four code sets (codeset 0, 1, 2, and 3) used in EUC encoding. distinct type A UDT that is based on a single predefined data type E2I External-to-Internal EUC Extended UNIX Code external routine UDF, UDM, or external stored procedure that is written using C or C++ external stored procedure a stored procedure that is written using C or C++ FK Foreign Key HI Hash Index I2E Internal-to-External JI Join Index JIS Japanese Industrial Standards LOB Large Object LT/ST Large Table/Small Table (join) NPPI Non-Partitioned Primary Index NUPI Non-Unique Primary Index NUSI Non-Unique Secondary Index OLAP On-Line Analytical Processing OLTP On-Line Transaction ProcessingGlossary 290 SQL Reference: Fundamentals QCD Query Capture Database PDE Parallel Database Extensions PE Parsing Engine vproc PI Primary Index PK Primary Key PPI Partitioned Primary Index predefined type Teradata Database system type such as INTEGER and VARCHAR RDBMS Relational Database Management System SDF Specification for Data Formatting stored procedure a stored procedure that is written using SQL statements structured type A UDT that is a collection of one or more fields called attributes, each of which is defined as a predefined data type or other UDT (which allows nesting) UCS-2 Universal Coded Character Set containing 2 bytes UDF User-Defined Function UDM User-Defined Method UDT User-Defined Type UPI Unique Primary Index USI Unique Secondary Index vproc Virtual ProcessSQL Reference: Fundamentals 291 Index Numerics 2PC, request processing 124 A ABORT statement 220 ABS function 281 ACCOUNT function 281 Account priority 141 ACOS function 281 ACOSH function 281 ACTIVITY_COUNT 144 ADD_MONTHS function 281 Aggregate join index 31 Aggregates, null and 137 ALL predicate 281 ALTER FUNCTION statement 220 ALTER METHOD statement 220 ALTER PROCEDURE statement 220 ALTER REPLICATION GROUP statement 221 ALTER SPECIFIC FUNCTION statement 220 ALTER SPECIFIC METHOD statement 220 ALTER TABLE statement 103, 221 ALTER TRIGGER statement 223 ALTER TYPE statement 223 Alternate key 37 AND operator 281 ANSI compliance and 218 ANSI DateTime, null and 134 ANSI SQL differences 218 Teradata compliance with 214 Teradata extensions to 218 Teradata terminology and 216 terminology differences 216 ANY predicate 281 ARC hash indexes and 35 join indexes and 32 referential integrity and 41 Archive and Recovery. See ARC Arithmetic function, nulls and 134 Arithmetic operators, nulls and 134 AS data type attribute 279 ASCII session character set 139 ASIN function 281 ASINH function 281 ASYNC statement modifier 275 ATAN function 281 ATAN2 function 281 ATANH function 281 AVE function 281 AVERAGE function 281 AVG function 281 B BEGIN DECLARE SECTION statement 223 BEGIN LOGGING statement 224 BEGIN QUERY LOGGING statement 224 BEGIN TRANSACTION statement 225 BETWEEN predicate 281 BIGINT data type 277 BINARY LARGE OBJECT. See BLOB BLOB data type 277 BYTE data type 277 Byte data types 15 BYTE function 281 BYTEINT data type 277 BYTES function 281 C CALL statement 225 Call-Level Interface. See CLI Cardinality, defined 2 CASE expression 281 CASE_N function 281 CASESPECIFIC data type attribute 279 CAST function 281 CD-ROM images v CHAR data type 277 CHAR function 282 CHAR VARYING data type 277 CHAR_LENGTH function 282 CHAR2HEXINT function 282 Character data literal 278 CHARACTER data type 277 Character data types 13 CHARACTER LARGE OBJECT. See CLOB Character literals 89 Character names 77 CHARACTER SET data type attribute 279 Character set, request change of 142 Character sets, Teradata SQL lexicon 67Index 292 SQL Reference: Fundamentals CHARACTER VARYING data type 277 CHARACTER_LENGTH function 282 CHARACTERS function 282 CHARS function 282 CHECK data type attribute 279 CHECKPOINT statement 225 Child table, defined 37 Circular reference, referential integrity 39 Classes of UDFs aggregate 54 scalar 54 CLI session management 143 CLOB data type 277 CLOSE statement 225 COALESCE expression 282 Collation sequences (SQL) 140 COLLECT DEMOGRAPHICS statement 225 COLLECT STAT INDEX statement 227 COLLECT STAT statement 226 COLLECT STATISTICS INDEX statement 227 COLLECT STATISTICS statement 226 COLLECT STATS INDEX statement 227 COLLECT STATS statement 226 Collecting statistics 164 Column alias 72 Columns definition 12 referencing, syntax for 72 COMMENT statement 227 Comments bracketed 96 multibyte character sets and 96 simple 95 COMMIT statement 228 Comparison operators, null and 135 COMPRESS data type attribute 279 CONNECT statement 228 Constants. See Literals CONSTRAINT data type attribute 279 CORR function 282 COS function 282 COSH function 282 COVAR_SAMP function 282 Covering index 31 Covering, secondary index, non-unique, and 27 CREATE AUTHORIZATION statement 228 CREATE CAST statement 229 CREATE DATABASE statement 229 CREATE FUNCTION statement 55, 229 CREATE HASH INDEX statement 231 CREATE INDEX statement 232 CREATE JOIN INDEX statement 232 CREATE MACRO statement 233 CREATE METHOD statement 233 CREATE ORDERING statement 234 CREATE PROCEDURE statement 53, 234 CREATE PROFILE statement 236 CREATE RECURSIVE VIEW statement 244 CREATE REPLICATION GROUP statement 237 CREATE ROLE statement 237 CREATE TABLE statement 237 CREATE TRANSFORM statement 239 CREATE TRIGGER statement 239 CREATE TYPE statement 240, 241 CREATE USER statement 242 CREATE VIEW statement 243 CS data type attribute 279 CSUM function 282 CURRENT_DATE function 282 CURRENT_TIME function 282 CURRENT_TIMESTAMP function 282 Cylinder reads 164 D Data Control Language. See DCL Data Definition Language. See DDL Data Manipulation Language. See DML Data types byte 15 character 13 DateTime 14 definition 13 interval 14 numeric 13 UDT 15, 58 Data, standard form of, Teradata Database 71 Database default, establishing for session 76 default, establishing permanent 75 DATABASE function 282 DATABASE statement 244 Database, defined 1 DATE data type 277 DATE function 282 DATE literal 278 Date literals 88 Date, change format of 142 DateTime data types 14 DCL statements, defined 105 DDL CREATE FUNCTION 55 CREATE PROCEDURE 53 REPLACE FUNCTION 55 REPLACE PROCEDURE 53 DDL statements, defined 101 DEC data type 277Index SQL Reference: Fundamentals 293 DECIMAL data type 277 Decimal literal 278 Decimal literals 87 DECLARE CURSOR statement 244 DECLARE STATEMENT statement 244 DECLARE TABLE statement 244 DEFAULT data type attribute 279 DEFAULT function 282 Degree, defined 2 DELETE DATABASE statement 245 DELETE statement 245 DELETE USER statement 245 Delimiters 93 DESCRIBE statement 246 DIAGNOSTIC "validate index" statement 246 DIAGNOSTIC DUMP SAMPLES statement 246 DIAGNOSTIC HELP SAMPLES statement 246 DIAGNOSTIC SET SAMPLES statement 246 Distinct UDTs 58 DML statements, defined 106 DOUBLE PRECISION data type 277 DROP AUTHORIZATION statement 246 DROP CAST statement 246 DROP DATABASE statement 246 DROP FUNCTION statement 246 DROP HASH INDEX statement 246 DROP INDEX statement 247 DROP JOIN INDEX statement 247 DROP MACRO statement 247 DROP ORDERING statement 247 DROP PROCEDURE statement 247 DROP PROFILE statement 247, 256 DROP REPLICATION GROUP statement 247 DROP ROLE statement 247 DROP SPECIFIC FUNCTION statement 246 DROP STATISTICS statement 247 DROP TABLE statement 248 DROP TRANSFORM statement 248 DROP TRIGGER statement 248 DROP TYPE statement 248 DROP USER statement 246 DROP VIEW statement 248 DUMP EXPLAIN statement 248 E EBCDIC session character set 139 ECHO statement 248 Embedded SQL binding style 100 macros 46 END DECLARE SECTION statement 248 END LOGGING statement 249 END QUERY LOGGING statement 249 END TRANSACTION statement 249 END-EXEC statement 248 EQ operator 282 Event processing SELECT AND CONSUME and 133 EXCEPT operator 282 EXEC SQL statement modifier 275 Executable SQL statements 119 EXECUTE IMMEDIATE statement 249 EXECUTE statement 249 EXISTS predicate 282 EXP function 283 EXPLAIN request modifier 19, 21, 275 Express logon 142 External stored procedures 53 usage 53 EXTRACT function 283 F Fallback hash indexes and 35 join indexes and 32 FastLoad hash indexes and 35 join indexes and 32 referential integrity and 42 FETCH statement 250 FLOAT data type 277 Floating point literal 278 Floating point literals 87 Foreign key defined 16 maintaining 40 FOREIGN KEY data type attribute 279 Foreign key. See also Key Foreign key. See also Referential integrity FORMAT data type attribute 279 FORMAT function 283 Full table scan 163 G GE operator 283 general information about Teradata vi GET CRASH statement 250 GIVE statement 250 GRANT statement 250 GRAPHIC data type 277 Graphic literal 278 Graphic literals 89 GROUP BY statement modifier 275 GROUPING function 283 GT operator 283Index 294 SQL Reference: Fundamentals H Hash buckets 18 Hash index ARC and 35 effects of 35 MultiLoad and 35 permanent journal and 35 TPump and 35 Hash mapping 18 HASHAMP function 283 HASHBAKAMP function 283 HASHBUCKET function 283 HASHROW function 283 HAVING statement modifier 275 HELP statement 252 HELP statements 116 HELP STATISTICS statement 253 Hexadecimal get representation of name 84 Hexadecimal literal 278 Hexadecimal literals 87 I IN predicate 283 INCLUDE SQLCA statement 254 INCLUDE SQLDA statement 254 INCLUDE statement 254 Index advantages of 18 covering 31 defined 17 disadvantages of 18 dropping 105 EXPLAIN, using 21 hash mapping and 18 join 20 keys and 16 maximum number of columns 206 non-unique 19 partitioned 20 row hash value and 17 RowID and 17 selectivity of 17 types of (Teradata) 19 unique 19 uniqueness value and 17 INDEX function 283 Information Products Publishing Library v INITIATE INDEX ANALYSIS statement 254 INSERT EXPLAIN statement 255 INSERT statement 254 INT data type 277 INTEGER data type 277 Integer literal 278 Integer literals 87 INTERSECT operator 283 Interval data types 14 INTERVAL DAY data type 277 INTERVAL DAY TO HOUR data type 277 INTERVAL DAY TO MINUTE data type 277 INTERVAL DAY TO SECOND data type 277 INTERVAL HOUR data type 277 INTERVAL HOUR TO MINUTE data type 277 INTERVAL HOUR TO SECOND data type 278 Interval literal 279 Interval literals 88 INTERVAL MINUTE data type 278 INTERVAL MINUTE TO SECOND data type 278 INTERVAL MONTH data type 278 INTERVAL SECOND data type 278 INTERVAL YEAR data type 278 INTERVAL YEAR TO MONTH data type 278 IS NOT NULL predicate 283 IS NULL predicate 283 Iterated requests 127 J Japanese character code notation, how to read 171 Japanese character names 77 JDBC 100 Join index aggregate 31 described 30 effects of 32 multitable 31 performance and 33 queries using 33 single-table 31 sparse 32 Join Index. See also Index K Key alternate 37 foreign 16 indexes and 16 primary 16 referential integrity and 16 Keywords 66 NULL 90 KURTOSIS function 283 L LE operator 283 Lexical separators 94Index SQL Reference: Fundamentals 295 LIKE predicate 283 Limits database 206 session 211 system 204 Literals character 89 date 88 decimal 87 floating point 87 graphic 89 hexadecimal 87 integer 87 interval 88 time 88 timestamp 88 LN function 283 LOCKING statement modifier 275 LOG function 283 LOGOFF statement 255 LOGON statement 255 Logon, express 142 LONG VARCHAR data type 278 LONG VARGRAPHIC data type 278 LOWER function 283 LT operator 283 M Macros contents 47 defined 46 executing 47 maximum expanded text size 207 maximum number of parameters 207 SQL statements and 46 MAVG function 283 MAX function 284 MAXIMUM function 284 MCHARACTERS function 284 MDIFF function 284 MERGE statement 255 MIN function 284 MINIMUM function 284 MINUS operator 284 MLINREG function 284 MOD operator 284 MODIFY DATABASE statement 256 MODIFY USER statement 257 MSUM function 284 MultiLoad hash indexes and 35 join indexes and 32 referential integrity and 42 Multi-statement requests, performance 125 Multi-statement transactions 125 Multitable join index 31 N Name calculate length of 78 fully qualified 72 get hexadecimal representation 84 identify in logon string 86 maximum size 206 multiword 69 object 77 resolving 74 translation and storage 81 NAMED data type attribute 279 NE operator 284 NEW expression 284 Nonexecutable SQL statements 120 Non-partitioned primary index. See NPPI. Non-unique index. See Index, Primary index, Secondary index NOT BETWEEN predicate 281 NOT CASESPECIFIC data type attribute 279 NOT CS data type attribute 279 NOT EXISTS predicate 282 NOT IN predicate 283 NOT LIKE predicate 283 NOT NULL data type attribute 279 NOT operator 284 NOT= operator 284 NPPI 20 Null aggregates and 137 ANSI DateTime and 134 arithmetic functions and 134 arithmetic operators and 134 collation sequence 136 comparison operators and 135 excluding 135 operations on (SQL) 134 searching for 136 searching for, null and non-null 136 NULL keyword 90 Null statement 98 NULLIF expression 284 NULLIFZERO function 284 NUMERIC data type 278 Numeric data types 13 NUPI. See Primary index, non-unique NUSI. See Secondary index, non-uniqueIndex 296 SQL Reference: Fundamentals O Object names 77 Object, name comparison 82 OCTET_LENGTH function 284 ODBC 100 OPEN statement 258 Operators 91 OR operator 284 ORDER BY statement modifier 276 ordering publications v OVERLAPS operator 285 P Parallel step processing 125 Parameters, session 138 Parent table, defined 37 Partial cover 30 Partition elimination 159 Partitioned primary index. See PPI. PERCENT_RANK function 285 Permanent journal creating 2 hash indexes and 35 join indexes and 32 POSITION function 285 POSITION statement 258 PPI defined 20 maximum number of partitions 206 partition elimination and 159 Precedence, SQL operators 91 PREPARE statement 258 Primary index choosing 23 default 22 described 22 non-unique 23 NULL and 136 summary 24 unique 23 PRIMARY KEY data type attribute 279 Primary key, defined 16 Primary key. See also Key Procedure, dropping 105 product-related information v PROFILE function 285 Profiles 55 publications related to this release v Q QCD tables populating 115 QUALIFY statement modifier 276 QUANTILE function 285 Query Capture Database. See QCD Query processing access types 162 all AMP request 156 AMP sort 158 BYNET merge 159 defined 153 full table scan 163 single AMP request 154 single AMP response 156 Query, defined 153 R RANDOM function 285 RANGE_N function 285 RANK function 285 REAL data type 278 Recursive queries (SQL) 112 Recursive query, defined 112 REFERENCES data type attribute 279 Referential integrity ARC and 41 circular references and 39 described 36 FastLoad and 42 foreign keys and 39 importance of 38 MultiLoad and 42 terminology 37 REGR_AVGX function 285 REGR_AVGY function 285 REGR_COUNT function 285 REGR_INTERCEPT function 285 REGR_R2 function 285 REGR_SLOPE function 285 REGR_SXX function 285 REGR_SXY function 285 REGR_SYY function 285 release definition v RENAME FUNCTION statement 259 RENAME MACRO statement 259 RENAME PROCEDURE statement 259 RENAME TABLE statement 259 RENAME TRIGGER statement 259 RENAME VIEW statement 259 REPLACE CAST statement 259 REPLACE FUNCTION statement 55, 259 REPLACE MACRO statement 261 REPLACE METHOD statement 261 REPLACE ORDERING statement 262 REPLACE PROCEDURE statement 53, 262, 263Index SQL Reference: Fundamentals 297 REPLACE TRANSFORM statement 265 REPLACE TRIGGER statement 266 REPLACE VIEW statement 266 Request processing 2PC 124 ANSI mode 123 Teradata mode 123 Request terminator 96 Requests iterated 127 maximum size 207 multi-statement 120 single-statement 120 Requests. See also Blocked requests, Multi-statement requests, Request processing Reserved words 219 RESTART INDEX ANALYSIS statement 266 Restricted words 173 REVOKE statement 266 REWIND statement 268 ROLE function 285 Roles 57 ROLLBACK statement 268 ROW_NUMBER function 286 Rows, maximum size 206 S SAMPLE statement modifier 276 Secondary index defined 25 dual 28 non-unique 26 bit mapping 28 covering and 27 value-ordered 27 NULL and 136 summary 29 unique 26 using Teradata Index Wizard 21 Security, user-level password attributes 56 Seed statements 113 SELECT statement 268 Selectivity high 17 low 17 Semicolon null statement 98 request terminator 96 statement separator 94 Separator lexical 94 statement 94 Session character set ASCII 139 EBCDIC 139 UTF16 139 UTF8 139 Session collation 140 Session control 138 SESSION function 286 Session handling, session control 144 Session management CLI 143 ODBC 143 requests 144 session reserve 143 Session parameters 138 SET BUFFERSIZE statement 270 SET CHARSET statement 270 SET CONNECTION statement 270 SET CRASH statement 270 SET ROLE statement 271 SET SESSION ACCOUNT statement 271 SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL statement 271 SET SESSION COLLATION statement 271 SET SESSION DATABASE statement 271 SET SESSION DATEFORM statement 271 SET SESSION FUNCTION TRACE statement 271 SET SESSION OVERRIDE REPLICATION statement 272 SET SESSION statement 271 SET TIME ZONE statement 272 SHOW CAST statement 272 SHOW FUNCTION statement 272 SHOW HASH INDEX statement 272 SHOW JOIN INDEX statement 272 SHOW MACRO statement 272 SHOW METHOD statement 272 SHOW PROCEDURE statement 272 SHOW REPLICATION GROUP statement 272 SHOW SPECIFIC FUNCTION statement 272 SHOW statement 272 SHOW statements 117 SHOW TABLE statement 272 SHOW TRIGGER statement 272 SHOW TYPE statement 272 SHOW VIEW statement 272 SIN function 286 Single-table join index 31 SINH function 286 SKEW function 286 SMALLINT data type 278 SOME predicate 286 SOUNDEX function 286 Sparse join index 32Index 298 SQL Reference: Fundamentals Specifications database 206 session 211 system 204 SQL dynamic 129 dynamic, SELECT statement and 131 static 129 SQL binding styles CLI 100 defined 100 direct 100 embedded 100 JDBC 100 ODBC 100 stored procedure 100 SQL data type attributes AS 279 CASESPECIFIC 279 CHARACTER SET 279 CHECK 279 COMPRESS 279 CONSTRAINT 279 CS 279 DEFAULT 279 FOREIGN KEY 279 FORMAT 279 NAMED 279 NOT CASESPECIFIC 279 NOT CS 279 NOT NULL 279 PRIMARY KEY 279 REFERENCES 279 TITLE 279 UC 279 UNIQUE 280 UPPERCASE 279 WITH CHECK OPTION 280 WITH DEFAULT 280 SQL data types BIGINT 277 BLOB 277 BYTE 277 BYTEINT 277 CHAR 277 CHAR VARYING 277 CHARACTER 277 CHARACTER VARYING 277 CLOB 277 DATE 277 DEC 277 DECIMAL 277 DOUBLE PRECISION 277 FLOAT 277 GRAPHIC 277 INT 277 INTEGER 277 INTERVAL DAY 277 INTERVAL DAY TO HOUR 277 INTERVAL DAY TO MINUTE 277 INTERVAL DAY TO SECOND 277 INTERVAL HOUR 277 INTERVAL HOUR TO MINUTE 277 INTERVAL HOUR TO SECOND 278 INTERVAL MINUTE 278 INTERVAL MINUTE TO SECOND 278 INTERVAL MONTH 278 INTERVAL SECOND 278 INTERVAL YEAR 278 INTERVAL YEAR TO MONTH 278 LONG VARCHAR 278 LONG VARGRAPHIC 278 NUMERIC 278 REAL 278 SMALLINT 278 TIME 278 TIME WITH TIMEZONE 278 TIMESTAMP 278 TIMESTAMP WITH TIMEZONE 278 UDT 278 VARBYTE 278 VARCHAR 278 VARGRAPHIC 278 SQL error response (ANSI) 149 SQL expressions CASE 281 COALESCE 282 NEW 284 NULLIF 284 SQL Flagger enabling and disabling 217 function 217 session control 139 SQL functional families, defined 99 SQL functions ABS 281 ACCOUNT 281 ACOS 281 ACOSH 281 ADD_MONTHS 281 ASIN 281 ASINH 281 ATAN 281 ATAN2 281 ATANH 281 AVE 281 AVERAGE 281 AVG 281Index SQL Reference: Fundamentals 299 BYTE 281 BYTES 281 CASE_N 281 CAST 281 CHAR 282 CHAR_LENGTH 282 CHAR2HEXINT 282 CHARACTER_LENGTH 282 CHARACTERS 282 CHARS 282 CORR 282 COS 282 COSH 282 COVAR_SAMP 282 CSUM 282 CURRENT_DATE 282 CURRENT_TIME 282 CURRENT_TIMESTAMP 282 DATABASE 282 DATE 282 DEFAULT 282 EXP 283 EXTRACT 283 FORMAT 283 GROUPING 283 HASHAMP 283 HASHBAKAMP 283 HASHBUCKET 283 HASHROW 283 INDEX 283 KURTOSIS 283 LN 283 LOG 283 LOWER 283 MAVG 283 MAX 284 MAXIMUM 284 MCHARACTERS 284 MDIFF 284 MIN 284 MINIMUM 284 MLINREG 284 MSUM 284 NULLIFZERO 284 OCTET_LENGTH 284 PERCENT_RANK 285 POSITION 285 PROFILE 285 QUANTILE 285 RANDOM 285 RANGE_N 285 RANK 285 REGR_AVGX 285 REGR_AVGY 285 REGR_COUNT 285 REGR_INTERCEPT 285 REGR_R2 285 REGR_SLOPE 285 REGR_SXX 285 REGR_SXY 285 REGR_SYY 285 ROLE 285 ROW_NUMBER 286 SESSION 286 SIN 286 SINH 286 SKEW 286 SOUNDEX 286 SQRT 286 STDDEV_POP 286 STDDEV_SAMP 286 SUBSTR 286 SUBSTRING 286 SUM 286 TAN 286 TANH 286 TIME 286 TITLE 286 TRANSLATE 286 TRANSLATE_CHK 286 TRIM 286 TYPE 287 UNION 287 UPPER 287 USER 287 VAR_POP 287 VAR_SAMP 287 VARGRAPHIC 287 WIDTH_BUCKET 287 ZEROIFNULL 287 SQL lexicon character names 77 delimiters 93 Japanese character names 67, 77 keywords 66 lexical separators 94 object names 77 operators 91 request terminator 96 statement separator 94 SQL literals Character data 278 DATE 278 Decimal 278 Floating point 278 Graphic 278 Hexadecimal 278 Integer 278Index 300 SQL Reference: Fundamentals Interval 279 TIME 279 TIMESTAMP 279 SQL operators AND 281 EQ 282 EXCEPT 282 GE 283 GT 283 INTERSECT 283 LE 283 LT 283 MINUS 284 MOD 284 NE 284 NOT 284 NOT= 284 OR 284 OVERLAPS 285 SQL predicates ALL 281 ANY 281 BETWEEN 281 EXISTS 282 IN 283 IS NOT NULL 283 IS NULL 283 LIKE 283 NOT BETWEEN 281 NOT EXISTS 282 NOT IN 283 NOT LIKE 283 SOME 286 SQL request modifier, EXPLAIN 19, 21, 275 SQL requests iterated 127 multi-statement 120 single-statement 120 SQL responses 147 failure 150 success 148 warning 149 SQL return codes 144 SQL statement modifiers ASYNC 275 EXEC SQL 275 GROUP BY 275 HAVING 275 LOCKING 275 ORDER BY 276 QUALIFY 276 SAMPLE 276 USING 276 WHERE 276 WITH 276 WITH RECURSIVE 276 SQL statements ABORT 220 ALTER FUNCTION 220 ALTER METHOD 220 ALTER PROCEDURE 220 ALTER REPLICATION GROUP 221 ALTER SPECIFIC FUNCTION 220 ALTER SPECIFIC METHOD 220 ALTER TABLE 221 ALTER TRIGGER 223 ALTER TYPE 223 BEGIN DECLARE SECTION 223 BEGIN LOGGING 224 BEGIN QUERY LOGGING 224 BEGIN TRANSACTION 225 CALL 225 CHECKPOINT 225 CLOSE 225 COLLECT DEMOGRAPHICS 225 COLLECT STAT 226 COLLECT STAT INDEX 227 COLLECT STATISTICS 226 COLLECT STATISTICS INDEX 227 COLLECT STATS 226 COLLECT STATS INDEX 227 COMMENT 227 COMMIT 228 CONNECT 228 CREATE AUTHORIZATION 228 CREATE CAST 229 CREATE DATABASE 229 CREATE FUNCTION 229 CREATE HASH INDEX 231 CREATE INDEX 232 CREATE JOIN INDEX 232 CREATE MACRO 233 CREATE METHOD 233 CREATE ORDERING 234 CREATE PROCEDURE 234 CREATE PROFILE 236 CREATE RECURSIVE VIEW 244 CREATE REPLICATION GROUP 237 CREATE ROLE 237 CREATE TABLE 237 CREATE TRANSFORM 239 CREATE TRIGGER 239 CREATE TYPE 240, 241 CREATE USER 242 CREATE VIEW 243 DATABASE 244 DECLARE CURSOR 244 DECLARE STATEMENT 244Index SQL Reference: Fundamentals 301 DECLARE TABLE 244 DELETE 245 DELETE DATABASE 245 DELETE USER 245 DESCRIBE 246 DIAGNOSTIC 115, 246 DIAGNOSTIC "validate index" 246 DIAGNOSTIC DUMP SAMPLES 246 DIAGNOSTIC HELP SAMPLES 246 DIAGNOSTIC SET SAMPLES 246 DROP AUTHORIZATION 246 DROP CAST 246 DROP DATABASE 246 DROP FUNCTION 246 DROP HASH INDEX 246 DROP INDEX 247 DROP JOIN INDEX 247 DROP MACRO 247 DROP ORDERING 247 DROP PROCEDURE 247 DROP PROFILE 247, 256 DROP REPLICATION GROUP 247 DROP ROLE 247 DROP SPECIFIC FUNCTION 246 DROP STATISTICS 247 DROP TABLE 248 DROP TRANSFORM 248 DROP TRIGGER 248 DROP TYPE 248 DROP USER 246 DROP VIEW 248 DUMP EXPLAIN 248 ECHO 248 END DECLARE SECTION 248 END LOGGING 249 END QUERY LOGGING 249 END TRANSACTION 249 END-EXEC 248 executable 119 EXECUTE 249 EXECUTE IMMEDIATE 249 FETCH 250 GET CRASH 250 GIVE 250 GRANT 250 HELP 252 HELP STATISTICS 253 INCLUDE 254 INCLUDE SQLCA 254 INCLUDE SQLDA 254 INITIATE INDEX ANALYSIS 254 INSERT 254 INSERT EXPLAIN 255 invoking 119 LOGOFF 255 LOGON 255 MERGE 255 MODIFY DATABASE 256 MODIFY USER 257 name resolution 74 nonexecutable 120 OPEN 258 partial names, use of 73 POSITION 258 PREPARE 258 RENAME FUNCTION 259 RENAME MACRO 259 RENAME PROCEDURE 259 RENAME TABLE 259 RENAME TRIGGER 259 RENAME VIEW 259 REPLACE CAST 259 REPLACE FUNCTION 259 REPLACE MACRO 261 REPLACE METHOD 261 REPLACE ORDERING 262 REPLACE PROCEDURE 262, 263 REPLACE TRANSFORM 265 REPLACE TRIGGER 266 REPLACE VIEW 266 RESTART INDEX ANALYSIS 266 REVOKE 266 REWIND 268 ROLLBACK 268 SELECT 268 SELECT, dynamic SQL 131 SET BUFFERSIZE 270 SET CHARSET 270 SET CONNECTION 270 SET CRASH 270 SET ROLE 271 SET SESSION 271 SET SESSION ACCOUNT 271 SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL 271 SET SESSION COLLATION 271 SET SESSION DATABASE 271 SET SESSION DATEFORM 271 SET SESSION FUNCTION TRACE 271 SET SESSION OVERRIDE REPLICATION 272 SET TIME ZONE 272 SHOW 272 SHOW CAST 272 SHOW FUNCTION 272 SHOW HASH INDEX 272 SHOW JOIN INDEX 272 SHOW MACRO 272 SHOW METHOD 272Index 302 SQL Reference: Fundamentals SHOW PROCEDURE 272 SHOW REPLICATION GROUP 272 SHOW SPECIFIC FUNCTION 272 SHOW TABLE 272 SHOW TRIGGER 272 SHOW TYPE 272 SHOW VIEW 272 structure 63 subqueries 110 TEST 273 UPDATE 273 WAIT 274 WHENEVER 274 SQL statements, macros and 46 SQL. See also Embedded SQL SQL-2003 non-reserved words 174 SQL-2003 reserved words 174 SQLCA 144 SQLCODE 144 SQLSTATE 144 SQRT function 286 Statement processing. See Query processing Statement separator 94 STDDEV_POP function 286 STDDEV_SAMP function 286 Stored procedures ACTIVITY_COUNT 144 creating 50 deleting 52 elements of 49 executing 51 modifying 51 privileges 49 renaming 52 Structured UDTs 58 Subqueries (SQL) 110 Subquery, defined 110 SUBSTR function 286 SUBSTRING function 286 SUM function 286 Syntax, how to read 167 T Table cardinality of 2 creating indexes for 20 defined 2 degree of 2 dropping 105 full table scan 163 global temporary 5 global temporary trace 4 maximum number of columns 206 maximum number of rows 206 queue 4 tuple and 2 volatile temporary 9 Table structure, altering 103 Table, change structure of 103 TAN function 286 TANH function 286 Target level emulation 115 Teradata Database database specifications 206 session specifications 211 system specifications 204 Teradata DBS, session management 143 Teradata Index Wizard 21 determining optimum secondary indexes 21 SQL diagnostic statements 115 Teradata SQL 218 Teradata SQL, ANSI SQL and 214 Terminator, request 96 TEST statement 273 TIME data type 278 TIME function 286 TIME literal 279 Time literals 88 TIME WITH TIMEZONE data type 278 TIMESTAMP data type 278 TIMESTAMP literal 279 Timestamp literals 88 TIMESTAMP WITH TIMEZONE data type 278 TITLE data type attribute 279 TITLE function 286 TITLE phrase, column definition 71 TPump hash indexes and 35 join indexes and 32 Transaction mode, session control 140 Transaction modes (SQL) 140 Transactions defined 122 explicit, defined 124 implicit, defined 124 TRANSLATE function 286 TRANSLATE_CHK function 286 Trigger altering 44 creating 44 defined 44 dropping 44, 105 process flow for 44 TRIM function 286 Two-phase commit. See 2PC TYPE function 287Index SQL Reference: Fundamentals 303 U UC data type attribute 279 UDFs classes 54 CREATE FUNCTION 55 CREATE PROCEDURE 53 usage 55 UDT data types 15, 58, 278 creating and using 59 distinct 58 structured 58 Unicode, notation 171 UNION function 287 UNIQUE alternate key 37 UNIQUE data type attribute 280 Unique index. See Index, Primary index, Secondary index UPDATE statement 273 UPI. See Primary index, unique UPPER function 287 UPPERCASE data type attribute 279 USER function 287 User, defined 1 User-defined types. See UDT data types USI. See Secondary index, unique USING statement modifier 276 UTF16 session character set 139 UTF8 session character set 139 V VAR_POP function 287 VAR_SAMP function 287 VARBYTE data type 278 VARCHAR data type 278 VARGRAPHIC data type 278 VARGRAPHIC function 287 View described 42 dropping 105 maximum expanded text size 207 maximum number of columns 206 restrictions 43 W WAIT statement 274 WHENEVER statement 274 WHERE statement modifier 276 WIDTH_BUCKET function 287 WITH DEFAULT data type attribute 280 WITH NO CHECK OPTION data type attribute 280 WITH RECURSIVE statement modifier 276 WITH statement modifier 276 Z ZEROIFNULL function 287 Zero-table SELECT statement 108Index 304 SQL Reference: Fundamentals