Domanda

Table Employee:

CREATE TABLE Employee(
    ID CHAR(10) PRIMARY KEY,
    SSN CHAR(15) NOT NULL,
    FNAME CHAR(15),
    LNAME CHAR(15),
    DOB DATE NOT NULL
);
INSERT INTO Employee VALUES('0000000001','078-05-1120','George','Brooks', '24-may-85');
INSERT INTO Employee VALUES('0000000002','917-34-6302','David','Adams', '01-apr-63');
INSERT INTO Employee VALUES('0000000003','078-05-1123','Yiling','Zhang', '02-feb-66');
INSERT INTO Employee VALUES('0000000004','078-05-1130','David','Gajos', '10-feb-65');
INSERT INTO Employee VALUES('0000000005','079-04-1120','Steven','Cox', '11-feb-79');
INSERT INTO Employee VALUES('0000000006','378-35-1108','Eddie','Gortler', '30-may-76');
INSERT INTO Employee VALUES('0000000007','278-05-1120','Henry','Kung', '22-may-81');
INSERT INTO Employee VALUES('0000000008','348-75-1450','Harry','Leitner', '29-oct-85');
INSERT INTO Employee VALUES('0000000009','256-90-4576','David','Malan', '14-oct-88');
INSERT INTO Employee VALUES('0000000010','025-45-1111','John','Brooks', '28-nov-78');
INSERT INTO Employee VALUES('0000000011','025-59-1919','Michael','Morrisett', '04-nov-85');
INSERT INTO Employee VALUES('0000000012','567-45-2351','David','Nelson', '10-nov-54');
INSERT INTO Employee VALUES('0000000013','100-40-0011','Jelani','Parkes', '20-dec-44');

When I use queries like:

SELECT * FROM EMPLOYEE WHERE DOB < '01-jan-80';

I did not get the record of '0000000013','100-40-0011','Jelani','Parkes', '20-dec-44'. I think that might be a date format problem, but I am not sure. Anyone have an idea? Thanks!

È stato utile?

Soluzione 2

you provided year in 2 digit format(rr)

then year < 50 will be 20xx and year > 50 will be 19xx

means 56 will be 1956 44 means 2044

that's why it was excluded

Altri suggerimenti

Well, you have a DATE datatype for your DOB column, so, that's a good thing. However, when using strings to specify dates, you should use explicit TO_DATE function, and 4 digit years, to be certain of exactly what's going on.

Try this:

INSERT INTO Employee VALUES('0000000001','078-05-1120','George','Brooks', to_date('24-may-1985','dd-mon-yyyy'));
INSERT INTO Employee VALUES('0000000002','917-34-6302','David','Adams', to_date('01-apr-1963','dd-mon-yyyy'));
INSERT INTO Employee VALUES('0000000003','078-05-1123','Yiling','Zhang', to_date('02-feb-1966','dd-mon-yyyy'));
INSERT INTO Employee VALUES('0000000004','078-05-1130','David','Gajos', to_date('10-feb-1965','dd-mon-yyyy'));
INSERT INTO Employee VALUES('0000000005','079-04-1120','Steven','Cox', to_date('11-feb-1979','dd-mon-yyyy'));
INSERT INTO Employee VALUES('0000000006','378-35-1108','Eddie','Gortler', to_date('30-may-1976','dd-mon-yyyy'));
INSERT INTO Employee VALUES('0000000007','278-05-1120','Henry','Kung', to_date('22-may-1981','dd-mon-yyyy'));
INSERT INTO Employee VALUES('0000000008','348-75-1450','Harry','Leitner', to_date('29-oct-1985','dd-mon-yyyy'));
INSERT INTO Employee VALUES('0000000009','256-90-4576','David','Malan', to_date('14-oct-1988','dd-mon-yyyy'));
INSERT INTO Employee VALUES('0000000010','025-45-1111','John','Brooks', to_date('28-nov-1978','dd-mon-yyyy'));
INSERT INTO Employee VALUES('0000000011','025-59-1919','Michael','Morrisett', to_date('04-nov-1985','dd-mon-yyyy'));
INSERT INTO Employee VALUES('0000000012','567-45-2351','David','Nelson', to_date('10-nov-1954','dd-mon-yyyy'));
INSERT INTO Employee VALUES('0000000013','100-40-0011','Jelani','Parkes', to_date('20-dec-1944','dd-mon-yyyy'));

SELECT * FROM EMPLOYEE WHERE DOB < to_date('01-jan-1980','dd-mon-yyyy');

Your insert statements have dates without specifying a mask. This means it will use the default date mask. You can check the value of that with this query:

select * from NLS_DATABASE_PARAMETERS
where parameter = 'NLS_DATE_FORMAT';

The years are in a two digit format; this means Oracle has to default a century. If the applicable mask is YY it will choose the current century; 44 will become 2044 and 80 will become 2080.

However, it seems likely you have a mask which uses the RR format, e,g, DD-MON-RR. This applies a blunt window to derive a century for input dates which lack one, pivoting at 50. Numbers less than 50 get the current century otherwise they get the previous one. So 80 will become 1950 and 44 will become 2044 . Clearly 2044 is not less than 1980.

If your database is using RR as its default date mask that is very bad practice. The RR format was introduced as an emergency kludge during the Y2K hysteria in the previous millennium. No modern system should still be using it. All input should include a proper full date and a format mask.

Systems which continue to rely on the RR format will become increasingly corrupted. As the 21st century moves on the pivot of the RR window is become less appropriate. There is no way to change the pivot point, because it was only intended as a kludge.

The original Y2K problem originated in a genuine storage problem: omitting the century digits saved a lot of space. Back in the 1980s that is. Space has not been that much of an issue for decades (except in certain specialist contexts such as embedded systems). Modern systems should always handle the century properly; not doing so is just lazy or ignorant.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top