dates in oracle - laurentian · • to_char converts an oracle date into a character string...

21
Dates in Oracle SysDate Oracle taps into the computers’ operating system for the current date and time. It makes these available to you through a special called SysDate. You can regard it as hidden column or pseudo-column that is in every table. select SysDate from Dual; SysDate --------- 01-Apr-98 describe dual Name NULL Type ---------------------- ---------- -------- Dummy Char(1) The actual column in Dual is irrelevant. So we can experiment with date formatting and arithmetic using the Dual table and the Date functions in order to understand how they work. CURRENT_DATE It reports the system date in the session’s time zone: Select current_date from DUAL; CURRENT_D ----------------- 15-OCT-03

Upload: others

Post on 16-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

Dates in Oracle SysDate • Oracle taps into the computers’ operating system for the current date and

time. It makes these available to you through a special called SysDate. • You can regard it as hidden column or pseudo-column that is in every

table. select SysDate from Dual; SysDate --------- 01-Apr-98 describe dual Name NULL Type ---------------------- ---------- -------- Dummy Char(1) The actual column in Dual is irrelevant. So we can experiment with date formatting and arithmetic using the Dual table and the Date functions in order to understand how they work. CURRENT_DATE It reports the system date in the session’s time zone: Select current_date from DUAL; CURRENT_D ----------------- 15-OCT-03

Page 2: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

SYSTIMESTAMP It reports the system date in the TIMESTAMP datatype format: Select SysTimeStamp from DUAL; SYSTIMESTAMP ------------------------------------------------------------------------------------- 15-OCT-03 02.41.31.000000 PM -05:00 The Difference Between Two Dates select Holiday, Actualdate, celebrateddate, from HOLIDAY; HOLIDAY ACTUALDAT CELEBRATE ------------------------------------- ------------------ ----------------- New Year Day 01-Jan-95 01-Jan-95 Martin Luther King, Jr. 15-Jan-95 16-Jan-95 Lincoln’s Birthday 12-Feb-95 20-Feb-95 Washington’s Birthday 22-Feb-95 20-Feb-95 Fast Day, New Hampshire 22-Feb-95 22-Feb-95 Memorial Day 30-May-95 29-May-95 Independence Day 04-Jul-95 04-Jul-95 Labor Day 04-Sep-95 04-Sep-95 Columbus Day 08-Oct-95 09-Oct-95 Thanksgiving 23-Nov-95 23-Nov-95 select Holiday, Actualdate, Celebrateddate from Holiday where Celebrateddate – Actualdate != 0; HOLIDAY ACTUALDAT CELEBRATE ------------------------------------- ------------------ ----------------- Martin Luther King, Jr. 15-Jan-95 16-Jan-95 Lincoln’s Birthday 12-Feb-95 20-Feb-95 Washington’s Birthday 22-Feb-95 20-Feb-95 Memorial Day 30-May-95 29-May-95 Columbus Day 08-Oct-95 09-Oct-95

Page 3: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

Date Functions • Adding Months column Feastday heading “Feast Day” select Add_Months(celebrateddate,6) Feastday from Holiday where Holiday like ‘Fast%’; Feast Day ----------- 22-Aug-95 • Subtracting Months column Lastday heading “Last Day” select Add_Months(celebrateddate,-6) - 1 Lastday from Holiday where Holiday = ‘Columbus Day’; Last Day ---------- 08-Apr-95 • Greatest and Least select Holiday, Least(Actualdate, Celebrateddate) First, Actualdate, Celebrateddate from Holiday where Actualdate – Celebrateddate != 0; HOLIDAY First ACTUALDAT CELEBRATE ----------------------------- ------- ------------------ ----------------- Martin Luther King, Jr. 15-Jan-95 15-Jan-95 16-Jan-95 Lincoln’s Birthday 12-Feb-95 12-Feb-95 20-Feb-95 Washington’s Birthday 20-Feb-95 22-Feb-95 20-Feb-95 Memorial Day 29-May-95 30-May-95 29-May-95 Columbus Day 08-Oct-95 08-Oct-95 09-Oct-95

Page 4: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

