Oracle PL/SQL/Date Timezone/SYSDATE — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 10:01, 26 мая 2010
Содержание
- 1 Adding One Month a certain date
- 2 Adding Two Months to SYSDATE
- 3 Combining TO_CHAR to Format SYSDATE
- 4 Finding the First Monday After the Current Date and Time
- 5 Get current operating system date
- 6 Minus days from sysdate
- 7 Removing the Time from SYSDATE
- 8 select sysdate
- 9 Truncating to the Quarter
- 10 use sysdate to init a variable
- 11 Using the SYSDATE function: return the current date from the system
Adding One Month a certain date
SQL>
SQL> Select ADD_MONTHS(TO_DATE("31-MAR-99"),1) from DUAL;
ADD_MONTHS(TO_DATE("
--------------------
30-APR-0099 00:00:00
1 row selected.
SQL>
SQL> --
Adding Two Months to SYSDATE
SQL>
SQL> SELECT ADD_MONTHS(SYSDATE,2) from DUAL;
ADD_MONTHS(SYSDATE,2
--------------------
16-AUG-2008 18:08:07
1 row selected.
SQL>
SQL>
SQL> --
Combining TO_CHAR to Format SYSDATE
SQL>
SQL> SELECT TO_CHAR(SYSDATE,"MM/DD/YYYY HH:MM:SS AM")
2 "Today"s Date and Time" from DUAL;
Today"s Date and Time
----------------------
06/16/2008 06:06:07 PM
1 row selected.
SQL>
SQL> --
Finding the First Monday After the Current Date and Time
SQL> --
SQL>
SQL> SELECT TO_CHAR(NEXT_DAY(SYSDATE,"Monday"),"MM/DD/YYYY HH:MM:SS AM")
2 "Next_Day"
3 from DUAL;
Next_Day
----------------------
06/23/2008 06:06:08 PM
1 row selected.
SQL>
SQL> --
Get current operating system date
SQL>
SQL> SELECT SYSDATE FROM dual;
SYSDATE
---------
30-AUG-06
SQL>
SQL>
Minus days from sysdate
SQL>
SQL> CREATE TABLE purchase (
2 product_name VARCHAR2(25),
3 salesperson VARCHAR2(3),
4 purchase_date DATE,
5 quantity NUMBER(4,2));
Table created.
SQL>
SQL> INSERT INTO purchase VALUES ("Small Widget", 10, sysdate, "1);
ERROR:
ORA-01756: quoted string not properly terminated
SQL> INSERT INTO purchase VALUES ("Medium Widget", 15, sysdate-14, 2);
1 row created.
SQL> INSERT INTO purchase VALUES ("Round Station", 25, sysdate-7, 3);
1 row created.
SQL> INSERT INTO purchase VALUES ("Product Number", 10, sysdate+7, 4);
1 row created.
SQL>
SQL>
SQL>
SQL> DROP TABLE purchase;
Table dropped.
SQL>
SQL>
Removing the Time from SYSDATE
SQL>
SQL> SELECT TO_CHAR(TRUNC(SYSDATE),"MM/DD/YYYY HH:MM:SS AM")
2 "Today"s Date and Time"
3 from DUAL;
Today"s Date and Time
----------------------
06/16/2008 12:06:00 AM
1 row selected.
SQL> --
select sysdate
SQL>
SQL> select sysdate
2 from dual
3 /
SYSDATE
---------
16-JUN-08
1 row selected.
SQL> --
Truncating to the Quarter
SQL> --
SQL>
SQL> SELECT TO_CHAR(TRUNC(SYSDATE,"Q"),"MM/DD/YYYY HH:MM:SS AM")
2 "Today"s Date and Time"
3 from DUAL
4
SQL> --
use sysdate to init a variable
SQL>
SQL> set echo on
SQL>
SQL> DECLARE
2 v_MyChar VARCHAR2(20) := "test";
3 v_NUMBER NUMBER;
4 V_Date DATE := SYSDATE;
5 v_counter INTEGER;
6 BEGIN
7 DBMS_OUTPUT.PUT_LINE("This is a Test");
8 DBMS_OUTPUT.PUT_LINE("Of Syntax Error Debugging");
9 For v_COUNTER IN 1..5 LOOP
10 DBMS_OUTPUT.PUT_LINE("You are in loop:" || v_counter);
11 END LOOP;
12 END;
13 /
This is a Test
Of Syntax Error Debugging
You are in loop:1
You are in loop:2
You are in loop:3
You are in loop:4
You are in loop:5
PL/SQL procedure successfully completed.
SQL>
SQL>
SQL>
SQL> --
Using the SYSDATE function: return the current date from the system
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values ("01","Jason", "Martin", to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto", "Programmer")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("02","Alison", "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("03","James", "Smith", to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("04","Celia", "Rice", to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("05","Robert", "Black", to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("06","Linda", "Green", to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York", "Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("07","David", "Larry", to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York", "Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("08","James", "Cat", to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
3 /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
8 rows selected.
SQL>
SQL>
SQL>
SQL>
SQL>
SQL>
SQL>
SQL> --Using the SYSDATE function: return the current date from the system.
SQL>
SQL> SELECT ID, Start_Date FROM Employee WHERE Start_Date < SYSDATE;
ID START_DAT
---- ---------
01 25-JUL-96
02 21-MAR-76
03 12-DEC-78
04 24-OCT-82
05 15-JAN-84
06 30-JUL-87
07 31-DEC-90
08 17-SEP-96
8 rows selected.
SQL>
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
SQL>
SQL>