Você está na página 1de 5

Using Extract function with any date field is easy.

Lets try this out with system date and system time commands. To see the current date we use SELECT DATE; This would give output as 11/06/2007. Suppose you want to EXTRACT the month from the date then use this query SELECT EXTRACT (MONTH FROM date); This would give 11 as the output. Similarly for YEAR and DAY use the following commands SELECT EXTRACT (YEAR FROM date); SELECT EXTRACT (DAY FROM date);

But for HOUR MINUTE and SECONDS extractions, we can not use date command, we have to use time instead. To see time use the following command; SELECT TIME; This would return 15:59:19 as the output and to extract HOUR MINUTE and SECONDS from this, use the following commands SELECT EXTRACT (MINUTE FROM time); SELECT EXTRACT (HOUR FROM time) ; SELECT EXTRACT (SECOND FROM time) ; If you use anything else than the expected inputs for the EXTRACT function you would end up getting 3707: Syntax error.

In Teradata from V2R3 onwards, it has a system calendar which is very helpful in case of the complex date comparisons like finding week number of a year or a month, or quarter of a year or month number in a particular quarter etc. The following command shows us the week of the month, week of the year, month of a quarter and the current quarter of the year for a particular date, which is July 20, 2007. SELECT week_of_month, week_of_year, month_of_quarter, quarter_of_year FROM sys_calendar.calendar

WHERE calendar_date=1070720; Where the string 1070720 is broken down as follows; 1st three digits represents the number of years from 1900. For example current year is 2007 and the base year is 1900 then first three digits are 2007-1900 which is 107. 2nd two digits represents month in the year. 3rd two digits represents day of the month. To see all the attributes of the sys calendar, try the following command. SELECT * FROM sys_calendar.calendar WHERE calendar_date=1070720; If there is anything lacking in SQL to make it a complete programming language, Stored Procedures fulfills the same. Stored Procedures are written in SQL and consist of a set of control and condition handling statements. These features provide a server-based procedural interface to the Teradata Database for application programmers. What does this mean? It is simple, when you use Teradata from any client software such as SQL Assistant, then Teradata becomes the server and the SQL Assistant becomes the client. But when you call a Stored Procedure from SQL Assistant, at this time the Stored Procedure itself becomes the server to the SQL Assistant and client to Teradata DB. Thus it acts as an interface in between SQL Assistant and Teradata DB. Things to NOTE while creating a Stored Procedure.

1. Stored Procedure is a database Object and DDL is used to create it. 2. The set of statements that forms the primary task of a Stored Procedure
contains within, what is called a Stored Procedure body.

3. Stored Procedure body can be either a single state a compound statement. 4. Single statement procedure can contain only one looping mechanism or SQL
statement, excluding Cursors.

5. Where as compound statements can have multiple statements but it has to be


within the BEGIN and END block.

6. It is not a good practice to use DDL statements within a Stored Procedure. 7. It is stored in USER DATABASE space. 8. Can have parameters like IN, OUT and INOUT.
Example: CREATE PROCEDURE MyFirstProc (IN emp_number INTEGER, IN dept_number INTEGER,

OUT dept_name CHAR(10), INOUT errstr VARCHAR(30)) BEGIN INSERT INTO Employee (EmpNo, DeptNo ) VALUES (emp_number, dept_number); SELECT DeptName INTO dept_name FROM Department WHERE DeptNo = dept_number; END; To execute/call the above Stored Procedure: CALL MyFirstProc (495, 211); here are some special keywords introduced here which are only used in case of a stored procedure. Lets see which all keywords are used for what purpose. There are 3 types of the parameter being specified. IN OUT INOUT used for input type parameter only. used for output type parameter only. INOUT can be both input and output type parameter.

By default, if the parameter type is not specified, the parameter is assumed to be of the IN type. BEGIN This keyword introduces a compound statement. A compound statement can contain all the SQL statements and variable declarations for the stored procedure. This compound statement has to be terminated with the END keyword. END statement. This is the terminating keyword for a BEGIN-END compound

DECLARE DECLARE keyword can be used for a local variable declaration or a cursor declaration or a condition (error/exception) handler declaration. ITERATE If you want to terminate the execution of the current iteration of a labeled iteration statement, and start the next iteration, use this keyword ITERATE. If you are using WHILE or FOR loops then the next iteration would start conditionally and in case if you are LOOP and REPEAT the next iteration would start unconditionally.

LEAVE Used to exit out from a current iteration and continue execution outside the current iteration. DEFAULT This is an optional keyword for introducing a default value for the local variables. SET parameter. CALL SET is the keyword that is used for assigning a value to a variable or

Used to invoke the procedure.

PRINT This command is used to display something to the screen. Mostly used for testing/debugging purpose. Remember to enable this option while creating the procedure.

Teradata and ANSI Mode


Teradata was originally written to be compatible with DB/2. As Teradata evolved it was able to include connections to network attached systems. Another evolution of Teradata is the inclusion of ANSI (American National Standards Institute) standards in its functionality and the format of its SQL commands. Teradata works in two modes to take advantage of the functionality of either Teradata or ANSI modes. Here is a summary of the differences of both modes:
18) What differences are between ANSI and Teradata must be taken into consideration? (Choose Three)

Teradata Mode:

Data compares are NOT case specific Allows truncation of displayed data Sets the CREATE Table default to SET Table A Transaction is implicit by nature

ANSI Mode:

Data compares are case specific Forbids truncation of display data CREATE Table default to Multiset

All Transactions are explicit and require a COMMIT Work to End the Transaction

Você também pode gostar