This is not true for literals such as ‘20-Jan-95’ because they are treated as strings Next_Day Next_Day figures the date of the next named day of the week after the given date select Cycledate from Payday; Cycledate ------------ 15-Jan-95 15-Feb-95 15-Mar-95 15-Apr-95 15-May-95 15-Jun-95 15-Jul-95 15-Aug-95 15-Sep-95 15-Oct-95 15-Nov-95 15-Dec-95 column Payday heading “Pay Day!” select Next_Day(cycledate, ‘Friday’) Payday from Payday;

Page 5: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

Pay Day! ----------- 20-Jan-95 17-Feb-95 17-Mar-95 21-Apr-95 19-May-95 16-Jun-95 21-Jul-95 18-Aug-95 22-Sep-95 20-Oct-95 17-Nov-95 22-Dec-95 This is nearly correct, except for September and December as they are Fridays column Payday heading “Pay Day” select Next_Day(cycledate-1, ‘Friday’) Payday from Payday; Pay Day! ----------- 20-Jan-95 17-Feb-95 17-Mar-95 21-Apr-95 19-May-95 16-Jun-95 21-Jul-95 18-Aug-95 15-Sep-95 20-Oct-95 17-Nov-95 15-Dec-95

Page 6: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

Last_Day Last_Day produces the date of the last day of the month column EndMonth heading “End Month” select Last_Day(cycledate) EndMonth from Payday; End Month -------------- 31-Jan-95 28-Feb-95 31-Mar-95 30-Apr-95 31-May-95 30-Jun-95 31-Jul-95 31-Aug-95 30-Sep-95 31-Oct-95 30-Nov-95 31-Dec-95 Months_Between Two Dates select * from Birthday; Firstname Lastname Birthdate Age ------------ ------------ ----------- ------ George Sand 12-May-46 46 Robert James 23-Aug-37 52 Nancy Lee 02-Feb-47 42 Victoria Lynn 20-May-49 42 Frank Pilot 11-Nov-42 42

Page 7: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

select Firstname, Lastname, Birthdate, Age, Months_Between(SysDate, Birthdate)/12 “Actual Age” from Birthday; Firstname Lastname Birthdate Age Actual Age ------------ ------------ ----------- ------ ------------- George Sand 12-May-46 46 51.99 Robert James 23-Aug-37 52 60.71 Nancy Lee 02-Feb-47 42 51.26 Victoria Lynn 20-May-49 42 48.97 Frank Pilot 11-Nov-42 42 55.49 Combining Date Functions select SysDate Today, Last_Day(Add_Months(SysDate, 6)) + 1 Review from Dual; Today Review ------- --------- 02-Apr-98 01-Nov-98 select (Last_Day(Add_Months(SysDate,6)) + 1) – SysDate Wait from Dual; Wait ------ 213 Round and Trunc in Date Calculations Sysdate ---------- 01-Aprl-98

Page 8: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

select To_Date(‘02-Apr-98’) - SysDate from Dual; To_Date(‘02-Apr-98’) - SysDate ----------------------------------------- .516 The reason for fractional number of days between today and tomorrow is that Oracle keeps hours, minutes and seconds with its dates. To get the rounded number of days between today and tomorrow, use this: select To_Date(‘02-Apr-98’) – Round(SysDate) from Dual; To_Date(‘02-Apr-98’) – Round(SysDate) --------------------------------------------------- 1 To_Date and To_char Formatting • To_Date converts a character string or a number into an oracle date • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]])) • Date must be column defined as a DATE datatype in Oracle. It cannot be

a string even if it is in the default date format of DD-MON-YY. • The only way to use a string where date appears in the To_Char function

is to enclose it within a To_Date function. • String is a literal string, a literal number, or a database column containing

a string or a number. The default format starts out as ‘DD-MON-YY’, but you can change this with

Page 9: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

Alter session set NLS_Date_Format =”dd/mon/yyyy”; for a given SQL session or with the NLS_Date_Format init.ora parameter • NLSparameters is a string that sets the NLS_DATE_LANGUAGE option

to a specific language, as opposed to using the language for the current SQL session.

TO_Char function • First define a column format for the To_Char function results; without it,

To_Char function will produce a column in SQLPLUS nearly 100 characters wide. By renaming the column for readability and setting its format to 30 characters, a practical display is produced:

