Chapter 5 Date and Conversion Functions

Similar documents
Date / Time Arithmetic with Oracle

TO_CHAR Function with Dates

USING CONVERSION FUNCTIONS

Conversion Functions

6. ARITHMETIC AND DATE FUNCTIONS

Key Functions in Oracle SQL

Advanced Tutorials. The Dark Side of the Transparent Moon

Utility Software II lab 1 Jacek Wiślicki, jacenty@kis.p.lodz.pl original material by Hubert Kołodziejski

3.GETTING STARTED WITH ORACLE8i

Oracle SQL. Course Summary. Duration. Objectives

Programming with SQL

Single-Row Functions Schedule: Timing Topic

Oracle Database: Introduction to SQL

2. Which of the following declarations is invalid? Mark for Review (1) Points

Oracle Database 12c: Introduction to SQL Ed 1.1

Oracle Database: SQL and PL/SQL Fundamentals

STRING, CONVERSION, AND MISCELLANEOUS FUNCTIONS

.NET Standard DateTime Format Strings

Oracle Database 11g SQL

Oracle Database: Introduction to SQL

Best Practices in SQL Programming. Madhivanan

Using Single-Row Functions to Customize Output. Copyright 2006, Oracle. All rights reserved.

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: Introduction to SQL

MONASH UNIVERSITY. Faculty of Information Technology

Oracle Database: SQL and PL/SQL Fundamentals NEW

SQL and Data. Learning to Retrieve Data Efficiently and Accurately

CONVERSION FUNCTIONS QUESTIONS

REPORT GENERATION USING SQL*PLUS COMMANDS

Field Properties Quick Reference

5.1 Database Schema Schema Generation in SQL

Short Notes on Stata (version 10) Data Functions and Stuff

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Creating PL/SQL Blocks. Copyright 2007, Oracle. All rights reserved.

How To Monitor A Log File On Kserver On A Linux Computer Or Macintosh (Amd64) On A Macintosh 2.5 (Amd32) On An Amd64 (Amd86) On Your Computer Or Your Macintosh 3.5

Writing Days and Months

MS Access: Advanced Tables and Queries. Lesson Notes Author: Pamela Schmidt

3 Data Properties and Validation Rules

A table is a collection of related data entries and it consists of columns and rows.

Kaseya 2. User Guide. Version 7.0. English

7 Using the Alarm Ready Schedule

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Microsoft Access 2010 Tables & Field Properties

SQL. Short introduction

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle For Beginners Page : 1

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro

1 Stored Procedures in PL/SQL 2 PL/SQL. 2.1 Variables. 2.2 PL/SQL Program Blocks

Oracle Database 10g: Introduction to SQL

TrendWorX32 SQL Query Engine V9.2 Beta III

Nasdaq Dubai TRADING HOLIDAYS AND SETTLEMENT CALENDAR REVISION For Equities Outsourced to the DFM (T+2)

Porting from Oracle to PostgreSQL

SOCIAL ENGAGEMENT BENCHMARK REPORT THE SALESFORCE MARKETING CLOUD. Metrics from 3+ Million Twitter* Messages Sent Through Our Platform

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

Creating Database Tables in Microsoft SQL Server

ETL TESTING TRAINING

Online Trade file structure for Equity Derivatives & Currency Derivatives segment

How to Fix Mail-Merge Number Formatting in Word 2010

Introduction To Microsoft Office Excel Bob Booth July 2008 AP-Excel8

TIMESHEET EXCEL TEMPLATES USER GUIDE. MS-Excel Tool User Guide

How To Create A Table In Sql (Ahem)

Exploring Microsoft Office Access Chapter 2: Relational Databases and Multi-Table Queries

Universe Best Practices Session Code: 806

Netezza SQL Class Outline

Note: The where clause of the SUBSET statement is sent "as is" to Oracle, so it must contain correct Oracle syntax.

MS ACCESS DATABASE DATA TYPES

Information Technology

MYSQL DATABASE ACCESS WITH PHP

Cruise Line Agencies of Alaska. Cruise Ship Calendar for 2016 FOR PORT(S) = KTN AND SHIP(S) = ALL AND VOYAGES = ALL

Cruise Line Agencies of Alaska. Cruise Ship Calendar for 2016 FOR PORT(S) = KTN AND SHIP(S) = ALL AND VOYAGES = ALL

Managing Users and Identity Stores

Firebird. Embedded SQL Guide for RM/Cobol

Ways We Use Integers. Negative Numbers in Bar Graphs

One positive experience I've had in the last 24 hours: Exercise today:

Data Validation, Consistency & Defaulting

Recognizing PL/SQL Lexical Units. Copyright 2007, Oracle. All rights reserved.

SQL Server An Overview

How To Understand The Basic Concepts Of A Database And Data Science

PAYLINE USER GUIDE. 1 Logging into Payline. 2 - Processing a Purchase

Breen Elementary School

Using AD fields in Policy Patrol

