我是Database的新手,我知道有些人以前已经问过这个问题,但是我有点棘手。在这里,假设我们有一个名为Employee的表,如下所示:
Create table People(
ID char(10) PK,
SSN char(15) Not Null,
First_Name char(15),
Last_Name char(15),
Birth_Day date NOT NULL
);
我们进行一些插入,例如:
INSERT INTO People VALUES('0000000001','078-05-1120','George','Brooks', '24-may-85');
INSERT INTO People VALUES('0000000002','917-34-6302','David','Adams', '01-apr-63');
INSERT INTO People VALUES('0000000003','078-05-1123','Yiling','Zhang', '02-feb-66');
INSERT INTO People VALUES('0000000004','078-05-1130','David','Gajos', '10-feb-65');
INSERT INTO People VALUES('0000000005','079-04-1120','Steven','Cox', '11-feb-79');
INSERT INTO People VALUES('0000000006','378-35-1108','Eddie','Gortler', '30-may-76');
INSERT INTO People VALUES('0000000007','278-05-1120','Henry','Kung', '22-may-81');
所以我的问题是我如何找到在1980年1月1日之前出生的人。我想简单地使用<或> opeator,但在这种情况下不起作用
SELECT ID,SSN, First_Name, Last_Name, Birth_Day
FROM People
WHERE Birth_Day < '01-jan-80';
任何人都可以帮忙吗?
答案 0 :(得分:3)
您正在使用日期字符串,并依赖于隐式转换将其与日期类型进行比较。
请改用DATE
文字或TO_DATE
函数。
SELECT ID,SSN, First_Name, Last_Name, Birth_Day
FROM People
WHERE Birth_Day < DATE '1980-01-01'
SELECT ID,SSN, First_Name, Last_Name, Birth_Day
FROM People
WHERE Birth_Day < TO_DATE('01-jan-1980','dd-mon-yyyy');
答案 1 :(得分:0)
尝试使用to_date
:
--this is only for testing
with brth as (
select to_date('24-may-85') as Birth from dual
union
select to_date('24-jan-79') as Birth from dual
)
-- actual solution
select * from brth where BIRTH < to_date('01-JAN-80')