我试图看看一个人的生日是否在两个日期之内,根据他们的出生日期而不管出生年份。
到目前为止,我正在尝试' ddd'获得一年中的日期选项。但我注意到我的代码是,它似乎省略了一年12月31日出生的任何人,因此得到了错误的结果。我尝试添加1,但这也没有帮助。这是我到目前为止所尝试的: -
select emp.employee_number,
person.title,
person.surname,
person.first_forname,
addr.line_one ||' '|| addr.line_two ||' '|| addr.line_three ||' '|| addr.line_four ||' '||
addr.postcode as Address,
post.long_desc as LONG_DESC,
email.email_address,
per.pers_mobile_tel_no as mobile_no
from person per
inner join emp emp on per.person_ref=emp.person_ref
inner join addr addr on per.address_ref= addr.ref
inner join ph ph on emp.person_ref=ph.person_ref
inner join post post on ph.ref=post.ref
inner join pl pl on pl.post_ref= post.ref
where
to_char( ( TO_DATE(person.birth_date,'DD-MM-YYYY')) , 'ddd' ) >=
to_char( ( TO_DATE(:Start_Date,'DD-MON-YYYY')) , 'ddd' )
AND
to_char( ( TO_DATE(person.birth_date,'DD-MM-YYYY')) , 'ddd' ) <=
to_char( ( TO_DATE(:End_Date,'DD-MON-YYYY')) , 'ddd' )
and
ph.end_date is null
and loc.location_number in ('501')
order by emp.employee_number
对此有任何帮助将不胜感激。提前谢谢。
答案 0 :(得分:2)
由于以下情况,您的查询在12月31日工作错误:
to_char( ( TO_DATE(person.birth_date,'DD-MM-YYYY')) , 'ddd' ) >=
to_char( ( TO_DATE(:Start_Date,'DD-MON-YYYY')) , 'ddd' )
AND
to_char( ( TO_DATE(person.birth_date,'DD-MM-YYYY')) , 'ddd' ) <=
to_char( ( TO_DATE(:End_Date,'DD-MON-YYYY')) , 'ddd' )
在两种情况下,您使用更少或相等且更多或相等。它应该是
to_char( ( TO_DATE(person.birth_date,'DD-MM-YYYY')) , 'ddd' ) >
to_char( ( TO_DATE(:Start_Date,'DD-MON-YYYY')) , 'ddd' )
AND
to_char( ( TO_DATE(person.birth_date,'DD-MM-YYYY')) , 'ddd' ) <=
to_char( ( TO_DATE(:End_Date,'DD-MON-YYYY')) , 'ddd' )
或
to_char( ( TO_DATE(person.birth_date,'DD-MM-YYYY')) , 'ddd' ) >=
to_char( ( TO_DATE(:Start_Date,'DD-MON-YYYY')) , 'ddd' )
AND
to_char( ( TO_DATE(person.birth_date,'DD-MM-YYYY')) , 'ddd' ) <
to_char( ( TO_DATE(:End_Date,'DD-MON-YYYY')) , 'ddd' )
编辑:
查询似乎不能解决您的问题。例如,假设出生日是1990年12月31日,开始日期是01.06.1990,结束日期是01.02.1991。获得“ddd&#39;”后,您的出生日为365天,开始日期为152天,结束日期为32天。但是,32小于152,因此您不会在输出中获得31.12.1990的人。因此,与&#39; ddd&#39;相比值不正确,因为它忽略年份值。相反,您应该将整个日期彼此进行比较:
TO_DATE(person.birth_date,'DD-MM-YYYY') >=
TO_DATE(:Start_Date,'DD-MON-YYYY')
AND
TO_DATE(person.birth_date,'DD-MM-YYYY') <
TO_DATE(:End_Date,'DD-MON-YYYY')