PL / SQL Basics. Chapter 3

Introduction to SAS Informats and Formats

Answers to the Try It Yourself Sections

Topics Advanced PL/SQL, Integration with PROIV SuperLayer and use within Glovia

Q&As: Microsoft Excel 2013: Chapter 2

Developing strategic analytics applications on OBICS PaaS. Or how we re doing so far

Template Guide. HTML . Release 8. This template guide is an overview of how to use and customize an HTML templates with Conga Composer.

PAYCHEX, INC. BASIC BUSINESS MATH TRAINING MODULE

CANTOR EXCHANGE FOREIGN EXCHANGE RATE SPOT INDEX DAILY FUTURES AND BINARY FLEX OPTION CONTRACT RULES

Access Tutorial 2: Tables

Appendix A Practices and Solutions

Network Planning and Analysis

ACADEMIC CALENDAR June 2016

Working with DB2 UDB objects

Choosing a Data Model for Your Database

Database Administration with MySQL

How to Find Coordinates on Common Smartphones

Transcription:

Chapter 5 Date and Conversion Functions Converting From One Datatype To Another Comparisons must involve items of the same data type WHERE type ='BUS' WHERE length > 1500 May need to convert an item to match the data type of another WHERE issue = TO_DATE('01/01/2001', 'MM/DD/YYYY'); 2 1

Datatype Conversion Functions Are single-row functions Do not change the value stored in the field provide a temporary, on-the-fly transformation for that specific SQL statement Use format masks to specify how to display the result of a conversion function 3 Converting From One Datatype To Another TO_NUMBER TO_DATE NUMBER CHARACTER DATE TO_CHAR TO_CHAR 4 2

TO_CHAR(date, 'format_mask') TO_CHAR TO_CHAR(number, 'format_mask') Converts a DATE or NUMBER to a VARCHAR2 Used to control the formatting when dates or numbers are displayed Practice Time SELECT ln, lastcontact, amount FROM writer; SELECT ln, TO_CHAR(lastcontact,'mm/dd/yyyy'), TO_CHAR(amount, '$9,990.99') FROM writer; 5 Numeric Format Elements (pg 255) Element Description Element Result 9 Single digit (zero-suppressed) 999999 1234 0 Single digit (enforce zero) 099999 01234 $ Floating dollar sign $9999 $1234 L Local currency symbol L9999 L1234. Decimal point separator 9999.99 1234.00, Thousand separator 9,999 1,234 6 3

Date Data Type Oracle stores in an internal numeric format that stores century, year, month, day, hours, minutes, seconds Can store values Jan 1, 4712 BC to Dec 31, 9999 AD SYSDATE function returns current date and time Default date format is DD-MON-RR Practice Time SELECT SYSDATE FROM dual; SELECT TO_CHAR(SYSDATE, 'mm/dd/yyyy HH:MI:SS AM') FROM dual; 7 Guessing Games What if you don t specify the century? eg: 03-Mar-33 Ambiguous when enter/display only 2-digit years eg: what year is 15-MAR-02? eg: what year is 15-MAR-88? To avoid ambiguity, always enter/display 4-digit years 8 4

RR Date Format When a 2-digit date is entered under RR mask, Oracle presumes the century using these rules If two digits of the current year are: 00 49 50 99 If the specified two-digit year is: 00 49 50 99 The return date is in the current century The return date is in the century after the current one The return date is in the century before the current one The return date is in the current century thus TO_DATE('15-MAR-02', 'DD-MON-RR') refers to '15-MAR-2002 thus TO_DATE('15-MAR-98', 'DD-MON-RR') refers to '15-MAR-1998' 9 YY Date Format (pg 198) When a two-digit date is entered under YY mask, the date always has the same first 2 digits as the current year (i.e., the current century) thus TO_DATE('15-MAR-02', 'DD-MON-YY') refers to '15-MAR-2002' thus TO_DATE('15-MAR-98', 'DD-MON-YY') refers to '15-MAR-2098' 10 5