Column Formatted format a30 word_wrapped Select Birthdate, To_Char(Birthdate,’MM/DD/YY’) Formatted From Birthday Where Firstname = ‘Victoria’; Birthdate Formatted ----------- ------------- 20-May-49 05/20/49 The slashes (/) are just punctuation Select birthdate, To_char(BirthDate, ‘YYMM>DD’) Formatted From Birthday Where FirstName = ‘Victoria’; Birthdate Formatted ----------- ------------- 20-May-49 4905>20 • Oracle also allows to insert text into the format by enclosing the desired

text in double quotation marks:

Page 10: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

Select Birthdate, To_Char(Birthdate, ‘Month, DDth “in, um,” YyyY’) Formatted From Birthday; Bithdate Formatted ---------- ------------- 12-May-46 May , 12TH in, um, 1946 23-Aug-37 August , 23RD in, um, 1937 02-Feb-47 February , 02ND in, um, 1947 20-May-49 May , 20TH in, um, 1949 11-Nov-42 November , 11TH in, um, 1942 A suffix of th on the DD tells Oracle to use Ordinal suffixes such as “TH”, “RD”, and “ND” with the number. YyyY is included simply to show that case is irrelevant unless a suffix such as Th is being used Select Birthdate, To_Char(birthdate, ‘Month, ddth, YyyY’) Formatted From Birthday; Birthdate Formatted ----------- ------------ 12-may-46 May , 12th, 1946 23-Aug-37 August , 23rd, 1937 02-Feb-47 February , 02nd, 1947 20-May-49 May , 20th, 1949 11-Nov-42 November , 11th, 1942

Page 11: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

Select Birthdate, To_Char(Birthdate, fmMonth, ddth, YyyY) Formatted From Birthday; Birthdate Formatted ----------- ------------ 12-May-46 May, 12th, 1946 23-Aug-37 August, 23rd, 1937 02-Feb-47 February, 2nd, 1947 20-May-49 May, 20th, 1949 11-Nov-42 November, 11th, 1942 select FirstName, Birthdate, To_Char(Birthdate, ‘”baby Girl on” fmMonth ddth, YYYY, “at” HH:MI “ in the Morning”’) Formatted from Birthday where FirstName = ‘Victoria’; FirstName Birthdate Formatted ------------ ----------- ------------- Victoria 20-May-49 Baby Girl on May 20th, 1949, at 3:27 in the morning • To spell out the date use sp Select FirstName, Birthdate, To_Char(Birthdate, ‘”Baby Girl on the” Ddsp “of” fmMonth, YYYY, “at” HH:MI’) Formatted from Birthday where FirstName = ‘Victoria’; FirstName Birthdate Formatted ------------ ------------- -------------------------------------------- Victoria 20-May-49 Baby Girl on the Twenty of May, 1949, at 3:27

Page 12: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

Add th suffix to the sp Select FirstName, Birthdate, To_Char(Birthdate, ‘”Baby Girl on the” Ddspth “of” fmMonth, YYYY, “at” HH:MI’) Formatted from Birthday where FirstName = ‘Victoria’; FirstName Birthdate Formatted ------------ ------------- ------------------------------------------------------------ Victoria 20-May-49 Baby Girl on the Twentieth of May, 1949, at 3:27 • Add A.M. or P.M. to the time Select FirstName, Birthdate, To_Char(Birthdate, ‘”Baby Girl on the” Ddspth “of” fmMonth, YYYY, “at” HH:MI P.M.’) Formatted from Birthday where FirstName = ‘Victoria’; FirstName Birthdate Formatted ------------ ------------- ------------------------------------------------------------ Victoria 20-May-49 Baby Girl on the Twentieth of May, 1949, at 3:27 A.M. The Most Common To_Char error Interchange the ‘MM’ (Month) format with the ‘MI’ (Minutes) format Select To_Char(sysDate, ‘HH:MM:SS’) Now From Dual; Now ----------- 10:01:30

Page 13: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