TO_DATE TO_DATE(char[, 'format_mask']) Converts a VARCHAR2, CHAR, or NUMBER to a DATE TO_DATE('01/01/2001', 'MM/DD/YYYY') TO_DATE('January 15, 2001, 11:00 A.M., 'Month dd, YYYY, HH:MI A.M.') 11 Date Formatting Elements (pg 192+) YYYY 4-digit numeric year YEAR Year spelled out RR 2-digit year in numbers, based on century rule MM 2-digit numeric month MON Abbreviated name of month (eg: JAN, AUG) MONTH Name of month (eg: May, September). Always length of 9 DD Numeric day of month DAY Full day of week (eg: Tuesday, Wednesday). Always length of 9 DY 3-letter abbreviation of the day of the week (MON, SAT) D Numeric day of the week (eg: 1 = Sunday, 7=Saturday) DDD Numeric day of year (1-366) DL Day Long (fmday, Month, DD, YYYY) WW Week of year (1-53) Q Quarter of the year 12 6

Date Datatype Implicit conversion SELECT title, issue FROM article WHERE issue = '01-JAN-01'; Explicit conversion SELECT title, issue FROM article WHERE issue = TO_DATE('01-JAN-2001', 'DD-MON-YYYY'); SELECT title, issue FROM article WHERE issue = TO_DATE('01/01/2001', 'MM/DD/YYYY'); 13 Time Formatting Elements (pg 193) HH or HH12 Hour (0-12) HH24 Hour in military format (0-23) MI Minutes SS Seconds SSSSS Seconds since midnight (0-86399) AM or PM Indicate AM or PM TS Time Short (HH:MI:SS AM) 14 7

Practice Time Using Date/Time Format Masks SELECT course_no, start_date_time ; SELECT course_no, TO_CHAR(start_date_time, 'DD-MON-YYYY HH24:MI') ; SELECT course_no, TO_CHAR(start_date_time, 'Day mm/dd/yyyy') ; 15 Using Date/Time Format Masks Watch out for case and for spaces provided by elements SELECT section_id, TO_CHAR(start_date_time, 'mm-dd-yyyy DAY') WHERE TO_CHAR(start_date_time, 'DAY') = 'Friday'; SELECT section_id, TO_CHAR(start_date_time, 'mm-dd-yyyy DAY') WHERE TO_CHAR(start_date_time, 'DAY') = 'FRIDAY'; 16 8

Using Date/Time Format Masks Fill Mode (pg204) suppresses extra blanks provided by certain format mask elements SELECT course_no, section_id, TO_CHAR(start_date_time, 'DAY') WHERE TO_CHAR(start_date_time, 'fmday') = 'FRIDAY'; 17 Character vs Date Comparisons (pg205+) Results when ascending sort? Tom Jon Dan Jim Pat April 14, 2011 July 13, 1988 February 3, 2011 Are These WHERE Clauses Equivalent? SELECT course_no, TO_CHAR(start_date_time,'Month dd, yyyy') WHERE start_date_time < TO_DATE('May 14, 2007','Month dd, yyyy'); SELECT course_no, TO_CHAR(start_date_time,'Month dd, yyyy') WHERE TO_CHAR(start_date_time,'Month dd, yyyy') < 'May 14, 2007'; 18 9

Using Dates and Times When no time is provided, Oracle stores a default time of midnight When no day of month is provided, Oracle stores a default day of 1 SELECT TO_CHAR(issue, 'mm/dd/yyyy hh:mi am') FROM article; Watch out for times queries may not perform as expected SELECT course_no, start_date_time WHERE start_date_time = '14-JUL-07'; SELECT DISTINCT TO_CHAR(start_date_time,'MM/DD/YYYY HH:MI AM') ; SELECT course_no, start_date_time WHERE TRUNC(start_date_time) = '14-JUL-07'; 19 Lab 5.2 Performing Date & Time Arithmetic Add/subtract a number to/from a date for a resulting date Subtract two dates to find the number of days in between SELECT registration_date, registration_date+7 AS "Fees Due" FROM student; SELECT TO_DATE('13-JAN-11')-TO_DATE('31-DEC-10') "Day #" FROM dual; Add hours to a date by dividing the number of hours by 24 Add minutes to a date by dividing the number of minutes by 1440 SELECT TO_CHAR(SYSDATE, 'mm/dd/yyyy hh:mi AM'), TO_CHAR(SYSDATE +3/24, 'mm/dd/yyyy hh:mi AM'), TO_CHAR(SYSDATE +30/1440, 'mm/dd/yyyy hh:mi AM') FROM dual; 20 10

Practice Time: Date/Time Arithmetic User ttrollen has a table named workorder Review its structure Display its contents, show date and time for each date Calculate the duration of each workorder incremental development 21 Date Functions SYSDATE TRUNC(date) ROUND(date, [format_mask]) Returns date/time from Oracle server machine Truncate date to omit time info Round date Extracts numeric year, month or day from DATE datatype (not time elements) EXTRACT(datepart FROM date) MONTHS_BETWEEN(date2, date1) ADD_MONTHS(date, integer) NEXT_DAY(date, dayofweeek) LAST_DAY(date) Extracts numeric year, month, day, hour, minute, second from TIMESTAMP datatype Number of months between two dates Add calendar months to date Next day of the date specified Last day of specified month 22 11

Lab 5.5 Cast Function CAST(expression AS datatype) Converts among common Oracle data types VARCHAR2, CHAR, NUMBER, DATE An alternative to using Oracle-specific type conversion functions SELECT title, issue FROM article WHERE issue = TO_DATE('01-JAN-2001', 'DD-MON-YYYY'); SELECT title, issue FROM article WHERE issue = CAST('01-JAN-2001' AS DATE); 23 TO_NUMBER TO_NUMBER(char[, 'format_mask']) Converts a VARCHAR2 or CHAR to a NUMBER 24 12