New_Time – Switching Time Zones Currently this function works only for the zones between Greenwich, England and Hawaii New_Time(date,’this’,’other’) select Birthdate, New_Time(Birthdate,’EST’,’HST’) from Birthday where FirstName = ‘Victoria’; Birthdate New_Time( ------------ ---------------- 20-May-49 19-May-49 select To_Char(Birthdate,’fmMonth Ddth, YYYY “at” HH:MI AM’) Birth, To_Char(New_Time(Birthdate,’EST’,’HST’), ‘fmMonth ddth, YYYY “at” HH:MI AM’) Birth from Birthday where FirstName = ‘Victoria’; Birth Birth ------------------------------------ --------------------------------------------- May 20th, 1949 at 3:27 AM May 19th, 1949 at 10:27 PM To_Date Calculations To_Date(string[,’format’]) To put the string 22-Feb-98 into Oracle date format, use this: select To_Date(‘22-Feb-98’,’DD-MON-YY’) from Dual; To_Date(‘ ------------- 22-Feb-98

Page 14: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

22-Feb-98 format is already in the default format in which Oracle displays and accepts dates, so the format can be left out. select To_Date(22-Feb-98’) from Dual; • When the format matches the literal string, the string is successfully

converted to a date and is then displayed in default date format: select To_Date(‘02/22/98’,’MM/DD/YY’) from Dual; To_Date(‘ ------------- 22-Feb-98 • Suppose you needed to know the day of the week of February 22. The

To_Char function will not work, even with the literal string in the proper format.

select To_Char(‘22-Feb-98’,’Day’) from Dual; Error at line 1: Invalid To_Char format select To_Char(To_Date(‘22-Feb-98’), ‘Day’) from Dual; To_Char(To_Date(‘22-Feb-98’), ‘Day’) ------------------------------------------------ Sunday • To_Date can also accept numbers, without single quotation marks,

instead of strings, as long as they are formatted consistently. select To_Date(11051946,’MM DD YYYY’) from Dual; To_Date(1 --------------- 05-Nov-46

Page 15: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

select To_Date(‘August, 20, 1949, 3:27 A.M. ‘, ‘fmMonth, Dd, YYYY, HH:MI P.M.’) Formatted from Birthday where FirstName = ‘Victoria’; Formatted -------------- 20-Aug-49 Dates in Where Clauses select Holiday, celebrateddate from Holiday where Celebrateddate BETWEEN To_Date(‘01-Jan-1995’,’DD-MON-YYYY’) and To_Date(‘22-Feb-1995’,’DD-MON-YYYY’); HOLIDAY CELEBRATE ------------------------------------- ----------------- New Year Day 01-Jan-95 Martin Luther King, Jr. 16-Jan-95 Lincoln’s Birthday 20-Feb-95 Washington’s Birthday 20-Feb-95 Fast Day, New Hampshire 22-Feb-95 The logical operator IN works as well with literal strings: select Holiday, celebrateddate from Holiday where Celebrateddate IN (‘01-Jan-1995’,‘22-Feb-1995’); HOLIDAY CELEBRATE ------------------------------------- ----------------- New Year Day 01-Jan-95 Fast Day, New Hampshire 22-Feb-95

Page 16: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

• Least and Greatest do not work, because they assume the literal strings are strings, not dates.

select Holiday, celebrateddate from Holiday where Celebrateddate = LEAST(‘16-Jan-95’,’04-Sep-95’); Holiday celebrate ------------------------ -------------- Labor Day 04-Sep-95 • In order for LEAST and GREATEST to work properly, the To_Date

function must be applied to the literal strings. Dealing with the Year 2000 • In Oracle, all date values have century values. If you only specify the last

two digits of the year value, then Oracle will, by default, use the current century as the century value when it inserts a record.

insert into Birthday (FirstName, LastName, Birthdate, Age) values(‘ALICIA’, ‘ANN’, ‘21-NOV-39’, NULL); select To_Char(Birthdate,’DD-MON-YYYY’) Bday from Birthday where FirstName = ‘ALICIA’ and LastName = ‘ANN’; Bday ---------------- 21-Nov-2039 • The ‘RR’ year format mask rounds the two-digit year to the closest

century. Year values of 00-49 use the current century (so ‘01’ becomes ‘2001’) while year values of 50-99 are rounded to the previous century (so ‘99’ becomes ‘1999’).

Page 17: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

select To_Char(To_Date(‘000102’,’RRMMDD’),’DD-MON-YYYY’) RRMMDD, To_Char(To_Date(‘000102’,’YYMMDD’),’DD-MON-YYYY’) YYMMDD from Dual; RRMMDD YYMMDD ----------------- ---------------- 02-Jan-1900 02-Jan-2000 EXTRACT Function Oracle 9i – you can use the EXTRACT function in place of the TO_CHAR function when you are selecting portions of date values – such as just the month or day from a date. The EXTRACT function’s syntax is EXTRACT ( { { YEAR

| MONTH | DAY | HOUR | MINUTE | SECOND }

| { TIMEZONE_HOUR | TIMEZONE_MINUTE }

| { TIMEZONE_REGION | TIMEZONE_ABBR }

} FROM {datetime_value_expression | interval_value_expression } ) For instance, to extract the month in which Victoria was born, you could execute the following:

Page 18: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

Select Birthdate, Extract(Month from Birthdate) AS Month From BIRTHDAY Where Firstname = ‘VICTORIA’; BIRTHDATE MONTH ----------------- ------------------ 20-MAY-49 5 For more complex extractions, you will need to use TO_CHAR but EXTRACT can support many common date value queries. TIMESTAMP Datatypes Oracle 9i – TIMESTAMP datatypes store date to the billioneth of a second. The base datatype for timestamp values is called TIMESTAMP. Like DATE it stores the year, month, day, hour, minute, and second. It also includes a fractional_seconds_precision setting that determines the number of digits in the fractional part of the seconds field. By default, the precision is 6; valid values are 0 to 9. Create table X1 (tscol TIMESTAMP(5)); insert into X1 values (SYSTIMESTAMP); select * from X1; TSCOL -------------------------------------------------------------------- 15-MAR-02 04:58:06.00542 PM Create table X2 (tscol TIMESTAMP(5) WITH TIME ZONE); insert into X2 values (SYSTIMESTAMP);

Page 19: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

select * from X2; TSCOL -------------------------------------------------------------------- 15-MAR-02 04:58:06.00542 PM -05:00 The time zone is displayed as an offset of Coordinated Universal Time (UTC). The database is presently set to a time zone that is five hours prior to UTC. Oracle also supports TIMESTAMP(fractional_seconds_precision) WITH LOCAL TIME ZONE datatype. The data is normalized to the database time zone when it is stored in the database, and during retrievals the users see data in the session time zones. INTERVAL YEAR (year_precision) TO MONTH INTERVAL DAY (day_precision) TO SECOND(fractional_seconds_precision) Other Data Functions Oracle 9i CURRENT_TIMESTAMP Returns the current timestamp with the active time zone information. DBTIMEZONE Returns the current database timezone, in UTC format. FROM_TZ(timestamp) Converts a timestamp value to a timestamp with time zone value. LOCALTIMESTAMP Returns the local timestamp in the active time zone, with no time zone information shown.

Page 20: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

Time zones are as follows: AST/ADT Atlantic standard/daylight time BST/BDT Bering standard/daylight time CST/CDT Central standard/daylight time EST/EDT Eastern standard/daylight time GMT Greenwich mean time HST/HDT Alaska-Hawaii standard/daylight time MST/MDT Mountain standard/daylight time NST Newfoundland standard time PST/PDT Pacific standard/daylight time YST/YDT Yukon standard/daylight time NUMTODSINTERVAL(‘value’, ‘dateunit’) Converts value to an INTERVAL DAY TO SECOND literal, where ‘dateunit’ is ‘DAY’, ‘HOUR’, ‘MINUTE’, or ‘SECOND’. NUMTOYMINTERVAL(‘value’, ‘dateunit’) Converts value to an INTERVAL YEAR TO MONTH literal, where ‘dateunit’ is ‘YEAR’, or ‘MONTH’. SESSIONTIMEZONE Returns the value of the current session’s timezone. SYS_EXTRACT_UTC Extracts the Coordinated Universal Time (UTC) from the current date. SYSTIMESTAMP Returns the system date, including fractional seconds and time zone of the database.

Page 21: Dates in Oracle - Laurentian · • To_Char converts an Oracle date into a character string To_Char(date[,’format’[,’NLSparameters’]])) To_Date(string[,’format’[,’NLSparameters]]))

TO_DSINTERVAL(‘value’) Converts value of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype to an INTERVAL DAY TO SECOND datatype. TO_YMINTERVAL(‘value’) Converts value of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype to a value of INTERVAL YEAR TO MONTH datatype. TO_TIMESTAMP(‘value’) Converts value of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype to a value of TIMESTAMP datatype. TO_TIMESTAMP_TZ(‘value’) Converts value of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype to a value of TIMESTAMP WITH TIMEZONE datatype. TZ_OFFSET(‘value’) returns the time zone offset corresponding to the value entered based on the date the statement is executed.